xref: /PHP-8.1/ext/mbstring/tests/gh9535b.phpt (revision fa0401b0)
1--TEST--
2Output of mb_strcut covers requested range of bytes even when output contains ending escape sequences
3--EXTENSIONS--
4mbstring
5--FILE--
6<?php
7// The existing behavior of mb_strcut is wrong for these encodings, when they add an extra closing
8// escape sequence to a string which would otherwise end in a non-default conversion mode
9// See https://github.com/php/php-src/pull/9562 for details on the bug
10
11// These tests were developed when fixing a different bug, but they don't pass because of
12// the bug involving the added closing escape sequences
13// When that bug is fixed, we can remove XFAIL (or combine this file with gh9535.phpt)
14
15$encodings = [
16    'JIS',
17    'ISO-2022-JP',
18    'ISO-2022-JP-2004',
19];
20
21$input = '宛如繁星般宛如皎月般';
22$bytes_length = 15;
23foreach($encodings as $encoding) {
24    $converted_str = mb_convert_encoding($input, $encoding, mb_internal_encoding());
25    $cut_str = mb_strcut($converted_str, 0, $bytes_length, $encoding);
26    $reconverted_str = mb_convert_encoding($cut_str, mb_internal_encoding(), $encoding);
27    echo $encoding.': '.$reconverted_str.PHP_EOL;
28}
29
30echo PHP_EOL;
31
32$input = '星のように月のように';
33$bytes_length = 20;
34foreach($encodings as $encoding) {
35    $converted_str = mb_convert_encoding($input, $encoding, mb_internal_encoding());
36    $cut_str = mb_strcut($converted_str, 0, $bytes_length, $encoding);
37    $reconverted_str = mb_convert_encoding($cut_str, mb_internal_encoding(), $encoding);
38    echo $encoding.': '.$reconverted_str.PHP_EOL;
39}
40
41echo PHP_EOL;
42
43$input = 'あaいb';
44$bytes_length = 10;
45foreach($encodings as $encoding) {
46    $converted_str = mb_convert_encoding($input, $encoding, mb_internal_encoding());
47    $cut_str = mb_strcut($converted_str, 0, $bytes_length, $encoding);
48    $reconverted_str = mb_convert_encoding($cut_str, mb_internal_encoding(), $encoding);
49    echo $encoding.': '.$reconverted_str.PHP_EOL;
50}
51
52echo PHP_EOL;
53
54$input = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
55$bytes_length = 10;
56foreach($encodings as $encoding) {
57    $converted_str = mb_convert_encoding($input, $encoding, mb_internal_encoding());
58    $cut_str = mb_strcut($converted_str, 0, $bytes_length, $encoding);
59    $reconverted_str = mb_convert_encoding($cut_str, mb_internal_encoding(), $encoding);
60    echo $encoding.': '.$reconverted_str.PHP_EOL;
61}
62
63echo PHP_EOL;
64
65$input = '???';
66$bytes_length = 2;
67foreach($encodings as $encoding) {
68    $converted_str = mb_convert_encoding($input, $encoding, mb_internal_encoding());
69    $cut_str = mb_strcut($converted_str, 0, $bytes_length, $encoding);
70    $reconverted_str = mb_convert_encoding($cut_str, mb_internal_encoding(), $encoding);
71    echo $encoding.': '.$reconverted_str.PHP_EOL;
72}
73
74echo PHP_EOL;
75
76foreach($encodings as $encoding) {
77    var_dump(mb_strcut($input, 0, $bytes_length, $encoding));
78}
79
80?>
81--XFAIL--
82Discussion: https://github.com/php/php-src/pull/9562
83--EXPECTF--
84JIS: 宛如繁星般
85ISO-2022-JP: 宛如繁星般
86ISO-2022-JP-2004: 宛如繁星
87
88JIS: 星のように月の
89ISO-2022-JP: 星のように月の
90ISO-2022-JP-2004: 星のように月の
91
92JIS: あa
93ISO-2022-JP: あa
94ISO-2022-JP-2004: あa
95
96JIS: AAAAAAAAAA
97ISO-2022-JP: AAAAAAAAAA
98ISO-2022-JP-2004: AAAAAAAAAA
99
100JIS: ??
101ISO-2022-JP: ??
102ISO-2022-JP-2004: ??
103
104string(2) "??"
105string(2) "??"
106string(2) "??"
107