Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a176d1a

Browse files
bug #33807 [String] fix toAscii() (nicolas-grekas)
This PR was merged into the 5.0-dev branch. Discussion ---------- [String] fix toAscii() | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | Deprecations? | | Tickets | - | License | MIT | Doc PR | - Commits ------- 46a23f8 [String] fix toAscii()
2 parents 564a657 + 46a23f8 commit a176d1a

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/Symfony/Component/String/AbstractUnicodeString.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,20 @@ public function ascii(array $rules = []): self
8181
$s = $str->string;
8282
$str->string = '';
8383

84-
$rules[] = 'nfkd';
85-
$rules[] = '[:nonspacing mark:] remove';
84+
if (!$rules) {
85+
$rules[] = '[:nonspacing mark:] remove';
86+
}
87+
88+
array_unshift($rules, 'nfd');
8689

8790
if (\function_exists('transliterator_transliterate')) {
8891
$rules[] = 'any-latin/bgn';
8992
$rules[] = 'nfkd';
9093
$rules[] = '[:nonspacing mark:] remove';
9194
}
9295

93-
while (\strlen($s) !== $i = strspn($s, self::ASCII)) {
94-
if (0 !== $i) {
96+
while (\strlen($s) - 1 > $i = strspn($s, self::ASCII)) {
97+
if (0 < --$i) {
9598
$str->string .= substr($s, 0, $i);
9699
$s = substr($s, $i);
97100
}
@@ -106,10 +109,10 @@ public function ascii(array $rules = []): self
106109
continue;
107110
}
108111

109-
if ('nfkd' === $rule = strtolower($rule)) {
110-
if (!normalizer_is_normalized($s, self::NFKD)) {
111-
$s = normalizer_normalize($s, self::NFKD);
112-
}
112+
if ('nfd' === $rule = strtolower($rule)) {
113+
normalizer_is_normalized($s, self::NFD) ?: $s = normalizer_normalize($s, self::NFD);
114+
} elseif ('nfkd' === $rule) {
115+
normalizer_is_normalized($s, self::NFKD) ?: $s = normalizer_normalize($s, self::NFKD);
113116
} elseif ('[:nonspacing mark:] remove' === $rule) {
114117
$s = preg_replace('/\p{Mn}++/u', '', $s);
115118
} elseif ('de-ascii' === $rule) {

src/Symfony/Component/String/Tests/AbstractUtf8TestCase.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ public function testCreateFromStringWithInvalidUtf8Input()
1313
static::createFromString("\xE9");
1414
}
1515

16+
public function testAscii()
17+
{
18+
$s = static::createFromString('Dieser Wert sollte größer oder gleich');
19+
$this->assertSame('Dieser Wert sollte grosser oder gleich', (string) $s->ascii());
20+
$this->assertSame('Dieser Wert sollte groesser oder gleich', (string) $s->ascii(['de-ASCII']));
21+
}
22+
1623
public function provideCreateFromCodePoint(): array
1724
{
1825
return [

0 commit comments

Comments
 (0)