Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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()
  • Loading branch information
nicolas-grekas committed Oct 2, 2019
2 parents 564a657 + 46a23f8 commit a176d1a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/Symfony/Component/String/AbstractUnicodeString.php
Expand Up @@ -81,17 +81,20 @@ 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';
$rules[] = 'nfkd';
$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);
}
Expand All @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/String/Tests/AbstractUtf8TestCase.php
Expand Up @@ -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 [
Expand Down

0 comments on commit a176d1a

Please sign in to comment.