Skip to content

Commit 06a3365

Browse files
committed
WIP
1 parent 41a9abf commit 06a3365

File tree

2 files changed

+61
-28
lines changed

2 files changed

+61
-28
lines changed

src/Php84/Php84.php

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,31 @@ public static function array_all(array $array, callable $callback): bool
112112

113113
public static function mb_trim(string $string, string $characters = self::CHARACTERS, ?string $encoding = null): string
114114
{
115-
return self::mb_ltrim(self::mb_rtrim($string, $characters, $encoding), $characters, $encoding);
115+
try {
116+
@mb_check_encoding('', $encoding);
117+
} catch (\ValueError $e) {
118+
throw new \ValueError(sprintf('%s(): Argument #3 ($encoding) must be a valid encoding, "%s" given', __METHOD__, $encoding));
119+
}
120+
121+
if ('' === $characters) {
122+
return null === $encoding ? $string : mb_convert_encoding($string, $encoding);
123+
}
124+
125+
if ($encoding !== null && $encoding !== 'UTF-8') {
126+
$string = mb_convert_encoding($string, "UTF-8", $encoding);
127+
$characters = mb_convert_encoding($characters, "UTF-8", $encoding);
128+
}
129+
130+
$regex = preg_quote($characters, '/');
131+
$regex = sprintf('^[%s]+|[%s]+$', $regex, $regex);
132+
133+
if ('ASCII' === mb_detect_encoding($characters) && 'ASCII' === mb_detect_encoding($string) && !empty(array_intersect(str_split(self::CHARACTERS), str_split($string)))) {
134+
$options = 'g';
135+
} else {
136+
$options = '';
137+
}
138+
139+
return mb_ereg_replace($regex, "", $string, $options);
116140
}
117141

118142
public static function mb_ltrim(string $string, string $characters = self::CHARACTERS, ?string $encoding = null): string
@@ -127,19 +151,15 @@ public static function mb_ltrim(string $string, string $characters = self::CHARA
127151
return null === $encoding ? $string : mb_convert_encoding($string, $encoding);
128152
}
129153

130-
$regex = sprintf('[%s]+', implode(array_map(fn (string $a): string => preg_quote($a, '/'), mb_str_split($characters, 1, $encoding))));
131-
132-
try {
133-
return mb_ereg_replace($regex, "", $string);
134-
} catch (\Throwable $e) {
135-
echo "\n";
136-
echo "\n";
137-
echo "\n";
138-
dump('', $regex);
139-
dump('', $characters);
154+
$regex = sprintf('^[%s]+', preg_quote($characters, '/'));
140155

141-
throw $e;
156+
if ('ASCII' === mb_detect_encoding($characters) && 'ASCII' === mb_detect_encoding($string)) {
157+
$options = 'g';
158+
} else {
159+
$options = '';
142160
}
161+
162+
return mb_ereg_replace($regex, "", $string, $options);
143163
}
144164

145165
public static function mb_rtrim(string $string, string $characters = self::CHARACTERS, ?string $encoding = null): string
@@ -150,6 +170,18 @@ public static function mb_rtrim(string $string, string $characters = self::CHARA
150170
throw new \ValueError(sprintf('%s(): Argument #3 ($encoding) must be a valid encoding, "%s" given', __METHOD__, $encoding));
151171
}
152172

153-
return "";
154-
}
173+
if ('' === $characters) {
174+
return null === $encoding ? $string : mb_convert_encoding($string, $encoding);
175+
}
176+
177+
$regex = sprintf('[%s]+$', preg_quote($characters, '/'));
178+
179+
if ('ASCII' === mb_detect_encoding($characters)) {
180+
$options = 'g';
181+
} else {
182+
$options = '';
183+
}
184+
185+
return mb_ereg_replace($regex, "", $string, $options);
186+
}
155187
}

tests/Php84/Php84Test.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -222,19 +222,20 @@ public function testMbTrimException(): void
222222
public static function mbTrimProvider(): iterable
223223
{
224224
yield ['ABC', 'ABC'];
225-
yield ['ABC', '\0\t\nABC \0\t\n'];
226-
yield ['\0\t\nABC \0\t\n', '\0\t\nABC \0\t\n', ''];
225+
yield ['ABC', "\0\t\nABC \0\t\n"];
226+
yield ["\0\t\nABC \0\t\n", "\0\t\nABC \0\t\n", ''];
227227

228228
yield ['', ''];
229229

230230
yield ["あいうえおあお", " あいうえおあお ", " ", "UTF-8"];
231-
yield ["foo BAR Spa", " foo BAR Spaß ", "ß", "UTF-8"];
232-
yield ["oo BAR Spaß", " oo BAR Spaß ", "f", "UTF-8"];
233-
234-
yield ["oo BAR Spaß", "foo BAR Spaß", "ßf", "UTF-8"];
235-
yield ["いうおえお ", " あいうおえお あ", "", "UTF-8"];
236-
yield ["いうおえお ", " あいうおえお あ", "", "UTF-8"];
237-
yield [" あいうおえお ", " あいうおえお a", "あa", "UTF-8"];
231+
yield ["foo BAR Spa", "foo BAR Spaß", "ß", "UTF-8"];
232+
yield ["oo BAR Spaß", "oo BAR Spaß", "f", "UTF-8"];
233+
234+
yield ["oo BAR Spa", "foo BAR Spaß", "ßf", "UTF-8"];
235+
yield ["oo BAR Spa", "foo BAR Spaß", "", "UTF-8"];
236+
yield ["いうおえお", " あいうおえお あ", "", "UTF-8"];
237+
yield ["いうおえお", " あいうおえお あ", "", "UTF-8"];
238+
yield [" あいうおえお ", " あいうおえお a", "あa", "UTF-8"];
238239
// yield [" あいうおえお a", " あいうおえお a", "\xe3", "UTF-8"];
239240

240241
yield ["", str_repeat(" ", 129)];
@@ -255,16 +256,16 @@ public static function mbTrimProvider(): iterable
255256
public static function mbLTrimProvider(): iterable
256257
{
257258
yield ['ABC', 'ABC'];
258-
yield ['ABC \0\t\n', '\0\t\nABC \0\t\n'];
259-
yield ['\0\t\nABC \0\t\n', '\0\t\nABC \0\t\n', ''];
259+
yield ["ABC \0\t\n", "\0\t\nABC \0\t\n"];
260+
yield ["\0\t\nABC \0\t\n", "\0\t\nABC \0\t\n", ''];
260261

261262
yield ['', ''];
262263

263264
yield [' test ', ' test ', ''];
264265

265266
yield ['いああああ', 'あああああああああああああああああああああああああああああああああいああああ', ''];
266267

267-
yield ['漢字', "\u{FFFE}漢字", "u{FFFE}\u{FEFF}"];
268+
yield ["漢字", "\u{FFFE}漢字", "\u{FFFE}\u{FEFF}"];
268269
// May does not work
269270
// yield ['226f575b', \bin2hex(mb_convert_encoding("\u{FFFE}漢字", "UTF-16LE", "UTF-8")), mb_convert_encoding("\u{FFFE}\u{FEFF}", "UTF-16LE", "UTF-8"), "UTF-16LE"];
270271
// yield ['漢字', \bin2hex(mb_convert_encoding("\u{FFFE}漢字", "UTF-16BE", "UTF-8")), mb_convert_encoding("\u{FFFE}\u{FEFF}", "UTF-16LE", "UTF-8"), "UTF-16BE"];
@@ -275,8 +276,8 @@ public static function mbLTrimProvider(): iterable
275276
public static function mbRTrimProvider(): iterable
276277
{
277278
yield ['ABC', 'ABC'];
278-
yield ['\0\t\nABC', '\0\t\nABC \0\t\n'];
279-
yield ['\0\t\nABC \0\t\n', '\0\t\nABC \0\t\n', ''];
279+
yield ["ABC", "ABC \0\t\n"];
280+
yield ["\0\t\nABC \0\t\n", "\0\t\nABC \0\t\n", ''];
280281

281282
yield ['', ''];
282283

0 commit comments

Comments
 (0)