Skip to content

Commit 301b845

Browse files
committed
Adapt to nullable characters
1 parent c44c705 commit 301b845

File tree

7 files changed

+89
-37
lines changed

7 files changed

+89
-37
lines changed

src/Mbstring/Mbstring.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ final class Mbstring
8484
private static $internalEncoding = 'UTF-8';
8585
private const CHARACTERS = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}";
8686

87-
8887
public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null)
8988
{
9089
if (\is_array($s)) {
@@ -985,29 +984,29 @@ private static function getEncoding($encoding)
985984
return $encoding;
986985
}
987986

988-
public static function mb_trim(string $string, string $characters = self::CHARACTERS, ?string $encoding = null): string
987+
public static function mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string
989988
{
990989
return self::mb_internal_trim('^[%s]+|[%s]+$', $string, $characters, $encoding);
991990
}
992991

993-
public static function mb_ltrim(string $string, string $characters = self::CHARACTERS, ?string $encoding = null): string
992+
public static function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null): string
994993
{
995994
return self::mb_internal_trim('^[%s]+', $string, $characters, $encoding);
996995
}
997996

998-
public static function mb_rtrim(string $string, string $characters = self::CHARACTERS, ?string $encoding = null): string
997+
public static function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string
999998
{
1000999
return self::mb_internal_trim('[%s]+$', $string, $characters, $encoding);
10011000
}
10021001

1003-
private static function mb_internal_trim(string $regex, string $string, string $characters = self::CHARACTERS, ?string $encoding = null): string
1002+
private static function mb_internal_trim(string $regex, string $string, ?string $characters = null, ?string $encoding = null): string
10041003
{
10051004
if (null === $encoding) {
1006-
$encoding = self::mb_internal_encoding();
1005+
$encoding = mb_internal_encoding();
10071006
}
10081007

10091008
try {
1010-
$validEncoding = @self::mb_check_encoding('', $encoding);
1009+
$validEncoding = @mb_check_encoding('', $encoding);
10111010
} catch (\ValueError $e) {
10121011
throw new \ValueError(sprintf('%s(): Argument #3 ($encoding) must be a valid encoding, "%s" given.', debug_backtrace()[1]['function'], $encoding));
10131012
}
@@ -1021,23 +1020,27 @@ private static function mb_internal_trim(string $regex, string $string, string $
10211020
return null === $encoding ? $string : mb_convert_encoding($string, $encoding);
10221021
}
10231022

1024-
$regexCharacter = preg_quote($characters, '/');
1023+
if (null === $characters) {
1024+
$characters = self::CHARACTERS;
1025+
}
1026+
1027+
$regexCharacter = preg_quote($characters ?? '', '/');
10251028
$regex = sprintf($regex, $regexCharacter, $regexCharacter);
10261029

10271030
if ('ASCII' === mb_detect_encoding($characters) && 'ASCII' === mb_detect_encoding($string) && !empty(array_intersect(str_split(self::CHARACTERS), str_split($string)))) {
10281031
$options = 'g';
10291032
} else {
10301033
$options = '';
10311034
}
1032-
1035+
10331036
try {
1034-
$a = mb_ereg_replace($regex, "", $string, $options);
1037+
$test = mb_ereg_replace($regex, "", $string, $options);
10351038

1036-
if (null === $a) {
1039+
if (null === $test) {
10371040
throw new \Exception();
10381041
}
10391042

1040-
return $a;
1043+
return $test;
10411044
} catch (\Exception $e) {
10421045
return preg_replace(sprintf('/%s/', $regex), "", $string);
10431046
}

src/Mbstring/bootstrap.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,15 @@ function mb_lcfirst(string $string, ?string $encoding = null): string { return p
145145
}
146146

147147
if (!function_exists('mb_trim')) {
148-
function mb_trim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): string { return p\Mbstring::mb_trim($string, $characters, $encoding); }
148+
function mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Mbstring::mb_trim($string, $characters, $encoding); }
149149
}
150150

151151
if (!function_exists('mb_ltrim')) {
152-
function mb_ltrim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): string { return p\Mbstring::mb_ltrim($string, $characters, $encoding); }
152+
function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Mbstring::mb_ltrim($string, $characters, $encoding); }
153153
}
154154

155155
if (!function_exists('mb_rtrim')) {
156-
function mb_rtrim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): string { return p\Mbstring::mb_rtrim($string, $characters, $encoding); }
156+
function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Mbstring::mb_rtrim($string, $characters, $encoding); }
157157
}
158158

159159

src/Mbstring/bootstrap80.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ function mb_ucfirst($string, ?string $encoding = null): string { return p\Mbstri
140140
function mb_lcfirst($string, ?string $encoding = null): string { return p\Mbstring::mb_lcfirst($string, $encoding); }
141141
}
142142

143+
if (!function_exists('mb_trim')) {
144+
function mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Mbstring::mb_trim($string, $characters, $encoding); }
145+
}
146+
147+
if (!function_exists('mb_ltrim')) {
148+
function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Mbstring::mb_ltrim($string, $characters, $encoding); }
149+
}
150+
151+
if (!function_exists('mb_rtrim')) {
152+
function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Mbstring::mb_rtrim($string, $characters, $encoding); }
153+
}
154+
143155
if (extension_loaded('mbstring')) {
144156
return;
145157
}

src/Php84/Php84.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
final class Php84
2121
{
22+
private const CHARACTERS = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}";
2223
public static function mb_ucfirst(string $string, ?string $encoding = null): string
2324
{
2425
if (null === $encoding) {
@@ -108,24 +109,23 @@ public static function array_all(array $array, callable $callback): bool
108109

109110
return true;
110111
}
111-
private const CHARACTERS = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}";
112112

113-
public static function mb_trim(string $string, string $characters = self::CHARACTERS, ?string $encoding = null): string
113+
public static function mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string
114114
{
115115
return self::mb_internal_trim('^[%s]+|[%s]+$', $string, $characters, $encoding);
116116
}
117117

118-
public static function mb_ltrim(string $string, string $characters = self::CHARACTERS, ?string $encoding = null): string
118+
public static function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null): string
119119
{
120120
return self::mb_internal_trim('^[%s]+', $string, $characters, $encoding);
121121
}
122122

123-
public static function mb_rtrim(string $string, string $characters = self::CHARACTERS, ?string $encoding = null): string
123+
public static function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string
124124
{
125125
return self::mb_internal_trim('[%s]+$', $string, $characters, $encoding);
126126
}
127127

128-
private static function mb_internal_trim(string $regex, string $string, string $characters = self::CHARACTERS, ?string $encoding = null): string
128+
private static function mb_internal_trim(string $regex, string $string, ?string $characters = null, ?string $encoding = null): string
129129
{
130130
if (null === $encoding) {
131131
$encoding = mb_internal_encoding();
@@ -146,23 +146,27 @@ private static function mb_internal_trim(string $regex, string $string, string $
146146
return null === $encoding ? $string : mb_convert_encoding($string, $encoding);
147147
}
148148

149-
$regexCharacter = preg_quote($characters, '/');
149+
if (null === $characters) {
150+
$characters = self::CHARACTERS;
151+
}
152+
153+
$regexCharacter = preg_quote($characters ?? '', '/');
150154
$regex = sprintf($regex, $regexCharacter, $regexCharacter);
151155

152156
if ('ASCII' === mb_detect_encoding($characters) && 'ASCII' === mb_detect_encoding($string) && !empty(array_intersect(str_split(self::CHARACTERS), str_split($string)))) {
153157
$options = 'g';
154158
} else {
155159
$options = '';
156160
}
157-
161+
158162
try {
159-
$a = mb_ereg_replace($regex, "", $string, $options);
163+
$test = mb_ereg_replace($regex, "", $string, $options);
160164

161-
if (null === $a) {
165+
if (null === $test) {
162166
throw new \Exception();
163167
}
164168

165-
return $a;
169+
return $test;
166170
} catch (\Exception $e) {
167171
return preg_replace(sprintf('/%s/', $regex), "", $string);
168172
}

src/Php84/bootstrap.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ function array_all(array $array, callable $callback): bool { return p\Php84::arr
4040
}
4141

4242
if (!function_exists('mb_trim')) {
43-
function mb_trim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): string { return p\Php84::mb_trim($string, $characters, $encoding); }
43+
function mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Php84::mb_trim($string, $characters, $encoding); }
4444
}
4545

4646
if (!function_exists('mb_ltrim')) {
47-
function mb_ltrim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): string { return p\Php84::mb_ltrim($string, $characters, $encoding); }
47+
function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Php84::mb_ltrim($string, $characters, $encoding); }
4848
}
4949

5050
if (!function_exists('mb_rtrim')) {
51-
function mb_rtrim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): string { return p\Php84::mb_rtrim($string, $characters, $encoding); }
51+
function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Php84::mb_rtrim($string, $characters, $encoding); }
5252
}

tests/Mbstring/MbstringTest.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -810,31 +810,31 @@ public static function lcFirstDataProvider(): array
810810
}
811811

812812
/**
813-
* @covers \Symfony\Polyfill\Mbstring\Mbstring::mb_trim
813+
* @covers \Symfony\Polyfill\Php84\Php84::mb_trim
814814
*
815815
* @dataProvider mbTrimProvider
816816
*/
817-
public function testMbTrim(string $expected, string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): void
817+
public function testMbTrim(string $expected, string $string, ?string $characters = null, ?string $encoding = null): void
818818
{
819819
$this->assertSame($expected, mb_trim($string, $characters, $encoding));
820820
}
821821

822822
/**
823-
* @covers \Symfony\Polyfill\Mbstring\Mbstring::mb_ltrim
823+
* @covers \Symfony\Polyfill\Php84\Php84::mb_ltrim
824824
*
825825
* @dataProvider mbLTrimProvider
826826
*/
827-
public function testMbLTrim(string $expected, string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): void
827+
public function testMbLTrim(string $expected, string $string, ?string $characters = null, ?string $encoding = null): void
828828
{
829829
$this->assertEquals($expected, mb_ltrim($string, $characters, $encoding));
830830
}
831831

832832
/**
833-
* @covers \Symfony\Polyfill\Mbstring\Mbstring::mb_rtrim
833+
* @covers \Symfony\Polyfill\Php84\Php84::mb_rtrim
834834
*
835835
* @dataProvider mbRTrimProvider
836836
*/
837-
public function testMbRTrim(string $expected, string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): void
837+
public function testMbRTrim(string $expected, string $string, ?string $characters = null, ?string $encoding = null): void
838838
{
839839
$this->assertSame($expected, mb_rtrim($string, $characters, $encoding));
840840
}
@@ -852,6 +852,22 @@ public function testMbTrimEncoding(): void
852852
$this->assertSame('6f225b57', bin2hex(mb_ltrim(mb_convert_encoding("\u{FEFF}漢字", "UTF-16BE", "UTF-8"), mb_convert_encoding("\u{FFFE}\u{FEFF}", "UTF-16BE", "UTF-8"), "UTF-16BE")));
853853
}
854854

855+
public function testMbTrimCharactersEncoding(): void
856+
{
857+
$strUtf8 = "\u{3042}\u{3000}";
858+
859+
$this->assertEquals(1, mb_strlen(mb_trim($strUtf8)));
860+
$this->assertEquals(1, mb_strlen(mb_trim($strUtf8, null, 'UTF-8')));
861+
862+
$old = mb_internal_encoding();
863+
mb_internal_encoding('Shift_JIS');
864+
$strSjis = mb_convert_encoding($strUtf8, 'Shift_JIS', 'UTF-8');
865+
866+
$this->assertEquals(1, mb_strlen(mb_trim($strSjis)));
867+
$this->assertEquals(1, mb_strlen(mb_trim($strSjis, null, 'Shift_JIS')));
868+
mb_internal_encoding($old);
869+
}
870+
855871
public static function mbTrimProvider(): iterable
856872
{
857873
yield ['ABC', 'ABC'];

tests/Php84/Php84Test.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,13 @@ public static function arrayAllDataProvider(): array
182182
[[1 => '1', 2 => '12', 3 => '123', 4 => '1234'], $callableKey, true],
183183
];
184184
}
185+
185186
/**
186187
* @covers \Symfony\Polyfill\Php84\Php84::mb_trim
187188
*
188189
* @dataProvider mbTrimProvider
189190
*/
190-
public function testMbTrim(string $expected, string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): void
191+
public function testMbTrim(string $expected, string $string, ?string $characters = null, ?string $encoding = null): void
191192
{
192193
$this->assertSame($expected, mb_trim($string, $characters, $encoding));
193194
}
@@ -197,7 +198,7 @@ public function testMbTrim(string $expected, string $string, string $characters
197198
*
198199
* @dataProvider mbLTrimProvider
199200
*/
200-
public function testMbLTrim(string $expected, string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): void
201+
public function testMbLTrim(string $expected, string $string, ?string $characters = null, ?string $encoding = null): void
201202
{
202203
$this->assertEquals($expected, mb_ltrim($string, $characters, $encoding));
203204
}
@@ -207,7 +208,7 @@ public function testMbLTrim(string $expected, string $string, string $characters
207208
*
208209
* @dataProvider mbRTrimProvider
209210
*/
210-
public function testMbRTrim(string $expected, string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): void
211+
public function testMbRTrim(string $expected, string $string, ?string $characters = null, ?string $encoding = null): void
211212
{
212213
$this->assertSame($expected, mb_rtrim($string, $characters, $encoding));
213214
}
@@ -225,6 +226,22 @@ public function testMbTrimEncoding(): void
225226
$this->assertSame('6f225b57', bin2hex(mb_ltrim(mb_convert_encoding("\u{FEFF}漢字", "UTF-16BE", "UTF-8"), mb_convert_encoding("\u{FFFE}\u{FEFF}", "UTF-16BE", "UTF-8"), "UTF-16BE")));
226227
}
227228

229+
public function testMbTrimCharactersEncoding(): void
230+
{
231+
$strUtf8 = "\u{3042}\u{3000}";
232+
233+
$this->assertEquals(1, mb_strlen(mb_trim($strUtf8)));
234+
$this->assertEquals(1, mb_strlen(mb_trim($strUtf8, null, 'UTF-8')));
235+
236+
$old = mb_internal_encoding();
237+
mb_internal_encoding('Shift_JIS');
238+
$strSjis = mb_convert_encoding($strUtf8, 'Shift_JIS', 'UTF-8');
239+
240+
$this->assertEquals(1, mb_strlen(mb_trim($strSjis)));
241+
$this->assertEquals(1, mb_strlen(mb_trim($strSjis, null, 'Shift_JIS')));
242+
mb_internal_encoding($old);
243+
}
244+
228245
public static function mbTrimProvider(): iterable
229246
{
230247
yield ['ABC', 'ABC'];

0 commit comments

Comments
 (0)