12 run_tests() unless caller;
16 my $foo = 'Now is the time for all good men to come to the aid of their country.';
18 my $first = substr($foo,0,index($foo,'the'));
19 is($first, "Now is ");
21 my $last = substr($foo,rindex($foo,'the'),100);
22 is($last, "their country.");
24 $last = substr($foo,index($foo,'Now'),2);
27 $last = substr($foo,rindex($foo,'Now'),2);
30 $last = substr($foo,index($foo,'.'),100);
33 $last = substr($foo,rindex($foo,'.'),100);
36 is(index("ababa","a",-1), 0);
37 is(index("ababa","a",0), 0);
38 is(index("ababa","a",1), 2);
39 is(index("ababa","a",2), 2);
40 is(index("ababa","a",3), 4);
41 is(index("ababa","a",4), 4);
42 is(index("ababa","a",5), -1);
44 is(rindex("ababa","a",-1), -1);
45 is(rindex("ababa","a",0), 0);
46 is(rindex("ababa","a",1), 0);
47 is(rindex("ababa","a",2), 2);
48 is(rindex("ababa","a",3), 2);
49 is(rindex("ababa","a",4), 4);
50 is(rindex("ababa","a",5), 4);
52 # tests for empty search string
53 is(index("abc", "", -1), 0);
54 is(index("abc", "", 0), 0);
55 is(index("abc", "", 1), 1);
56 is(index("abc", "", 2), 2);
57 is(index("abc", "", 3), 3);
58 is(index("abc", "", 4), 3);
59 is(rindex("abc", "", -1), 0);
60 is(rindex("abc", "", 0), 0);
61 is(rindex("abc", "", 1), 1);
62 is(rindex("abc", "", 2), 2);
63 is(rindex("abc", "", 3), 3);
64 is(rindex("abc", "", 4), 3);
66 $a = "foo \x{1234}bar";
68 is(index($a, "\x{1234}"), 4);
69 is(index($a, "bar", ), 5);
71 is(rindex($a, "\x{1234}"), 4);
72 is(rindex($a, "foo", ), 0);
75 my $needle = "\x{1230}\x{1270}";
76 my @needles = split ( //, $needle );
77 my $haystack = "\x{1228}\x{1228}\x{1230}\x{1270}";
78 foreach ( @needles ) {
79 my $a = index ( "\x{1228}\x{1228}\x{1230}\x{1270}", $_ );
80 my $b = index ( $haystack, $_ );
81 is($a, $b, q{[perl #22375] 'split'/'index' problem for utf8});
83 $needle = "\x{1270}\x{1230}"; # Transpose them.
84 @needles = split ( //, $needle );
85 foreach ( @needles ) {
86 my $a = index ( "\x{1228}\x{1228}\x{1230}\x{1270}", $_ );
87 my $b = index ( $haystack, $_ );
88 is($a, $b, q{[perl #22375] 'split'/'index' problem for utf8});
95 $search = latin1_to_native("foo \xc9 bar");
96 $text = latin1_to_native("a\xa3\xa3a $search $search quux");
98 my $text_utf8 = $text;
99 utf8::upgrade($text_utf8);
100 my $search_utf8 = $search;
101 utf8::upgrade($search_utf8);
103 is (index($text, $search), 5);
104 is (rindex($text, $search), 18);
105 is (index($text, $search_utf8), 5);
106 is (rindex($text, $search_utf8), 18);
107 is (index($text_utf8, $search), 5);
108 is (rindex($text_utf8, $search), 18);
109 is (index($text_utf8, $search_utf8), 5);
110 is (rindex($text_utf8, $search_utf8), 18);
112 my $text_octets = $text_utf8;
113 utf8::encode ($text_octets);
114 my $search_octets = $search_utf8;
115 utf8::encode ($search_octets);
117 is (index($text_octets, $search_octets), 7, "index octets, octets")
118 or _diag ($text_octets, $search_octets);
119 is (rindex($text_octets, $search_octets), 21, "rindex octets, octets");
120 is (index($text_octets, $search_utf8), -1);
121 is (rindex($text_octets, $search_utf8), -1);
122 is (index($text_utf8, $search_octets), -1);
123 is (rindex($text_utf8, $search_octets), -1);
125 is (index($text_octets, $search), -1);
126 is (rindex($text_octets, $search), -1);
127 is (index($text, $search_octets), -1);
128 is (rindex($text, $search_octets), -1);
132 skip "UTF-EBCDIC is limited to 0x7fffffff", 3 if ord("A") == 193;
134 my $a = "\x{80000000}";
136 is(index($s, 'def'), 1, "0x80000000 is a single character");
138 my $b = "\x{fffffffd}";
140 is(index($t, 'pqr'), 1, "0xfffffffd is a single character");
142 local ${^UTF8CACHE} = -1;
143 is(index($t, 'xyz'), 4, "0xfffffffd and utf8cache");
147 # Tests for NUL characters.
155 ["foo\0", 3, -1, -1],
156 ["foo\0foo\0\0", 3, 7, -1],
158 foreach my $l (1 .. 3) {
161 foreach my $test (@tests) {
163 my $str = $$test [0];
164 my $res = $$test [$l];
167 is (index ($str, $q), $res, "Find NUL character(s)");
171 # Bug #53746 shows a difference between variables and literals,
172 # so test literals as well.
174 my $test_str = qq {is (index ("$str", "$q"), $res, } .
175 qq {"Find NUL character(s)")};
176 $test_str =~ s/\0/\\0/g;
186 is(eval { utf8::upgrade($_ = " "); index $_, " ", 72 }, -1,
187 'UTF-8 cache handles offset beyond the end of the string');
189 is(index($_, "C", 4), -1,
190 'UTF-8 cache handles offset beyond the end of the string');
194 use constant {PVBM => 'galumphing', PVBM2 => 'bang'};
197 is(index('galumphing', PVBM), 0,
198 "index isn't confused by format compilation");
202 is($^A, '', '$^A is empty');
204 is($^A, 'galumphing', "formline isn't confused by index compilation");
208 # must not do index here before formline.
209 is($^A, '', '$^A is empty');
211 is($^A, 'bang', "formline isn't confused by index compilation");
212 is(index('bang', PVBM2), 0, "index isn't confused by format compilation");
215 use constant perl => "rules";
216 is(index("perl rules", perl), 5, 'first index of a constant works');
217 is(index("rules 1 & 2", perl), 0, 'second index of the same constant works');
220 # PVBM compilation should not flatten ref constants
221 use constant riffraff => \our $referent;
222 index "foo", riffraff;
223 is ref riffraff, 'SCALAR', 'index does not flatten ref constants';
225 package o { use overload '""' => sub { "foo" } }
226 bless \our $referent, o::;
227 is index("foo", riffraff), 0,
228 'index respects changes in ref stringification';
230 use constant quire => ${qr/(?{})/}; # A REGEXP, not a reference to one
232 eval ' "" =~ quire ';
233 is $@, "", 'regexp constants containing code blocks are not flattened';
235 use constant bang => $! = 8;
237 cmp_ok bang, '==', 8, 'dualvar constants are not flattened';
239 use constant u => undef;
242 local $SIG{__WARN__} = sub { $w .= shift };
245 sub { () = index "foo", u; }
247 is $w, undef, 'no warnings from compiling index($foo, undef_constant)';
249 is u, undef, 'undef constant is still undef';
251 } # end of sub run_tests