Permalink
Browse files

Remove unnecessary { } in compile

  • Loading branch information...
1 parent 8339b14 commit b0666e537da71a9c72cfd8f5ee81cde4e4b25c53 @vrana committed May 16, 2012
Showing with 29 additions and 2 deletions.
  1. +1 −1 adminer/include/editing.inc.php
  2. +28 −1 compile.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);
View
@@ -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]]++;

0 comments on commit b0666e5

Please sign in to comment.