11
11
12
12
namespace Symfony \UX \Translator \Intl ;
13
13
14
+ use Symfony \Component \String \AbstractString ;
14
15
use function Symfony \Component \String \s ;
15
16
16
17
/**
20
21
*/
21
22
class IntlMessageParser
22
23
{
23
- private string $ message ;
24
+ private AbstractString $ message ;
24
25
private Position $ position ;
25
26
private bool $ ignoreTag ;
26
27
private bool $ requiresOtherClause ;
27
28
28
29
public function __construct (
29
30
string $ message ,
30
31
) {
31
- $ this ->message = $ message ;
32
+ $ this ->message = s ( $ message) ;
32
33
$ this ->position = new Position (0 , 1 , 1 );
33
34
$ this ->ignoreTag = true ;
34
35
$ this ->requiresOtherClause = true ;
@@ -112,14 +113,14 @@ private function parseMessage(int $nestingLevel, mixed $parentArgType, bool $exp
112
113
*/
113
114
private function parseTagName (): string
114
115
{
115
- $ startOffset = $ this ->offset () ;
116
+ $ startOffset = $ this ->position -> offset ;
116
117
117
118
$ this ->bump (); // the first tag name character
118
119
while (!$ this ->isEOF () && Utils::isPotentialElementNameChar ($ this ->char ())) {
119
120
$ this ->bump ();
120
121
}
121
122
122
- return s ( $ this ->message ) ->slice ($ startOffset , $ this ->offset () - $ startOffset )->toString ();
123
+ return $ this ->message ->slice ($ startOffset , $ this ->position -> offset - $ startOffset )->toString ();
123
124
}
124
125
125
126
/**
@@ -365,7 +366,7 @@ private function parseIdentifierIfPossible(): array
365
366
{
366
367
$ startingPosition = clone $ this ->position ;
367
368
368
- $ startOffset = $ this ->offset () ;
369
+ $ startOffset = $ this ->position -> offset ;
369
370
$ value = Utils::matchIdentifierAtIndex ($ this ->message , $ startOffset );
370
371
$ endOffset = $ startOffset + s ($ value )->length ();
371
372
@@ -445,9 +446,9 @@ private function parseArgumentOptions(
445
446
);
446
447
447
448
// Extract style or skeleton
448
- if ($ styleAndLocation && s ($ styleAndLocation ['style ' ] ?? '' )->startsWith (':: ' )) {
449
+ if ($ styleAndLocation && ( $ style = s ($ styleAndLocation ['style ' ] ?? '' ) )->startsWith (':: ' )) {
449
450
// Skeleton starts with `::`.
450
- $ skeleton = s ( $ styleAndLocation [ ' style ' ]) ->slice (2 )->trimStart ()->toString ();
451
+ $ skeleton = $ style ->slice (2 )->trimStart ()->toString ();
451
452
452
453
if ('number ' === $ argType ) {
453
454
$ result = $ this ->parseNumberSkeletonFromString (
@@ -663,7 +664,7 @@ private function parseSimpleArgStyleIfPossible(): array
663
664
--$ nestedBraces ;
664
665
} else {
665
666
return [
666
- 'val ' => s ( $ this ->message ) ->slice ($ startPosition ->offset , $ this ->offset () - $ startPosition ->offset )->toString (),
667
+ 'val ' => $ this ->message ->slice ($ startPosition ->offset , $ this ->position -> offset - $ startPosition ->offset )->toString (),
667
668
'err ' => null ,
668
669
];
669
670
}
@@ -676,7 +677,7 @@ private function parseSimpleArgStyleIfPossible(): array
676
677
}
677
678
678
679
return [
679
- 'val ' => s ( $ this ->message ) ->slice ($ startPosition ->offset , $ this ->offset () - $ startPosition ->offset )->toString (),
680
+ 'val ' => $ this ->message ->slice ($ startPosition ->offset , $ this ->position -> offset - $ startPosition ->offset )->toString (),
680
681
'err ' => null ,
681
682
];
682
683
}
@@ -735,7 +736,7 @@ private function tryParsePluralOrSelectOptions(
735
736
return $ result ;
736
737
}
737
738
$ selectorLocation = new Location ($ startPosition , clone $ this ->position );
738
- $ selector = s ( $ this ->message ) ->slice ($ startPosition ->offset , $ this ->offset () - $ startPosition ->offset )->toString ();
739
+ $ selector = $ this ->message ->slice ($ startPosition ->offset , $ this ->position -> offset - $ startPosition ->offset )->toString ();
739
740
} else {
740
741
break ;
741
742
}
@@ -864,14 +865,9 @@ private function tryParseDecimalInteger(
864
865
];
865
866
}
866
867
867
- private function offset (): int
868
- {
869
- return $ this ->position ->offset ;
870
- }
871
-
872
868
private function isEOF (): bool
873
869
{
874
- return $ this ->offset () === s ( $ this ->message ) ->length ();
870
+ return $ this ->position -> offset === $ this ->message ->length ();
875
871
}
876
872
877
873
/**
@@ -882,14 +878,12 @@ private function isEOF(): bool
882
878
*/
883
879
private function char (): int
884
880
{
885
- $ message = s ($ this ->message );
886
-
887
881
$ offset = $ this ->position ->offset ;
888
- if ($ offset >= $ message ->length ()) {
882
+ if ($ offset >= $ this -> message ->length ()) {
889
883
throw new \OutOfBoundsException ();
890
884
}
891
885
892
- $ code = $ message ->slice ($ offset , 1 )->codePointsAt (0 )[0 ] ?? null ;
886
+ $ code = $ this -> message ->slice ($ offset , 1 )->codePointsAt (0 )[0 ] ?? null ;
893
887
if (null === $ code ) {
894
888
throw new \Exception ("Offset {$ offset } is at invalid UTF-16 code unit boundary " );
895
889
}
@@ -909,7 +903,7 @@ private function error(string $kind, Location $location): array
909
903
'err ' => [
910
904
'kind ' => $ kind ,
911
905
'location ' => $ location ,
912
- 'message ' => $ this ->message ,
906
+ 'message ' => $ this ->message -> toString () ,
913
907
],
914
908
];
915
909
}
@@ -941,7 +935,7 @@ private function bump(): void
941
935
*/
942
936
private function bumpIf (string $ prefix ): bool
943
937
{
944
- if (s ( $ this ->message ) ->slice ($ this ->offset () )->startsWith ($ prefix )) {
938
+ if ($ this ->message ->slice ($ this ->position -> offset )->startsWith ($ prefix )) {
945
939
for ($ i = 0 , $ len = \strlen ($ prefix ); $ i < $ len ; ++$ i ) {
946
940
$ this ->bump ();
947
941
}
@@ -958,14 +952,13 @@ private function bumpIf(string $prefix): bool
958
952
*/
959
953
private function bumpUntil (string $ pattern ): bool
960
954
{
961
- $ currentOffset = $ this ->offset ();
962
- $ index = s ($ this ->message )->indexOf ($ pattern , $ currentOffset );
955
+ $ index = $ this ->message ->indexOf ($ pattern , $ this ->position ->offset );
963
956
if ($ index >= 0 ) {
964
957
$ this ->bumpTo ($ index );
965
958
966
959
return true ;
967
960
} else {
968
- $ this ->bumpTo (s ( $ this ->message ) ->length ());
961
+ $ this ->bumpTo ($ this ->message ->length ());
969
962
970
963
return false ;
971
964
}
@@ -979,13 +972,13 @@ private function bumpUntil(string $pattern): bool
979
972
*/
980
973
private function bumpTo (int $ targetOffset )
981
974
{
982
- if ($ this ->offset () > $ targetOffset ) {
983
- throw new \Exception (\sprintf ('targetOffset %s must be greater than or equal to the current offset %d ' , $ targetOffset , $ this ->offset () ));
975
+ if ($ this ->position -> offset > $ targetOffset ) {
976
+ throw new \Exception (\sprintf ('targetOffset %s must be greater than or equal to the current offset %d ' , $ targetOffset , $ this ->position -> offset ));
984
977
}
985
978
986
- $ targetOffset = min ($ targetOffset , s ( $ this ->message ) ->length ());
979
+ $ targetOffset = min ($ targetOffset , $ this ->message ->length ());
987
980
while (true ) {
988
- $ offset = $ this ->offset () ;
981
+ $ offset = $ this ->position -> offset ;
989
982
if ($ offset === $ targetOffset ) {
990
983
break ;
991
984
}
@@ -1019,8 +1012,7 @@ private function peek(): ?int
1019
1012
}
1020
1013
1021
1014
$ code = $ this ->char ();
1022
- $ offset = $ this ->offset ();
1023
- $ nextCodes = s ($ this ->message )->codePointsAt ($ offset + ($ code >= 0x10000 ? 2 : 1 ));
1015
+ $ nextCodes = $ this ->message ->codePointsAt ($ this ->position ->offset + ($ code >= 0x10000 ? 2 : 1 ));
1024
1016
1025
1017
return $ nextCodes [0 ] ?? null ;
1026
1018
}
0 commit comments