Commit | Line | Data |
---|---|---|
a687059c LW |
1 | #!./perl |
2 | ||
c39c4c41 JH |
3 | BEGIN { |
4 | chdir 't' if -d 't'; | |
5 | @INC = '../lib'; | |
6 | } | |
a687059c | 7 | |
e609e586 | 8 | use strict; |
c39c4c41 | 9 | require './test.pl'; |
e609e586 | 10 | plan( tests => 46 ); |
a687059c | 11 | |
e609e586 | 12 | my $foo = 'Now is the time for all good men to come to the aid of their country.'; |
a687059c | 13 | |
e609e586 | 14 | my $first = substr($foo,0,index($foo,'the')); |
c39c4c41 | 15 | is($first, "Now is "); |
a687059c | 16 | |
e609e586 | 17 | my $last = substr($foo,rindex($foo,'the'),100); |
c39c4c41 | 18 | is($last, "their country."); |
a687059c LW |
19 | |
20 | $last = substr($foo,index($foo,'Now'),2); | |
c39c4c41 | 21 | is($last, "No"); |
a687059c LW |
22 | |
23 | $last = substr($foo,rindex($foo,'Now'),2); | |
c39c4c41 | 24 | is($last, "No"); |
a687059c LW |
25 | |
26 | $last = substr($foo,index($foo,'.'),100); | |
c39c4c41 | 27 | is($last, "."); |
a687059c LW |
28 | |
29 | $last = substr($foo,rindex($foo,'.'),100); | |
c39c4c41 | 30 | is($last, "."); |
d9d8d8de | 31 | |
c39c4c41 JH |
32 | is(index("ababa","a",-1), 0); |
33 | is(index("ababa","a",0), 0); | |
34 | is(index("ababa","a",1), 2); | |
35 | is(index("ababa","a",2), 2); | |
36 | is(index("ababa","a",3), 4); | |
37 | is(index("ababa","a",4), 4); | |
38 | is(index("ababa","a",5), -1); | |
d9d8d8de | 39 | |
c39c4c41 JH |
40 | is(rindex("ababa","a",-1), -1); |
41 | is(rindex("ababa","a",0), 0); | |
42 | is(rindex("ababa","a",1), 0); | |
43 | is(rindex("ababa","a",2), 2); | |
44 | is(rindex("ababa","a",3), 2); | |
45 | is(rindex("ababa","a",4), 4); | |
46 | is(rindex("ababa","a",5), 4); | |
4f593451 JH |
47 | |
48 | $a = "foo \x{1234}bar"; | |
49 | ||
c39c4c41 JH |
50 | is(index($a, "\x{1234}"), 4); |
51 | is(index($a, "bar", ), 5); | |
4f593451 | 52 | |
c39c4c41 JH |
53 | is(rindex($a, "\x{1234}"), 4); |
54 | is(rindex($a, "foo", ), 0); | |
d69d2d9f JH |
55 | |
56 | { | |
d69d2d9f JH |
57 | my $needle = "\x{1230}\x{1270}"; |
58 | my @needles = split ( //, $needle ); | |
59 | my $haystack = "\x{1228}\x{1228}\x{1230}\x{1270}"; | |
60 | foreach ( @needles ) { | |
61 | my $a = index ( "\x{1228}\x{1228}\x{1230}\x{1270}", $_ ); | |
62 | my $b = index ( $haystack, $_ ); | |
c39c4c41 | 63 | is($a, $b, q{[perl #22375] 'split'/'index' problem for utf8}); |
d69d2d9f JH |
64 | } |
65 | $needle = "\x{1270}\x{1230}"; # Transpose them. | |
66 | @needles = split ( //, $needle ); | |
67 | foreach ( @needles ) { | |
68 | my $a = index ( "\x{1228}\x{1228}\x{1230}\x{1270}", $_ ); | |
69 | my $b = index ( $haystack, $_ ); | |
c39c4c41 | 70 | is($a, $b, q{[perl #22375] 'split'/'index' problem for utf8}); |
d69d2d9f JH |
71 | } |
72 | } | |
e609e586 NC |
73 | |
74 | { | |
75 | my $search = "foo \xc9 bar"; | |
76 | my $text = "a\xa3\xa3a $search $search quux"; | |
77 | ||
78 | my $text_utf8 = $text; | |
79 | utf8::upgrade($text_utf8); | |
80 | my $search_utf8 = $search; | |
81 | utf8::upgrade($search_utf8); | |
82 | ||
83 | is (index($text, $search), 5); | |
84 | is (rindex($text, $search), 18); | |
85 | is (index($text, $search_utf8), 5); | |
86 | is (rindex($text, $search_utf8), 18); | |
87 | is (index($text_utf8, $search), 5); | |
88 | is (rindex($text_utf8, $search), 18); | |
89 | is (index($text_utf8, $search_utf8), 5); | |
90 | is (rindex($text_utf8, $search_utf8), 18); | |
91 | ||
92 | my $text_octets = $text_utf8; | |
93 | utf8::encode ($text_octets); | |
94 | my $search_octets = $search_utf8; | |
95 | utf8::encode ($search_octets); | |
96 | ||
97 | is (index($text_octets, $search_octets), 7, "index octets, octets") | |
98 | or _diag ($text_octets, $search_octets); | |
99 | is (rindex($text_octets, $search_octets), 21, "rindex octets, octets"); | |
100 | is (index($text_octets, $search_utf8), -1); | |
101 | is (rindex($text_octets, $search_utf8), -1); | |
102 | is (index($text_utf8, $search_octets), -1); | |
103 | is (rindex($text_utf8, $search_octets), -1); | |
104 | ||
105 | is (index($text_octets, $search), -1); | |
106 | is (rindex($text_octets, $search), -1); | |
107 | is (index($text, $search_octets), -1); | |
108 | is (rindex($text, $search_octets), -1); | |
109 | } |