From 67452dbe73af1be9d7565d483b6a1ce3f8c313c7 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 3 Dec 2010 18:35:29 +0100 Subject: [PATCH] made a speed optimization in Lexer --- lib/Twig/Lexer.php | 11 ++++------- 1 files changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/Twig/Lexer.php b/lib/Twig/Lexer.php index 2803824..e405be5 100644 --- a/lib/Twig/Lexer.php +++ b/lib/Twig/Lexer.php @@ -51,11 +51,6 @@ class Twig_Lexer implements Twig_LexerInterface ), $options); } - public function sortByLength($a, $b) - { - return strlen($a) > strlen($b) ? -1 : 1; - } - /** * Tokenizes a source code. * @@ -336,10 +331,12 @@ class Twig_Lexer implements Twig_LexerInterface $operators = array('='); $operators = array_merge($operators, array_keys($this->env->getUnaryOperators())); $operators = array_merge($operators, array_keys($this->env->getBinaryOperators())); - usort($operators, array($this, 'sortByLength')); + + $operators = array_combine($operators, array_map('strlen', $operators)); + arsort($operators); $regex = array(); - foreach ($operators as $operator) { + foreach (array_keys($operators) as $operator) { $last = ord(substr($operator, -1)); // an operator that ends with a character must be followed by // a whitespace or a parenthese -- 1.7.2.5