Skip to content

Commit bc662aa

Browse files
committed
Optimize consumeUntil()
1 parent 5bfe3df commit bc662aa

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

lib/Sabberworm/CSS/Parser.php

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ private function parseAtRule() {
106106
} else if (self::identifierIs($sIdentifier, 'keyframes')) {
107107
$oResult = new KeyFrame();
108108
$oResult->setVendorKeyFrame($sIdentifier);
109-
$oResult->setAnimationName(trim($this->consumeUntil('{')));
110-
$this->consume('{');
109+
$oResult->setAnimationName(trim($this->consumeUntil('{', false, true)));
111110
$this->consumeWhiteSpace();
112111
$this->parseList($oResult);
113112
return $oResult;
@@ -128,8 +127,7 @@ private function parseAtRule() {
128127
return new CSSNamespace($mUrl, $sPrefix);
129128
} else {
130129
//Unknown other at rule (font-face or such)
131-
$sArgs = $this->consumeUntil('{');
132-
$this->consume('{');
130+
$sArgs = $this->consumeUntil('{', false, true);
133131
$this->consumeWhiteSpace();
134132
$bUseRuleSet = true;
135133
foreach(explode('/', AtRule::BLOCK_RULES) as $sBlockRuleName) {
@@ -246,8 +244,7 @@ private function parseCharacter($bIsForIdentifier) {
246244

247245
private function parseSelector() {
248246
$oResult = new DeclarationBlock();
249-
$oResult->setSelector($this->consumeUntil('{'));
250-
$this->consume('{');
247+
$oResult->setSelector($this->consumeUntil('{', false, true));
251248
$this->consumeWhiteSpace();
252249
$this->parseRuleSet($oResult);
253250
return $oResult;
@@ -518,9 +515,13 @@ private function consumeWhiteSpace() {
518515

519516
private function consumeComment() {
520517
if ($this->comes('/*')) {
521-
$this->consumeUntil('*/');
522-
$this->consume('*/');
523-
return true;
518+
$this->consume(2);
519+
while ($this->consumeUntil('*', false, true)) {
520+
if ($this->comes('/')) {
521+
$this->consume(1);
522+
return true;
523+
}
524+
}
524525
}
525526
return false;
526527
}
@@ -529,22 +530,25 @@ private function isEnd() {
529530
return $this->iCurrentPosition >= $this->iLength;
530531
}
531532

532-
private function consumeUntil($aEnd, $bIncludeEnd = false) {
533+
private function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false) {
533534
$aEnd = is_array($aEnd) ? $aEnd : array($aEnd);
534-
$iEndPos = null;
535-
foreach ($aEnd as $sEnd) {
536-
$iCurrentEndPos = $this->strpos($this->sText, $sEnd, $this->iCurrentPosition);
537-
if($iCurrentEndPos === false) {
538-
continue;
539-
}
540-
if($iEndPos === null || $iCurrentEndPos < $iEndPos) {
541-
$iEndPos = $iCurrentEndPos + ($bIncludeEnd ? $this->strlen($sEnd) : 0);
542-
}
543-
}
544-
if ($iEndPos === null) {
545-
throw new UnexpectedTokenException('One of ("'.implode('","', $aEnd).'")', $this->peek(5), 'search');
546-
}
547-
return $this->consume($iEndPos - $this->iCurrentPosition);
535+
$out = '';
536+
$start = $this->iCurrentPosition;
537+
538+
while (($char = $this->consume(1)) !== '') {
539+
if (in_array($char, $aEnd)) {
540+
if ($bIncludeEnd) {
541+
$out .= $char;
542+
} elseif (!$consumeEnd) {
543+
$this->iCurrentPosition -= $this->strlen($char);
544+
}
545+
return $out;
546+
}
547+
$out .= $char;
548+
}
549+
550+
$this->iCurrentPosition = $start;
551+
throw new UnexpectedTokenException('One of ("'.implode('","', $aEnd).'")', $this->peek(5), 'search');
548552
}
549553

550554
private function inputLeft() {

0 commit comments

Comments
 (0)