From 46a23f87d9a119b8e7bce34ba5dad56d12ee5da6 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 2 Oct 2019 14:01:44 +0200 Subject: [PATCH] [String] fix toAscii() --- .../String/AbstractUnicodeString.php | 19 +++++++++++-------- .../String/Tests/AbstractUtf8TestCase.php | 7 +++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/String/AbstractUnicodeString.php b/src/Symfony/Component/String/AbstractUnicodeString.php index 06aff1b4510c4..b5a6661494876 100644 --- a/src/Symfony/Component/String/AbstractUnicodeString.php +++ b/src/Symfony/Component/String/AbstractUnicodeString.php @@ -81,8 +81,11 @@ public function ascii(array $rules = []): self $s = $str->string; $str->string = ''; - $rules[] = 'nfkd'; - $rules[] = '[:nonspacing mark:] remove'; + if (!$rules) { + $rules[] = '[:nonspacing mark:] remove'; + } + + array_unshift($rules, 'nfd'); if (\function_exists('transliterator_transliterate')) { $rules[] = 'any-latin/bgn'; @@ -90,8 +93,8 @@ public function ascii(array $rules = []): self $rules[] = '[:nonspacing mark:] remove'; } - while (\strlen($s) !== $i = strspn($s, self::ASCII)) { - if (0 !== $i) { + while (\strlen($s) - 1 > $i = strspn($s, self::ASCII)) { + if (0 < --$i) { $str->string .= substr($s, 0, $i); $s = substr($s, $i); } @@ -106,10 +109,10 @@ public function ascii(array $rules = []): self continue; } - if ('nfkd' === $rule = strtolower($rule)) { - if (!normalizer_is_normalized($s, self::NFKD)) { - $s = normalizer_normalize($s, self::NFKD); - } + if ('nfd' === $rule = strtolower($rule)) { + normalizer_is_normalized($s, self::NFD) ?: $s = normalizer_normalize($s, self::NFD); + } elseif ('nfkd' === $rule) { + normalizer_is_normalized($s, self::NFKD) ?: $s = normalizer_normalize($s, self::NFKD); } elseif ('[:nonspacing mark:] remove' === $rule) { $s = preg_replace('/\p{Mn}++/u', '', $s); } elseif ('de-ascii' === $rule) { diff --git a/src/Symfony/Component/String/Tests/AbstractUtf8TestCase.php b/src/Symfony/Component/String/Tests/AbstractUtf8TestCase.php index 9f63161ce01aa..5eed9fc224ef5 100644 --- a/src/Symfony/Component/String/Tests/AbstractUtf8TestCase.php +++ b/src/Symfony/Component/String/Tests/AbstractUtf8TestCase.php @@ -13,6 +13,13 @@ public function testCreateFromStringWithInvalidUtf8Input() static::createFromString("\xE9"); } + public function testAscii() + { + $s = static::createFromString('Dieser Wert sollte größer oder gleich'); + $this->assertSame('Dieser Wert sollte grosser oder gleich', (string) $s->ascii()); + $this->assertSame('Dieser Wert sollte groesser oder gleich', (string) $s->ascii(['de-ASCII'])); + } + public function provideCreateFromCodePoint(): array { return [