xref: /php-src/ext/mbstring/tests/mb_stripos.phpt (revision 7f445595)
1--TEST--
2mb_stripos()
3--EXTENSIONS--
4mbstring
5--FILE--
6<?php
7// TODO: Add more encodings
8
9ini_set('include_path','.');
10include_once('common.inc');
11
12// Test string
13$euc_jp = "0123\xA4\xB3\xA4\xCE\xCA\xB8\xBB\xFA\xCE\xF3\xA4\xCF\xC6\xFC\xCB\xDC\xB8\xEC\xA4\xC7\xA4\xB9\xA1\xA3EUC-JP\xA4\xF2\xBB\xC8\xA4\xC3\xA4\xC6\xA4\xA4\xA4\xDE\xA4\xB9\xA1\xA30123\xC6\xFC\xCB\xDC\xB8\xEC\xA4\xCF\xCC\xCC\xC5\xDD\xBD\xAD\xA4\xA4\xA1\xA3";
14
15$slen = mb_strlen($euc_jp, 'EUC-JP');
16echo "String len: $slen\n";
17
18// EUC-JP - With encoding parameter
19mb_internal_encoding('UTF-8') or print("mb_internal_encoding() failed\n");
20
21echo  "== POSITIVE OFFSET ==\n";
22
23print  mb_stripos($euc_jp, "\xC6\xFC\xCB\xDC\xB8\xEC", 0, 'EUC-JP') . "\n";
24print  mb_stripos($euc_jp, '0', 0,     'EUC-JP') . "\n";
25print  mb_stripos($euc_jp, 3, 0,       'EUC-JP') . "\n";
26print  mb_stripos($euc_jp, 0, 0,       'EUC-JP') . "\n";
27print  mb_stripos($euc_jp, "\xC6\xFC\xCB\xDC\xB8\xEC", 15, 'EUC-JP') . "\n";
28print  mb_stripos($euc_jp, '0', 15,     'EUC-JP') . "\n";
29print  mb_stripos($euc_jp, 3, 15,       'EUC-JP') . "\n";
30print  mb_stripos($euc_jp, 0, 15,       'EUC-JP') . "\n";
31
32
33// Negative offset
34echo "== NEGATIVE OFFSET ==\n";
35
36print mb_stripos($euc_jp, "\xC6\xFC\xCB\xDC\xB8\xEC", -15, 'EUC-JP') . "\n";
37print mb_stripos($euc_jp, '0', -15,     'EUC-JP') . "\n";
38print mb_stripos($euc_jp, 3, -15,       'EUC-JP') . "\n";
39print mb_stripos($euc_jp, 0, -15,       'EUC-JP') . "\n";
40print mb_stripos($euc_jp, 0, -43,       'EUC-JP') . "\n";
41
42
43// Out of range - should return false
44print "== OUT OF RANGE ==\n";
45
46$r =  mb_stripos($euc_jp, "\xC6\xFC\xCB\xDC\xB8\xEC", 40, 'EUC-JP');
47($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
48$r =  mb_stripos($euc_jp, '0', 40,     'EUC-JP');
49($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
50$r =  mb_stripos($euc_jp, 3, 40,       'EUC-JP');
51($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
52$r =   mb_stripos($euc_jp, 0, 40,       'EUC-JP');
53($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
54$r =  mb_stripos($euc_jp, "\xC6\xFC\xCB\xDC\xB8\xEC", -3, 'EUC-JP');
55($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
56$r =  mb_stripos($euc_jp, '0', -3,     'EUC-JP');
57($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
58$r =  mb_stripos($euc_jp, 3, -3,       'EUC-JP');
59($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
60$r =   mb_stripos($euc_jp, 0, -3,       'EUC-JP');
61($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
62
63
64// Non-existent
65echo "== NON-EXISTENT ==\n";
66
67$r = mb_stripos($euc_jp, "\xB4\xDA\xB9\xF1\xB8\xEC", 0, 'EUC-JP');
68($r === FALSE) ? print "OK_STR\n"     : print "NG_STR\n";
69$r = mb_stripos($euc_jp, "\n",     0, 'EUC-JP');
70($r === FALSE) ? print "OK_NEWLINE\n" : print "NG_NEWLINE\n";
71
72
73// EUC-JP - No encoding parameter
74echo "== NO ENCODING PARAMETER ==\n";
75
76mb_internal_encoding('EUC-JP')  or print("mb_internal_encoding() failed\n");
77
78print  mb_stripos($euc_jp, "\xC6\xFC\xCB\xDC\xB8\xEC", 0) . "\n";
79print  mb_stripos($euc_jp, '0', 0) . "\n";
80print  mb_stripos($euc_jp, 3, 0) . "\n";
81print  mb_stripos($euc_jp, 0, 0) . "\n";
82
83$r = mb_stripos($euc_jp, "\xB4\xDA\xB9\xF1\xB8\xEC", 0);
84($r === FALSE) ? print "OK_STR\n"     : print "NG_STR\n";
85$r = mb_stripos($euc_jp, "\n", 0);
86($r === FALSE) ? print "OK_NEWLINE\n" : print "NG_NEWLINE\n";
87
88// EUC-JP - No offset and encoding parameter
89echo "== NO OFFSET AND ENCODING PARAMETER ==\n";
90
91mb_internal_encoding('EUC-JP')  or print("mb_internal_encoding() failed\n");
92
93print  mb_stripos($euc_jp, "\xC6\xFC\xCB\xDC\xB8\xEC") . "\n";
94print  mb_stripos($euc_jp, '0') . "\n";
95print  mb_stripos($euc_jp, 3) . "\n";
96print  mb_stripos($euc_jp, 0) . "\n";
97
98$r = mb_stripos($euc_jp, "\xB4\xDA\xB9\xF1\xB8\xEC");
99($r === FALSE) ? print "OK_STR\n"     : print "NG_STR\n";
100$r = mb_stripos($euc_jp, "\n");
101($r === FALSE) ? print "OK_NEWLINE\n" : print "NG_NEWLINE\n";
102
103echo "== INVALID STRINGS ==\n";
104
105// Previously, mb_stripos would internally convert invalid byte sequences to the
106// mb_substitute_char BEFORE performing search
107// So invalid byte sequences would match the substitute char, both from haystack
108// to needle and needle to haystack
109
110mb_substitute_character(0x25); // '%'
111var_dump(mb_stripos("abc%%", "\xFF", 0, "UTF-8")); // should be false
112var_dump(mb_stripos("abc\xFF", "%", 0, "UTF-8")); // should be false
113
114// However, invalid byte sequences can still match invalid byte sequences:
115var_dump(mb_stripos("abc\x80\x80", "\xFF", 0, "UTF-8"));
116var_dump(mb_stripos("abc\xFF", "c\x80", 0, "UTF-8"));
117
118
119?>
120--EXPECT--
121String len: 43
122== POSITIVE OFFSET ==
12310
1240
1253
1260
12734
12830
12933
13030
131== NEGATIVE OFFSET ==
13234
13330
13433
13530
1360
137== OUT OF RANGE ==
138OK_OUT_RANGE
139OK_OUT_RANGE
140OK_OUT_RANGE
141OK_OUT_RANGE
142OK_OUT_RANGE
143OK_OUT_RANGE
144OK_OUT_RANGE
145OK_OUT_RANGE
146== NON-EXISTENT ==
147OK_STR
148OK_NEWLINE
149== NO ENCODING PARAMETER ==
15010
1510
1523
1530
154OK_STR
155OK_NEWLINE
156== NO OFFSET AND ENCODING PARAMETER ==
15710
1580
1593
1600
161OK_STR
162OK_NEWLINE
163== INVALID STRINGS ==
164bool(false)
165bool(false)
166int(3)
167int(2)
168