From e976209438ef02ab99411f911d6f5508c17b3119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Massart?= Date: Wed, 31 May 2017 15:48:21 +0800 Subject: [PATCH] Consume expression to indicate how many characters to look ahead Where possible consumeExpression should be provided with the maximum number of characters the expression could be made of. This allows for more efficiently peeking for a few characters rather than the entire remaining character map. --- lib/Sabberworm/CSS/Parser.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/Sabberworm/CSS/Parser.php b/lib/Sabberworm/CSS/Parser.php index 409b796d..65ea2f0d 100644 --- a/lib/Sabberworm/CSS/Parser.php +++ b/lib/Sabberworm/CSS/Parser.php @@ -265,7 +265,7 @@ private function parseCharacter($bIsForIdentifier) { if (preg_match('/[0-9a-fA-F]/Su', $this->peek()) === 0) { return $this->consume(1); } - $sUnicode = $this->consumeExpression('/^[0-9a-fA-F]{1,6}/u'); + $sUnicode = $this->consumeExpression('/^[0-9a-fA-F]{1,6}/u', 6); if ($this->strlen($sUnicode) < 6) { //Consume whitespace after incomplete unicode escape if (preg_match('/\\s/isSu', $this->peek())) { @@ -565,9 +565,10 @@ private function consume($mValue = 1) { } } - private function consumeExpression($mExpression) { + private function consumeExpression($mExpression, $iMaxLength = null) { $aMatches = null; - if (preg_match($mExpression, $this->inputLeft(), $aMatches, PREG_OFFSET_CAPTURE) === 1) { + $sInput = $iMaxLength !== null ? $this->peek($iMaxLength) : $this->inputLeft(); + if (preg_match($mExpression, $sInput, $aMatches, PREG_OFFSET_CAPTURE) === 1) { return $this->consume($aMatches[0][0]); } throw new UnexpectedTokenException($mExpression, $this->peek(5), 'expression', $this->iLineNo);