[icinga-checkins] icinga.org: icingaweb2/feature/translation-catalog-statistics-object-13013 : CatalogParser: Replace manual approach with a regex one in method readUntil

git at icinga.org git at icinga.org
Mon Dec 12 13:06:19 CET 2016


Module: icingaweb2
Branch: feature/translation-catalog-statistics-object-13013
Commit: a7fb64050f7af242e9eede599378fb9cce764ad3
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=a7fb64050f7af242e9eede599378fb9cce764ad3

Author: Johannes Meyer <johannes.meyer at netways.de>
Date:   Mon Dec 12 12:19:28 2016 +0100

CatalogParser: Replace manual approach with a regex one in method readUntil

This improves the performance by about 60-70%.

refs #13013

---

 .../library/Translation/Catalog/CatalogParser.php  |   44 ++++++++------------
 1 file changed, 18 insertions(+), 26 deletions(-)

diff --git a/modules/translation/library/Translation/Catalog/CatalogParser.php b/modules/translation/library/Translation/Catalog/CatalogParser.php
index a37b5e4..f8a3858 100644
--- a/modules/translation/library/Translation/Catalog/CatalogParser.php
+++ b/modules/translation/library/Translation/Catalog/CatalogParser.php
@@ -3,6 +3,7 @@
 
 namespace Icinga\Module\Translation\Catalog;
 
+use Exception;
 use SplFileObject;
 use Icinga\Application\Benchmark;
 use Icinga\Module\Translation\Exception\CatalogParserException;
@@ -339,7 +340,10 @@ class CatalogParser
     {
         $oldStack = $this->stack;
         $this->stack = ltrim($this->stack);
-        $this->position += strlen($oldStack) - strlen($this->stack);
+
+        if ($this->stack !== $oldStack) {
+            $this->position += strlen($oldStack) - strlen($this->stack);
+        }
     }
 
     /**
@@ -353,33 +357,21 @@ class CatalogParser
      */
     protected function readUntil($endPoint)
     {
-        $string = '';
-        for ($i = 0; $i < strlen($this->stack); $i++) {
-            if ($this->stack[$i] !== $endPoint || ($i > 0 && $this->stack[$i - 1] === '\\')) {
-                $string .= $this->stack[$i];
-            } else {
-                $this->removeCharsFromStack($i + 1);
-                return $string;
-            }
-        }
+        $pattern = '/(?<!\\\\)' . preg_quote($endPoint, '/') . '/';
 
-        throw new CatalogParserException(
-            $this->catalogPath,
-            $this->lineNumber,
-            $this->position + strlen($string) + 1,
-            "Missing \"$endPoint\""
-        );
-    }
+        try {
+            list($string, $this->stack) = preg_split($pattern, $this->stack, 2);
+        } catch (Exception $_) {
+            throw new CatalogParserException(
+                $this->catalogPath,
+                $this->lineNumber,
+                $this->position + strlen($this->stack) + 1,
+                "Missing \"$endPoint\""
+            );
+        }
 
-    /**
-     * Remove string with a given length from stack
-     *
-     * @param   $lengthToRemove
-     */
-    protected function removeCharsFromStack($lengthToRemove)
-    {
-        $this->stack = substr($this->stack, $lengthToRemove);
-        $this->position += $lengthToRemove;
+        $this->position += strlen($string) + 1;
+        return $string;
     }
 
     /**



More information about the icinga-checkins mailing list