Skip to content

Commit 84d5522

Browse files
committed
Optimize comes() behavior
Only very rarely do we care about case sensitivity. Most of the time, we are dealing with symbols. This saves on a ton of strtolower() calls. Also can save on a more expensive mb_strlen() call if we know we are dealing with symbols (for any practical charset).
1 parent de486a7 commit 84d5522

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

lib/Sabberworm/CSS/Parser.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,9 @@ private function parsePrimitiveValue() {
367367
$this->consumeWhiteSpace();
368368
if (is_numeric($this->peek()) || ($this->comes('-.') && is_numeric($this->peek(1, 2))) || (($this->comes('-') || $this->comes('.')) && is_numeric($this->peek(1, 1)))) {
369369
$oValue = $this->parseNumericValue();
370-
} else if ($this->comes('#') || $this->comes('rgb') || $this->comes('hsl')) {
370+
} else if ($this->comes('#') || $this->comes('rgb', true) || $this->comes('hsl', true)) {
371371
$oValue = $this->parseColorValue();
372-
} else if ($this->comes('url')) {
372+
} else if ($this->comes('url', true)) {
373373
$oValue = $this->parseURLValue();
374374
} else if ($this->comes("'") || $this->comes('"')) {
375375
$oValue = $this->parseStringValue();
@@ -395,7 +395,7 @@ private function parseNumericValue($bForColor = false) {
395395
$fSize = floatval($sSize);
396396
$sUnit = null;
397397
foreach(explode('/', Size::ABSOLUTE_SIZE_UNITS.'/'.Size::RELATIVE_SIZE_UNITS.'/'.Size::NON_SIZE_UNITS) as $sDefinedUnit) {
398-
if ($this->comes($sDefinedUnit, 0, true)) {
398+
if ($this->comes($sDefinedUnit, true)) {
399399
$sUnit = $sDefinedUnit;
400400
$this->consume($sDefinedUnit);
401401
break;
@@ -432,7 +432,7 @@ private function parseColorValue() {
432432
}
433433

434434
private function parseURLValue() {
435-
$bUseUrl = $this->comes('url');
435+
$bUseUrl = $this->comes('url', true);
436436
if ($bUseUrl) {
437437
$this->consume('url');
438438
$this->consumeWhiteSpace();
@@ -454,11 +454,11 @@ private static function identifierIs($sIdentifier, $sMatch, $bCaseInsensitive =
454454
return preg_match("/^(-\\w+-)?$sMatch$/".($bCaseInsensitive ? 'i' : ''), $sIdentifier) === 1;
455455
}
456456

457-
private function comes($sString, $iOffset = 0, $bCaseInsensitive = true) {
458-
$sPeek = $this->peek($sString, $iOffset);
457+
private function comes($sString, $alpha = false) {
458+
$sPeek = $this->peek($alpha ? strlen($sString) : $sString);
459459
return ($sPeek == '')
460460
? false
461-
: $this->streql($sPeek, $sString, $bCaseInsensitive);
461+
: $this->streql($sPeek, $sString, $alpha);
462462
}
463463

464464
private function peek($iLength = 1, $iOffset = 0) {

0 commit comments

Comments
 (0)