Skip to content

Commit

Permalink
Adapt to nullable characters
Browse files Browse the repository at this point in the history
  • Loading branch information
Fan2Shrek committed Apr 24, 2024
1 parent 6b79fa9 commit a5d67ef
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 37 deletions.
27 changes: 15 additions & 12 deletions src/Mbstring/Mbstring.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ final class Mbstring
private static $internalEncoding = 'UTF-8';
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}";


public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null)
{
if (\is_array($fromEncoding) || (null !== $fromEncoding && false !== strpos($fromEncoding, ','))) {
Expand Down Expand Up @@ -997,29 +996,29 @@ private static function getEncoding($encoding)
return $encoding;
}

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

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

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

private static function mb_internal_trim(string $regex, string $string, string $characters = self::CHARACTERS, ?string $encoding = null): string
private static function mb_internal_trim(string $regex, string $string, ?string $characters = null, ?string $encoding = null): string
{
if (null === $encoding) {
$encoding = self::mb_internal_encoding();
$encoding = mb_internal_encoding();
}

try {
$validEncoding = @self::mb_check_encoding('', $encoding);
$validEncoding = @mb_check_encoding('', $encoding);
} catch (\ValueError $e) {
throw new \ValueError(sprintf('%s(): Argument #3 ($encoding) must be a valid encoding, "%s" given.', debug_backtrace()[1]['function'], $encoding));
}
Expand All @@ -1033,23 +1032,27 @@ private static function mb_internal_trim(string $regex, string $string, string $
return null === $encoding ? $string : mb_convert_encoding($string, $encoding);
}

$regexCharacter = preg_quote($characters, '/');
if (null === $characters) {
$characters = self::CHARACTERS;
}

$regexCharacter = preg_quote($characters ?? '', '/');
$regex = sprintf($regex, $regexCharacter, $regexCharacter);

if ('ASCII' === mb_detect_encoding($characters) && 'ASCII' === mb_detect_encoding($string) && !empty(array_intersect(str_split(self::CHARACTERS), str_split($string)))) {
$options = 'g';
} else {
$options = '';
}

try {
$a = mb_ereg_replace($regex, "", $string, $options);
$test = mb_ereg_replace($regex, "", $string, $options);

if (null === $a) {
if (null === $test) {
throw new \Exception();
}

return $a;
return $test;
} catch (\Exception $e) {
return preg_replace(sprintf('/%s/', $regex), "", $string);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Mbstring/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ function mb_lcfirst(string $string, ?string $encoding = null): string { return p
}

if (!function_exists('mb_trim')) {
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); }
function mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Mbstring::mb_trim($string, $characters, $encoding); }
}

if (!function_exists('mb_ltrim')) {
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); }
function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Mbstring::mb_ltrim($string, $characters, $encoding); }
}

if (!function_exists('mb_rtrim')) {
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); }
function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Mbstring::mb_rtrim($string, $characters, $encoding); }
}


Expand Down
12 changes: 12 additions & 0 deletions src/Mbstring/bootstrap80.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ function mb_ucfirst($string, ?string $encoding = null): string { return p\Mbstri
function mb_lcfirst($string, ?string $encoding = null): string { return p\Mbstring::mb_lcfirst($string, $encoding); }
}

if (!function_exists('mb_trim')) {
function mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Mbstring::mb_trim($string, $characters, $encoding); }
}

if (!function_exists('mb_ltrim')) {
function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Mbstring::mb_ltrim($string, $characters, $encoding); }
}

if (!function_exists('mb_rtrim')) {
function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Mbstring::mb_rtrim($string, $characters, $encoding); }
}

if (extension_loaded('mbstring')) {
return;
}
Expand Down
24 changes: 14 additions & 10 deletions src/Php84/Php84.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
final class Php84
{
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}";
public static function mb_ucfirst(string $string, ?string $encoding = null): string
{
if (null === $encoding) {
Expand Down Expand Up @@ -64,24 +65,23 @@ public static function mb_lcfirst(string $string, ?string $encoding = null): str

return $firstChar . mb_substr($string, 1, null, $encoding);
}
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}";

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

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

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

private static function mb_internal_trim(string $regex, string $string, string $characters = self::CHARACTERS, ?string $encoding = null): string
private static function mb_internal_trim(string $regex, string $string, ?string $characters = null, ?string $encoding = null): string
{
if (null === $encoding) {
$encoding = mb_internal_encoding();
Expand All @@ -102,23 +102,27 @@ private static function mb_internal_trim(string $regex, string $string, string $
return null === $encoding ? $string : mb_convert_encoding($string, $encoding);
}

$regexCharacter = preg_quote($characters, '/');
if (null === $characters) {
$characters = self::CHARACTERS;
}

$regexCharacter = preg_quote($characters ?? '', '/');
$regex = sprintf($regex, $regexCharacter, $regexCharacter);

if ('ASCII' === mb_detect_encoding($characters) && 'ASCII' === mb_detect_encoding($string) && !empty(array_intersect(str_split(self::CHARACTERS), str_split($string)))) {
$options = 'g';
} else {
$options = '';
}

try {
$a = mb_ereg_replace($regex, "", $string, $options);
$test = mb_ereg_replace($regex, "", $string, $options);

if (null === $a) {
if (null === $test) {
throw new \Exception();
}

return $a;
return $test;
} catch (\Exception $e) {
return preg_replace(sprintf('/%s/', $regex), "", $string);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Php84/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ function mb_lcfirst($string, ?string $encoding = null): string { return p\Php84:
}

if (!function_exists('mb_trim')) {
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); }
function mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Php84::mb_trim($string, $characters, $encoding); }
}

if (!function_exists('mb_ltrim')) {
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); }
function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Php84::mb_ltrim($string, $characters, $encoding); }
}

if (!function_exists('mb_rtrim')) {
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); }
function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Php84::mb_rtrim($string, $characters, $encoding); }
}
28 changes: 22 additions & 6 deletions tests/Mbstring/MbstringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -782,31 +782,31 @@ public static function lcFirstDataProvider(): array {
}

/**
* @covers \Symfony\Polyfill\Mbstring\Mbstring::mb_trim
* @covers \Symfony\Polyfill\Php84\Php84::mb_trim
*
* @dataProvider mbTrimProvider
*/
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
public function testMbTrim(string $expected, string $string, ?string $characters = null, ?string $encoding = null): void
{
$this->assertSame($expected, mb_trim($string, $characters, $encoding));
}

/**
* @covers \Symfony\Polyfill\Mbstring\Mbstring::mb_ltrim
* @covers \Symfony\Polyfill\Php84\Php84::mb_ltrim
*
* @dataProvider mbLTrimProvider
*/
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
public function testMbLTrim(string $expected, string $string, ?string $characters = null, ?string $encoding = null): void
{
$this->assertEquals($expected, mb_ltrim($string, $characters, $encoding));
}

/**
* @covers \Symfony\Polyfill\Mbstring\Mbstring::mb_rtrim
* @covers \Symfony\Polyfill\Php84\Php84::mb_rtrim
*
* @dataProvider mbRTrimProvider
*/
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
public function testMbRTrim(string $expected, string $string, ?string $characters = null, ?string $encoding = null): void
{
$this->assertSame($expected, mb_rtrim($string, $characters, $encoding));
}
Expand All @@ -824,6 +824,22 @@ public function testMbTrimEncoding(): void
$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")));
}

public function testMbTrimCharactersEncoding(): void
{
$strUtf8 = "\u{3042}\u{3000}";

$this->assertEquals(1, mb_strlen(mb_trim($strUtf8)));
$this->assertEquals(1, mb_strlen(mb_trim($strUtf8, null, 'UTF-8')));

$old = mb_internal_encoding();
mb_internal_encoding('Shift_JIS');
$strSjis = mb_convert_encoding($strUtf8, 'Shift_JIS', 'UTF-8');

$this->assertEquals(1, mb_strlen(mb_trim($strSjis)));
$this->assertEquals(1, mb_strlen(mb_trim($strSjis, null, 'Shift_JIS')));
mb_internal_encoding($old);
}

public static function mbTrimProvider(): iterable
{
yield ['ABC', 'ABC'];
Expand Down
23 changes: 20 additions & 3 deletions tests/Php84/Php84Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ public static function lcFirstDataProvider(): array {
["ß", "ß"],
];
}

/**
* @covers \Symfony\Polyfill\Php84\Php84::mb_trim
*
* @dataProvider mbTrimProvider
*/
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
public function testMbTrim(string $expected, string $string, ?string $characters = null, ?string $encoding = null): void
{
$this->assertSame($expected, mb_trim($string, $characters, $encoding));
}
Expand All @@ -83,7 +84,7 @@ public function testMbTrim(string $expected, string $string, string $characters
*
* @dataProvider mbLTrimProvider
*/
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
public function testMbLTrim(string $expected, string $string, ?string $characters = null, ?string $encoding = null): void
{
$this->assertEquals($expected, mb_ltrim($string, $characters, $encoding));
}
Expand All @@ -93,7 +94,7 @@ public function testMbLTrim(string $expected, string $string, string $characters
*
* @dataProvider mbRTrimProvider
*/
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
public function testMbRTrim(string $expected, string $string, ?string $characters = null, ?string $encoding = null): void
{
$this->assertSame($expected, mb_rtrim($string, $characters, $encoding));
}
Expand All @@ -111,6 +112,22 @@ public function testMbTrimEncoding(): void
$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")));
}

public function testMbTrimCharactersEncoding(): void
{
$strUtf8 = "\u{3042}\u{3000}";

$this->assertEquals(1, mb_strlen(mb_trim($strUtf8)));
$this->assertEquals(1, mb_strlen(mb_trim($strUtf8, null, 'UTF-8')));

$old = mb_internal_encoding();
mb_internal_encoding('Shift_JIS');
$strSjis = mb_convert_encoding($strUtf8, 'Shift_JIS', 'UTF-8');

$this->assertEquals(1, mb_strlen(mb_trim($strSjis)));
$this->assertEquals(1, mb_strlen(mb_trim($strSjis, null, 'Shift_JIS')));
mb_internal_encoding($old);
}

public static function mbTrimProvider(): iterable
{
yield ['ABC', 'ABC'];
Expand Down

0 comments on commit a5d67ef

Please sign in to comment.