Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Fan2Shrek committed Mar 29, 2024
1 parent cb21a76 commit 67d131a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 28 deletions.
60 changes: 46 additions & 14 deletions src/Php84/Php84.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,31 @@ final class Php84

public static function mb_trim(string $string, string $characters = self::CHARACTERS, ?string $encoding = null): string
{
return self::mb_ltrim(self::mb_rtrim($string, $characters, $encoding), $characters, $encoding);
try {
@mb_check_encoding('', $encoding);
} catch (\ValueError $e) {
throw new \ValueError(sprintf('%s(): Argument #3 ($encoding) must be a valid encoding, "%s" given', __METHOD__, $encoding));
}

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

if ($encoding !== null && $encoding !== 'UTF-8') {
$string = mb_convert_encoding($string, "UTF-8", $encoding);
$characters = mb_convert_encoding($characters, "UTF-8", $encoding);
}

$regex = preg_quote($characters, '/');
$regex = sprintf('^[%s]+|[%s]+$', $regex, $regex);

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 = '';
}

return mb_ereg_replace($regex, "", $string, $options);
}

public static function mb_ltrim(string $string, string $characters = self::CHARACTERS, ?string $encoding = null): string
Expand All @@ -38,19 +62,15 @@ public static function mb_ltrim(string $string, string $characters = self::CHARA
return null === $encoding ? $string : mb_convert_encoding($string, $encoding);
}

$regex = sprintf('[%s]+', implode(array_map(fn (string $a): string => preg_quote($a, '/'), mb_str_split($characters, 1, $encoding))));
$regex = sprintf('^[%s]+', preg_quote($characters, '/'));

try {
return mb_ereg_replace($regex, "", $string);
} catch (\Throwable $e) {
echo "\n";
echo "\n";
echo "\n";
dump('', $regex);
dump('', $characters);

throw $e;
if ('ASCII' === mb_detect_encoding($characters) && 'ASCII' === mb_detect_encoding($string)) {
$options = 'g';
} else {
$options = '';
}

return mb_ereg_replace($regex, "", $string, $options);
}

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

return "";
}
if ('' === $characters) {
return null === $encoding ? $string : mb_convert_encoding($string, $encoding);
}

$regex = sprintf('[%s]+$', preg_quote($characters, '/'));

if ('ASCII' === mb_detect_encoding($characters)) {
$options = 'g';
} else {
$options = '';
}

return mb_ereg_replace($regex, "", $string, $options);
}
}
29 changes: 15 additions & 14 deletions tests/Php84/Php84Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,20 @@ public function testMbTrimException(): void
public static function mbTrimProvider(): iterable
{
yield ['ABC', 'ABC'];
yield ['ABC', '\0\t\nABC \0\t\n'];
yield ['\0\t\nABC \0\t\n', '\0\t\nABC \0\t\n', ''];
yield ['ABC', "\0\t\nABC \0\t\n"];
yield ["\0\t\nABC \0\t\n", "\0\t\nABC \0\t\n", ''];

yield ['', ''];

yield ["あいうえおあお", " あいうえおあお ", " ", "UTF-8"];
yield ["foo BAR Spa", " foo BAR Spaß ", "ß", "UTF-8"];
yield ["oo BAR Spaß", " oo BAR Spaß ", "f", "UTF-8"];

yield ["oo BAR Spaß", "foo BAR Spaß", "ßf", "UTF-8"];
yield ["いうおえお ", " あいうおえお あ", "", "UTF-8"];
yield ["いうおえお ", " あいうおえお あ", "", "UTF-8"];
yield [" あいうおえお ", " あいうおえお a", "あa", "UTF-8"];
yield ["foo BAR Spa", "foo BAR Spaß", "ß", "UTF-8"];
yield ["oo BAR Spaß", "oo BAR Spaß", "f", "UTF-8"];

yield ["oo BAR Spa", "foo BAR Spaß", "ßf", "UTF-8"];
yield ["oo BAR Spa", "foo BAR Spaß", "", "UTF-8"];
yield ["いうおえお", " あいうおえお あ", "", "UTF-8"];
yield ["いうおえお", " あいうおえお あ", "", "UTF-8"];
yield [" あいうおえお ", " あいうおえお a", "あa", "UTF-8"];
// yield [" あいうおえお a", " あいうおえお a", "\xe3", "UTF-8"];

yield ["", str_repeat(" ", 129)];
Expand All @@ -88,16 +89,16 @@ public static function mbTrimProvider(): iterable
public static function mbLTrimProvider(): iterable
{
yield ['ABC', 'ABC'];
yield ['ABC \0\t\n', '\0\t\nABC \0\t\n'];
yield ['\0\t\nABC \0\t\n', '\0\t\nABC \0\t\n', ''];
yield ["ABC \0\t\n", "\0\t\nABC \0\t\n"];
yield ["\0\t\nABC \0\t\n", "\0\t\nABC \0\t\n", ''];

yield ['', ''];

yield [' test ', ' test ', ''];

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

yield ['漢字', "\u{FFFE}漢字", "u{FFFE}\u{FEFF}"];
yield ["漢字", "\u{FFFE}漢字", "\u{FFFE}\u{FEFF}"];
// May does not work
// 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"];
// yield ['漢字', \bin2hex(mb_convert_encoding("\u{FFFE}漢字", "UTF-16BE", "UTF-8")), mb_convert_encoding("\u{FFFE}\u{FEFF}", "UTF-16LE", "UTF-8"), "UTF-16BE"];
Expand All @@ -108,8 +109,8 @@ public static function mbLTrimProvider(): iterable
public static function mbRTrimProvider(): iterable
{
yield ['ABC', 'ABC'];
yield ['\0\t\nABC', '\0\t\nABC \0\t\n'];
yield ['\0\t\nABC \0\t\n', '\0\t\nABC \0\t\n', ''];
yield ["ABC", "ABC \0\t\n"];
yield ["\0\t\nABC \0\t\n", "\0\t\nABC \0\t\n", ''];

yield ['', ''];

Expand Down

0 comments on commit 67d131a

Please sign in to comment.