--TEST-- mb_strimwidth() --EXTENSIONS-- mbstring --INI-- output_handler= --FILE-- getMessage() . \PHP_EOL; } try { var_dump(mb_strimwidth($euc_jp, 100, 10,'...','EUC-JP')); } catch (\ValueError $e) { echo $e->getMessage() . \PHP_EOL; } try { var_dump(mb_strimwidth($euc_jp, -100, 10,'...','EUC-JP')); } catch (\ValueError $e) { echo $e->getMessage() . \PHP_EOL; } try { // We also suppress the deprecation for negative width as of PHP 8.3 var_dump(@mb_strimwidth($euc_jp, -10, -21,'...','EUC-JP')); } catch (\ValueError $e) { echo $e->getMessage() . \PHP_EOL; } echo "\n== Other ==\n"; $str = 'abcdefghijklmnop'; for ($from = -5; $from <= 5; $from++) { for ($width = -5; $width <= 5; $width++) { if ($from < 0 && $width < 0 && $width < $from) { // This case is illegal and will throw an exception $pass = false; try { /* Shut up deprecation notice for now */ @mb_strimwidth($str, $from, $width, '...', 'ASCII'); } catch (\ValueError $e) { $pass = true; } if (!$pass) die("Expected exception to be thrown"); continue; } /* Shut up deprecation notice for now */ $result = @mb_strimwidth($str, $from, $width, '...', 'ASCII'); if ($from < 0 && $width < 0 && ($width - $from) <= 3) { if ($result !== '...') die("Output should have just been trim marker. string=" . $str . " result=" . $result . " from=" . $from . " width=" . $width); continue; } if ($width < 0 || $width > 3) { if (mb_substr($result, 0, 1, 'ASCII') !== mb_substr($str, $from, 1, 'ASCII')) die("Output string did not start at the expected point! string=" . $str . " result=" . $result . " from=" . $from . " width=" . $width); } if ($width >= 3 && ($from >= 0 || $width <= abs($from))) { if (mb_strwidth($result, 'ASCII') !== $width) die("Width was different from requested. string=" . $str . " result=" . $result . " from=" . $from . " width=" . $width); } if ($width < 0) { if (mb_substr($result, -3, 3, 'ASCII') !== '...') die("Expected trim marker"); if (mb_substr($result, -4, 1, 'ASCII') !== mb_substr($str, $width-4, 1, 'ASCII')) die("Output string did not end at the expected point. string=" . $str . " result=" . $result . " from=" . $from . " width=" . $width); } } } // Regression test found by fuzzer; old implementation would pass input string // through when requested width=0 testStrimwidth("\x00\x00\x00\x00+\x00\x00\x00\x00k\x00'\x11Yz", 1, 0, 'greek'); // Regression test; new implementation originally had a bug whereby it would // sometimes not skip over characters at beginning of string when requested to testStrimwidthWithMarker(str_repeat("a", 268), '', 12, 255, 'ASCII'); // Try invalid string; invalid sequences will be converted to error markers testStrimwidth("\x80\x80\x80", 0, 10, 'UTF-8'); // Try invalid marker // It will be directly concatenated onto truncated string without checking for validity testStrimwidthWithMarker("abcdefghijklmnop", "\x80\x80\x80", 0, 10, 'UTF-8'); // Regression test; old implementation would pass input string through, even when // it was wider than requested width, if the trim marker string was wider than // the input string testStrimwidthWithMarker("abc", "abcdefghijklmnop", 0, 1, 'ASCII'); // Regression test; old implementation did not handle negative 'from' argument // correctly when portion being skipped over included fullwidth characters testStrimwidth("日本語abc", -3, 10, 'UTF-8'); // Regression test; old implementation did not handle positive 'from' argument // combined with negative 'width' argument correctly when portion being skipped // over included fullwidth characters // We also suppress the deprecation for negative width as of PHP 8.3 @testStrimwidth("日本語abcdef", 3, -1, 'UTF-8'); ?> --EXPECT-- == UTF-16LE == String length: 10 String width: 14 Characters: [3100 3200 3300 6100 6200 6300 226f 575b 4b30 6a30] Character widths: [1 1 1 1 1 1 2 2 2 2] start=0 width=100 result=[3100 3200 3300 6100 6200 6300 226f 575b 4b30 6a30] length=10 width=14 start=0 width=14 result=[3100 3200 3300 6100 6200 6300 226f 575b 4b30 6a30] length=10 width=14 start=0 width=13 result=[3100 3200 3300 6100 6200 6300 226f 575b 2e00 2e00 2e00] length=11 width=13 start=0 width=12 result=[3100 3200 3300 6100 6200 6300 226f 2e00 2e00 2e00] length=10 width=11 start=0 width=0 result=[2e00 2e00 2e00] length=3 width=3 start=0 width=1 result=[2e00 2e00 2e00] length=3 width=3 start=-3 width=5 result=[575b 2e00 2e00 2e00] length=4 width=5 start=-4 width=-2 result=[226f 2e00 2e00 2e00] length=4 width=5 == EUC-JP == String length: 39 String width: 68 Characters: [30 31 32 33 a4b3 a4ce cab8 bbfa cef3 a4cf c6fc cbdc b8ec a4c7 a4b9 a1a3 45 55 43 2d 4a 50 a4f2 bbc8 a4c3 a4c6 a4a4 a4de a4b9 a1a3 c6fc cbdc b8ec a4cf cccc c5dd bdad a4a4 a1a3] Character widths: [1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2] start=0 width=15 result=[30 31 32 33 a4b3 a4ce cab8 bbfa 2e 2e 2e] length=11 width=15 start=0 width=100 result=[30 31 32 33 a4b3 a4ce cab8 bbfa cef3 a4cf c6fc cbdc b8ec a4c7 a4b9 a1a3 45 55 43 2d 4a 50 a4f2 bbc8 a4c3 a4c6 a4a4 a4de a4b9 a1a3 c6fc cbdc b8ec a4cf cccc c5dd bdad a4a4 a1a3] length=39 width=68 start=15 width=100 result=[a1a3 45 55 43 2d 4a 50 a4f2 bbc8 a4c3 a4c6 a4a4 a4de a4b9 a1a3 c6fc cbdc b8ec a4cf cccc c5dd bdad a4a4 a1a3] length=24 width=42 start=-30 width=5 result=[a4cf 2e 2e 2e] length=4 width=5 start=9 width=5 result=[a4cf 2e 2e 2e] length=4 width=5 start=15 width=-5 result=[a1a3 45 55 43 2d 4a 50 a4f2 bbc8 a4c3 a4c6 a4a4 a4de a4b9 a1a3 c6fc cbdc b8ec a4cf cccc 2e 2e 2e] length=23 width=37 start=-30 width=-25 result=[a4cf c6fc cbdc b8ec a4c7 a4b9 a1a3 45 55 43 2d 4a 50 a4f2 bbc8 a4c3 2e 2e 2e] length=19 width=29 start=39 width=10 result=[] length=0 width=0 start=-10 width=-12 result=[a1a3 c6fc 2e 2e 2e] length=5 width=7 mb_strimwidth(): Argument #3 ($width) is out of range mb_strimwidth(): Argument #2 ($start) is out of range mb_strimwidth(): Argument #2 ($start) is out of range mb_strimwidth(): Argument #3 ($width) is out of range == Other == start=1 width=0 result=[2e 2e 2e] length=3 width=3 start=12 width=255 result=[61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61] length=255 width=255 start=0 width=10 result=[3f 3f 3f] length=3 width=3 start=0 width=10 result=[61 62 63 64 65 66 67 80 80 80] length=10 width=10 start=0 width=1 result=[61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70] length=16 width=16 start=-3 width=10 result=[61 62 63] length=3 width=3 start=3 width=-1 result=[61 62 2e 2e 2e] length=5 width=5