Skip to content

Commit 38319b2

Browse files
author
Andreas Sandberg
committed
Add ability to not use mb_* related string functions
1 parent e40af61 commit 38319b2

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

CSSParser.php

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ class CSSParser {
1414
private $sText;
1515
private $iCurrentPosition;
1616
private $iLength;
17+
18+
/**
19+
* Should we use mb_* string functions
20+
*
21+
* @var bool
22+
*/
23+
private $bUseMbFunctions = TRUE;
1724

1825
public function __construct($sText, $sDefaultCharset = 'utf-8') {
1926
$this->sText = $sText;
@@ -23,7 +30,7 @@ public function __construct($sText, $sDefaultCharset = 'utf-8') {
2330

2431
public function setCharset($sCharset) {
2532
$this->sCharset = $sCharset;
26-
$this->iLength = mb_strlen($this->sText, $this->sCharset);
33+
$this->iLength = $this->strlen($this->sText, $this->sCharset);
2734
}
2835

2936
public function getCharset() {
@@ -35,7 +42,11 @@ public function parse() {
3542
$this->parseDocument($oResult);
3643
return $oResult;
3744
}
38-
45+
46+
public function setUseMbFlag($bFlag){
47+
$this->bUseMbFunctions = (bool) $bFlag;
48+
}
49+
3950
private function parseDocument(CSSDocument $oDocument) {
4051
$this->consumeWhiteSpace();
4152
$this->parseList($oDocument, true);
@@ -157,7 +168,7 @@ private function parseCharacter($bIsForIdentifier) {
157168
return $this->consume(1);
158169
}
159170
$sUnicode = $this->consumeExpression('/^[0-9a-fA-F]{1,6}/u');
160-
if(mb_strlen($sUnicode, $this->sCharset) < 6) {
171+
if($this->strlen($sUnicode, $this->sCharset) < 6) {
161172
//Consume whitespace after incomplete unicode escape
162173
if(preg_match('/\\s/isSu', $this->peek())) {
163174
if($this->comes('\r\n')) {
@@ -345,15 +356,15 @@ private function parseColorValue() {
345356
if($this->comes('#')) {
346357
$this->consume('#');
347358
$sValue = $this->parseIdentifier(false);
348-
if(mb_strlen($sValue, $this->sCharset) === 3) {
359+
if($this->strlen($sValue, $this->sCharset) === 3) {
349360
$sValue = $sValue[0].$sValue[0].$sValue[1].$sValue[1].$sValue[2].$sValue[2];
350361
}
351362
$aColor = array('r' => new CSSSize(intval($sValue[0].$sValue[1], 16), null, true), 'g' => new CSSSize(intval($sValue[2].$sValue[3], 16), null, true), 'b' => new CSSSize(intval($sValue[4].$sValue[5], 16), null, true));
352363
} else {
353364
$sColorMode = $this->parseIdentifier(false);
354365
$this->consumeWhiteSpace();
355366
$this->consume('(');
356-
$iLength = mb_strlen($sColorMode, $this->sCharset);
367+
$iLength = $this->strlen($sColorMode, $this->sCharset);
357368
for($i=0;$i<$iLength;$i++) {
358369
$this->consumeWhiteSpace();
359370
$aColor[$sColorMode[$i]] = $this->parseNumericValue(true);
@@ -395,27 +406,27 @@ private function peek($iLength = 1, $iOffset = 0) {
395406
return '';
396407
}
397408
if(is_string($iLength)) {
398-
$iLength = mb_strlen($iLength, $this->sCharset);
409+
$iLength = $this->strlen($iLength, $this->sCharset);
399410
}
400411
if(is_string($iOffset)) {
401-
$iOffset = mb_strlen($iOffset, $this->sCharset);
412+
$iOffset = $this->strlen($iOffset, $this->sCharset);
402413
}
403-
return mb_substr($this->sText, $this->iCurrentPosition+$iOffset, $iLength, $this->sCharset);
414+
return $this->substr($this->sText, $this->iCurrentPosition+$iOffset, $iLength, $this->sCharset);
404415
}
405416

406417
private function consume($mValue = 1) {
407418
if(is_string($mValue)) {
408-
$iLength = mb_strlen($mValue, $this->sCharset);
409-
if(mb_substr($this->sText, $this->iCurrentPosition, $iLength, $this->sCharset) !== $mValue) {
419+
$iLength = $this->strlen($mValue, $this->sCharset);
420+
if($this->substr($this->sText, $this->iCurrentPosition, $iLength, $this->sCharset) !== $mValue) {
410421
throw new Exception("Expected $mValue, got ".$this->peek(5));
411422
}
412-
$this->iCurrentPosition += mb_strlen($mValue, $this->sCharset);
423+
$this->iCurrentPosition += $this->strlen($mValue, $this->sCharset);
413424
return $mValue;
414425
} else {
415426
if($this->iCurrentPosition+$mValue > $this->iLength) {
416427
throw new Exception("Tried to consume $mValue chars, exceeded file end");
417428
}
418-
$sResult = mb_substr($this->sText, $this->iCurrentPosition, $mValue, $this->sCharset);
429+
$sResult = $this->substr($this->sText, $this->iCurrentPosition, $mValue, $this->sCharset);
419430
$this->iCurrentPosition += $mValue;
420431
return $sResult;
421432
}
@@ -459,7 +470,27 @@ private function consumeUntil($sEnd) {
459470
}
460471

461472
private function inputLeft() {
462-
return mb_substr($this->sText, $this->iCurrentPosition, -1, $this->sCharset);
473+
return $this->substr($this->sText, $this->iCurrentPosition, -1, $this->sCharset);
463474
}
475+
476+
private function substr($string, $start, $length){
477+
if($this->bUseMbFunctions) {
478+
return mb_substr($string, $start, $length, $this->sCharset);
479+
}
480+
else {
481+
return substr($string, $start, $length);
482+
}
483+
}
484+
485+
private function strlen($text)
486+
{
487+
if($this->bUseMbFunctions) {
488+
return mb_strlen($text, $this->sCharset);
489+
}
490+
else {
491+
return strlen($text);
492+
}
493+
494+
}
464495
}
465496

0 commit comments

Comments
 (0)