From cd378830a71d4a71c5f4898bea531814151bc2f3 Mon Sep 17 00:00:00 2001
From: uwetews <uwe.tews@googlemail.com>
Date: Mon, 7 Nov 2016 00:51:31 +0100
Subject: [PATCH] optimize lexer code for speed 
 https://github.com/smarty-php/smarty/issues/311

---
 LexerGenerator/Parser.php | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/LexerGenerator/Parser.php b/LexerGenerator/Parser.php
index 2e154bd..dce414b 100644
--- a/LexerGenerator/Parser.php
+++ b/LexerGenerator/Parser.php
@@ -350,7 +350,10 @@ public function doFirstMatch($rules, $statename, $ruleindex)
         if (!isset($this->yy_global_pattern' . $ruleindex . ')) {
             $this->yy_global_pattern' . $ruleindex . ' = "' . $pattern . 'isS";
         }
-        if (' . $this->counter . ' >=  strlen(' . $this->input . ')) {
+        if (!isset($this->dataLenght)) {
+            $this->dataLenght = strlen(' . $this->input . ');
+        }
+        if (' . $this->counter . ' >=  $this->dataLenght) {
             return false; // end of input
         }
         ');
@@ -359,13 +362,8 @@ public function doFirstMatch($rules, $statename, $ruleindex)
             if (preg_match($this->yy_global_pattern' . $ruleindex . ',' . $this->input . ', $yymatches, null, ' .
              $this->counter .
                     ')) {
-                $yysubmatches = $yymatches;
-                if (strlen($yysubmatches[0]) < 200) {
-                    $yymatches = preg_grep("/(.|\s)+/", $yysubmatches);
-                } else {
-                    $yymatches = array_filter($yymatches, \'strlen\');
-                }
-                if (empty($yymatches)) {
+                 $yymatches = array_filter($yymatches);
+                 if (empty($yymatches)) {
                     throw new Exception(\'Error: lexing failed because a rule matched\' .
                         \' an empty string.  Input "\' . substr(' . $this->input . ',
                         ' . $this->counter . ', 5) . \'... state ' . $statename . '\');
@@ -386,7 +384,7 @@ public function doFirstMatch($rules, $statename, $ruleindex)
                 } elseif ($r === false) {
                     ' . $this->counter . ' += strlen(' . $this->value . ');
                     ' . $this->line . ' += substr_count(' . $this->value . ', "\n");
-                    if (' . $this->counter . ' >=  strlen(' . $this->input . ')) {
+                    if (' . $this->counter . ' >=  $this->dataLenght) {
                         return false; // end of input
                     }
                     // skip this token
@@ -435,7 +433,7 @@ public function doFirstMatch($rules, $statename, $ruleindex)
                     } elseif ($r === false) {
                         ' . $this->counter . ' += strlen(' . $this->value . ');
                         ' . $this->line . ' += substr_count(' . $this->value . ', "\n");
-                        if (' . $this->counter . ' >=  strlen(' . $this->input . ')) {
+                        if (' . $this->counter . ' >=  $this->dataLenght) {
                             return false; // end of input
                         }
                         // skip this token