Skip to content

Commit e976209

Browse files
author
Frédéric Massart
committed
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.
1 parent 50a802f commit e976209

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

lib/Sabberworm/CSS/Parser.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ private function parseCharacter($bIsForIdentifier) {
265265
if (preg_match('/[0-9a-fA-F]/Su', $this->peek()) === 0) {
266266
return $this->consume(1);
267267
}
268-
$sUnicode = $this->consumeExpression('/^[0-9a-fA-F]{1,6}/u');
268+
$sUnicode = $this->consumeExpression('/^[0-9a-fA-F]{1,6}/u', 6);
269269
if ($this->strlen($sUnicode) < 6) {
270270
//Consume whitespace after incomplete unicode escape
271271
if (preg_match('/\\s/isSu', $this->peek())) {
@@ -565,9 +565,10 @@ private function consume($mValue = 1) {
565565
}
566566
}
567567

568-
private function consumeExpression($mExpression) {
568+
private function consumeExpression($mExpression, $iMaxLength = null) {
569569
$aMatches = null;
570-
if (preg_match($mExpression, $this->inputLeft(), $aMatches, PREG_OFFSET_CAPTURE) === 1) {
570+
$sInput = $iMaxLength !== null ? $this->peek($iMaxLength) : $this->inputLeft();
571+
if (preg_match($mExpression, $sInput, $aMatches, PREG_OFFSET_CAPTURE) === 1) {
571572
return $this->consume($aMatches[0][0]);
572573
}
573574
throw new UnexpectedTokenException($mExpression, $this->peek(5), 'expression', $this->iLineNo);

0 commit comments

Comments
 (0)