26
26
*/
27
27
class Parser {
28
28
29
+ private $ sText ;
29
30
private $ aText ;
30
31
private $ iCurrentPosition ;
31
32
private $ oParserSettings ;
@@ -35,20 +36,12 @@ class Parser {
35
36
private $ aSizeUnits ;
36
37
37
38
public function __construct ($ sText , Settings $ oParserSettings = null ) {
39
+ $ this ->sText = $ sText ;
38
40
$ this ->iCurrentPosition = 0 ;
39
41
if ($ oParserSettings === null ) {
40
42
$ oParserSettings = Settings::create ();
41
43
}
42
44
$ this ->oParserSettings = $ oParserSettings ;
43
- if ($ this ->oParserSettings ->bMultibyteSupport ) {
44
- $ this ->aText = preg_split ('//u ' , $ sText , null , PREG_SPLIT_NO_EMPTY );
45
- } else {
46
- if ($ sText === '' ) {
47
- $ this ->aText = array ();
48
- } else {
49
- $ this ->aText = str_split ($ sText );
50
- }
51
- }
52
45
$ this ->blockRules = explode ('/ ' , AtRule::BLOCK_RULES );
53
46
54
47
foreach (explode ('/ ' , Size::ABSOLUTE_SIZE_UNITS .'/ ' .Size::RELATIVE_SIZE_UNITS .'/ ' .Size::NON_SIZE_UNITS ) as $ val ) {
@@ -63,6 +56,7 @@ public function __construct($sText, Settings $oParserSettings = null) {
63
56
64
57
public function setCharset ($ sCharset ) {
65
58
$ this ->sCharset = $ sCharset ;
59
+ $ this ->aText = $ this ->strsplit ($ this ->sText );
66
60
$ this ->iLength = count ($ this ->aText );
67
61
}
68
62
@@ -494,8 +488,7 @@ private function peek($iLength = 1, $iOffset = 0) {
494
488
if ($ iOffset >= $ this ->iLength ) {
495
489
return '' ;
496
490
}
497
- $ out = $ this ->substr ($ iOffset , $ iLength );
498
- return $ out ;
491
+ return $ this ->substr ($ iOffset , $ iLength );
499
492
}
500
493
501
494
private function consume ($ mValue = 1 ) {
@@ -593,13 +586,13 @@ private function substr($iStart, $iLength) {
593
586
if ($ iStart + $ iLength > $ this ->iLength ) {
594
587
$ iLength = $ this ->iLength - $ iStart ;
595
588
}
596
- $ out = '' ;
589
+ $ sResult = '' ;
597
590
while ($ iLength > 0 ) {
598
- $ out .= $ this ->aText [$ iStart ];
591
+ $ sResult .= $ this ->aText [$ iStart ];
599
592
$ iStart ++;
600
593
$ iLength --;
601
594
}
602
- return $ out ;
595
+ return $ sResult ;
603
596
}
604
597
605
598
private function strlen ($ sString ) {
@@ -626,6 +619,27 @@ private function strtolower($sString) {
626
619
}
627
620
}
628
621
622
+ private function strsplit ($ sString ) {
623
+ if ($ this ->oParserSettings ->bMultibyteSupport ) {
624
+ if ($ this ->streql ($ this ->sCharset , 'utf-8 ' )) {
625
+ return preg_split ('//u ' , $ sString , null , PREG_SPLIT_NO_EMPTY );
626
+ } else {
627
+ $ iLength = mb_strlen ($ sString , $ this ->sCharset );
628
+ $ aResult = array ();
629
+ for ($ i = 0 ; $ i < $ iLength ; ++$ i ) {
630
+ $ aResult [] = mb_substr ($ sString , $ i , 1 , $ this ->sCharset );
631
+ }
632
+ return $ aResult ;
633
+ }
634
+ } else {
635
+ if ($ sString === '' ) {
636
+ return array ();
637
+ } else {
638
+ return str_split ($ sString );
639
+ }
640
+ }
641
+ }
642
+
629
643
private function strpos ($ sString , $ sNeedle , $ iOffset ) {
630
644
if ($ this ->oParserSettings ->bMultibyteSupport ) {
631
645
return mb_strpos ($ sString , $ sNeedle , $ iOffset , $ this ->sCharset );
0 commit comments