From 9c1d5484a25da97767e6e54a7a94874a535a431c Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Sat, 15 Mar 2025 07:20:12 +0100 Subject: [PATCH] php_shrink: Join echos interleaved with comments --- php_shrink.inc.php | 12 +++++++++--- tests/php_shrink.php | 4 +--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/php_shrink.inc.php b/php_shrink.inc.php index fe3b1c18..a15ffd0f 100644 --- a/php_shrink.inc.php +++ b/php_shrink.inc.php @@ -98,11 +98,11 @@ function php_shrink($input) { } elseif ($token[0] == T_ECHO) { $in_echo = true; } elseif ($token[1] == ';' && $in_echo) { - if ($tokens[$i+1][0] === T_WHITESPACE && $tokens[$i+2][0] === T_ECHO) { + $next_echo = next_token($tokens, $i, T_ECHO, array(T_WHITESPACE, T_COMMENT)); + for (; $i < $next_echo - 1; $i++) { next($tokens); - $i++; } - if ($tokens[$i+1][0] === T_ECHO) { + if ($next_echo) { // join two consecutive echos next($tokens); $token[1] = ','; // '.' would conflict with "a".1+2 and would use more memory //! remove ',' and "," but not $var"," @@ -122,6 +122,12 @@ function php_shrink($input) { return $output; } +function next_token($tokens, $i, $search, $allowed = array()) { + for ($i += 1; in_array($tokens[$i][0], $allowed); $i++) { + } + return ($tokens[$i][0] === $search ? $i : 0); +} + function short_identifier($number, $chars) { $return = ''; while ($number >= 0) { diff --git a/tests/php_shrink.php b/tests/php_shrink.php index bc917e75..41f4cb9c 100644 --- a/tests/php_shrink.php +++ b/tests/php_shrink.php @@ -13,9 +13,6 @@ function check($code, $expected) { //! bugs: check('{if (true) {} echo 1;}', '{if(true);echo 1;}'); -//! inefficiencies: -check("echo 1; //\necho 2;", 'echo 1,2;'); - check('$ab = 1; echo $ab;', '$a=1;echo$a;'); check('$ab = 1; $cd = 2;', '$a=1;$b=2;'); check('define("AB", 1);', 'define("AB",1);'); @@ -37,6 +34,7 @@ check('if (true) { echo "a"; } else { echo "b"; }', 'if(true)echo"a";else echo"b check('echo $_GET["a"];', 'echo$_GET["a"];'); check('$ab = 1; echo "$ab";', '$a=1;echo"$a";'); check('echo 1; echo 3;', 'echo 1,3;'); +check('echo 1; /**/ echo 2;', 'echo 1,2;'); check('echo 1; ?>2