|
| 1 | +--TEST-- |
| 2 | +GH-22779: mb_strrpos() wrong result for a negative offset in a non-UTF-8 encoding |
| 3 | +--EXTENSIONS-- |
| 4 | +mbstring |
| 5 | +--FILE-- |
| 6 | +<?php |
| 7 | +/* mb_strrpos() with a negative offset must return the correct position in non-UTF-8 encodings. */ |
| 8 | +$haystack = "\xA9\xA9X"; |
| 9 | +$needle = "\xA9"; |
| 10 | +foreach ([-1, -2, -3] as $offset) { |
| 11 | + var_dump(mb_strrpos($haystack, $needle, $offset, 'ISO-8859-1')); |
| 12 | +} |
| 13 | +var_dump(mb_strrpos("X\xA9", "\xA9", -1, 'ISO-8859-1')); |
| 14 | +var_dump(mb_strrpos("\x95Z\x95Z", "\x95", -1, 'Windows-1252')); |
| 15 | +var_dump(mb_strrpos("\x95Z\x95Z", "\x95", -2, 'Windows-1252')); |
| 16 | +// Two-byte needle in a single-byte encoding. |
| 17 | +var_dump(mb_strrpos("\xA9\xB0\xA9\xB0Z", "\xA9\xB0", -2, 'ISO-8859-1')); |
| 18 | +// Multibyte: "ああA" in Shift_JIS, needle "あ" (\x82\xA0). |
| 19 | +var_dump(mb_strrpos("\x82\xA0\x82\xA0A", "\x82\xA0", -2, 'SJIS')); |
| 20 | +var_dump(mb_strrpos("\x82\xA0\x82\xA0A", "\x82\xA0", -3, 'SJIS')); |
| 21 | +// UTF-16: an ASCII needle miscounts the other way. |
| 22 | +var_dump(mb_strrpos("\x00A\x00X\x00A", "\x00A", -2, 'UTF-16BE')); |
| 23 | +?> |
| 24 | +--EXPECT-- |
| 25 | +int(1) |
| 26 | +int(1) |
| 27 | +int(0) |
| 28 | +int(1) |
| 29 | +int(2) |
| 30 | +int(2) |
| 31 | +int(2) |
| 32 | +int(1) |
| 33 | +int(0) |
| 34 | +int(0) |
0 commit comments