Skip to content

Commit

Permalink
bug #507 mbstring: Fix mb_rtrim() for UTF-8 text (nsfisis)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.x branch.

Discussion
----------

mbstring: Fix mb_rtrim() for UTF-8 text

# Component

Polyfill-mbstring

# Problem

`mb_rtrim()` returns an empty string for UTF-8 text.

# How to Reproduce

```php
echo \Symfony\Polyfill\Mbstring\Mbstring::mb_rtrim(' あいうえお ');
// => '' (empty string)
```

Expected result: `' あいうえお'`

# Cause

The regular expression used in `mb_rtrim()` implementation does not have `u` flag.

# Changes

* Add `u` flag to the regular expression used in `mb_rtrim()` implementation as `mb_trim()` and `mb_ltrim()` do.
* Add tests.

Commits
-------

65d1260 mbstring: Fix mb_rtrim() for UTF-8 text
  • Loading branch information
GromNaN committed Dec 22, 2024
2 parents 2ef3477 + 65d1260 commit d7e262e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Mbstring/Mbstring.php
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ public static function mb_ltrim(string $string, ?string $characters = null, ?str

public static function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string
{
return self::mb_internal_trim('{[%s]+$}D', $string, $characters, $encoding, __FUNCTION__);
return self::mb_internal_trim('{[%s]+$}Du', $string, $characters, $encoding, __FUNCTION__);
}

private static function mb_internal_trim(string $regex, string $string, ?string $characters, ?string $encoding, string $function): string
Expand Down
2 changes: 2 additions & 0 deletions tests/Mbstring/MbstringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@ public static function mbLTrimProvider(): iterable
yield [' test ', ' test ', ''];

yield ['いああああ', 'あああああああああああああああああああああああああああああああああいああああ', ''];
yield ['あいうえお ', ' あいうえお '];

yield ['漢字', "\u{FFFE}漢字", "\u{FFFE}\u{FEFF}"];
yield [' abcd ', ' abcd ', ''];
Expand All @@ -902,6 +903,7 @@ public static function mbRTrimProvider(): iterable
yield [' a', str_repeat(' ', 129).'a'];

yield ['あああああああああああああああああああああああああああああああああい', 'あああああああああああああああああああああああああああああああああいああああ', ''];
yield [' あいうえお', ' あいうえお '];

yield [' abcd ', ' abcd ', ''];

Expand Down

0 comments on commit d7e262e

Please sign in to comment.