10
10
11
11
/**
12
12
* An override of Twig's Lexer to add whitespace and new line detection.
13
+ *
14
+ * Since the regex are using bytes as position, mb_ methods are voluntary not used.
15
+ * phpcs:disable SymfonyCustom.PHP.EncourageMultiBytes
13
16
*/
14
17
class Tokenizer
15
18
{
@@ -193,7 +196,7 @@ protected function resetState(Source $source): void
193
196
$ this ->bracketsAndTernary = [];
194
197
195
198
$ this ->code = str_replace (["\r\n" , "\r" ], "\n" , $ source ->getCode ());
196
- $ this ->end = mb_strlen ($ this ->code );
199
+ $ this ->end = strlen ($ this ->code );
197
200
$ this ->filename = $ source ->getName ();
198
201
}
199
202
@@ -310,8 +313,8 @@ protected function moveCurrentPosition(int $value = 1): void
310
313
*/
311
314
protected function moveCursor (string $ value ): void
312
315
{
313
- $ this ->cursor += mb_strlen ($ value );
314
- $ this ->line += mb_substr_count ($ value , "\n" );
316
+ $ this ->cursor += strlen ($ value );
317
+ $ this ->line += substr_count ($ value , "\n" );
315
318
}
316
319
317
320
/**
@@ -322,7 +325,7 @@ protected function moveCursor(string $value): void
322
325
*/
323
326
protected function pushToken (int $ type , string $ value = null ): void
324
327
{
325
- $ tokenPositionInLine = $ this ->cursor - mb_strrpos ( mb_substr ($ this ->code , 0 , $ this ->cursor ), PHP_EOL );
328
+ $ tokenPositionInLine = $ this ->cursor - strrpos ( substr ($ this ->code , 0 , $ this ->cursor ), PHP_EOL );
326
329
$ this ->tokens [] = new Token ($ type , $ this ->line , $ tokenPositionInLine , $ this ->filename , $ value );
327
330
}
328
331
@@ -350,7 +353,7 @@ protected function lexExpression(): void
350
353
$ this ->lexName ($ match [0 ]);
351
354
} elseif (preg_match (self ::REGEX_NUMBER , $ this ->code , $ match , 0 , $ this ->cursor )) {
352
355
$ this ->lexNumber ($ match [0 ]);
353
- } elseif (false !== mb_strpos (self ::PUNCTUATION , $ this ->code [$ this ->cursor ])) {
356
+ } elseif (false !== strpos (self ::PUNCTUATION , $ this ->code [$ this ->cursor ])) {
354
357
$ this ->lexPunctuation ();
355
358
} elseif (preg_match (self ::REGEX_STRING , $ this ->code , $ match , 0 , $ this ->cursor )) {
356
359
$ this ->lexString ($ match [0 ]);
@@ -436,7 +439,7 @@ protected function lexDqString(): void
436
439
$ this ->lexStartInterpolation ();
437
440
} elseif (
438
441
preg_match (self ::REGEX_DQ_STRING_PART , $ this ->code , $ match , 0 , $ this ->cursor )
439
- && mb_strlen ($ match [0 ]) > 0
442
+ && strlen ($ match [0 ]) > 0
440
443
) {
441
444
$ this ->pushToken (Token::STRING_TYPE , $ match [0 ]);
442
445
$ this ->moveCursor ($ match [0 ]);
@@ -500,8 +503,8 @@ protected function lexData(int $limit = 0): void
500
503
$ value = $ match [0 ];
501
504
502
505
// Stop if cursor reaches the next token start.
503
- if (0 !== $ limit && $ limit <= ($ this ->cursor + mb_strlen ($ value ))) {
504
- $ value = mb_substr ($ value , 0 , $ limit - $ this ->cursor );
506
+ if (0 !== $ limit && $ limit <= ($ this ->cursor + strlen ($ value ))) {
507
+ $ value = substr ($ value , 0 , $ limit - $ this ->cursor );
505
508
}
506
509
507
510
// Fixing token start among expressions and comments.
@@ -702,7 +705,7 @@ protected function lexPunctuation(): void
702
705
703
706
return ;
704
707
}
705
- if (false !== mb_strpos (',)]} ' , $ currentToken )) {
708
+ if (false !== strpos (',)]} ' , $ currentToken )) {
706
709
// Because {{ foo ? 'yes' }} is the same as {{ foo ? 'yes' : '' }}
707
710
do {
708
711
array_pop ($ this ->bracketsAndTernary );
@@ -716,9 +719,9 @@ protected function lexPunctuation(): void
716
719
}
717
720
}
718
721
719
- if (false !== mb_strpos ('([{ ' , $ currentToken )) {
722
+ if (false !== strpos ('([{ ' , $ currentToken )) {
720
723
$ this ->bracketsAndTernary [] = [$ currentToken , $ this ->line ];
721
- } elseif (false !== mb_strpos (')]} ' , $ currentToken )) {
724
+ } elseif (false !== strpos (')]} ' , $ currentToken )) {
722
725
if (0 === count ($ this ->bracketsAndTernary )) {
723
726
throw new Exception (sprintf ('Unexpected "%s" ' , $ currentToken ));
724
727
}
0 commit comments