xref: /PHP-8.1/ext/mbstring/tests/bug45923.phpt (revision 39131219)
1--TEST--
2Bug #45923 (mb_st[r]ripos() offset not handled correctly)
3--EXTENSIONS--
4mbstring
5--FILE--
6<?php
7
8function section($func, $haystack, $needle)
9{
10    echo "\n------- $func -----------\n\n";
11    foreach([0, 3, 6, 9, 11, 12, -1, -3, -6, -20] as $offset) {
12        echo "> Offset: $offset\n";
13        try {
14            var_dump($func($haystack, $needle, $offset));
15        } catch (\ValueError $e) {
16            echo $e->getMessage() . \PHP_EOL;
17        }
18    }
19}
20
21section('strpos'     , "abc abc abc"  , "abc");
22section('mb_strpos'  , "●○◆ ●○◆ ●○◆", "●○◆");
23
24section('stripos'    , "abc abc abc"  , "abc");
25section('mb_stripos' , "●○◆ ●○◆ ●○◆", "●○◆");
26
27section('strrpos'    , "abc abc abc"  , "abc");
28section('mb_strrpos' , "●○◆ ●○◆ ●○◆", "●○◆");
29
30section('strripos'   , "abc abc abc"  , "abc");
31section('mb_strripos', "●○◆ ●○◆ ●○◆", "●○◆");
32?>
33--EXPECT--
34------- strpos -----------
35
36> Offset: 0
37int(0)
38> Offset: 3
39int(4)
40> Offset: 6
41int(8)
42> Offset: 9
43bool(false)
44> Offset: 11
45bool(false)
46> Offset: 12
47strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
48> Offset: -1
49bool(false)
50> Offset: -3
51int(8)
52> Offset: -6
53int(8)
54> Offset: -20
55strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
56
57------- mb_strpos -----------
58
59> Offset: 0
60int(0)
61> Offset: 3
62int(4)
63> Offset: 6
64int(8)
65> Offset: 9
66bool(false)
67> Offset: 11
68bool(false)
69> Offset: 12
70mb_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
71> Offset: -1
72bool(false)
73> Offset: -3
74int(8)
75> Offset: -6
76int(8)
77> Offset: -20
78mb_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
79
80------- stripos -----------
81
82> Offset: 0
83int(0)
84> Offset: 3
85int(4)
86> Offset: 6
87int(8)
88> Offset: 9
89bool(false)
90> Offset: 11
91bool(false)
92> Offset: 12
93stripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
94> Offset: -1
95bool(false)
96> Offset: -3
97int(8)
98> Offset: -6
99int(8)
100> Offset: -20
101stripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
102
103------- mb_stripos -----------
104
105> Offset: 0
106int(0)
107> Offset: 3
108int(4)
109> Offset: 6
110int(8)
111> Offset: 9
112bool(false)
113> Offset: 11
114bool(false)
115> Offset: 12
116mb_stripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
117> Offset: -1
118bool(false)
119> Offset: -3
120int(8)
121> Offset: -6
122int(8)
123> Offset: -20
124mb_stripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
125
126------- strrpos -----------
127
128> Offset: 0
129int(8)
130> Offset: 3
131int(8)
132> Offset: 6
133int(8)
134> Offset: 9
135bool(false)
136> Offset: 11
137bool(false)
138> Offset: 12
139strrpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
140> Offset: -1
141int(8)
142> Offset: -3
143int(8)
144> Offset: -6
145int(4)
146> Offset: -20
147strrpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
148
149------- mb_strrpos -----------
150
151> Offset: 0
152int(8)
153> Offset: 3
154int(8)
155> Offset: 6
156int(8)
157> Offset: 9
158bool(false)
159> Offset: 11
160bool(false)
161> Offset: 12
162mb_strrpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
163> Offset: -1
164int(8)
165> Offset: -3
166int(8)
167> Offset: -6
168int(4)
169> Offset: -20
170mb_strrpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
171
172------- strripos -----------
173
174> Offset: 0
175int(8)
176> Offset: 3
177int(8)
178> Offset: 6
179int(8)
180> Offset: 9
181bool(false)
182> Offset: 11
183bool(false)
184> Offset: 12
185strripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
186> Offset: -1
187int(8)
188> Offset: -3
189int(8)
190> Offset: -6
191int(4)
192> Offset: -20
193strripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
194
195------- mb_strripos -----------
196
197> Offset: 0
198int(8)
199> Offset: 3
200int(8)
201> Offset: 6
202int(8)
203> Offset: 9
204bool(false)
205> Offset: 11
206bool(false)
207> Offset: 12
208mb_strripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
209> Offset: -1
210int(8)
211> Offset: -3
212int(8)
213> Offset: -6
214int(4)
215> Offset: -20
216mb_strripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
217