Skip to content

Commit 364a897

Browse files
committed
Work with array instead of string
1 parent 003e4fb commit 364a897

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

lib/Sabberworm/CSS/Parser.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*/
2727
class Parser {
2828

29-
private $sText;
29+
private $aText;
3030
private $iCurrentPosition;
3131
private $oParserSettings;
3232
private $sCharset;
@@ -36,12 +36,16 @@ class Parser {
3636
private $aSizeUnits;
3737

3838
public function __construct($sText, Settings $oParserSettings = null) {
39-
$this->sText = $sText;
4039
$this->iCurrentPosition = 0;
4140
if ($oParserSettings === null) {
4241
$oParserSettings = Settings::create();
4342
}
4443
$this->oParserSettings = $oParserSettings;
44+
if ($this->oParserSettings->bMultibyteSupport) {
45+
$this->aText = preg_split('//u', $sText, null, PREG_SPLIT_NO_EMPTY);
46+
} else {
47+
$this->aText = str_split($sText);
48+
}
4549
$this->blockRules = explode('/', AtRule::BLOCK_RULES);
4650

4751
foreach (explode('/', Size::ABSOLUTE_SIZE_UNITS.'/'.Size::RELATIVE_SIZE_UNITS.'/'.Size::NON_SIZE_UNITS) as $val) {
@@ -56,7 +60,7 @@ public function __construct($sText, Settings $oParserSettings = null) {
5660

5761
public function setCharset($sCharset) {
5862
$this->sCharset = $sCharset;
59-
$this->iLength = $this->strlen($this->sText);
63+
$this->iLength = count($this->aText);
6064
}
6165

6266
public function getCharset() {
@@ -284,7 +288,7 @@ private function parseRuleSet($oRuleSet) {
284288
try {
285289
$sConsume = $this->consumeUntil(array("\n", ";", '}'), true);
286290
// We need to “unfind” the matches to the end of the ruleSet as this will be matched later
287-
if($this->streql($this->substr($sConsume, $this->strlen($sConsume)-1, 1), '}')) {
291+
if($this->streql(substr($sConsume, -1), '}')) {
288292
--$this->iCurrentPosition;
289293
$this->peekCache = null;
290294
} else {
@@ -469,8 +473,8 @@ private function parseURLValue() {
469473
}
470474

471475
/**
472-
* Tests an identifier for a given value. Since identifiers are all keywords, they can be vendor-prefixed. We need to check for these versions too.
473-
*/
476+
* Tests an identifier for a given value. Since identifiers are all keywords, they can be vendor-prefixed. We need to check for these versions too.
477+
*/
474478
private function identifierIs($sIdentifier, $sMatch) {
475479
return (strcasecmp($sIdentifier, $sMatch) === 0)
476480
?: preg_match("/^(-\\w+-)?$sMatch$/i", $sIdentifier) === 1;
@@ -493,7 +497,7 @@ private function peek($iLength = 1, $iOffset = 0) {
493497
return '';
494498
}
495499
$iLength = min($iLength, $this->iLength-$iOffset);
496-
$out = $this->substr($this->sText, $iOffset, $iLength);
500+
$out = $this->substr($iOffset, $iLength);
497501
if ($peek) {
498502
$this->peekCache = $out;
499503
}
@@ -503,7 +507,7 @@ private function peek($iLength = 1, $iOffset = 0) {
503507
private function consume($mValue = 1) {
504508
if (is_string($mValue)) {
505509
$iLength = $this->strlen($mValue);
506-
if (!$this->streql($this->substr($this->sText, $this->iCurrentPosition, $iLength), $mValue)) {
510+
if (!$this->streql($this->substr($this->iCurrentPosition, $iLength), $mValue)) {
507511
throw new UnexpectedTokenException($mValue, $this->peek(max($iLength, 5)));
508512
}
509513
$this->iCurrentPosition += $this->strlen($mValue);
@@ -513,7 +517,7 @@ private function consume($mValue = 1) {
513517
if ($this->iCurrentPosition + $mValue > $this->iLength) {
514518
throw new UnexpectedTokenException($mValue, $this->peek(5), 'count');
515519
}
516-
$sResult = $this->substr($this->sText, $this->iCurrentPosition, $mValue);
520+
$sResult = $this->substr($this->iCurrentPosition, $mValue);
517521
$this->iCurrentPosition += $mValue;
518522
$this->peekCache = null;
519523
return $sResult;
@@ -587,15 +591,17 @@ private function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false)
587591
}
588592

589593
private function inputLeft() {
590-
return $this->substr($this->sText, $this->iCurrentPosition, -1);
594+
return $this->substr($this->iCurrentPosition, -1);
591595
}
592596

593-
private function substr($sString, $iStart, $iLength) {
594-
if ($this->oParserSettings->bMultibyteSupport) {
595-
return mb_substr($sString, $iStart, $iLength, $this->sCharset);
596-
} else {
597-
return substr($sString, $iStart, $iLength);
597+
private function substr($iStart, $iLength) {
598+
$out = '';
599+
while ($iLength > 0) {
600+
$out .= $this->aText[$iStart];
601+
$iStart++;
602+
$iLength--;
598603
}
604+
return $out;
599605
}
600606

601607
private function strlen($sString) {

0 commit comments

Comments
 (0)