Skip to content

Commit d50e862

Browse files
committed
Merge branch '7.3' into 7.4
* 7.3: fix test Restore Relay 8.5 test account for PHP_ZTS being a boolean value on PHP 8.4+ [Intl] Update data to ICU 78.1 [Notifier][Smsbox] Add tests for `Mode` enum [DependencyInjection] Remove unused variable [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 [Security] Fix UserBadge validation bypass via identifier normalizer [DependencyInjection] Fix invalid PHP syntax for nullable TypedReference in PhpDumper Fix typo in comment [Translation][Routing] Fix typos [Config] Fix nullable EnumNode with BackedEnum [String] Fix normalization in trimPrefix/trimSuffix
2 parents bc78521 + 0b1f55d commit d50e862

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

Tests/UnicodeStringTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,58 @@
1111

1212
namespace Symfony\Component\String\Tests;
1313

14+
use PHPUnit\Framework\Attributes\DataProvider;
1415
use Symfony\Component\String\AbstractString;
1516
use Symfony\Component\String\UnicodeString;
1617

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

UnicodeString.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,44 @@ public function startsWith(string|iterable|AbstractString $prefix): bool
366366
return $prefix === $grapheme;
367367
}
368368

369+
public function trimPrefix($prefix): static
370+
{
371+
if (\is_array($prefix) || $prefix instanceof \Traversable) {
372+
return parent::trimPrefix($prefix);
373+
}
374+
375+
if ($prefix instanceof AbstractString) {
376+
$prefix = $prefix->string;
377+
} else {
378+
$prefix = (string) $prefix;
379+
}
380+
381+
if (!normalizer_is_normalized($prefix, \Normalizer::NFC)) {
382+
$prefix = normalizer_normalize($prefix, \Normalizer::NFC);
383+
}
384+
385+
return parent::trimPrefix($prefix);
386+
}
387+
388+
public function trimSuffix($suffix): static
389+
{
390+
if (\is_array($suffix) || $suffix instanceof \Traversable) {
391+
return parent::trimSuffix($suffix);
392+
}
393+
394+
if ($suffix instanceof AbstractString) {
395+
$suffix = $suffix->string;
396+
} else {
397+
$suffix = (string) $suffix;
398+
}
399+
400+
if (!normalizer_is_normalized($suffix, \Normalizer::NFC)) {
401+
$suffix = normalizer_normalize($suffix, \Normalizer::NFC);
402+
}
403+
404+
return parent::trimSuffix($suffix);
405+
}
406+
369407
public function __unserialize(array $data): void
370408
{
371409
if ($wakeup = self::class !== (new \ReflectionMethod($this, '__wakeup'))->class && self::class === (new \ReflectionMethod($this, '__unserialize'))->class) {

0 commit comments

Comments
 (0)