From b0666e537da71a9c72cfd8f5ee81cde4e4b25c53 Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Wed, 16 May 2012 16:42:45 -0700 Subject: [PATCH] Remove unnecessary { } in compile --- adminer/include/editing.inc.php | 2 +- compile.php | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/adminer/include/editing.inc.php b/adminer/include/editing.inc.php index 25c8fef4..5f0002ad 100644 --- a/adminer/include/editing.inc.php +++ b/adminer/include/editing.inc.php @@ -367,7 +367,7 @@ function tar_file($filename, $contents) { $return = pack("a100a8a8a8a12a12", $filename, 644, 0, 0, decoct(strlen($contents)), decoct(time())); $checksum = 8*32; // space for checksum itself for ($i=0; $i < strlen($return); $i++) { - $checksum += ord($return{$i}); + $checksum += ord($return[$i]); } $return .= sprintf("%06o", $checksum) . "\0 "; return $return . str_repeat("\0", 512 - strlen($return)) . $contents . str_repeat("\0", 511 - (strlen($contents) + 511) % 512); diff --git a/compile.php b/compile.php index b32cd7d1..f2710d32 100644 --- a/compile.php +++ b/compile.php @@ -84,7 +84,7 @@ function put_file_lang($match) { function short_identifier($number, $chars) { $return = ''; while ($number >= 0) { - $return .= $chars{$number % strlen($chars)}; + $return .= $chars[$number % strlen($chars)]; $number = floor($number / strlen($chars)) - 1; } return $return; @@ -98,6 +98,33 @@ function php_shrink($input) { $shortening = true; $tokens = token_get_all($input); + // remove unnecessary { } + //! change also `while () { if () {;} }` to `while () if () ;` but be careful about `if () { if () { } } else { } + $shorten = 0; + $opening = -1; + foreach ($tokens as $i => $token) { + if (in_array($token[0], array(T_IF, T_ELSE, T_ELSEIF, T_WHILE, T_DO, T_FOR, T_FOREACH), true)) { + $shorten = ($token[0] == T_FOR ? 4 : 2); + $opening = -1; + } elseif (in_array($token[0], array(T_SWITCH, T_FUNCTION, T_CLASS, T_CLOSE_TAG), true)) { + $shorten = 0; + } elseif ($token === ';') { + $shorten--; + } elseif ($token === '{') { + if ($opening < 0) { + $opening = $i; + } elseif ($shorten > 1) { + $shorten = 0; + } + } elseif ($token === '}' && $opening >= 0 && $shorten == 1) { + unset($tokens[$opening]); + unset($tokens[$i]); + $shorten = 0; + $opening = -1; + } + } + $tokens = array_values($tokens); + foreach ($tokens as $i => $token) { if ($token[0] === T_VARIABLE && !isset($special_variables[$token[1]])) { $short_variables[$token[1]]++;