File: strings_marked_as_utf8.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (210 lines) | stat: -rw-r--r-- 5,972 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
--TEST--
Check that strings are marked as valid UTF-8
--EXTENSIONS--
zend_test
--FILE--
<?php
echo "Empty strings:\n";
$s = "";
var_dump(zend_test_is_string_marked_as_valid_utf8($s));

echo "Known strings:\n";
$s = "c";
var_dump(zend_test_is_string_marked_as_valid_utf8($s));

echo "Integer cast to string:\n";
$i = 2563;
$s = (string) $i;
var_dump($s);
var_dump(zend_test_is_string_marked_as_valid_utf8($s));

echo "Float cast to string:\n";
$f = 26.7;
$s = (string) $f;
var_dump($s);
var_dump(zend_test_is_string_marked_as_valid_utf8($s));
$f = 2e100;
$s = (string) $f;
var_dump($s);
var_dump(zend_test_is_string_marked_as_valid_utf8($s));

echo "Concatenation known valid UTF-8 strings in variables:\n";
$s1 = "f";
$s2 = "o";
$s = $s1 . $s2;
var_dump($s);
var_dump(zend_test_is_string_marked_as_valid_utf8($s));

echo "Multiple concatenation known valid UTF-8 strings in variables:\n";
$s1 = "f";
$s2 = "o";
$s = $s1 . $s2 . $s2;
var_dump($s);
var_dump(zend_test_is_string_marked_as_valid_utf8($s));

echo "Concatenation known valid UTF-8 in assignment:\n";
$s = "f" . "o";
var_dump($s);
var_dump(zend_test_is_string_marked_as_valid_utf8($s));

// The "foo" string matches with a "Foo" class which is registered by the zend_test extension.
// That class name does not have the "valid UTF-8" flag because class names in general
// don't have to be UTF-8. As the "foo" string here goes through the interning logic,
// the string gets replaced by the "foo" string from the class, which does
// not have the "valid UTF-8" flag. We therefore choose a different test case: "fxo".
// The previous "foo" test case works because it is not interned.
echo "Multiple concatenation known valid UTF-8 in assignment:\n";
$s = "f" . "o" . "o";
var_dump($s);
var_dump(zend_test_is_string_marked_as_valid_utf8($s));
$s = "f" . "x" . "o";
var_dump($s);
var_dump(zend_test_is_string_marked_as_valid_utf8($s));

echo "Concatenation known valid UTF-8 string with empty string in variables:\n";
$s1 = "f";
$s2 = "";
$s = $s1 . $s2;
var_dump(zend_test_is_string_marked_as_valid_utf8($s));
$s1 = "f";
$s2 = "";
$s = $s2 . $s1;
var_dump(zend_test_is_string_marked_as_valid_utf8($s));

echo "Concatenation known valid UTF-8 string with empty string in assignment:\n";
$s = "f" . "";
var_dump(zend_test_is_string_marked_as_valid_utf8($s));
$s = "" . "f";
var_dump(zend_test_is_string_marked_as_valid_utf8($s));

echo "Concatenation in loop:\n";
const COPY_TIMES = 10_000;
$string = "a";

$string_concat = $string;
for ($i = 1; $i < COPY_TIMES; $i++) {
    $string_concat = $string_concat . $string;
}
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));

echo "Concatenation in loop (compound assignment):\n";
$string = "a";

$string_concat = $string;
for ($i = 1; $i < COPY_TIMES; $i++) {
    $string_concat .= $string;
}
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));

echo "Concatenation of objects:\n";
class ToString {
    public function __toString() : string{
        return "z";
    }
}
$o = new ToString();
$s = $o . $o;
var_dump($s);
var_dump(zend_test_is_string_marked_as_valid_utf8($s));

echo "Rope concat:\n";
$foo = 'f';
$bar = 'b';
$baz = 'a';
$rope = "$foo$bar$baz";
var_dump(zend_test_is_string_marked_as_valid_utf8($rope));

echo "str_repeat:\n";
$string = "a";
$string_concat = str_repeat($string, 100);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string = "\xff";
$string_concat = str_repeat($string, 100);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));

echo "implode:\n";
$arr = ['a', 'b'];
$string_concat = implode('', $arr);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode('|', $arr);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode('', ['c', ...$arr]);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode('', [...$arr, 'c']);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode('', ["\xff", ...$arr]);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode('', [...$arr, "\xff"]);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode("\xff", $arr);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode('', []);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode("\xff", []);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode('', ['a']);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode("\xff", ['a']);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode('', [1, 1.0, 'a']);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));

?>
--EXPECT--
Empty strings:
bool(true)
Known strings:
bool(true)
Integer cast to string:
string(4) "2563"
bool(true)
Float cast to string:
string(4) "26.7"
bool(true)
string(8) "2.0E+100"
bool(true)
Concatenation known valid UTF-8 strings in variables:
string(2) "fo"
bool(true)
Multiple concatenation known valid UTF-8 strings in variables:
string(3) "foo"
bool(true)
Concatenation known valid UTF-8 in assignment:
string(2) "fo"
bool(true)
Multiple concatenation known valid UTF-8 in assignment:
string(3) "foo"
bool(false)
string(3) "fxo"
bool(true)
Concatenation known valid UTF-8 string with empty string in variables:
bool(true)
bool(true)
Concatenation known valid UTF-8 string with empty string in assignment:
bool(true)
bool(true)
Concatenation in loop:
bool(true)
Concatenation in loop (compound assignment):
bool(true)
Concatenation of objects:
string(2) "zz"
bool(true)
Rope concat:
bool(true)
str_repeat:
bool(true)
bool(false)
implode:
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)