Skip to content

Commit e053dd0

Browse files
committed
[Translator] Improve performance, reduce s() calls and inline IntlMessageParser#offset()
1 parent e51918f commit e053dd0

File tree

2 files changed

+26
-34
lines changed

2 files changed

+26
-34
lines changed

src/Translator/src/Intl/IntlMessageParser.php

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\UX\Translator\Intl;
1313

14+
use Symfony\Component\String\AbstractString;
1415
use function Symfony\Component\String\s;
1516

1617
/**
@@ -20,15 +21,15 @@
2021
*/
2122
class IntlMessageParser
2223
{
23-
private string $message;
24+
private AbstractString $message;
2425
private Position $position;
2526
private bool $ignoreTag;
2627
private bool $requiresOtherClause;
2728

2829
public function __construct(
2930
string $message,
3031
) {
31-
$this->message = $message;
32+
$this->message = s($message);
3233
$this->position = new Position(0, 1, 1);
3334
$this->ignoreTag = true;
3435
$this->requiresOtherClause = true;
@@ -112,14 +113,14 @@ private function parseMessage(int $nestingLevel, mixed $parentArgType, bool $exp
112113
*/
113114
private function parseTagName(): string
114115
{
115-
$startOffset = $this->offset();
116+
$startOffset = $this->position->offset;
116117

117118
$this->bump(); // the first tag name character
118119
while (!$this->isEOF() && Utils::isPotentialElementNameChar($this->char())) {
119120
$this->bump();
120121
}
121122

122-
return s($this->message)->slice($startOffset, $this->offset() - $startOffset)->toString();
123+
return $this->message->slice($startOffset, $this->position->offset - $startOffset)->toString();
123124
}
124125

125126
/**
@@ -365,7 +366,7 @@ private function parseIdentifierIfPossible(): array
365366
{
366367
$startingPosition = clone $this->position;
367368

368-
$startOffset = $this->offset();
369+
$startOffset = $this->position->offset;
369370
$value = Utils::matchIdentifierAtIndex($this->message, $startOffset);
370371
$endOffset = $startOffset + s($value)->length();
371372

@@ -445,9 +446,9 @@ private function parseArgumentOptions(
445446
);
446447

447448
// Extract style or skeleton
448-
if ($styleAndLocation && s($styleAndLocation['style'] ?? '')->startsWith('::')) {
449+
if ($styleAndLocation && ($style = s($styleAndLocation['style'] ?? ''))->startsWith('::')) {
449450
// Skeleton starts with `::`.
450-
$skeleton = s($styleAndLocation['style'])->slice(2)->trimStart()->toString();
451+
$skeleton = $style->slice(2)->trimStart()->toString();
451452

452453
if ('number' === $argType) {
453454
$result = $this->parseNumberSkeletonFromString(
@@ -663,7 +664,7 @@ private function parseSimpleArgStyleIfPossible(): array
663664
--$nestedBraces;
664665
} else {
665666
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(),
667668
'err' => null,
668669
];
669670
}
@@ -676,7 +677,7 @@ private function parseSimpleArgStyleIfPossible(): array
676677
}
677678

678679
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(),
680681
'err' => null,
681682
];
682683
}
@@ -735,7 +736,7 @@ private function tryParsePluralOrSelectOptions(
735736
return $result;
736737
}
737738
$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();
739740
} else {
740741
break;
741742
}
@@ -864,14 +865,9 @@ private function tryParseDecimalInteger(
864865
];
865866
}
866867

867-
private function offset(): int
868-
{
869-
return $this->position->offset;
870-
}
871-
872868
private function isEOF(): bool
873869
{
874-
return $this->offset() === s($this->message)->length();
870+
return $this->position->offset === $this->message->length();
875871
}
876872

877873
/**
@@ -882,14 +878,12 @@ private function isEOF(): bool
882878
*/
883879
private function char(): int
884880
{
885-
$message = s($this->message);
886-
887881
$offset = $this->position->offset;
888-
if ($offset >= $message->length()) {
882+
if ($offset >= $this->message->length()) {
889883
throw new \OutOfBoundsException();
890884
}
891885

892-
$code = $message->slice($offset, 1)->codePointsAt(0)[0] ?? null;
886+
$code = $this->message->slice($offset, 1)->codePointsAt(0)[0] ?? null;
893887
if (null === $code) {
894888
throw new \Exception("Offset {$offset} is at invalid UTF-16 code unit boundary");
895889
}
@@ -909,7 +903,7 @@ private function error(string $kind, Location $location): array
909903
'err' => [
910904
'kind' => $kind,
911905
'location' => $location,
912-
'message' => $this->message,
906+
'message' => $this->message->toString(),
913907
],
914908
];
915909
}
@@ -941,7 +935,7 @@ private function bump(): void
941935
*/
942936
private function bumpIf(string $prefix): bool
943937
{
944-
if (s($this->message)->slice($this->offset())->startsWith($prefix)) {
938+
if ($this->message->slice($this->position->offset)->startsWith($prefix)) {
945939
for ($i = 0, $len = \strlen($prefix); $i < $len; ++$i) {
946940
$this->bump();
947941
}
@@ -958,14 +952,13 @@ private function bumpIf(string $prefix): bool
958952
*/
959953
private function bumpUntil(string $pattern): bool
960954
{
961-
$currentOffset = $this->offset();
962-
$index = s($this->message)->indexOf($pattern, $currentOffset);
955+
$index = $this->message->indexOf($pattern, $this->position->offset);
963956
if ($index >= 0) {
964957
$this->bumpTo($index);
965958

966959
return true;
967960
} else {
968-
$this->bumpTo(s($this->message)->length());
961+
$this->bumpTo($this->message->length());
969962

970963
return false;
971964
}
@@ -979,13 +972,13 @@ private function bumpUntil(string $pattern): bool
979972
*/
980973
private function bumpTo(int $targetOffset)
981974
{
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));
984977
}
985978

986-
$targetOffset = min($targetOffset, s($this->message)->length());
979+
$targetOffset = min($targetOffset, $this->message->length());
987980
while (true) {
988-
$offset = $this->offset();
981+
$offset = $this->position->offset;
989982
if ($offset === $targetOffset) {
990983
break;
991984
}
@@ -1019,8 +1012,7 @@ private function peek(): ?int
10191012
}
10201013

10211014
$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));
10241016

10251017
return $nextCodes[0] ?? null;
10261018
}

src/Translator/src/Intl/Utils.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\UX\Translator\Intl;
1313

14-
use function Symfony\Component\String\s;
14+
use Symfony\Component\String\AbstractString;
1515

1616
/**
1717
* @experimental
@@ -355,12 +355,12 @@ public static function fromCodePoint(int ...$codePoints): string
355355
return $elements;
356356
}
357357

358-
public static function matchIdentifierAtIndex(string $s, int $index): string
358+
public static function matchIdentifierAtIndex(AbstractString $s, int $index): string
359359
{
360360
$match = [];
361361

362362
while (true) {
363-
$c = s($s)->codePointsAt($index)[0] ?? null;
363+
$c = $s->codePointsAt($index)[0] ?? null;
364364
if (null === $c || self::isWhiteSpace($c) || self::isPatternSyntax($c)) {
365365
break;
366366
}

0 commit comments

Comments
 (0)