xref: /php-src/ext/mbstring/tests/bug43840.phpt (revision a06d015e)
1--TEST--
2Test mb_strpos() function : mb_strpos bounds check is byte count rather than a character count
3--EXTENSIONS--
4mbstring
5--FILE--
6<?php
7/*
8 * mb_strpos bounds check is byte count rather than a character count:
9 * The multibyte string should be returning the same results as the ASCII string.
10 * Multibyte string was not returning error message until offset was passed the
11 * byte count of the string. Should return error message when passed character count.
12 */
13
14$offsets = array(20, 21, 22, 53, 54);
15$string_mb = base64_decode('5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII=');
16$needle = base64_decode('44CC');
17
18foreach($offsets as $i) {
19    echo "\n-- Offset is $i --\n";
20    echo "--Multibyte String:--\n";
21    try {
22        var_dump( mb_strpos($string_mb, $needle, $i, 'UTF-8') );
23    } catch (\ValueError $e) {
24        echo $e->getMessage() . \PHP_EOL;
25    }
26    echo"--ASCII String:--\n";
27    try {
28        var_dump(mb_strpos('This is na English ta', 'a', $i));
29    } catch (\ValueError $e) {
30        echo $e->getMessage() . \PHP_EOL;
31    }
32}
33?>
34--EXPECT--
35-- Offset is 20 --
36--Multibyte String:--
37int(20)
38--ASCII String:--
39int(20)
40
41-- Offset is 21 --
42--Multibyte String:--
43bool(false)
44--ASCII String:--
45bool(false)
46
47-- Offset is 22 --
48--Multibyte String:--
49mb_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
50--ASCII String:--
51mb_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
52
53-- Offset is 53 --
54--Multibyte String:--
55mb_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
56--ASCII String:--
57mb_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
58
59-- Offset is 54 --
60--Multibyte String:--
61mb_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
62--ASCII String:--
63mb_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
64