Skip to content

Commit 0b1f55d

Browse files
committed
Merge branch '6.4' into 7.3
* 6.4: Restore Relay 8.5 test account for PHP_ZTS being a boolean value on PHP 8.4+ [Intl] Update data to ICU 78.1 [Console] Fix exception message when abbreviation matches multiple hidden commands [FrameworkBundle] Fix TypeError when traversing scalar values in debug:config [DependencyInjection] Fix loop corruption in CheckTypeDeclarationsPass [DependencyInjection] Fix invalid PHP syntax for nullable TypedReference in PhpDumper Fix typo in comment [Translation][Routing] Fix typos [String] Fix normalization in trimPrefix/trimSuffix
2 parents f964760 + 50590a0 commit 0b1f55d

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

Tests/UnicodeStringTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,56 @@
1616

1717
class UnicodeStringTest extends AbstractUnicodeTestCase
1818
{
19+
/**
20+
* @dataProvider provideTrimNormalization
21+
*/
22+
public function testTrimPrefixNormalization(string $expected, string $string, $prefix)
23+
{
24+
$str = new UnicodeString($string);
25+
$this->assertSame($expected, $str->trimPrefix($prefix)->toString());
26+
}
27+
28+
/**
29+
* @dataProvider provideTrimNormalization
30+
*/
31+
public function testTrimSuffixNormalization(string $expected, string $string, $suffix)
32+
{
33+
$suffixStr = match (true) {
34+
$suffix instanceof AbstractString => $suffix->toString(),
35+
\is_array($suffix) => implode('', $suffix),
36+
$suffix instanceof \Traversable => implode('', iterator_to_array($suffix)),
37+
default => (string) $suffix,
38+
};
39+
40+
$str = new UnicodeString($expected.$suffixStr);
41+
$this->assertSame($expected, $str->trimSuffix($suffix)->toString());
42+
}
43+
44+
public static function provideTrimNormalization(): iterable
45+
{
46+
// "é" in NFC (\xC3\xA9) vs NFD (\x65\xCC\x81)
47+
$nfc = "\xC3\xA9";
48+
$nfd = "\x65\xCC\x81";
49+
50+
yield 'nfc_string_nfd_prefix' => ['abc', $nfc.'abc', $nfd];
51+
yield 'nfd_string_nfc_prefix' => ['abc', $nfd.'abc', $nfc];
52+
53+
yield 'abstract_string' => ['abc', $nfc.'abc', new UnicodeString($nfd)];
54+
55+
yield 'array' => ['abc', $nfc.'abc', [$nfd]];
56+
57+
yield 'stringable' => ['abc', $nfc.'abc', new class($nfd) implements \Stringable {
58+
public function __construct(private string $s)
59+
{
60+
}
61+
62+
public function __toString(): string
63+
{
64+
return $this->s;
65+
}
66+
}];
67+
}
68+
1969
protected static function createFromString(string $string): AbstractString
2070
{
2171
return new UnicodeString($string);

UnicodeString.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,44 @@ public function startsWith(string|iterable|AbstractString $prefix): bool
362362
return $prefix === grapheme_extract($this->string, \strlen($prefix), \GRAPHEME_EXTR_MAXBYTES);
363363
}
364364

365+
public function trimPrefix($prefix): static
366+
{
367+
if (\is_array($prefix) || $prefix instanceof \Traversable) {
368+
return parent::trimPrefix($prefix);
369+
}
370+
371+
if ($prefix instanceof AbstractString) {
372+
$prefix = $prefix->string;
373+
} else {
374+
$prefix = (string) $prefix;
375+
}
376+
377+
if (!normalizer_is_normalized($prefix, \Normalizer::NFC)) {
378+
$prefix = normalizer_normalize($prefix, \Normalizer::NFC);
379+
}
380+
381+
return parent::trimPrefix($prefix);
382+
}
383+
384+
public function trimSuffix($suffix): static
385+
{
386+
if (\is_array($suffix) || $suffix instanceof \Traversable) {
387+
return parent::trimSuffix($suffix);
388+
}
389+
390+
if ($suffix instanceof AbstractString) {
391+
$suffix = $suffix->string;
392+
} else {
393+
$suffix = (string) $suffix;
394+
}
395+
396+
if (!normalizer_is_normalized($suffix, \Normalizer::NFC)) {
397+
$suffix = normalizer_normalize($suffix, \Normalizer::NFC);
398+
}
399+
400+
return parent::trimSuffix($suffix);
401+
}
402+
365403
public function __wakeup(): void
366404
{
367405
if (!\is_string($this->string)) {

0 commit comments

Comments
 (0)