Skip to content

Commit 7a35f5a

Browse files
authored
Merge pull request #141 from mdetrano/enhancement_endsInKeyword_139
implement fix to reduce amount of output examined for ends-in-keyword
2 parents 35a83e0 + c7abc68 commit 7a35f5a

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/JShrink/Minifier.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ class Minifier
112112

113113
protected static $keywords = ["delete", "do", "for", "in", "instanceof", "return", "typeof", "yield"];
114114

115+
protected $max_keyword_len;
116+
115117
/**
116118
* Contains lock ids which are used to replace certain code patterns and
117119
* prevent them from being minified
@@ -189,6 +191,8 @@ protected function initialize($js, $options)
189191
$this->b = "\n";
190192
$this->last_char = "\n";
191193
$this->output = "";
194+
195+
$this->max_keyword_len = max(array_map('strlen', static::$keywords));
192196
}
193197

194198
/**
@@ -658,7 +662,8 @@ protected static function isAlphaNumeric($char)
658662
protected function endsInKeyword() {
659663

660664
# When this function is called A is not yet assigned to output.
661-
$testOutput = $this->output . $this->a;
665+
# Regular expression only needs to check final part of output for keyword.
666+
$testOutput = substr($this->output . $this->a, -1 * ($this->max_keyword_len + 10));
662667

663668
foreach(static::$keywords as $keyword) {
664669
if (preg_match('/[^\w]'.$keyword.'[ ]?$/i', $testOutput) === 1) {

0 commit comments

Comments
 (0)