Commit | Line | Data |
---|---|---|
8d063cd8 | 1 | #!./perl |
8d37f932 DD |
2 | # |
3 | # This is a home for regular expression tests that don't fit into | |
67a2b8c6 | 4 | # the format supported by re/regexp.t. If you want to add a test |
9fa196e1 | 5 | # that does fit that format, add it to re/re_tests, not here. |
8d063cd8 | 6 | |
84281c31 A |
7 | use strict; |
8 | use warnings; | |
9 | use 5.010; | |
10 | ||
84281c31 A |
11 | sub run_tests; |
12 | ||
9133bbab | 13 | $| = 1; |
3568d838 | 14 | |
8d37f932 | 15 | |
e4d48cc9 GS |
16 | BEGIN { |
17 | chdir 't' if -d 't'; | |
9d45b377 | 18 | @INC = ('../lib','.'); |
6f4e0180 | 19 | require './test.pl'; |
e4d48cc9 | 20 | } |
84281c31 | 21 | |
6ae44cd2 | 22 | plan tests => 438; # Update this when adding/deleting tests. |
b7a35066 | 23 | |
9d45b377 | 24 | run_tests() unless caller; |
b7a35066 | 25 | |
84281c31 A |
26 | # |
27 | # Tests start here. | |
28 | # | |
29 | sub run_tests { | |
0ef3e39e | 30 | |
84281c31 | 31 | { |
84281c31 | 32 | my $x = "abc\ndef\n"; |
5895685f | 33 | (my $x_pretty = $x) =~ s/\n/\\n/g; |
fd291da9 | 34 | |
5895685f NC |
35 | ok $x =~ /^abc/, qq ["$x_pretty" =~ /^abc/]; |
36 | ok $x !~ /^def/, qq ["$x_pretty" !~ /^def/]; | |
fd291da9 | 37 | |
84281c31 | 38 | # used to be a test for $* |
5895685f | 39 | ok $x =~ /^def/m, qq ["$x_pretty" =~ /^def/m]; |
fd291da9 | 40 | |
b33825c4 NC |
41 | ok(!($x =~ /^xxx/), qq ["$x_pretty" =~ /^xxx/]); |
42 | ok(!($x !~ /^abc/), qq ["$x_pretty" !~ /^abc/]); | |
fd291da9 | 43 | |
5895685f | 44 | ok $x =~ /def/, qq ["$x_pretty" =~ /def/]; |
b33825c4 | 45 | ok(!($x !~ /def/), qq ["$x_pretty" !~ /def/]); |
4765795a | 46 | |
5895685f | 47 | ok $x !~ /.def/, qq ["$x_pretty" !~ /.def/]; |
b33825c4 | 48 | ok(!($x =~ /.def/), qq ["$x_pretty" =~ /.def/]); |
4765795a | 49 | |
5895685f | 50 | ok $x =~ /\ndef/, qq ["$x_pretty" =~ /\\ndef/]; |
b33825c4 | 51 | ok(!($x !~ /\ndef/), qq ["$x_pretty" !~ /\\ndef/]); |
84281c31 | 52 | } |
4765795a | 53 | |
84281c31 A |
54 | { |
55 | $_ = '123'; | |
56 | ok /^([0-9][0-9]*)/, qq [\$_ = '$_'; /^([0-9][0-9]*)/]; | |
57 | } | |
f9969324 | 58 | |
84281c31 A |
59 | { |
60 | $_ = 'aaabbbccc'; | |
61 | ok /(a*b*)(c*)/ && $1 eq 'aaabbb' && $2 eq 'ccc', | |
62 | qq [\$_ = '$_'; /(a*b*)(c*)/]; | |
63 | ok /(a+b+c+)/ && $1 eq 'aaabbbccc', qq [\$_ = '$_'; /(a+b+c+)/]; | |
b33825c4 | 64 | unlike($_, qr/a+b?c+/, qq [\$_ = '$_'; /a+b?c+/]); |
84281c31 A |
65 | |
66 | $_ = 'aaabccc'; | |
67 | ok /a+b?c+/, qq [\$_ = '$_'; /a+b?c+/]; | |
68 | ok /a*b?c*/, qq [\$_ = '$_'; /a*b?c*/]; | |
69 | ||
70 | $_ = 'aaaccc'; | |
71 | ok /a*b?c*/, qq [\$_ = '$_'; /a*b?c*/]; | |
b33825c4 | 72 | unlike($_, qr/a*b+c*/, qq [\$_ = '$_'; /a*b+c*/]); |
84281c31 A |
73 | |
74 | $_ = 'abcdef'; | |
75 | ok /bcd|xyz/, qq [\$_ = '$_'; /bcd|xyz/]; | |
76 | ok /xyz|bcd/, qq [\$_ = '$_'; /xyz|bcd/]; | |
77 | ok m|bc/*d|, qq [\$_ = '$_'; m|bc/*d|]; | |
78 | ok /^$_$/, qq [\$_ = '$_'; /^\$_\$/]; | |
4765795a | 79 | } |
4765795a | 80 | |
84281c31 A |
81 | { |
82 | # used to be a test for $* | |
5895685f | 83 | ok "ab\ncd\n" =~ /^cd/m, q ["ab\ncd\n" =~ /^cd/m]; |
84281c31 | 84 | } |
4765795a | 85 | |
84281c31 A |
86 | { |
87 | our %XXX = map {($_ => $_)} 123, 234, 345; | |
88 | ||
89 | our @XXX = ('ok 1','not ok 1', 'ok 2','not ok 2','not ok 3'); | |
90 | while ($_ = shift(@XXX)) { | |
b33825c4 | 91 | my $e = index ($_, 'not') >= 0 ? '' : 1; |
725a61d7 | 92 | my $r = m?(.*)?; |
b33825c4 | 93 | is($r, $e, "?(.*)?"); |
84281c31 A |
94 | /not/ && reset; |
95 | if (/not ok 2/) { | |
96 | if ($^O eq 'VMS') { | |
97 | $_ = shift(@XXX); | |
98 | } | |
99 | else { | |
100 | reset 'X'; | |
101 | } | |
102 | } | |
103 | } | |
4765795a | 104 | |
84281c31 A |
105 | SKIP: { |
106 | if ($^O eq 'VMS') { | |
107 | skip "Reset 'X'", 1; | |
108 | } | |
109 | ok !keys %XXX, "%XXX is empty"; | |
110 | } | |
4765795a | 111 | |
84281c31 | 112 | } |
4765795a | 113 | |
84281c31 | 114 | { |
4f890a30 | 115 | my $message = "Test empty pattern"; |
84281c31 A |
116 | my $xyz = 'xyz'; |
117 | my $cde = 'cde'; | |
118 | ||
119 | $cde =~ /[^ab]*/; | |
120 | $xyz =~ //; | |
4f890a30 | 121 | is($&, $xyz, $message); |
84281c31 A |
122 | |
123 | my $foo = '[^ab]*'; | |
124 | $cde =~ /$foo/; | |
125 | $xyz =~ //; | |
4f890a30 | 126 | is($&, $xyz, $message); |
84281c31 A |
127 | |
128 | $cde =~ /$foo/; | |
129 | my $null; | |
130 | no warnings 'uninitialized'; | |
131 | $xyz =~ /$null/; | |
4f890a30 | 132 | is($&, $xyz, $message); |
84281c31 A |
133 | |
134 | $null = ""; | |
135 | $xyz =~ /$null/; | |
4f890a30 | 136 | is($&, $xyz, $message); |
84281c31 | 137 | } |
4765795a | 138 | |
84281c31 | 139 | { |
4f890a30 | 140 | my $message = q !Check $`, $&, $'!; |
84281c31 | 141 | $_ = 'abcdefghi'; |
0f289c68 | 142 | /def/; # optimized up to cmd |
4f890a30 | 143 | is("$`:$&:$'", 'abc:def:ghi', $message); |
4765795a | 144 | |
84281c31 | 145 | no warnings 'void'; |
0f289c68 | 146 | /cde/ + 0; # optimized only to spat |
4f890a30 | 147 | is("$`:$&:$'", 'ab:cde:fghi', $message); |
4765795a | 148 | |
0f289c68 | 149 | /[d][e][f]/; # not optimized |
4f890a30 | 150 | is("$`:$&:$'", 'abc:def:ghi', $message); |
84281c31 | 151 | } |
4765795a | 152 | |
84281c31 A |
153 | { |
154 | $_ = 'now is the {time for all} good men to come to.'; | |
2a53d331 | 155 | / \{([^}]*)}/; |
de26e0cc | 156 | is($1, 'time for all', "Match braces"); |
84281c31 | 157 | } |
4765795a | 158 | |
84281c31 | 159 | { |
4f890a30 | 160 | my $message = "{N,M} quantifier"; |
84281c31 | 161 | $_ = 'xxx {3,4} yyy zzz'; |
4f890a30 NC |
162 | ok(/( {3,4})/, $message); |
163 | is($1, ' ', $message); | |
164 | unlike($_, qr/( {4,})/, $message); | |
165 | ok(/( {2,3}.)/, $message); | |
166 | is($1, ' y', $message); | |
167 | ok(/(y{2,3}.)/, $message); | |
168 | is($1, 'yyy ', $message); | |
169 | unlike($_, qr/x {3,4}/, $message); | |
170 | unlike($_, qr/^xxx {3,4}/, $message); | |
84281c31 | 171 | } |
4765795a | 172 | |
84281c31 | 173 | { |
4f890a30 | 174 | my $message = "Test /g"; |
84281c31 A |
175 | local $" = ":"; |
176 | $_ = "now is the time for all good men to come to."; | |
177 | my @words = /(\w+)/g; | |
178 | my $exp = "now:is:the:time:for:all:good:men:to:come:to"; | |
4765795a | 179 | |
4f890a30 | 180 | is("@words", $exp, $message); |
4765795a | 181 | |
84281c31 A |
182 | @words = (); |
183 | while (/\w+/g) { | |
184 | push (@words, $&); | |
185 | } | |
4f890a30 | 186 | is("@words", $exp, $message); |
4765795a | 187 | |
84281c31 A |
188 | @words = (); |
189 | pos = 0; | |
190 | while (/to/g) { | |
191 | push(@words, $&); | |
192 | } | |
4f890a30 | 193 | is("@words", "to:to", $message); |
4765795a | 194 | |
84281c31 A |
195 | pos $_ = 0; |
196 | @words = /to/g; | |
4f890a30 | 197 | is("@words", "to:to", $message); |
84281c31 | 198 | } |
4765795a | 199 | |
84281c31 A |
200 | { |
201 | $_ = "abcdefghi"; | |
202 | ||
203 | my $pat1 = 'def'; | |
204 | my $pat2 = '^def'; | |
205 | my $pat3 = '.def.'; | |
206 | my $pat4 = 'abc'; | |
207 | my $pat5 = '^abc'; | |
208 | my $pat6 = 'abc$'; | |
209 | my $pat7 = 'ghi'; | |
210 | my $pat8 = '\w*ghi'; | |
211 | my $pat9 = 'ghi$'; | |
212 | ||
213 | my $t1 = my $t2 = my $t3 = my $t4 = my $t5 = | |
214 | my $t6 = my $t7 = my $t8 = my $t9 = 0; | |
215 | ||
216 | for my $iter (1 .. 5) { | |
217 | $t1++ if /$pat1/o; | |
218 | $t2++ if /$pat2/o; | |
219 | $t3++ if /$pat3/o; | |
220 | $t4++ if /$pat4/o; | |
221 | $t5++ if /$pat5/o; | |
222 | $t6++ if /$pat6/o; | |
223 | $t7++ if /$pat7/o; | |
224 | $t8++ if /$pat8/o; | |
225 | $t9++ if /$pat9/o; | |
226 | } | |
227 | my $x = "$t1$t2$t3$t4$t5$t6$t7$t8$t9"; | |
de26e0cc | 228 | is($x, '505550555', "Test /o"); |
84281c31 | 229 | } |
4765795a | 230 | |
4f890a30 | 231 | { |
84281c31 A |
232 | my $xyz = 'xyz'; |
233 | ok "abc" =~ /^abc$|$xyz/, "| after \$"; | |
4765795a | 234 | |
84281c31 | 235 | # perl 4.009 says "unmatched ()" |
4f890a30 | 236 | my $message = '$ inside ()'; |
4765795a | 237 | |
84281c31 A |
238 | my $result; |
239 | eval '"abc" =~ /a(bc$)|$xyz/; $result = "$&:$1"'; | |
4f890a30 NC |
240 | is($@, "", $message); |
241 | is($result, "abc:bc", $message); | |
84281c31 | 242 | } |
4765795a | 243 | |
84281c31 | 244 | { |
4f890a30 | 245 | my $message = "Scalar /g"; |
84281c31 A |
246 | $_ = "abcfooabcbar"; |
247 | ||
4f890a30 NC |
248 | ok( /abc/g && $` eq "", $message); |
249 | ok( /abc/g && $` eq "abcfoo", $message); | |
250 | ok(!/abc/g, $message); | |
84281c31 | 251 | |
4f890a30 | 252 | $message = "Scalar /gi"; |
84281c31 | 253 | pos = 0; |
4f890a30 NC |
254 | ok( /ABC/gi && $` eq "", $message); |
255 | ok( /ABC/gi && $` eq "abcfoo", $message); | |
256 | ok(!/ABC/gi, $message); | |
84281c31 | 257 | |
4f890a30 | 258 | $message = "Scalar /g"; |
84281c31 | 259 | pos = 0; |
4f890a30 NC |
260 | ok( /abc/g && $' eq "fooabcbar", $message); |
261 | ok( /abc/g && $' eq "bar", $message); | |
84281c31 A |
262 | |
263 | $_ .= ''; | |
264 | my @x = /abc/g; | |
de26e0cc | 265 | is(@x, 2, "/g reset after assignment"); |
4765795a | 266 | } |
4765795a | 267 | |
84281c31 | 268 | { |
4f890a30 | 269 | my $message = '/g, \G and pos'; |
84281c31 A |
270 | $_ = "abdc"; |
271 | pos $_ = 2; | |
272 | /\Gc/gc; | |
4f890a30 | 273 | is(pos $_, 2, $message); |
84281c31 | 274 | /\Gc/g; |
4f890a30 | 275 | is(pos $_, undef, $message); |
84281c31 | 276 | } |
4765795a | 277 | |
84281c31 | 278 | { |
4f890a30 | 279 | my $message = '(?{ })'; |
84281c31 A |
280 | our $out = 1; |
281 | 'abc' =~ m'a(?{ $out = 2 })b'; | |
4f890a30 | 282 | is($out, 2, $message); |
84281c31 A |
283 | |
284 | $out = 1; | |
285 | 'abc' =~ m'a(?{ $out = 3 })c'; | |
4f890a30 | 286 | is($out, 1, $message); |
84281c31 | 287 | } |
4765795a | 288 | |
84281c31 A |
289 | { |
290 | $_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6'; | |
291 | my @out = /(?<!foo)bar./g; | |
de26e0cc | 292 | is("@out", 'bar2 barf', "Negative lookbehind"); |
84281c31 | 293 | } |
4765795a | 294 | |
84281c31 | 295 | { |
4f890a30 | 296 | my $message = "REG_INFTY tests"; |
84281c31 | 297 | # Tests which depend on REG_INFTY |
19d6612d NC |
298 | |
299 | # Defaults assumed if this fails | |
300 | eval { require Config; }; | |
301 | $::reg_infty = $Config::Config{reg_infty} // 32767; | |
84281c31 A |
302 | $::reg_infty_m = $::reg_infty - 1; |
303 | $::reg_infty_p = $::reg_infty + 1; | |
93f09d7b | 304 | $::reg_infty_m = $::reg_infty_m; # Suppress warning. |
84281c31 A |
305 | |
306 | # As well as failing if the pattern matches do unexpected things, the | |
307 | # next three tests will fail if you should have picked up a lower-than- | |
308 | # default value for $reg_infty from Config.pm, but have not. | |
309 | ||
14358a41 NC |
310 | is(eval q{('aaa' =~ /(a{1,$::reg_infty_m})/)[0]}, 'aaa', $message); |
311 | is($@, '', $message); | |
312 | is(eval q{('a' x $::reg_infty_m) =~ /a{$::reg_infty_m}/}, 1, $message); | |
313 | is($@, '', $message); | |
314 | isnt(q{('a' x ($::reg_infty_m - 1)) !~ /a{$::reg_infty_m}/}, 1, $message); | |
315 | is($@, '', $message); | |
316 | ||
84281c31 | 317 | eval "'aaa' =~ /a{1,$::reg_infty}/"; |
224b2e7e | 318 | like($@, qr/^\QQuantifier in {,} bigger than/, $message); |
84281c31 | 319 | eval "'aaa' =~ /a{1,$::reg_infty_p}/"; |
4f890a30 | 320 | like($@, qr/^\QQuantifier in {,} bigger than/, $message); |
4765795a | 321 | } |
8269fa76 | 322 | |
84281c31 A |
323 | { |
324 | # Poke a couple more parse failures | |
325 | my $context = 'x' x 256; | |
326 | eval qq("${context}y" =~ /(?<=$context)y/); | |
327 | ok $@ =~ /^\QLookbehind longer than 255 not/, "Lookbehind limit"; | |
328 | } | |
8269fa76 | 329 | |
84281c31 A |
330 | { |
331 | # Long Monsters | |
84281c31 A |
332 | for my $l (125, 140, 250, 270, 300000, 30) { # Ordered to free memory |
333 | my $a = 'a' x $l; | |
4f890a30 NC |
334 | my $message = "Long monster, length = $l"; |
335 | like("ba$a=", qr/a$a=/, $message); | |
336 | unlike("b$a=", qr/a$a=/, $message); | |
337 | like("b$a=", qr/ba+=/, $message); | |
84281c31 | 338 | |
224b2e7e | 339 | like("ba$a=", qr/b(?:a|b)+=/, $message); |
84281c31 A |
340 | } |
341 | } | |
8269fa76 | 342 | |
84281c31 A |
343 | { |
344 | # 20000 nodes, each taking 3 words per string, and 1 per branch | |
345 | my $long_constant_len = join '|', 12120 .. 32645; | |
346 | my $long_var_len = join '|', 8120 .. 28645; | |
347 | my %ans = ( 'ax13876y25677lbc' => 1, | |
348 | 'ax13876y25677mcb' => 0, # not b. | |
349 | 'ax13876y35677nbc' => 0, # Num too big | |
350 | 'ax13876y25677y21378obc' => 1, | |
0f289c68 | 351 | 'ax13876y25677y21378zbc' => 0, # Not followed by [k-o] |
84281c31 A |
352 | 'ax13876y25677y21378y21378kbc' => 1, |
353 | 'ax13876y25677y21378y21378kcb' => 0, # Not b. | |
354 | 'ax13876y25677y21378y21378y21378kbc' => 0, # 5 runs | |
355 | ); | |
356 | ||
84281c31 | 357 | for (keys %ans) { |
4f890a30 NC |
358 | my $message = "20000 nodes, const-len '$_'"; |
359 | ok !($ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o), $message; | |
84281c31 | 360 | |
4f890a30 NC |
361 | $message = "20000 nodes, var-len '$_'"; |
362 | ok !($ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o,), $message; | |
84281c31 | 363 | } |
b8ef571c | 364 | } |
209a9bc1 | 365 | |
84281c31 | 366 | { |
4f890a30 | 367 | my $message = "Complicated backtracking"; |
84281c31 A |
368 | $_ = " a (bla()) and x(y b((l)u((e))) and b(l(e)e)e"; |
369 | my $expect = "(bla()) ((l)u((e))) (l(e)e)"; | |
370 | ||
371 | use vars '$c'; | |
372 | sub matchit { | |
373 | m/ | |
374 | ( | |
375 | \( | |
0f289c68 | 376 | (?{ $c = 1 }) # Initialize |
84281c31 A |
377 | (?: |
378 | (?(?{ $c == 0 }) # PREVIOUS iteration was OK, stop the loop | |
379 | (?! | |
0f289c68 YO |
380 | ) # Fail: will unwind one iteration back |
381 | ) | |
84281c31 | 382 | (?: |
0f289c68 | 383 | [^()]+ # Match a big chunk |
84281c31 A |
384 | (?= |
385 | [()] | |
0f289c68 | 386 | ) # Do not try to match subchunks |
84281c31 A |
387 | | |
388 | \( | |
389 | (?{ ++$c }) | |
390 | | | |
391 | \) | |
392 | (?{ --$c }) | |
393 | ) | |
0f289c68 | 394 | )+ # This may not match with different subblocks |
84281c31 A |
395 | ) |
396 | (?(?{ $c != 0 }) | |
397 | (?! | |
0f289c68 YO |
398 | ) # Fail |
399 | ) # Otherwise the chunk 1 may succeed with $c>0 | |
84281c31 A |
400 | /xg; |
401 | } | |
3568d838 | 402 | |
84281c31 A |
403 | my @ans = (); |
404 | my $res; | |
405 | push @ans, $res while $res = matchit; | |
4f890a30 | 406 | is("@ans", "1 1 1", $message); |
3568d838 | 407 | |
84281c31 | 408 | @ans = matchit; |
4f890a30 | 409 | is("@ans", $expect, $message); |
3568d838 | 410 | |
4f890a30 | 411 | $message = "Recursion with (??{ })"; |
84281c31 A |
412 | our $matched; |
413 | $matched = qr/\((?:(?>[^()]+)|(??{$matched}))*\)/; | |
3568d838 | 414 | |
84281c31 A |
415 | @ans = my @ans1 = (); |
416 | push (@ans, $res), push (@ans1, $&) while $res = m/$matched/g; | |
3568d838 | 417 | |
4f890a30 NC |
418 | is("@ans", "1 1 1", $message); |
419 | is("@ans1", $expect, $message); | |
3568d838 | 420 | |
84281c31 | 421 | @ans = m/$matched/g; |
4f890a30 | 422 | is("@ans", $expect, $message); |
3568d838 | 423 | |
84281c31 | 424 | } |
3568d838 | 425 | |
84281c31 A |
426 | { |
427 | ok "abc" =~ /^(??{"a"})b/, '"abc" =~ /^(??{"a"})b/'; | |
428 | } | |
3568d838 | 429 | |
84281c31 | 430 | { |
0f289c68 | 431 | my @ans = ('a/b' =~ m%(.*/)?(.*)%); # Stack may be bad |
de26e0cc | 432 | is("@ans", 'a/ b', "Stack may be bad"); |
84281c31 | 433 | } |
3568d838 | 434 | |
84281c31 | 435 | { |
4f890a30 | 436 | my $message = "Eval-group not allowed at runtime"; |
84281c31 A |
437 | my $code = '{$blah = 45}'; |
438 | our $blah = 12; | |
439 | eval { /(?$code)/ }; | |
4f890a30 | 440 | ok($@ && $@ =~ /not allowed at runtime/ && $blah == 12, $message); |
84281c31 | 441 | |
3044771b NC |
442 | $blah = 12; |
443 | my $res = eval { "xx" =~ /(?$code)/o }; | |
444 | { | |
445 | no warnings 'uninitialized'; | |
5895685f | 446 | chomp $@; my $message = "$message '$@', '$res', '$blah'"; |
4f890a30 | 447 | ok($@ && $@ =~ /not allowed at runtime/ && $blah == 12, $message); |
3044771b NC |
448 | } |
449 | ||
450 | $code = '=xx'; | |
451 | $blah = 12; | |
452 | $res = eval { "xx" =~ /(?$code)/o }; | |
453 | { | |
454 | no warnings 'uninitialized'; | |
4f890a30 NC |
455 | my $message = "$message '$@', '$res', '$blah'"; |
456 | ok(!$@ && $res, $message); | |
3044771b | 457 | } |
3568d838 | 458 | |
84281c31 A |
459 | $code = '{$blah = 45}'; |
460 | $blah = 12; | |
461 | eval "/(?$code)/"; | |
4f890a30 | 462 | is($blah, 45, $message); |
3568d838 | 463 | |
84281c31 A |
464 | $blah = 12; |
465 | /(?{$blah = 45})/; | |
4f890a30 | 466 | is($blah, 45, $message); |
84281c31 | 467 | } |
3568d838 | 468 | |
84281c31 | 469 | { |
4f890a30 | 470 | my $message = "Pos checks"; |
84281c31 A |
471 | my $x = 'banana'; |
472 | $x =~ /.a/g; | |
4f890a30 | 473 | is(pos $x, 2, $message); |
3568d838 | 474 | |
84281c31 | 475 | $x =~ /.z/gc; |
4f890a30 | 476 | is(pos $x, 2, $message); |
3568d838 | 477 | |
84281c31 A |
478 | sub f { |
479 | my $p = $_[0]; | |
480 | return $p; | |
481 | } | |
3568d838 | 482 | |
84281c31 | 483 | $x =~ /.a/g; |
4f890a30 | 484 | is(f (pos $x), 4, $message); |
84281c31 | 485 | } |
3568d838 | 486 | |
84281c31 | 487 | { |
4f890a30 | 488 | my $message = 'Checking $^R'; |
84281c31 A |
489 | our $x = $^R = 67; |
490 | 'foot' =~ /foo(?{$x = 12; 75})[t]/; | |
4f890a30 | 491 | is($^R, 75, $message); |
84281c31 A |
492 | |
493 | $x = $^R = 67; | |
494 | 'foot' =~ /foo(?{$x = 12; 75})[xy]/; | |
4f890a30 | 495 | ok($^R eq '67' && $x eq '12', $message); |
84281c31 A |
496 | |
497 | $x = $^R = 67; | |
498 | 'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/; | |
4f890a30 | 499 | ok($^R eq '79' && $x eq '12', $message); |
84281c31 | 500 | } |
3568d838 | 501 | |
84281c31 | 502 | { |
de26e0cc NC |
503 | is(qr/\b\v$/i, '(?^i:\b\v$)', 'qr/\b\v$/i'); |
504 | is(qr/\b\v$/s, '(?^s:\b\v$)', 'qr/\b\v$/s'); | |
505 | is(qr/\b\v$/m, '(?^m:\b\v$)', 'qr/\b\v$/m'); | |
506 | is(qr/\b\v$/x, '(?^x:\b\v$)', 'qr/\b\v$/x'); | |
507 | is(qr/\b\v$/xism, '(?^msix:\b\v$)', 'qr/\b\v$/xism'); | |
508 | is(qr/\b\v$/, '(?^:\b\v$)', 'qr/\b\v$/'); | |
84281c31 | 509 | } |
3568d838 | 510 | |
9de15fec | 511 | { # Test that charset modifier work, and are interpolated |
de26e0cc NC |
512 | is(qr/\b\v$/, '(?^:\b\v$)', 'Verify no locale, no unicode_strings gives default modifier'); |
513 | is(qr/(?l:\b\v$)/, '(?^:(?l:\b\v$))', 'Verify infix l modifier compiles'); | |
514 | is(qr/(?u:\b\v$)/, '(?^:(?u:\b\v$))', 'Verify infix u modifier compiles'); | |
515 | is(qr/(?l)\b\v$/, '(?^:(?l)\b\v$)', 'Verify (?l) compiles'); | |
516 | is(qr/(?u)\b\v$/, '(?^:(?u)\b\v$)', 'Verify (?u) compiles'); | |
9de15fec KW |
517 | |
518 | my $dual = qr/\b\v$/; | |
519 | use locale; | |
520 | my $locale = qr/\b\v$/; | |
de26e0cc | 521 | is($locale, '(?^l:\b\v$)', 'Verify has l modifier when compiled under use locale'); |
9de15fec KW |
522 | no locale; |
523 | ||
524 | use feature 'unicode_strings'; | |
525 | my $unicode = qr/\b\v$/; | |
de26e0cc NC |
526 | is($unicode, '(?^u:\b\v$)', 'Verify has u modifier when compiled under unicode_strings'); |
527 | is(qr/abc$dual/, '(?^u:abc(?^:\b\v$))', 'Verify retains d meaning when interpolated under locale'); | |
528 | is(qr/abc$locale/, '(?^u:abc(?^l:\b\v$))', 'Verify retains l when interpolated under unicode_strings'); | |
9de15fec KW |
529 | |
530 | no feature 'unicode_strings'; | |
de26e0cc NC |
531 | is(qr/abc$locale/, '(?^:abc(?^l:\b\v$))', 'Verify retains l when interpolated outside locale and unicode strings'); |
532 | is(qr/def$unicode/, '(?^:def(?^u:\b\v$))', 'Verify retains u when interpolated outside locale and unicode strings'); | |
9de15fec KW |
533 | |
534 | use locale; | |
de26e0cc NC |
535 | is(qr/abc$dual/, '(?^l:abc(?^:\b\v$))', 'Verify retains d meaning when interpolated under locale'); |
536 | is(qr/abc$unicode/, '(?^l:abc(?^u:\b\v$))', 'Verify retains u when interpolated under locale'); | |
9de15fec KW |
537 | } |
538 | ||
84281c31 | 539 | { |
4f890a30 | 540 | my $message = "Look around"; |
84281c31 | 541 | $_ = 'xabcx'; |
84281c31 | 542 | foreach my $ans ('', 'c') { |
4f890a30 NC |
543 | ok(/(?<=(?=a)..)((?=c)|.)/g, $message); |
544 | is($1, $ans, $message); | |
84281c31 A |
545 | } |
546 | } | |
3568d838 | 547 | |
84281c31 | 548 | { |
4f890a30 | 549 | my $message = "Empty clause"; |
84281c31 A |
550 | $_ = 'a'; |
551 | foreach my $ans ('', 'a', '') { | |
4f890a30 NC |
552 | ok(/^|a|$/g, $message); |
553 | is($&, $ans, $message); | |
84281c31 A |
554 | } |
555 | } | |
3568d838 | 556 | |
84281c31 | 557 | { |
84281c31 | 558 | sub prefixify { |
4f890a30 NC |
559 | my $message = "Prefixify"; |
560 | { | |
84281c31 | 561 | my ($v, $a, $b, $res) = @_; |
4f890a30 NC |
562 | ok($v =~ s/\Q$a\E/$b/, $message); |
563 | is($v, $res, $message); | |
84281c31 A |
564 | } |
565 | } | |
3568d838 | 566 | |
84281c31 A |
567 | prefixify ('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch'); |
568 | prefixify ('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch'); | |
569 | } | |
3568d838 | 570 | |
84281c31 A |
571 | { |
572 | $_ = 'var="foo"'; | |
573 | /(\")/; | |
574 | ok $1 && /$1/, "Capture a quote"; | |
575 | } | |
3568d838 | 576 | |
84281c31 | 577 | { |
84281c31 | 578 | no warnings 'closure'; |
4f890a30 | 579 | my $message = '(?{ $var } refers to package vars'; |
84281c31 A |
580 | package aa; |
581 | our $c = 2; | |
582 | $::c = 3; | |
583 | '' =~ /(?{ $c = 4 })/; | |
4f890a30 NC |
584 | main::is($c, 4, $message); |
585 | main::is($::c, 3, $message); | |
84281c31 | 586 | } |
3568d838 | 587 | |
84281c31 | 588 | { |
cb124425 NC |
589 | is(eval 'q(a:[b]:) =~ /[x[:foo:]]/', undef); |
590 | like ($@, qr/POSIX class \[:[^:]+:\] unknown in regex/, | |
591 | 'POSIX class [: :] must have valid name'); | |
84281c31 A |
592 | |
593 | for my $d (qw [= .]) { | |
cb124425 NC |
594 | is(eval "/[[${d}foo${d}]]/", undef); |
595 | like ($@, qr/\QPOSIX syntax [$d $d] is reserved for future extensions/, | |
596 | "POSIX syntax [[$d $d]] is an error"); | |
84281c31 A |
597 | } |
598 | } | |
3568d838 | 599 | |
84281c31 A |
600 | { |
601 | # test if failure of patterns returns empty list | |
4f890a30 | 602 | my $message = "Failed pattern returns empty list"; |
84281c31 A |
603 | $_ = 'aaa'; |
604 | @_ = /bbb/; | |
4f890a30 | 605 | is("@_", "", $message); |
3568d838 | 606 | |
84281c31 | 607 | @_ = /bbb/g; |
4f890a30 | 608 | is("@_", "", $message); |
a72deede | 609 | |
84281c31 | 610 | @_ = /(bbb)/; |
4f890a30 | 611 | is("@_", "", $message); |
a72deede | 612 | |
84281c31 | 613 | @_ = /(bbb)/g; |
4f890a30 | 614 | is("@_", "", $message); |
84281c31 | 615 | } |
a72deede | 616 | |
84281c31 | 617 | { |
4f890a30 | 618 | my $message = '@- and @+ tests'; |
84281c31 A |
619 | |
620 | /a(?=.$)/; | |
4f890a30 NC |
621 | is($#+, 0, $message); |
622 | is($#-, 0, $message); | |
623 | is($+ [0], 2, $message); | |
624 | is($- [0], 1, $message); | |
625 | ok(!defined $+ [1] && !defined $- [1] && | |
626 | !defined $+ [2] && !defined $- [2], $message); | |
84281c31 A |
627 | |
628 | /a(a)(a)/; | |
4f890a30 NC |
629 | is($#+, 2, $message); |
630 | is($#-, 2, $message); | |
631 | is($+ [0], 3, $message); | |
632 | is($- [0], 0, $message); | |
633 | is($+ [1], 2, $message); | |
634 | is($- [1], 1, $message); | |
635 | is($+ [2], 3, $message); | |
636 | is($- [2], 2, $message); | |
637 | ok(!defined $+ [3] && !defined $- [3] && | |
638 | !defined $+ [4] && !defined $- [4], $message); | |
84281c31 | 639 | |
54a4274e | 640 | # Exists has a special check for @-/@+ - bug 45147 |
4f890a30 NC |
641 | ok(exists $-[0], $message); |
642 | ok(exists $+[0], $message); | |
643 | ok(exists $-[2], $message); | |
644 | ok(exists $+[2], $message); | |
645 | ok(!exists $-[3], $message); | |
646 | ok(!exists $+[3], $message); | |
647 | ok(exists $-[-1], $message); | |
648 | ok(exists $+[-1], $message); | |
649 | ok(exists $-[-3], $message); | |
650 | ok(exists $+[-3], $message); | |
651 | ok(!exists $-[-4], $message); | |
652 | ok(!exists $+[-4], $message); | |
84281c31 A |
653 | |
654 | /.(a)(b)?(a)/; | |
4f890a30 NC |
655 | is($#+, 3, $message); |
656 | is($#-, 3, $message); | |
657 | is($+ [1], 2, $message); | |
658 | is($- [1], 1, $message); | |
659 | is($+ [3], 3, $message); | |
660 | is($- [3], 2, $message); | |
661 | ok(!defined $+ [2] && !defined $- [2] && | |
662 | !defined $+ [4] && !defined $- [4], $message); | |
84281c31 | 663 | |
84281c31 | 664 | /.(a)/; |
4f890a30 NC |
665 | is($#+, 1, $message); |
666 | is($#-, 1, $message); | |
667 | is($+ [0], 2, $message); | |
668 | is($- [0], 0, $message); | |
669 | is($+ [1], 2, $message); | |
670 | is($- [1], 1, $message); | |
671 | ok(!defined $+ [2] && !defined $- [2] && | |
672 | !defined $+ [3] && !defined $- [3], $message); | |
84281c31 A |
673 | |
674 | /.(a)(ba*)?/; | |
4f890a30 NC |
675 | is($#+, 2, $message); |
676 | is($#-, 1, $message); | |
84281c31 | 677 | } |
a72deede | 678 | |
d9bad346 FC |
679 | foreach ('$+[0] = 13', '$-[0] = 13', '@+ = (7, 6, 5)', |
680 | '@- = qw (foo bar)', '$^N = 42') { | |
cb124425 NC |
681 | is(eval $_, undef); |
682 | like($@, qr/^Modification of a read-only value attempted/, | |
d9bad346 | 683 | '$^N, @- and @+ are read-only'); |
84281c31 | 684 | } |
a72deede | 685 | |
84281c31 | 686 | { |
4f890a30 | 687 | my $message = '\G testing'; |
84281c31 A |
688 | $_ = 'aaa'; |
689 | pos = 1; | |
690 | my @a = /\Ga/g; | |
4f890a30 | 691 | is("@a", "a a", $message); |
84281c31 A |
692 | |
693 | my $str = 'abcde'; | |
694 | pos $str = 2; | |
4f890a30 NC |
695 | unlike($str, qr/^\G/, $message); |
696 | unlike($str, qr/^.\G/, $message); | |
697 | like($str, qr/^..\G/, $message); | |
698 | unlike($str, qr/^...\G/, $message); | |
699 | ok($str =~ /\G../ && $& eq 'cd', $message); | |
84281c31 | 700 | |
04934b6d | 701 | local $::TODO = $::running_as_thread; |
4f890a30 | 702 | ok($str =~ /.\G./ && $& eq 'bc', $message); |
84281c31 | 703 | } |
a72deede | 704 | |
84281c31 | 705 | { |
4f890a30 | 706 | my $message = 'pos inside (?{ })'; |
84281c31 A |
707 | my $str = 'abcde'; |
708 | our ($foo, $bar); | |
4f890a30 NC |
709 | like($str, qr/b(?{$foo = $_; $bar = pos})c/, $message); |
710 | is($foo, $str, $message); | |
711 | is($bar, 2, $message); | |
712 | is(pos $str, undef, $message); | |
84281c31 A |
713 | |
714 | undef $foo; | |
715 | undef $bar; | |
716 | pos $str = undef; | |
4f890a30 NC |
717 | ok($str =~ /b(?{$foo = $_; $bar = pos})c/g, $message); |
718 | is($foo, $str, $message); | |
719 | is($bar, 2, $message); | |
720 | is(pos $str, 3, $message); | |
84281c31 A |
721 | |
722 | $_ = $str; | |
723 | undef $foo; | |
724 | undef $bar; | |
4f890a30 NC |
725 | like($_, qr/b(?{$foo = $_; $bar = pos})c/, $message); |
726 | is($foo, $str, $message); | |
727 | is($bar, 2, $message); | |
84281c31 A |
728 | |
729 | undef $foo; | |
730 | undef $bar; | |
4f890a30 NC |
731 | ok(/b(?{$foo = $_; $bar = pos})c/g, $message); |
732 | is($foo, $str, $message); | |
733 | is($bar, 2, $message); | |
734 | is(pos, 3, $message); | |
84281c31 A |
735 | |
736 | undef $foo; | |
737 | undef $bar; | |
738 | pos = undef; | |
739 | 1 while /b(?{$foo = $_; $bar = pos})c/g; | |
4f890a30 NC |
740 | is($foo, $str, $message); |
741 | is($bar, 2, $message); | |
742 | is(pos, undef, $message); | |
84281c31 A |
743 | |
744 | undef $foo; | |
745 | undef $bar; | |
746 | $_ = 'abcde|abcde'; | |
4f890a30 NC |
747 | ok(s/b(?{$foo = $_; $bar = pos})c/x/g, $message); |
748 | is($foo, 'abcde|abcde', $message); | |
749 | is($bar, 8, $message); | |
750 | is($_, 'axde|axde', $message); | |
84281c31 A |
751 | |
752 | # List context: | |
753 | $_ = 'abcde|abcde'; | |
754 | our @res; | |
755 | () = /([ace]).(?{push @res, $1,$2})([ce])(?{push @res, $1,$2})/g; | |
756 | @res = map {defined $_ ? "'$_'" : 'undef'} @res; | |
4f890a30 | 757 | is("@res", "'a' undef 'a' 'c' 'e' undef 'a' undef 'a' 'c'", $message); |
84281c31 A |
758 | |
759 | @res = (); | |
760 | () = /([ace]).(?{push @res, $`,$&,$'})([ce])(?{push @res, $`,$&,$'})/g; | |
761 | @res = map {defined $_ ? "'$_'" : 'undef'} @res; | |
4f890a30 | 762 | is("@res", "'' 'ab' 'cde|abcde' " . |
84281c31 A |
763 | "'' 'abc' 'de|abcde' " . |
764 | "'abcd' 'e|' 'abcde' " . | |
765 | "'abcde|' 'ab' 'cde' " . | |
4f890a30 | 766 | "'abcde|' 'abc' 'de'", $message); |
84281c31 | 767 | } |
f33976b4 | 768 | |
84281c31 | 769 | { |
4f890a30 | 770 | my $message = '\G anchor checks'; |
84281c31 A |
771 | my $foo = 'aabbccddeeffgg'; |
772 | pos ($foo) = 1; | |
773 | { | |
04934b6d | 774 | local $::TODO = $::running_as_thread; |
84281c31 | 775 | no warnings 'uninitialized'; |
4f890a30 NC |
776 | ok($foo =~ /.\G(..)/g, $message); |
777 | is($1, 'ab', $message); | |
cce850e4 | 778 | |
84281c31 | 779 | pos ($foo) += 1; |
4f890a30 NC |
780 | ok($foo =~ /.\G(..)/g, $message); |
781 | is($1, 'cc', $message); | |
cce850e4 | 782 | |
84281c31 | 783 | pos ($foo) += 1; |
4f890a30 NC |
784 | ok($foo =~ /.\G(..)/g, $message); |
785 | is($1, 'de', $message); | |
cce850e4 | 786 | |
4f890a30 | 787 | ok($foo =~ /\Gef/g, $message); |
84281c31 | 788 | } |
cce850e4 | 789 | |
84281c31 | 790 | undef pos $foo; |
4f890a30 NC |
791 | ok($foo =~ /\G(..)/g, $message); |
792 | is($1, 'aa', $message); | |
cce850e4 | 793 | |
4f890a30 NC |
794 | ok($foo =~ /\G(..)/g, $message); |
795 | is($1, 'bb', $message); | |
cce850e4 | 796 | |
84281c31 | 797 | pos ($foo) = 5; |
4f890a30 NC |
798 | ok($foo =~ /\G(..)/g, $message); |
799 | is($1, 'cd', $message); | |
84281c31 | 800 | } |
cce850e4 | 801 | |
84281c31 A |
802 | { |
803 | $_ = '123x123'; | |
804 | my @res = /(\d*|x)/g; | |
805 | local $" = '|'; | |
de26e0cc | 806 | is("@res", "123||x|123|", "0 match in alternation"); |
84281c31 | 807 | } |
cce850e4 | 808 | |
84281c31 | 809 | { |
4f890a30 | 810 | my $message = "Match against temporaries (created via pp_helem())" . |
84281c31 | 811 | " is safe"; |
4f890a30 NC |
812 | ok({foo => "bar\n" . $^X} -> {foo} =~ /^(.*)\n/g, $message); |
813 | is($1, "bar", $message); | |
84281c31 | 814 | } |
75685a94 | 815 | |
84281c31 | 816 | { |
4f890a30 | 817 | my $message = 'package $i inside (?{ }), ' . |
84281c31 A |
818 | 'saved substrings and changing $_'; |
819 | our @a = qw [foo bar]; | |
820 | our @b = (); | |
821 | s/(\w)(?{push @b, $1})/,$1,/g for @a; | |
4f890a30 NC |
822 | is("@b", "f o o b a r", $message); |
823 | is("@a", ",f,,o,,o, ,b,,a,,r,", $message); | |
84281c31 | 824 | |
4f890a30 | 825 | $message = 'lexical $i inside (?{ }), ' . |
84281c31 A |
826 | 'saved substrings and changing $_'; |
827 | no warnings 'closure'; | |
828 | my @c = qw [foo bar]; | |
829 | my @d = (); | |
830 | s/(\w)(?{push @d, $1})/,$1,/g for @c; | |
4f890a30 NC |
831 | is("@d", "f o o b a r", $message); |
832 | is("@c", ",f,,o,,o, ,b,,a,,r,", $message); | |
d9f424b2 JH |
833 | } |
834 | ||
84281c31 | 835 | { |
4f890a30 | 836 | my $message = 'Brackets'; |
84281c31 A |
837 | our $brackets; |
838 | $brackets = qr { | |
839 | { (?> [^{}]+ | (??{ $brackets }) )* } | |
840 | }x; | |
841 | ||
4f890a30 NC |
842 | ok("{{}" =~ $brackets, $message); |
843 | is($&, "{}", $message); | |
844 | ok("something { long { and } hairy" =~ $brackets, $message); | |
845 | is($&, "{ and }", $message); | |
846 | ok("something { long { and } hairy" =~ m/((??{ $brackets }))/, $message); | |
847 | is($&, "{ and }", $message); | |
84281c31 | 848 | } |
a4c04bdc | 849 | |
84281c31 A |
850 | { |
851 | $_ = "a-a\nxbb"; | |
852 | pos = 1; | |
b33825c4 | 853 | ok(!m/^-.*bb/mg, '$_ = "a-a\nxbb"; m/^-.*bb/mg'); |
84281c31 | 854 | } |
a4c04bdc | 855 | |
84281c31 | 856 | { |
4f890a30 | 857 | my $message = '\G anchor checks'; |
84281c31 A |
858 | my $text = "aaXbXcc"; |
859 | pos ($text) = 0; | |
4f890a30 | 860 | ok($text !~ /\GXb*X/g, $message); |
84281c31 | 861 | } |
a4c04bdc | 862 | |
84281c31 A |
863 | { |
864 | $_ = "xA\n" x 500; | |
b33825c4 | 865 | unlike($_, qr/^\s*A/m, '$_ = "xA\n" x 500; /^\s*A/m"'); |
a4c04bdc | 866 | |
84281c31 A |
867 | my $text = "abc dbf"; |
868 | my @res = ($text =~ /.*?(b).*?\b/g); | |
de26e0cc | 869 | is("@res", "b b", '\b is not special'); |
987aaf07 | 870 | } |
a4c04bdc | 871 | |
84281c31 | 872 | { |
4f890a30 | 873 | my $message = '\S, [\S], \s, [\s]'; |
84281c31 | 874 | my @a = map chr, 0 .. 255; |
9d45b377 YO |
875 | my @b = grep m/\S/, @a; |
876 | my @c = grep m/[^\s]/, @a; | |
4f890a30 | 877 | is("@b", "@c", $message); |
84281c31 A |
878 | |
879 | @b = grep /\S/, @a; | |
880 | @c = grep /[\S]/, @a; | |
4f890a30 | 881 | is("@b", "@c", $message); |
84281c31 A |
882 | |
883 | @b = grep /\s/, @a; | |
884 | @c = grep /[^\S]/, @a; | |
4f890a30 | 885 | is("@b", "@c", $message); |
84281c31 A |
886 | |
887 | @b = grep /\s/, @a; | |
888 | @c = grep /[\s]/, @a; | |
4f890a30 | 889 | is("@b", "@c", $message); |
84281c31 A |
890 | } |
891 | { | |
4f890a30 | 892 | my $message = '\D, [\D], \d, [\d]'; |
84281c31 A |
893 | my @a = map chr, 0 .. 255; |
894 | my @b = grep /\D/, @a; | |
895 | my @c = grep /[^\d]/, @a; | |
4f890a30 | 896 | is("@b", "@c", $message); |
84281c31 A |
897 | |
898 | @b = grep /\D/, @a; | |
899 | @c = grep /[\D]/, @a; | |
4f890a30 | 900 | is("@b", "@c", $message); |
84281c31 A |
901 | |
902 | @b = grep /\d/, @a; | |
903 | @c = grep /[^\D]/, @a; | |
4f890a30 | 904 | is("@b", "@c", $message); |
84281c31 A |
905 | |
906 | @b = grep /\d/, @a; | |
907 | @c = grep /[\d]/, @a; | |
4f890a30 | 908 | is("@b", "@c", $message); |
84281c31 A |
909 | } |
910 | { | |
4f890a30 | 911 | my $message = '\W, [\W], \w, [\w]'; |
84281c31 A |
912 | my @a = map chr, 0 .. 255; |
913 | my @b = grep /\W/, @a; | |
914 | my @c = grep /[^\w]/, @a; | |
4f890a30 | 915 | is("@b", "@c", $message); |
84281c31 A |
916 | |
917 | @b = grep /\W/, @a; | |
918 | @c = grep /[\W]/, @a; | |
4f890a30 | 919 | is("@b", "@c", $message); |
84281c31 A |
920 | |
921 | @b = grep /\w/, @a; | |
922 | @c = grep /[^\W]/, @a; | |
4f890a30 | 923 | is("@b", "@c", $message); |
84281c31 A |
924 | |
925 | @b = grep /\w/, @a; | |
926 | @c = grep /[\w]/, @a; | |
4f890a30 | 927 | is("@b", "@c", $message); |
84281c31 | 928 | } |
a4c04bdc | 929 | |
84281c31 A |
930 | { |
931 | # see if backtracking optimization works correctly | |
4f890a30 NC |
932 | my $message = 'Backtrack optimization'; |
933 | like("\n\n", qr/\n $ \n/x, $message); | |
934 | like("\n\n", qr/\n* $ \n/x, $message); | |
935 | like("\n\n", qr/\n+ $ \n/x, $message); | |
936 | like("\n\n", qr/\n? $ \n/x, $message); | |
937 | like("\n\n", qr/\n*? $ \n/x, $message); | |
938 | like("\n\n", qr/\n+? $ \n/x, $message); | |
939 | like("\n\n", qr/\n?? $ \n/x, $message); | |
940 | unlike("\n\n", qr/\n*+ $ \n/x, $message); | |
941 | unlike("\n\n", qr/\n++ $ \n/x, $message); | |
942 | like("\n\n", qr/\n?+ $ \n/x, $message); | |
84281c31 | 943 | } |
a4c04bdc | 944 | |
84281c31 A |
945 | { |
946 | package S; | |
947 | use overload '""' => sub {'Object S'}; | |
948 | sub new {bless []} | |
0f289c68 | 949 | |
4f890a30 | 950 | my $message = "Ref stringification"; |
5895685f NC |
951 | ::ok(do { \my $v} =~ /^SCALAR/, "Scalar ref stringification") or diag($message); |
952 | ::ok(do {\\my $v} =~ /^REF/, "Ref ref stringification") or diag($message); | |
953 | ::ok([] =~ /^ARRAY/, "Array ref stringification") or diag($message); | |
954 | ::ok({} =~ /^HASH/, "Hash ref stringification") or diag($message); | |
955 | ::ok('S' -> new =~ /^Object S/, "Object stringification") or diag($message); | |
84281c31 | 956 | } |
a4c04bdc | 957 | |
84281c31 | 958 | { |
4f890a30 NC |
959 | my $message = "Test result of match used as match"; |
960 | ok('a1b' =~ ('xyz' =~ /y/), $message); | |
961 | is($`, 'a', $message); | |
962 | ok('a1b' =~ ('xyz' =~ /t/), $message); | |
963 | is($`, 'a', $message); | |
84281c31 | 964 | } |
a4c04bdc | 965 | |
84281c31 | 966 | { |
d728c370 | 967 | my $message = '"1" is not \s'; |
c11a8df3 NC |
968 | warning_is(sub {unlike("1\n" x 102, qr/^\s*\n/m, $message)}, |
969 | undef, "$message (did not warn)"); | |
84281c31 | 970 | } |
a4c04bdc | 971 | |
84281c31 | 972 | { |
4f890a30 | 973 | my $message = '\s, [[:space:]] and [[:blank:]]'; |
84281c31 A |
974 | my %space = (spc => " ", |
975 | tab => "\t", | |
976 | cr => "\r", | |
977 | lf => "\n", | |
978 | ff => "\f", | |
979 | # There's no \v but the vertical tabulator seems miraculously | |
980 | # be 11 both in ASCII and EBCDIC. | |
981 | vt => chr(11), | |
982 | false => "space"); | |
983 | ||
984 | my @space0 = sort grep {$space {$_} =~ /\s/ } keys %space; | |
985 | my @space1 = sort grep {$space {$_} =~ /[[:space:]]/} keys %space; | |
986 | my @space2 = sort grep {$space {$_} =~ /[[:blank:]]/} keys %space; | |
987 | ||
075b9d7d | 988 | is("@space0", "cr ff lf spc tab vt", $message); |
4f890a30 NC |
989 | is("@space1", "cr ff lf spc tab vt", $message); |
990 | is("@space2", "spc tab", $message); | |
84281c31 | 991 | } |
a4c04bdc | 992 | |
ff3f963a | 993 | { |
c9415951 | 994 | my $n= 50; |
93f09d7b | 995 | # this must be a high number and go from 0 to N, as the bug we are looking for doesn't |
c9415951 YO |
996 | # seem to be predictable. Slight changes to the test make it fail earlier or later. |
997 | foreach my $i (0 .. $n) | |
998 | { | |
999 | my $str= "\n" x $i; | |
93f09d7b | 1000 | ok $str=~/.*\z/, "implicit MBOL check string disable does not break things length=$i"; |
c9415951 YO |
1001 | } |
1002 | } | |
92f3d482 YO |
1003 | { |
1004 | # we are actually testing that we dont die when executing these patterns | |
1005 | use utf8; | |
1006 | my $e = "Böck"; | |
1007 | ok(utf8::is_utf8($e),"got a unicode string - rt75680"); | |
1008 | ||
1009 | ok($e !~ m/.*?[x]$/, "unicode string against /.*?[x]\$/ - rt75680"); | |
1010 | ok($e !~ m/.*?\p{Space}$/i, "unicode string against /.*?\\p{space}\$/i - rt75680"); | |
1011 | ok($e !~ m/.*?[xyz]$/, "unicode string against /.*?[xyz]\$/ - rt75680"); | |
1012 | ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/, "unicode string against big pattern - rt75680"); | |
1013 | } | |
1014 | { | |
1015 | # we are actually testing that we dont die when executing these patterns | |
1016 | my $e = "B\x{f6}ck"; | |
1017 | ok(!utf8::is_utf8($e), "got a latin string - rt75680"); | |
1018 | ||
1019 | ok($e !~ m/.*?[x]$/, "latin string against /.*?[x]\$/ - rt75680"); | |
1020 | ok($e !~ m/.*?\p{Space}$/i, "latin string against /.*?\\p{space}\$/i - rt75680"); | |
1021 | ok($e !~ m/.*?[xyz]$/,"latin string against /.*?[xyz]\$/ - rt75680"); | |
1022 | ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/,"latin string against big pattern - rt75680"); | |
1023 | } | |
c920e018 A |
1024 | |
1025 | { | |
1026 | # | |
1027 | # Tests for bug 77414. | |
1028 | # | |
1029 | ||
4f890a30 | 1030 | my $message = '\p property after empty * match'; |
c920e018 | 1031 | { |
4f890a30 NC |
1032 | like("1", qr/\s*\pN/, $message); |
1033 | like("-", qr/\s*\p{Dash}/, $message); | |
1034 | like(" ", qr/\w*\p{Blank}/, $message); | |
c920e018 A |
1035 | } |
1036 | ||
4f890a30 NC |
1037 | like("1", qr/\s*\pN+/, $message); |
1038 | like("-", qr/\s*\p{Dash}{1}/, $message); | |
1039 | like(" ", qr/\w*\p{Blank}{1,4}/, $message); | |
c920e018 A |
1040 | |
1041 | } | |
1042 | ||
7c17ea2f KW |
1043 | SKIP: { # Some constructs with Latin1 characters cause a utf8 string not |
1044 | # to match itself in non-utf8 | |
ef237063 | 1045 | if ($::IS_EBCDIC) { |
7c17ea2f KW |
1046 | skip "Needs to be customized to run on EBCDIC", 6; |
1047 | } | |
634c83a2 KW |
1048 | my $c = "\xc0"; |
1049 | my $pattern = my $utf8_pattern = qr/((\xc0)+,?)/; | |
1050 | utf8::upgrade($utf8_pattern); | |
1051 | ok $c =~ $pattern, "\\xc0 =~ $pattern; Neither pattern nor target utf8"; | |
d4e0b827 | 1052 | ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; Neither pattern nor target utf8"; |
634c83a2 | 1053 | ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; pattern utf8, target not"; |
d4e0b827 | 1054 | ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; pattern utf8, target not"; |
634c83a2 KW |
1055 | utf8::upgrade($c); |
1056 | ok $c =~ $pattern, "\\xc0 =~ $pattern; target utf8, pattern not"; | |
d4e0b827 | 1057 | ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; target utf8, pattern not"; |
634c83a2 | 1058 | ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; Both target and pattern utf8"; |
d4e0b827 | 1059 | ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; Both target and pattern utf8"; |
634c83a2 KW |
1060 | } |
1061 | ||
8cc86590 | 1062 | SKIP: { # Make sure can override the formatting |
ef237063 | 1063 | if ($::IS_EBCDIC) { |
8cc86590 KW |
1064 | skip "Needs to be customized to run on EBCDIC", 2; |
1065 | } | |
1066 | use feature 'unicode_strings'; | |
1067 | ok "\xc0" =~ /\w/, 'Under unicode_strings: "\xc0" =~ /\w/'; | |
1068 | ok "\xc0" !~ /(?d:\w)/, 'Under unicode_strings: "\xc0" !~ /(?d:\w)/'; | |
1069 | } | |
1070 | ||
704f71be | 1071 | { |
5b6010b3 YO |
1072 | my $str= "\x{100}"; |
1073 | chop $str; | |
1074 | my $qr= qr/$str/; | |
de26e0cc | 1075 | is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag enabled - Bug #80212"); |
5b6010b3 YO |
1076 | $str= ""; |
1077 | $qr= qr/$str/; | |
de26e0cc | 1078 | is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag disabled - Bug #80212"); |
5b6010b3 YO |
1079 | |
1080 | } | |
1081 | ||
72aa120d | 1082 | { |
04934b6d | 1083 | local $::TODO = "[perl #38133]"; |
72aa120d KW |
1084 | |
1085 | "A" =~ /(((?:A))?)+/; | |
1086 | my $first = $2; | |
1087 | ||
1088 | "A" =~ /(((A))?)+/; | |
1089 | my $second = $2; | |
1090 | ||
de26e0cc | 1091 | is($first, $second); |
72aa120d KW |
1092 | } |
1093 | ||
99ca48e1 DM |
1094 | { |
1095 | # RT #3516: \G in a m//g expression causes problems | |
1096 | my $count = 0; | |
1097 | while ("abc" =~ m/(\G[ac])?/g) { | |
1098 | last if $count++ > 10; | |
1099 | } | |
1100 | ok($count < 10, 'RT #3516 A'); | |
1101 | ||
1102 | $count = 0; | |
1103 | while ("abc" =~ m/(\G|.)[ac]/g) { | |
1104 | last if $count++ > 10; | |
1105 | } | |
1106 | ok($count < 10, 'RT #3516 B'); | |
1107 | ||
1108 | $count = 0; | |
1109 | while ("abc" =~ m/(\G?[ac])?/g) { | |
1110 | last if $count++ > 10; | |
1111 | } | |
1112 | ok($count < 10, 'RT #3516 C'); | |
1113 | } | |
d774cd11 YO |
1114 | { |
1115 | # RT #84294: Is this a bug in the simple Perl regex? | |
1116 | # : Nested buffers and (?{...}) dont play nicely on partial matches | |
1117 | our @got= (); | |
1118 | ok("ab" =~ /((\w+)(?{ push @got, $2 })){2}/,"RT #84294: Pattern should match"); | |
d774cd11 YO |
1119 | my $want= "'ab', 'a', 'b'"; |
1120 | my $got= join(", ", map { defined($_) ? "'$_'" : "undef" } @got); | |
1121 | is($got,$want,'RT #84294: check that "ab" =~ /((\w+)(?{ push @got, $2 })){2}/ leaves @got in the correct state'); | |
1122 | } | |
1123 | ||
0bda3001 KW |
1124 | { |
1125 | # Suppress warnings, as the non-unicode one comes out even if turn off | |
1126 | # warnings here (because the execution is done in another scope). | |
1127 | local $SIG{__WARN__} = sub {}; | |
1128 | my $str = "\x{110000}"; | |
1129 | ||
1130 | # No non-unicode code points match any Unicode property, even inverse | |
1131 | # ones | |
1132 | unlike($str, qr/\p{ASCII_Hex_Digit=True}/, "Non-Unicode doesn't match \\p{}"); | |
1133 | unlike($str, qr/\p{ASCII_Hex_Digit=False}/, "Non-Unicode doesn't match \\p{}"); | |
1134 | like($str, qr/\P{ASCII_Hex_Digit=True}/, "Non-Unicode matches \\P{}"); | |
1135 | like($str, qr/\P{ASCII_Hex_Digit=False}/, "Non-Unicode matches \\P{}"); | |
1136 | } | |
1137 | ||
b289b443 | 1138 | { |
c256547e | 1139 | # Test that IDstart works, but because the author (khw) knows |
b289b443 KW |
1140 | # regexes much better than the rest of the core, it is being done here |
1141 | # in the context of a regex which relies on buffer names beginng with | |
1142 | # IDStarts. | |
1143 | use utf8; | |
1144 | my $str = "abc"; | |
1145 | like($str, qr/(?<a>abc)/, "'a' is legal IDStart"); | |
1146 | like($str, qr/(?<_>abc)/, "'_' is legal IDStart"); | |
1147 | like($str, qr/(?<ß>abc)/, "U+00DF is legal IDStart"); | |
1148 | like($str, qr/(?<ℕ>abc)/, "U+2115' is legal IDStart"); | |
1149 | ||
1150 | # This test works on Unicode 6.0 in which U+2118 and U+212E are legal | |
1151 | # IDStarts there, but are not Word characters, and therefore Perl | |
1152 | # doesn't allow them to be IDStarts. But there is no guarantee that | |
1153 | # Unicode won't change things around in the future so that at some | |
1154 | # future Unicode revision these tests would need to be revised. | |
1155 | foreach my $char ("%", "×", chr(0x2118), chr(0x212E)) { | |
1156 | my $prog = <<"EOP"; | |
1157 | use utf8;; | |
1158 | "abc" =~ qr/(?<$char>abc)/; | |
1159 | EOP | |
1160 | utf8::encode($prog); | |
1161 | fresh_perl_like($prog, qr!Sequence.* not recognized!, "", | |
1162 | sprintf("'U+%04X not legal IDFirst'", ord($char))); | |
1163 | } | |
1164 | } | |
bbdd8bad KW |
1165 | |
1166 | { # [perl #101710] | |
1167 | my $pat = "b"; | |
1168 | utf8::upgrade($pat); | |
1169 | like("\xffb", qr/$pat/i, "/i: utf8 pattern, non-utf8 string, latin1-char preceding matching char in string"); | |
1170 | } | |
1171 | ||
ba510004 FC |
1172 | { # Crash with @a =~ // warning |
1173 | local $SIG{__WARN__} = sub { | |
1174 | pass 'no crash for @a =~ // warning' | |
1175 | }; | |
1176 | eval ' sub { my @a =~ // } '; | |
1177 | } | |
1178 | ||
cc88c9aa FC |
1179 | { # Concat overloading and qr// thingies |
1180 | my @refs; | |
1181 | my $qr = qr//; | |
3bbc3d30 YO |
1182 | package Cat { |
1183 | require overload; | |
1184 | overload->import( | |
cc88c9aa FC |
1185 | '""' => sub { ${$_[0]} }, |
1186 | '.' => sub { | |
1187 | push @refs, ref $_[1] if ref $_[1]; | |
1188 | bless $_[2] ? \"$_[1]${$_[0]}" : \"${$_[0]}$_[1]" | |
1189 | } | |
3bbc3d30 | 1190 | ); |
cc88c9aa FC |
1191 | } |
1192 | my $s = "foo"; | |
1193 | my $o = bless \$s, Cat::; | |
1194 | /$o$qr/; | |
1195 | is "@refs", "Regexp", '/$o$qr/ passes qr ref to cat overload meth'; | |
1196 | } | |
1197 | ||
21eede78 YO |
1198 | { |
1199 | my $count=0; | |
1200 | my $str="\n"; | |
1201 | $count++ while $str=~/.*/g; | |
1202 | is $count, 2, 'test that ANCH_MBOL works properly. We should get 2 from $count++ while "\n"=~/.*/g'; | |
1203 | my $class_count= 0; | |
1204 | $class_count++ while $str=~/[^\n]*/g; | |
1205 | is $class_count, $count, 'while "\n"=~/.*/g and while "\n"=~/[^\n]*/g should behave the same'; | |
1206 | my $anch_count= 0; | |
1207 | $anch_count++ while $str=~/^.*/mg; | |
1208 | is $anch_count, 1, 'while "\n"=~/^.*/mg should match only once'; | |
1209 | } | |
96f54887 KW |
1210 | |
1211 | { # [perl #111174] | |
1212 | use re '/u'; | |
1213 | like "\xe0", qr/(?i:\xc0)/, "(?i: shouldn't lose the passed in /u"; | |
1214 | use re '/a'; | |
1215 | unlike "\x{100}", qr/(?i:\w)/, "(?i: shouldn't lose the passed in /a"; | |
1216 | use re '/aa'; | |
1217 | unlike 'k', qr/(?i:\N{KELVIN SIGN})/, "(?i: shouldn't lose the passed in /aa"; | |
1218 | } | |
6ae44cd2 DM |
1219 | |
1220 | { | |
1221 | # the test for whether the pattern should be re-compiled should | |
1222 | # consider the UTF8ness of the previous and current pattern | |
1223 | # string, as well as the physical bytes of the pattern string | |
1224 | ||
1225 | for my $s ("\xc4\x80", "\x{100}") { | |
1226 | ok($s =~ /^$s$/, "re-compile check is UTF8-aware"); | |
1227 | } | |
1228 | } | |
1229 | ||
84281c31 A |
1230 | } # End of sub run_tests |
1231 | ||
1232 | 1; |