Skip to content

Commit dd8aab5

Browse files
Improve mb_*trim polyfills
1 parent bf383b3 commit dd8aab5

File tree

9 files changed

+140
-131
lines changed

9 files changed

+140
-131
lines changed

src/Mbstring/Mbstring.php

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,14 @@ final class Mbstring
7979
['μ', 's', 'ι', 'σ', 'β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', "\xE1\xB9\xA1", 'ι'],
8080
];
8181

82-
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}";
83-
8482
private static $encodingList = ['ASCII', 'UTF-8'];
8583
private static $language = 'neutral';
8684
private static $internalEncoding = 'UTF-8';
8785

8886
public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null)
8987
{
9088
if (\is_array($s)) {
91-
if (PHP_VERSION_ID < 70200) {
89+
if (\PHP_VERSION_ID < 70200) {
9290
trigger_error('mb_convert_encoding() expects parameter 1 to be string, array given', \E_USER_WARNING);
9391

9492
return null;
@@ -987,68 +985,73 @@ private static function getEncoding($encoding)
987985

988986
public static function mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string
989987
{
990-
return self::mb_internal_trim('^[%s]+|[%s]+$', $string, $characters, $encoding);
988+
return self::mb_internal_trim('{^[%s]+|[%1$s]+$}Du', $string, $characters, $encoding, __FUNCTION__);
991989
}
992990

993991
public static function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null): string
994992
{
995-
return self::mb_internal_trim('^[%s]+', $string, $characters, $encoding);
993+
return self::mb_internal_trim('{^[%s]+}Du', $string, $characters, $encoding, __FUNCTION__);
996994
}
997995

998996
public static function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string
999997
{
1000-
return self::mb_internal_trim('[%s]+$', $string, $characters, $encoding);
998+
return self::mb_internal_trim('{[%s]+$}D', $string, $characters, $encoding, __FUNCTION__);
1001999
}
10021000

1003-
private static function mb_internal_trim(string $regex, string $string, ?string $characters = null, ?string $encoding = null): string
1001+
private static function mb_internal_trim(string $regex, string $string, ?string $characters, ?string $encoding, string $function): string
10041002
{
10051003
if (null === $encoding) {
1006-
$encoding = mb_internal_encoding();
1004+
$encoding = self::mb_internal_encoding();
1005+
} else {
1006+
self::assertEncoding($encoding, $function.'(): Argument #3 ($encoding) must be a valid encoding, "%s" given');
10071007
}
10081008

1009-
self::assertEncoding($encoding, debug_backtrace()[1]['function'].'(): Argument #3 ($encoding) must be a valid encoding, "%s" given.');
1010-
10111009
if ('' === $characters) {
1012-
return null === $encoding ? $string : mb_convert_encoding($string, $encoding);
1010+
return null === $encoding ? $string : self::mb_convert_encoding($string, $encoding);
10131011
}
10141012

1015-
if (null === $characters) {
1016-
$characters = self::CHARACTERS;
1017-
}
1013+
if ('UTF-8' === $encoding) {
1014+
$encoding = null;
1015+
if (!preg_match('//u', $string)) {
1016+
$string = @iconv('UTF-8', 'UTF-8//IGNORE', $string);
1017+
}
1018+
if (null !== $characters && !preg_match('//u', $characters)) {
1019+
$characters = @iconv('UTF-8', 'UTF-8//IGNORE', $characters);
1020+
}
1021+
} else {
1022+
$string = iconv($encoding, 'UTF-8//IGNORE', $string);
10181023

1019-
$regexCharacter = preg_quote($characters ?? '', '/');
1020-
$regex = sprintf($regex, $regexCharacter, $regexCharacter);
1024+
if (null !== $characters) {
1025+
$characters = iconv($encoding, 'UTF-8//IGNORE', $characters);
1026+
}
1027+
}
10211028

1022-
if ('ASCII' === mb_detect_encoding($characters) && 'ASCII' === mb_detect_encoding($string) && !empty(array_intersect(str_split(self::CHARACTERS), str_split($string)))) {
1023-
$options = 'g';
1029+
if (null === $characters) {
1030+
$characters = "\\0 \f\n\r\t\v\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}";
10241031
} else {
1025-
$options = '';
1032+
$characters = preg_quote($characters);
10261033
}
1027-
1028-
try {
1029-
$test = mb_ereg_replace($regex, "", $string, $options);
10301034

1031-
if (null === $test) {
1032-
throw new \Exception();
1033-
}
1035+
$string = preg_replace(sprintf($regex, $characters), '', $string);
10341036

1035-
return $test;
1036-
} catch (\Exception $e) {
1037-
return preg_replace('/'.$regex.'/', "", $string);
1037+
if (null === $encoding) {
1038+
return $string;
10381039
}
1039-
}
1040+
1041+
return iconv('UTF-8', $encoding.'//IGNORE', $string);
1042+
}
10401043

10411044
private static function assertEncoding(string $encoding, string $errorFormat): void
10421045
{
10431046
try {
10441047
$validEncoding = @self::mb_check_encoding('', $encoding);
10451048
} catch (\ValueError $e) {
1046-
throw new \ValueError(\sprintf($errorFormat, $encoding));
1049+
throw new \ValueError(sprintf($errorFormat, $encoding));
10471050
}
10481051

10491052
// BC for PHP 7.3 and lower
10501053
if (!$validEncoding) {
1051-
throw new \ValueError(\sprintf($errorFormat, $encoding));
1054+
throw new \ValueError(sprintf($errorFormat, $encoding));
10521055
}
10531056
}
10541057
}

src/Php72/bootstrap.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,15 @@ function utf8_decode($string) { return p\Php72::utf8_decode($string); }
4646
if (!function_exists('spl_object_id')) {
4747
function spl_object_id($object) { return p\Php72::spl_object_id($object); }
4848
}
49-
if (!function_exists('mb_ord')) {
50-
function mb_ord($string, $encoding = null) { return p\Php72::mb_ord($string, $encoding); }
51-
}
52-
if (!function_exists('mb_chr')) {
53-
function mb_chr($codepoint, $encoding = null) { return p\Php72::mb_chr($codepoint, $encoding); }
54-
}
55-
if (!function_exists('mb_scrub')) {
56-
function mb_scrub($string, $encoding = null) { $encoding = null === $encoding ? mb_internal_encoding() : $encoding; return mb_convert_encoding($string, $encoding, $encoding); }
49+
50+
if (extension_loaded('mbstring')) {
51+
if (!function_exists('mb_ord')) {
52+
function mb_ord($string, $encoding = null) { return p\Php72::mb_ord($string, $encoding); }
53+
}
54+
if (!function_exists('mb_chr')) {
55+
function mb_chr($codepoint, $encoding = null) { return p\Php72::mb_chr($codepoint, $encoding); }
56+
}
57+
if (!function_exists('mb_scrub')) {
58+
function mb_scrub($string, $encoding = null) { $encoding = null === $encoding ? mb_internal_encoding() : $encoding; return mb_convert_encoding($string, $encoding, $encoding); }
59+
}
5760
}

src/Php74/bootstrap.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
if (!function_exists('get_mangled_object_vars')) {
1919
function get_mangled_object_vars($object) { return p\Php74::get_mangled_object_vars($object); }
2020
}
21-
if (!function_exists('mb_str_split') && function_exists('mb_substr')) {
22-
function mb_str_split($string, $length = 1, $encoding = null) { return p\Php74::mb_str_split($string, $length, $encoding); }
23-
}
2421
if (!function_exists('password_algos')) {
2522
function password_algos() { return p\Php74::password_algos(); }
2623
}
24+
if (extension_loaded('mbstring')) {
25+
if (!function_exists('mb_str_split')) {
26+
function mb_str_split($string, $length = 1, $encoding = null) { return p\Php74::mb_str_split($string, $length, $encoding); }
27+
}
28+
}

src/Php83/bootstrap.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
function json_validate(string $json, int $depth = 512, int $flags = 0): bool { return p\Php83::json_validate($json, $depth, $flags); }
2020
}
2121

22-
if (!function_exists('mb_str_pad') && function_exists('mb_substr')) {
23-
function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = STR_PAD_RIGHT, ?string $encoding = null): string { return p\Php83::mb_str_pad($string, $length, $pad_string, $pad_type, $encoding); }
22+
if (extension_loaded('mbstring')) {
23+
if (!function_exists('mb_str_pad')) {
24+
function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = STR_PAD_RIGHT, ?string $encoding = null): string { return p\Php83::mb_str_pad($string, $length, $pad_string, $pad_type, $encoding); }
25+
}
2426
}
2527

2628
if (!function_exists('stream_context_set_options')) {

src/Php84/Php84.php

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
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}";
23-
2422
public static function mb_ucfirst(string $string, ?string $encoding = null): string
2523
{
2624
if (null === $encoding) {
@@ -113,20 +111,20 @@ public static function array_all(array $array, callable $callback): bool
113111

114112
public static function mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string
115113
{
116-
return self::mb_internal_trim('^[%s]+|[%s]+$', $string, $characters, $encoding);
114+
return self::mb_internal_trim('{^[%s]+|[%1$s]+$}Du', $string, $characters, $encoding, __FUNCTION__);
117115
}
118116

119117
public static function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null): string
120118
{
121-
return self::mb_internal_trim('^[%s]+', $string, $characters, $encoding);
119+
return self::mb_internal_trim('{^[%s]+}Du', $string, $characters, $encoding, __FUNCTION__);
122120
}
123121

124122
public static function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string
125123
{
126-
return self::mb_internal_trim('[%s]+$', $string, $characters, $encoding);
124+
return self::mb_internal_trim('{[%s]+$}Du', $string, $characters, $encoding, __FUNCTION__);
127125
}
128126

129-
private static function mb_internal_trim(string $regex, string $string, ?string $characters = null, ?string $encoding = null): string
127+
private static function mb_internal_trim(string $regex, string $string, ?string $characters, ?string $encoding, string $function): string
130128
{
131129
if (null === $encoding) {
132130
$encoding = mb_internal_encoding();
@@ -135,41 +133,40 @@ private static function mb_internal_trim(string $regex, string $string, ?string
135133
try {
136134
$validEncoding = @mb_check_encoding('', $encoding);
137135
} catch (\ValueError $e) {
138-
throw new \ValueError(sprintf('%s(): Argument #3 ($encoding) must be a valid encoding, "%s" given.', debug_backtrace()[1]['function'], $encoding));
136+
throw new \ValueError(sprintf('%s(): Argument #3 ($encoding) must be a valid encoding, "%s" given', $function, $encoding));
139137
}
140138

141139
// BC for PHP 7.3 and lower
142140
if (!$validEncoding) {
143-
throw new \ValueError(sprintf('%s(): Argument #3 ($encoding) must be a valid encoding, "%s" given.', debug_backtrace()[1]['function'], $encoding));
141+
throw new \ValueError(sprintf('%s(): Argument #3 ($encoding) must be a valid encoding, "%s" given', $function, $encoding));
144142
}
145143

146144
if ('' === $characters) {
147145
return null === $encoding ? $string : mb_convert_encoding($string, $encoding);
148146
}
149147

150-
if (null === $characters) {
151-
$characters = self::CHARACTERS;
148+
if ('UTF-8' === $encoding || \in_array(strtolower($encoding), ['utf-8', 'utf8'], true)) {
149+
$encoding = 'UTF-8';
152150
}
153151

154-
$regexCharacter = preg_quote($characters ?? '', '/');
155-
$regex = sprintf($regex, $regexCharacter, $regexCharacter);
152+
$string = mb_convert_encoding($string, 'UTF-8', $encoding);
156153

157-
if ('ASCII' === mb_detect_encoding($characters) && 'ASCII' === mb_detect_encoding($string) && !empty(array_intersect(str_split(self::CHARACTERS), str_split($string)))) {
158-
$options = 'g';
154+
if (null !== $characters) {
155+
$characters = mb_convert_encoding($characters, 'UTF-8', $encoding);
156+
}
157+
158+
if (null === $characters) {
159+
$characters = "\\0 \f\n\r\t\v\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}";
159160
} else {
160-
$options = '';
161+
$characters = preg_quote($characters);
161162
}
162-
163-
try {
164-
$test = mb_ereg_replace($regex, "", $string, $options);
165163

166-
if (null === $test) {
167-
throw new \Exception();
168-
}
164+
$string = preg_replace(sprintf($regex, $characters), '', $string);
169165

170-
return $test;
171-
} catch (\Exception $e) {
172-
return preg_replace(sprintf('/%s/', $regex), "", $string);
166+
if ('UTF-8' === $encoding) {
167+
return $string;
173168
}
174-
}
169+
170+
return mb_convert_encoding($string, $encoding, 'UTF-8');
171+
}
175172
}

src/Php84/Resources/stubs/Deprecated.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class Deprecated
1515
{
1616
public readonly ?string $message;
1717
public readonly ?string $since;
18-
18+
1919
public function __construct(?string $message = null, ?string $since = null)
2020
{
2121
$this->message = $message;

src/Php84/bootstrap.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@
1515
return;
1616
}
1717

18-
if (!function_exists('mb_ucfirst')) {
19-
function mb_ucfirst($string, ?string $encoding = null): string { return p\Php84::mb_ucfirst($string, $encoding); }
20-
}
21-
22-
if (!function_exists('mb_lcfirst')) {
23-
function mb_lcfirst($string, ?string $encoding = null): string { return p\Php84::mb_lcfirst($string, $encoding); }
24-
}
25-
2618
if (!function_exists('array_find')) {
2719
function array_find(array $array, callable $callback) { return p\Php84::array_find($array, $callback); }
2820
}
@@ -39,14 +31,24 @@ function array_any(array $array, callable $callback): bool { return p\Php84::arr
3931
function array_all(array $array, callable $callback): bool { return p\Php84::array_all($array, $callback); }
4032
}
4133

42-
if (!function_exists('mb_trim') && extension_loaded('mbstring')) {
43-
function mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Php84::mb_trim($string, $characters, $encoding); }
44-
}
34+
if (extension_loaded('mbstring')) {
35+
if (!function_exists('mb_ucfirst')) {
36+
function mb_ucfirst($string, ?string $encoding = null): string { return p\Php84::mb_ucfirst($string, $encoding); }
37+
}
4538

46-
if (!function_exists('mb_ltrim') && extension_loaded('mbstring')) {
47-
function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Php84::mb_ltrim($string, $characters, $encoding); }
48-
}
39+
if (!function_exists('mb_lcfirst')) {
40+
function mb_lcfirst($string, ?string $encoding = null): string { return p\Php84::mb_lcfirst($string, $encoding); }
41+
}
42+
43+
if (!function_exists('mb_trim')) {
44+
function mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Php84::mb_trim($string, $characters, $encoding); }
45+
}
46+
47+
if (!function_exists('mb_ltrim')) {
48+
function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Php84::mb_ltrim($string, $characters, $encoding); }
49+
}
4950

50-
if (!function_exists('mb_rtrim') && extension_loaded('mbstring')) {
51-
function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Php84::mb_rtrim($string, $characters, $encoding); }
51+
if (!function_exists('mb_rtrim')) {
52+
function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Php84::mb_rtrim($string, $characters, $encoding); }
53+
}
5254
}

0 commit comments

Comments
 (0)