From d0b40aff672c3905455c84a8ce6677c5a0c1e469 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 2 Apr 2011 10:08:51 -0400 Subject: [PATCH] Applying changes suggested by nikic to simplify how end of comments are processed. --- lib/Twig/Lexer.php | 24 ++++++------------------ 1 files changed, 6 insertions(+), 18 deletions(-) diff --git a/lib/Twig/Lexer.php b/lib/Twig/Lexer.php index ba7e457..0664e8d 100644 --- a/lib/Twig/Lexer.php +++ b/lib/Twig/Lexer.php @@ -144,27 +144,15 @@ class Twig_Lexer implements Twig_LexerInterface switch ($token) { case $this->options['tag_comment'][0]: - $endPos = strpos($this->code, $this->options['tag_comment'][1], $this->cursor); - if (false === $endPos) { - throw new Twig_Error_Syntax('unclosed comment', $this->lineno, $this->filename); - } - $trimLen = strlen($this->options['whitespace_trim']); - if (strpos($this->code, $this->options['whitespace_trim'], $endPos - $trimLen) === $endPos - $trimLen) { - $endTag = preg_quote($this->options['tag_comment'][1], '/'); - preg_match('/' . $endTag . '(\h*)/', $this->code, $match, null, $this->cursor); - if (isset($match[1])) { - $endPos += strlen($match[1]); - } - } + $commentEndRegex = '/.*?(?:' . preg_quote($this->options['whitespace_trim'], '/') + . preg_quote($this->options['tag_comment'][1], '/') . '\h*|' + . preg_quote($this->options['tag_comment'][1], '/') . ')\n?/As'; - $this->moveCursor(substr($this->code, $this->cursor, $endPos - $this->cursor) . $this->options['tag_comment'][1]); - - // mimics the behavior of PHP by removing the newline that follows instructions if present - if ("\n" === substr($this->code, $this->cursor, 1)) { - ++$this->cursor; - ++$this->lineno; + if (!preg_match($commentEndRegex, $this->code, $match, null, $this->cursor)) { + throw new Twig_Error_Syntax('unclosed comment', $this->lineno, $this->filename); } + $this->moveCursor($match[0]); break; case $this->options['tag_block'][0]: -- 1.7.2.5