From 4588e043653a37568f7f1c5c58b994064d510363 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 13 May 2013 03:39:14 -0700 Subject: [PATCH] No need to loop through pragma tokens twice. Add them directly to the token list as soon as they are tokenized. --- src/Mustache/Tokenizer.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Mustache/Tokenizer.php b/src/Mustache/Tokenizer.php index 5827abc..6ff122b 100644 --- a/src/Mustache/Tokenizer.php +++ b/src/Mustache/Tokenizer.php @@ -70,7 +70,6 @@ class Mustache_Tokenizer const NODES = 'nodes'; const VALUE = 'value'; - private $pragmas; private $state; private $tagType; private $tag; @@ -177,14 +176,6 @@ class Mustache_Tokenizer $this->flushBuffer(); - // Pragmas are hoisted to the front of the template. - foreach ($this->pragmas as $pragma) { - array_unshift($this->tokens, array( - self::TYPE => self::T_PRAGMA, - self::NAME => $pragma, - )); - } - return $this->tokens; } @@ -202,7 +193,6 @@ class Mustache_Tokenizer $this->line = 0; $this->otag = '{{'; $this->ctag = '}}'; - $this->pragmas = array(); } /** @@ -242,7 +232,10 @@ class Mustache_Tokenizer } /** - * Add pragma tag this template's list of pragmas. + * Add pragma token. + * + * Pragmas are hoisted to the front of the template, so all pragma tokens + * will appear at the front of the token list. * * @param string $text * @param int $index @@ -251,8 +244,15 @@ class Mustache_Tokenizer */ private function addPragma($text, $index) { - $end = strpos($text, $this->ctag, $index); - $this->pragmas[] = trim(substr($text, $index + 2, $end - $index - 2)); + $end = strpos($text, $this->ctag, $index); + $pragma = trim(substr($text, $index + 2, $end - $index - 2)); + + // Pragmas are hoisted to the front of the template. + array_unshift($this->tokens, array( + self::TYPE => self::T_PRAGMA, + self::NAME => $pragma, + self::LINE => 0, + )); return $end + strlen($this->ctag) - 1; }