This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Clarify FIRSTKEY and NEXTKEY usage.
[perl5.git] / t / re / pat.t
CommitLineData
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
7use strict;
8use warnings;
9use 5.010;
10
84281c31
A
11sub run_tests;
12
9133bbab 13$| = 1;
3568d838 14
8d37f932 15
e4d48cc9
GS
16BEGIN {
17 chdir 't' if -d 't';
cc7e6304 18 @INC = ('../lib','.','../ext/re');
569f7fc5 19 require Config; import Config;
6d937e95 20 require './test.pl'; require './charset_tools.pl';
ef9d5242 21 require './loc_tools.pl';
b51555b9 22 skip_all('no re module') unless defined &DynaLoader::boot_DynaLoader;
2b08d1e2 23 skip_all_without_unicode_tables();
e4d48cc9 24}
84281c31 25
22b433ef 26plan tests => 775; # Update this when adding/deleting tests.
b7a35066 27
9d45b377 28run_tests() unless caller;
b7a35066 29
84281c31
A
30#
31# Tests start here.
32#
33sub run_tests {
0ef3e39e 34
84281c31 35 {
84281c31 36 my $x = "abc\ndef\n";
5895685f 37 (my $x_pretty = $x) =~ s/\n/\\n/g;
fd291da9 38
5895685f
NC
39 ok $x =~ /^abc/, qq ["$x_pretty" =~ /^abc/];
40 ok $x !~ /^def/, qq ["$x_pretty" !~ /^def/];
fd291da9 41
84281c31 42 # used to be a test for $*
5895685f 43 ok $x =~ /^def/m, qq ["$x_pretty" =~ /^def/m];
fd291da9 44
b33825c4
NC
45 ok(!($x =~ /^xxx/), qq ["$x_pretty" =~ /^xxx/]);
46 ok(!($x !~ /^abc/), qq ["$x_pretty" !~ /^abc/]);
fd291da9 47
5895685f 48 ok $x =~ /def/, qq ["$x_pretty" =~ /def/];
b33825c4 49 ok(!($x !~ /def/), qq ["$x_pretty" !~ /def/]);
4765795a 50
5895685f 51 ok $x !~ /.def/, qq ["$x_pretty" !~ /.def/];
b33825c4 52 ok(!($x =~ /.def/), qq ["$x_pretty" =~ /.def/]);
4765795a 53
5895685f 54 ok $x =~ /\ndef/, qq ["$x_pretty" =~ /\\ndef/];
b33825c4 55 ok(!($x !~ /\ndef/), qq ["$x_pretty" !~ /\\ndef/]);
84281c31 56 }
4765795a 57
84281c31
A
58 {
59 $_ = '123';
60 ok /^([0-9][0-9]*)/, qq [\$_ = '$_'; /^([0-9][0-9]*)/];
61 }
f9969324 62
84281c31
A
63 {
64 $_ = 'aaabbbccc';
65 ok /(a*b*)(c*)/ && $1 eq 'aaabbb' && $2 eq 'ccc',
66 qq [\$_ = '$_'; /(a*b*)(c*)/];
67 ok /(a+b+c+)/ && $1 eq 'aaabbbccc', qq [\$_ = '$_'; /(a+b+c+)/];
b33825c4 68 unlike($_, qr/a+b?c+/, qq [\$_ = '$_'; /a+b?c+/]);
84281c31
A
69
70 $_ = 'aaabccc';
71 ok /a+b?c+/, qq [\$_ = '$_'; /a+b?c+/];
72 ok /a*b?c*/, qq [\$_ = '$_'; /a*b?c*/];
73
74 $_ = 'aaaccc';
75 ok /a*b?c*/, qq [\$_ = '$_'; /a*b?c*/];
b33825c4 76 unlike($_, qr/a*b+c*/, qq [\$_ = '$_'; /a*b+c*/]);
84281c31
A
77
78 $_ = 'abcdef';
79 ok /bcd|xyz/, qq [\$_ = '$_'; /bcd|xyz/];
80 ok /xyz|bcd/, qq [\$_ = '$_'; /xyz|bcd/];
81 ok m|bc/*d|, qq [\$_ = '$_'; m|bc/*d|];
82 ok /^$_$/, qq [\$_ = '$_'; /^\$_\$/];
4765795a 83 }
4765795a 84
84281c31
A
85 {
86 # used to be a test for $*
5895685f 87 ok "ab\ncd\n" =~ /^cd/m, q ["ab\ncd\n" =~ /^cd/m];
84281c31 88 }
4765795a 89
84281c31
A
90 {
91 our %XXX = map {($_ => $_)} 123, 234, 345;
92
93 our @XXX = ('ok 1','not ok 1', 'ok 2','not ok 2','not ok 3');
94 while ($_ = shift(@XXX)) {
b33825c4 95 my $e = index ($_, 'not') >= 0 ? '' : 1;
725a61d7 96 my $r = m?(.*)?;
b33825c4 97 is($r, $e, "?(.*)?");
84281c31
A
98 /not/ && reset;
99 if (/not ok 2/) {
100 if ($^O eq 'VMS') {
101 $_ = shift(@XXX);
102 }
103 else {
104 reset 'X';
105 }
106 }
107 }
4765795a 108
84281c31
A
109 SKIP: {
110 if ($^O eq 'VMS') {
111 skip "Reset 'X'", 1;
112 }
113 ok !keys %XXX, "%XXX is empty";
114 }
4765795a 115
84281c31 116 }
4765795a 117
84281c31 118 {
4f890a30 119 my $message = "Test empty pattern";
84281c31
A
120 my $xyz = 'xyz';
121 my $cde = 'cde';
122
123 $cde =~ /[^ab]*/;
124 $xyz =~ //;
4f890a30 125 is($&, $xyz, $message);
84281c31
A
126
127 my $foo = '[^ab]*';
128 $cde =~ /$foo/;
129 $xyz =~ //;
4f890a30 130 is($&, $xyz, $message);
84281c31
A
131
132 $cde =~ /$foo/;
133 my $null;
134 no warnings 'uninitialized';
135 $xyz =~ /$null/;
4f890a30 136 is($&, $xyz, $message);
84281c31
A
137
138 $null = "";
139 $xyz =~ /$null/;
4f890a30 140 is($&, $xyz, $message);
84281c31 141 }
4765795a 142
84281c31 143 {
4f890a30 144 my $message = q !Check $`, $&, $'!;
84281c31 145 $_ = 'abcdefghi';
0f289c68 146 /def/; # optimized up to cmd
4f890a30 147 is("$`:$&:$'", 'abc:def:ghi', $message);
4765795a 148
84281c31 149 no warnings 'void';
0f289c68 150 /cde/ + 0; # optimized only to spat
4f890a30 151 is("$`:$&:$'", 'ab:cde:fghi', $message);
4765795a 152
0f289c68 153 /[d][e][f]/; # not optimized
4f890a30 154 is("$`:$&:$'", 'abc:def:ghi', $message);
84281c31 155 }
4765795a 156
84281c31
A
157 {
158 $_ = 'now is the {time for all} good men to come to.';
2a53d331 159 / \{([^}]*)}/;
de26e0cc 160 is($1, 'time for all', "Match braces");
84281c31 161 }
4765795a 162
84281c31 163 {
4f890a30 164 my $message = "{N,M} quantifier";
84281c31 165 $_ = 'xxx {3,4} yyy zzz';
4f890a30
NC
166 ok(/( {3,4})/, $message);
167 is($1, ' ', $message);
168 unlike($_, qr/( {4,})/, $message);
169 ok(/( {2,3}.)/, $message);
170 is($1, ' y', $message);
171 ok(/(y{2,3}.)/, $message);
172 is($1, 'yyy ', $message);
173 unlike($_, qr/x {3,4}/, $message);
174 unlike($_, qr/^xxx {3,4}/, $message);
84281c31 175 }
4765795a 176
84281c31 177 {
4f890a30 178 my $message = "Test /g";
84281c31
A
179 local $" = ":";
180 $_ = "now is the time for all good men to come to.";
181 my @words = /(\w+)/g;
182 my $exp = "now:is:the:time:for:all:good:men:to:come:to";
4765795a 183
4f890a30 184 is("@words", $exp, $message);
4765795a 185
84281c31
A
186 @words = ();
187 while (/\w+/g) {
188 push (@words, $&);
189 }
4f890a30 190 is("@words", $exp, $message);
4765795a 191
84281c31
A
192 @words = ();
193 pos = 0;
194 while (/to/g) {
195 push(@words, $&);
196 }
4f890a30 197 is("@words", "to:to", $message);
4765795a 198
84281c31
A
199 pos $_ = 0;
200 @words = /to/g;
4f890a30 201 is("@words", "to:to", $message);
84281c31 202 }
4765795a 203
84281c31
A
204 {
205 $_ = "abcdefghi";
206
207 my $pat1 = 'def';
208 my $pat2 = '^def';
209 my $pat3 = '.def.';
210 my $pat4 = 'abc';
211 my $pat5 = '^abc';
212 my $pat6 = 'abc$';
213 my $pat7 = 'ghi';
214 my $pat8 = '\w*ghi';
215 my $pat9 = 'ghi$';
216
217 my $t1 = my $t2 = my $t3 = my $t4 = my $t5 =
218 my $t6 = my $t7 = my $t8 = my $t9 = 0;
219
220 for my $iter (1 .. 5) {
221 $t1++ if /$pat1/o;
222 $t2++ if /$pat2/o;
223 $t3++ if /$pat3/o;
224 $t4++ if /$pat4/o;
225 $t5++ if /$pat5/o;
226 $t6++ if /$pat6/o;
227 $t7++ if /$pat7/o;
228 $t8++ if /$pat8/o;
229 $t9++ if /$pat9/o;
230 }
231 my $x = "$t1$t2$t3$t4$t5$t6$t7$t8$t9";
de26e0cc 232 is($x, '505550555', "Test /o");
84281c31 233 }
4765795a 234
4f890a30 235 {
84281c31
A
236 my $xyz = 'xyz';
237 ok "abc" =~ /^abc$|$xyz/, "| after \$";
4765795a 238
84281c31 239 # perl 4.009 says "unmatched ()"
4f890a30 240 my $message = '$ inside ()';
4765795a 241
84281c31
A
242 my $result;
243 eval '"abc" =~ /a(bc$)|$xyz/; $result = "$&:$1"';
4f890a30
NC
244 is($@, "", $message);
245 is($result, "abc:bc", $message);
84281c31 246 }
4765795a 247
84281c31 248 {
4f890a30 249 my $message = "Scalar /g";
84281c31
A
250 $_ = "abcfooabcbar";
251
4f890a30
NC
252 ok( /abc/g && $` eq "", $message);
253 ok( /abc/g && $` eq "abcfoo", $message);
254 ok(!/abc/g, $message);
84281c31 255
4f890a30 256 $message = "Scalar /gi";
84281c31 257 pos = 0;
4f890a30
NC
258 ok( /ABC/gi && $` eq "", $message);
259 ok( /ABC/gi && $` eq "abcfoo", $message);
260 ok(!/ABC/gi, $message);
84281c31 261
4f890a30 262 $message = "Scalar /g";
84281c31 263 pos = 0;
4f890a30
NC
264 ok( /abc/g && $' eq "fooabcbar", $message);
265 ok( /abc/g && $' eq "bar", $message);
84281c31
A
266
267 $_ .= '';
268 my @x = /abc/g;
de26e0cc 269 is(@x, 2, "/g reset after assignment");
4765795a 270 }
4765795a 271
84281c31 272 {
4f890a30 273 my $message = '/g, \G and pos';
84281c31
A
274 $_ = "abdc";
275 pos $_ = 2;
276 /\Gc/gc;
4f890a30 277 is(pos $_, 2, $message);
84281c31 278 /\Gc/g;
4f890a30 279 is(pos $_, undef, $message);
84281c31 280 }
4765795a 281
84281c31 282 {
4f890a30 283 my $message = '(?{ })';
84281c31
A
284 our $out = 1;
285 'abc' =~ m'a(?{ $out = 2 })b';
4f890a30 286 is($out, 2, $message);
84281c31
A
287
288 $out = 1;
289 'abc' =~ m'a(?{ $out = 3 })c';
4f890a30 290 is($out, 1, $message);
84281c31 291 }
4765795a 292
84281c31
A
293 {
294 $_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6';
295 my @out = /(?<!foo)bar./g;
de26e0cc 296 is("@out", 'bar2 barf', "Negative lookbehind");
84281c31 297 }
4765795a 298
84281c31 299 {
4f890a30 300 my $message = "REG_INFTY tests";
84281c31 301 # Tests which depend on REG_INFTY
19d6612d
NC
302
303 # Defaults assumed if this fails
304 eval { require Config; };
305 $::reg_infty = $Config::Config{reg_infty} // 32767;
84281c31
A
306 $::reg_infty_m = $::reg_infty - 1;
307 $::reg_infty_p = $::reg_infty + 1;
93f09d7b 308 $::reg_infty_m = $::reg_infty_m; # Suppress warning.
84281c31
A
309
310 # As well as failing if the pattern matches do unexpected things, the
311 # next three tests will fail if you should have picked up a lower-than-
312 # default value for $reg_infty from Config.pm, but have not.
313
14358a41
NC
314 is(eval q{('aaa' =~ /(a{1,$::reg_infty_m})/)[0]}, 'aaa', $message);
315 is($@, '', $message);
316 is(eval q{('a' x $::reg_infty_m) =~ /a{$::reg_infty_m}/}, 1, $message);
317 is($@, '', $message);
318 isnt(q{('a' x ($::reg_infty_m - 1)) !~ /a{$::reg_infty_m}/}, 1, $message);
319 is($@, '', $message);
320
84281c31 321 eval "'aaa' =~ /a{1,$::reg_infty}/";
224b2e7e 322 like($@, qr/^\QQuantifier in {,} bigger than/, $message);
84281c31 323 eval "'aaa' =~ /a{1,$::reg_infty_p}/";
4f890a30 324 like($@, qr/^\QQuantifier in {,} bigger than/, $message);
4765795a 325 }
8269fa76 326
84281c31
A
327 {
328 # Poke a couple more parse failures
329 my $context = 'x' x 256;
330 eval qq("${context}y" =~ /(?<=$context)y/);
331 ok $@ =~ /^\QLookbehind longer than 255 not/, "Lookbehind limit";
332 }
8269fa76 333
84281c31
A
334 {
335 # Long Monsters
84281c31
A
336 for my $l (125, 140, 250, 270, 300000, 30) { # Ordered to free memory
337 my $a = 'a' x $l;
4f890a30
NC
338 my $message = "Long monster, length = $l";
339 like("ba$a=", qr/a$a=/, $message);
340 unlike("b$a=", qr/a$a=/, $message);
341 like("b$a=", qr/ba+=/, $message);
84281c31 342
224b2e7e 343 like("ba$a=", qr/b(?:a|b)+=/, $message);
84281c31
A
344 }
345 }
8269fa76 346
84281c31
A
347 {
348 # 20000 nodes, each taking 3 words per string, and 1 per branch
349 my $long_constant_len = join '|', 12120 .. 32645;
350 my $long_var_len = join '|', 8120 .. 28645;
351 my %ans = ( 'ax13876y25677lbc' => 1,
352 'ax13876y25677mcb' => 0, # not b.
353 'ax13876y35677nbc' => 0, # Num too big
354 'ax13876y25677y21378obc' => 1,
0f289c68 355 'ax13876y25677y21378zbc' => 0, # Not followed by [k-o]
84281c31
A
356 'ax13876y25677y21378y21378kbc' => 1,
357 'ax13876y25677y21378y21378kcb' => 0, # Not b.
358 'ax13876y25677y21378y21378y21378kbc' => 0, # 5 runs
359 );
360
84281c31 361 for (keys %ans) {
4f890a30
NC
362 my $message = "20000 nodes, const-len '$_'";
363 ok !($ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o), $message;
84281c31 364
4f890a30
NC
365 $message = "20000 nodes, var-len '$_'";
366 ok !($ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o,), $message;
84281c31 367 }
b8ef571c 368 }
209a9bc1 369
84281c31 370 {
4f890a30 371 my $message = "Complicated backtracking";
84281c31
A
372 $_ = " a (bla()) and x(y b((l)u((e))) and b(l(e)e)e";
373 my $expect = "(bla()) ((l)u((e))) (l(e)e)";
374
375 use vars '$c';
376 sub matchit {
377 m/
378 (
379 \(
0f289c68 380 (?{ $c = 1 }) # Initialize
84281c31
A
381 (?:
382 (?(?{ $c == 0 }) # PREVIOUS iteration was OK, stop the loop
383 (?!
0f289c68
YO
384 ) # Fail: will unwind one iteration back
385 )
84281c31 386 (?:
0f289c68 387 [^()]+ # Match a big chunk
84281c31
A
388 (?=
389 [()]
0f289c68 390 ) # Do not try to match subchunks
84281c31
A
391 |
392 \(
393 (?{ ++$c })
394 |
395 \)
396 (?{ --$c })
397 )
0f289c68 398 )+ # This may not match with different subblocks
84281c31
A
399 )
400 (?(?{ $c != 0 })
401 (?!
0f289c68
YO
402 ) # Fail
403 ) # Otherwise the chunk 1 may succeed with $c>0
84281c31
A
404 /xg;
405 }
3568d838 406
84281c31
A
407 my @ans = ();
408 my $res;
409 push @ans, $res while $res = matchit;
4f890a30 410 is("@ans", "1 1 1", $message);
3568d838 411
84281c31 412 @ans = matchit;
4f890a30 413 is("@ans", $expect, $message);
3568d838 414
4f890a30 415 $message = "Recursion with (??{ })";
84281c31
A
416 our $matched;
417 $matched = qr/\((?:(?>[^()]+)|(??{$matched}))*\)/;
3568d838 418
84281c31
A
419 @ans = my @ans1 = ();
420 push (@ans, $res), push (@ans1, $&) while $res = m/$matched/g;
3568d838 421
4f890a30
NC
422 is("@ans", "1 1 1", $message);
423 is("@ans1", $expect, $message);
3568d838 424
84281c31 425 @ans = m/$matched/g;
4f890a30 426 is("@ans", $expect, $message);
3568d838 427
84281c31 428 }
3568d838 429
84281c31
A
430 {
431 ok "abc" =~ /^(??{"a"})b/, '"abc" =~ /^(??{"a"})b/';
432 }
3568d838 433
84281c31 434 {
0f289c68 435 my @ans = ('a/b' =~ m%(.*/)?(.*)%); # Stack may be bad
de26e0cc 436 is("@ans", 'a/ b', "Stack may be bad");
84281c31 437 }
3568d838 438
84281c31 439 {
4f890a30 440 my $message = "Eval-group not allowed at runtime";
84281c31
A
441 my $code = '{$blah = 45}';
442 our $blah = 12;
443 eval { /(?$code)/ };
4f890a30 444 ok($@ && $@ =~ /not allowed at runtime/ && $blah == 12, $message);
84281c31 445
3044771b
NC
446 $blah = 12;
447 my $res = eval { "xx" =~ /(?$code)/o };
448 {
449 no warnings 'uninitialized';
5895685f 450 chomp $@; my $message = "$message '$@', '$res', '$blah'";
4f890a30 451 ok($@ && $@ =~ /not allowed at runtime/ && $blah == 12, $message);
3044771b
NC
452 }
453
454 $code = '=xx';
455 $blah = 12;
456 $res = eval { "xx" =~ /(?$code)/o };
457 {
458 no warnings 'uninitialized';
4f890a30
NC
459 my $message = "$message '$@', '$res', '$blah'";
460 ok(!$@ && $res, $message);
3044771b 461 }
3568d838 462
84281c31
A
463 $code = '{$blah = 45}';
464 $blah = 12;
465 eval "/(?$code)/";
4f890a30 466 is($blah, 45, $message);
3568d838 467
84281c31
A
468 $blah = 12;
469 /(?{$blah = 45})/;
4f890a30 470 is($blah, 45, $message);
84281c31 471 }
3568d838 472
84281c31 473 {
4f890a30 474 my $message = "Pos checks";
84281c31
A
475 my $x = 'banana';
476 $x =~ /.a/g;
4f890a30 477 is(pos $x, 2, $message);
3568d838 478
84281c31 479 $x =~ /.z/gc;
4f890a30 480 is(pos $x, 2, $message);
3568d838 481
84281c31
A
482 sub f {
483 my $p = $_[0];
484 return $p;
485 }
3568d838 486
84281c31 487 $x =~ /.a/g;
4f890a30 488 is(f (pos $x), 4, $message);
84281c31 489 }
3568d838 490
84281c31 491 {
4f890a30 492 my $message = 'Checking $^R';
84281c31
A
493 our $x = $^R = 67;
494 'foot' =~ /foo(?{$x = 12; 75})[t]/;
4f890a30 495 is($^R, 75, $message);
84281c31
A
496
497 $x = $^R = 67;
498 'foot' =~ /foo(?{$x = 12; 75})[xy]/;
4f890a30 499 ok($^R eq '67' && $x eq '12', $message);
84281c31
A
500
501 $x = $^R = 67;
502 'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/;
4f890a30 503 ok($^R eq '79' && $x eq '12', $message);
84281c31 504 }
3568d838 505
84281c31 506 {
de26e0cc
NC
507 is(qr/\b\v$/i, '(?^i:\b\v$)', 'qr/\b\v$/i');
508 is(qr/\b\v$/s, '(?^s:\b\v$)', 'qr/\b\v$/s');
509 is(qr/\b\v$/m, '(?^m:\b\v$)', 'qr/\b\v$/m');
510 is(qr/\b\v$/x, '(?^x:\b\v$)', 'qr/\b\v$/x');
511 is(qr/\b\v$/xism, '(?^msix:\b\v$)', 'qr/\b\v$/xism');
512 is(qr/\b\v$/, '(?^:\b\v$)', 'qr/\b\v$/');
84281c31 513 }
3568d838 514
9de15fec 515 { # Test that charset modifier work, and are interpolated
de26e0cc
NC
516 is(qr/\b\v$/, '(?^:\b\v$)', 'Verify no locale, no unicode_strings gives default modifier');
517 is(qr/(?l:\b\v$)/, '(?^:(?l:\b\v$))', 'Verify infix l modifier compiles');
518 is(qr/(?u:\b\v$)/, '(?^:(?u:\b\v$))', 'Verify infix u modifier compiles');
519 is(qr/(?l)\b\v$/, '(?^:(?l)\b\v$)', 'Verify (?l) compiles');
520 is(qr/(?u)\b\v$/, '(?^:(?u)\b\v$)', 'Verify (?u) compiles');
9de15fec
KW
521
522 my $dual = qr/\b\v$/;
569f7fc5
JR
523 my $locale;
524
525 SKIP: {
ef9d5242 526 skip 'Locales not available', 1 unless locales_enabled('LC_CTYPE');
569f7fc5 527
5f1269ab 528 use locale;
569f7fc5
JR
529 $locale = qr/\b\v$/;
530 is($locale, '(?^l:\b\v$)', 'Verify has l modifier when compiled under use locale');
531 no locale;
532 }
9de15fec
KW
533
534 use feature 'unicode_strings';
535 my $unicode = qr/\b\v$/;
de26e0cc
NC
536 is($unicode, '(?^u:\b\v$)', 'Verify has u modifier when compiled under unicode_strings');
537 is(qr/abc$dual/, '(?^u:abc(?^:\b\v$))', 'Verify retains d meaning when interpolated under locale');
569f7fc5
JR
538
539 SKIP: {
ef9d5242 540 skip 'Locales not available', 1 unless locales_enabled('LC_CTYPE');
569f7fc5
JR
541
542 is(qr/abc$locale/, '(?^u:abc(?^l:\b\v$))', 'Verify retains l when interpolated under unicode_strings');
543 }
9de15fec
KW
544
545 no feature 'unicode_strings';
569f7fc5 546 SKIP: {
ef9d5242 547 skip 'Locales not available', 1 unless locales_enabled('LC_CTYPE');
569f7fc5
JR
548 is(qr/abc$locale/, '(?^:abc(?^l:\b\v$))', 'Verify retains l when interpolated outside locale and unicode strings');
549 }
550
de26e0cc 551 is(qr/def$unicode/, '(?^:def(?^u:\b\v$))', 'Verify retains u when interpolated outside locale and unicode strings');
9de15fec 552
569f7fc5 553 SKIP: {
ef9d5242 554 skip 'Locales not available', 2 unless locales_enabled('LC_CTYPE');
569f7fc5 555
5f1269ab 556 use locale;
569f7fc5
JR
557 is(qr/abc$dual/, '(?^l:abc(?^:\b\v$))', 'Verify retains d meaning when interpolated under locale');
558 is(qr/abc$unicode/, '(?^l:abc(?^u:\b\v$))', 'Verify retains u when interpolated under locale');
559 }
9de15fec
KW
560 }
561
84281c31 562 {
4f890a30 563 my $message = "Look around";
84281c31 564 $_ = 'xabcx';
84281c31 565 foreach my $ans ('', 'c') {
4f890a30
NC
566 ok(/(?<=(?=a)..)((?=c)|.)/g, $message);
567 is($1, $ans, $message);
84281c31
A
568 }
569 }
3568d838 570
84281c31 571 {
4f890a30 572 my $message = "Empty clause";
84281c31
A
573 $_ = 'a';
574 foreach my $ans ('', 'a', '') {
4f890a30
NC
575 ok(/^|a|$/g, $message);
576 is($&, $ans, $message);
84281c31
A
577 }
578 }
3568d838 579
84281c31 580 {
84281c31 581 sub prefixify {
4f890a30
NC
582 my $message = "Prefixify";
583 {
84281c31 584 my ($v, $a, $b, $res) = @_;
4f890a30
NC
585 ok($v =~ s/\Q$a\E/$b/, $message);
586 is($v, $res, $message);
84281c31
A
587 }
588 }
3568d838 589
84281c31
A
590 prefixify ('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch');
591 prefixify ('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch');
592 }
3568d838 593
84281c31
A
594 {
595 $_ = 'var="foo"';
596 /(\")/;
597 ok $1 && /$1/, "Capture a quote";
598 }
3568d838 599
84281c31 600 {
84281c31 601 no warnings 'closure';
4f890a30 602 my $message = '(?{ $var } refers to package vars';
84281c31
A
603 package aa;
604 our $c = 2;
605 $::c = 3;
606 '' =~ /(?{ $c = 4 })/;
4f890a30
NC
607 main::is($c, 4, $message);
608 main::is($::c, 3, $message);
84281c31 609 }
3568d838 610
84281c31 611 {
cb124425
NC
612 is(eval 'q(a:[b]:) =~ /[x[:foo:]]/', undef);
613 like ($@, qr/POSIX class \[:[^:]+:\] unknown in regex/,
614 'POSIX class [: :] must have valid name');
84281c31
A
615
616 for my $d (qw [= .]) {
cb124425
NC
617 is(eval "/[[${d}foo${d}]]/", undef);
618 like ($@, qr/\QPOSIX syntax [$d $d] is reserved for future extensions/,
619 "POSIX syntax [[$d $d]] is an error");
84281c31
A
620 }
621 }
3568d838 622
84281c31
A
623 {
624 # test if failure of patterns returns empty list
4f890a30 625 my $message = "Failed pattern returns empty list";
84281c31
A
626 $_ = 'aaa';
627 @_ = /bbb/;
4f890a30 628 is("@_", "", $message);
3568d838 629
84281c31 630 @_ = /bbb/g;
4f890a30 631 is("@_", "", $message);
a72deede 632
84281c31 633 @_ = /(bbb)/;
4f890a30 634 is("@_", "", $message);
a72deede 635
84281c31 636 @_ = /(bbb)/g;
4f890a30 637 is("@_", "", $message);
84281c31 638 }
a72deede 639
84281c31 640 {
4f890a30 641 my $message = '@- and @+ tests';
84281c31
A
642
643 /a(?=.$)/;
4f890a30
NC
644 is($#+, 0, $message);
645 is($#-, 0, $message);
646 is($+ [0], 2, $message);
647 is($- [0], 1, $message);
648 ok(!defined $+ [1] && !defined $- [1] &&
649 !defined $+ [2] && !defined $- [2], $message);
84281c31
A
650
651 /a(a)(a)/;
4f890a30
NC
652 is($#+, 2, $message);
653 is($#-, 2, $message);
654 is($+ [0], 3, $message);
655 is($- [0], 0, $message);
656 is($+ [1], 2, $message);
657 is($- [1], 1, $message);
658 is($+ [2], 3, $message);
659 is($- [2], 2, $message);
660 ok(!defined $+ [3] && !defined $- [3] &&
661 !defined $+ [4] && !defined $- [4], $message);
84281c31 662
54a4274e 663 # Exists has a special check for @-/@+ - bug 45147
4f890a30
NC
664 ok(exists $-[0], $message);
665 ok(exists $+[0], $message);
666 ok(exists $-[2], $message);
667 ok(exists $+[2], $message);
668 ok(!exists $-[3], $message);
669 ok(!exists $+[3], $message);
670 ok(exists $-[-1], $message);
671 ok(exists $+[-1], $message);
672 ok(exists $-[-3], $message);
673 ok(exists $+[-3], $message);
674 ok(!exists $-[-4], $message);
675 ok(!exists $+[-4], $message);
84281c31
A
676
677 /.(a)(b)?(a)/;
4f890a30
NC
678 is($#+, 3, $message);
679 is($#-, 3, $message);
680 is($+ [1], 2, $message);
681 is($- [1], 1, $message);
682 is($+ [3], 3, $message);
683 is($- [3], 2, $message);
684 ok(!defined $+ [2] && !defined $- [2] &&
685 !defined $+ [4] && !defined $- [4], $message);
84281c31 686
84281c31 687 /.(a)/;
4f890a30
NC
688 is($#+, 1, $message);
689 is($#-, 1, $message);
690 is($+ [0], 2, $message);
691 is($- [0], 0, $message);
692 is($+ [1], 2, $message);
693 is($- [1], 1, $message);
694 ok(!defined $+ [2] && !defined $- [2] &&
695 !defined $+ [3] && !defined $- [3], $message);
84281c31
A
696
697 /.(a)(ba*)?/;
4f890a30
NC
698 is($#+, 2, $message);
699 is($#-, 1, $message);
232af1f8
FC
700
701 # Check that values don’t stick
702 " "=~/()()()(.)(..)/;
703 my($m,$p) = (\$-[5], \$+[5]);
704 () = "$$_" for $m, $p; # FETCH (or eqv.)
705 " " =~ /()/;
706 is $$m, undef, 'values do not stick to @- elements';
707 is $$p, undef, 'values do not stick to @+ elements';
84281c31 708 }
a72deede 709
d9bad346
FC
710 foreach ('$+[0] = 13', '$-[0] = 13', '@+ = (7, 6, 5)',
711 '@- = qw (foo bar)', '$^N = 42') {
cb124425
NC
712 is(eval $_, undef);
713 like($@, qr/^Modification of a read-only value attempted/,
d9bad346 714 '$^N, @- and @+ are read-only');
84281c31 715 }
a72deede 716
84281c31 717 {
4f890a30 718 my $message = '\G testing';
84281c31
A
719 $_ = 'aaa';
720 pos = 1;
721 my @a = /\Ga/g;
4f890a30 722 is("@a", "a a", $message);
84281c31
A
723
724 my $str = 'abcde';
725 pos $str = 2;
4f890a30
NC
726 unlike($str, qr/^\G/, $message);
727 unlike($str, qr/^.\G/, $message);
728 like($str, qr/^..\G/, $message);
729 unlike($str, qr/^...\G/, $message);
730 ok($str =~ /\G../ && $& eq 'cd', $message);
4f890a30 731 ok($str =~ /.\G./ && $& eq 'bc', $message);
cf44e600
DM
732
733 }
734
735 {
736 my $message = '\G and intuit and anchoring';
737 $_ = "abcdef";
738 pos = 0;
739 ok($_ =~ /\Gabc/, $message);
740 ok($_ =~ /^\Gabc/, $message);
741
742 pos = 3;
743 ok($_ =~ /\Gdef/, $message);
744 pos = 3;
745 ok($_ =~ /\Gdef$/, $message);
746 pos = 3;
747 ok($_ =~ /abc\Gdef$/, $message);
748 pos = 3;
749 ok($_ =~ /^abc\Gdef$/, $message);
750 pos = 3;
751 ok($_ =~ /c\Gd/, $message);
e0362b86
DM
752 pos = 3;
753 ok($_ =~ /..\GX?def/, $message);
84281c31 754 }
a72deede 755
84281c31 756 {
fe3974be
DM
757 my $s = '123';
758 pos($s) = 1;
759 my @a = $s =~ /(\d)\G/g; # this infinitely looped up till 5.19.1
760 is("@a", "1", '\G looping');
761 }
762
763
764 {
4f890a30 765 my $message = 'pos inside (?{ })';
84281c31
A
766 my $str = 'abcde';
767 our ($foo, $bar);
4f890a30
NC
768 like($str, qr/b(?{$foo = $_; $bar = pos})c/, $message);
769 is($foo, $str, $message);
770 is($bar, 2, $message);
771 is(pos $str, undef, $message);
84281c31
A
772
773 undef $foo;
774 undef $bar;
775 pos $str = undef;
4f890a30
NC
776 ok($str =~ /b(?{$foo = $_; $bar = pos})c/g, $message);
777 is($foo, $str, $message);
778 is($bar, 2, $message);
779 is(pos $str, 3, $message);
84281c31
A
780
781 $_ = $str;
782 undef $foo;
783 undef $bar;
4f890a30
NC
784 like($_, qr/b(?{$foo = $_; $bar = pos})c/, $message);
785 is($foo, $str, $message);
786 is($bar, 2, $message);
84281c31
A
787
788 undef $foo;
789 undef $bar;
4f890a30
NC
790 ok(/b(?{$foo = $_; $bar = pos})c/g, $message);
791 is($foo, $str, $message);
792 is($bar, 2, $message);
793 is(pos, 3, $message);
84281c31
A
794
795 undef $foo;
796 undef $bar;
797 pos = undef;
798 1 while /b(?{$foo = $_; $bar = pos})c/g;
4f890a30
NC
799 is($foo, $str, $message);
800 is($bar, 2, $message);
801 is(pos, undef, $message);
84281c31
A
802
803 undef $foo;
804 undef $bar;
805 $_ = 'abcde|abcde';
4f890a30
NC
806 ok(s/b(?{$foo = $_; $bar = pos})c/x/g, $message);
807 is($foo, 'abcde|abcde', $message);
808 is($bar, 8, $message);
809 is($_, 'axde|axde', $message);
84281c31
A
810
811 # List context:
812 $_ = 'abcde|abcde';
813 our @res;
814 () = /([ace]).(?{push @res, $1,$2})([ce])(?{push @res, $1,$2})/g;
815 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
4f890a30 816 is("@res", "'a' undef 'a' 'c' 'e' undef 'a' undef 'a' 'c'", $message);
84281c31
A
817
818 @res = ();
819 () = /([ace]).(?{push @res, $`,$&,$'})([ce])(?{push @res, $`,$&,$'})/g;
820 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
4f890a30 821 is("@res", "'' 'ab' 'cde|abcde' " .
84281c31
A
822 "'' 'abc' 'de|abcde' " .
823 "'abcd' 'e|' 'abcde' " .
824 "'abcde|' 'ab' 'cde' " .
4f890a30 825 "'abcde|' 'abc' 'de'", $message);
84281c31 826 }
f33976b4 827
84281c31 828 {
4f890a30 829 my $message = '\G anchor checks';
84281c31
A
830 my $foo = 'aabbccddeeffgg';
831 pos ($foo) = 1;
cce850e4 832
98bee633
DM
833 ok($foo =~ /.\G(..)/g, $message);
834 is($1, 'ab', $message);
cce850e4 835
98bee633
DM
836 pos ($foo) += 1;
837 ok($foo =~ /.\G(..)/g, $message);
838 is($1, 'cc', $message);
cce850e4 839
98bee633
DM
840 pos ($foo) += 1;
841 ok($foo =~ /.\G(..)/g, $message);
842 is($1, 'de', $message);
843
844 ok($foo =~ /\Gef/g, $message);
cce850e4 845
84281c31 846 undef pos $foo;
4f890a30
NC
847 ok($foo =~ /\G(..)/g, $message);
848 is($1, 'aa', $message);
cce850e4 849
4f890a30
NC
850 ok($foo =~ /\G(..)/g, $message);
851 is($1, 'bb', $message);
cce850e4 852
84281c31 853 pos ($foo) = 5;
4f890a30
NC
854 ok($foo =~ /\G(..)/g, $message);
855 is($1, 'cd', $message);
84281c31 856 }
cce850e4 857
84281c31 858 {
9c13586e
DM
859 my $message = 'basic \G floating checks';
860 my $foo = 'aabbccddeeffgg';
861 pos ($foo) = 1;
862
863 ok($foo =~ /a+\G(..)/g, "$message: a+\\G");
864 is($1, 'ab', "$message: ab");
865
866 pos ($foo) += 1;
867 ok($foo =~ /b+\G(..)/g, "$message: b+\\G");
868 is($1, 'cc', "$message: cc");
869
870 pos ($foo) += 1;
871 ok($foo =~ /d+\G(..)/g, "$message: d+\\G");
872 is($1, 'de', "$message: de");
873
874 ok($foo =~ /\Gef/g, "$message: \\Gef");
875
876 pos ($foo) = 1;
877
878 ok($foo =~ /(?=a+\G)(..)/g, "$message: (?a+\\G)");
879 is($1, 'aa', "$message: aa");
880
881 pos ($foo) = 2;
882
883 ok($foo =~ /a(?=a+\G)(..)/g, "$message: a(?=a+\\G)");
884 is($1, 'ab', "$message: ab");
885
886 }
887
888 {
84281c31
A
889 $_ = '123x123';
890 my @res = /(\d*|x)/g;
891 local $" = '|';
de26e0cc 892 is("@res", "123||x|123|", "0 match in alternation");
84281c31 893 }
cce850e4 894
84281c31 895 {
4f890a30 896 my $message = "Match against temporaries (created via pp_helem())" .
84281c31 897 " is safe";
4f890a30
NC
898 ok({foo => "bar\n" . $^X} -> {foo} =~ /^(.*)\n/g, $message);
899 is($1, "bar", $message);
84281c31 900 }
75685a94 901
84281c31 902 {
4f890a30 903 my $message = 'package $i inside (?{ }), ' .
84281c31
A
904 'saved substrings and changing $_';
905 our @a = qw [foo bar];
906 our @b = ();
907 s/(\w)(?{push @b, $1})/,$1,/g for @a;
4f890a30
NC
908 is("@b", "f o o b a r", $message);
909 is("@a", ",f,,o,,o, ,b,,a,,r,", $message);
84281c31 910
4f890a30 911 $message = 'lexical $i inside (?{ }), ' .
84281c31
A
912 'saved substrings and changing $_';
913 no warnings 'closure';
914 my @c = qw [foo bar];
915 my @d = ();
916 s/(\w)(?{push @d, $1})/,$1,/g for @c;
4f890a30
NC
917 is("@d", "f o o b a r", $message);
918 is("@c", ",f,,o,,o, ,b,,a,,r,", $message);
d9f424b2
JH
919 }
920
84281c31 921 {
4f890a30 922 my $message = 'Brackets';
84281c31
A
923 our $brackets;
924 $brackets = qr {
925 { (?> [^{}]+ | (??{ $brackets }) )* }
926 }x;
927
4f890a30
NC
928 ok("{{}" =~ $brackets, $message);
929 is($&, "{}", $message);
930 ok("something { long { and } hairy" =~ $brackets, $message);
931 is($&, "{ and }", $message);
932 ok("something { long { and } hairy" =~ m/((??{ $brackets }))/, $message);
933 is($&, "{ and }", $message);
84281c31 934 }
a4c04bdc 935
84281c31
A
936 {
937 $_ = "a-a\nxbb";
938 pos = 1;
b33825c4 939 ok(!m/^-.*bb/mg, '$_ = "a-a\nxbb"; m/^-.*bb/mg');
84281c31 940 }
a4c04bdc 941
84281c31 942 {
4f890a30 943 my $message = '\G anchor checks';
84281c31
A
944 my $text = "aaXbXcc";
945 pos ($text) = 0;
4f890a30 946 ok($text !~ /\GXb*X/g, $message);
84281c31 947 }
a4c04bdc 948
84281c31
A
949 {
950 $_ = "xA\n" x 500;
b33825c4 951 unlike($_, qr/^\s*A/m, '$_ = "xA\n" x 500; /^\s*A/m"');
a4c04bdc 952
84281c31
A
953 my $text = "abc dbf";
954 my @res = ($text =~ /.*?(b).*?\b/g);
de26e0cc 955 is("@res", "b b", '\b is not special');
987aaf07 956 }
a4c04bdc 957
84281c31 958 {
4f890a30 959 my $message = '\S, [\S], \s, [\s]';
84281c31 960 my @a = map chr, 0 .. 255;
9d45b377
YO
961 my @b = grep m/\S/, @a;
962 my @c = grep m/[^\s]/, @a;
4f890a30 963 is("@b", "@c", $message);
84281c31
A
964
965 @b = grep /\S/, @a;
966 @c = grep /[\S]/, @a;
4f890a30 967 is("@b", "@c", $message);
84281c31
A
968
969 @b = grep /\s/, @a;
970 @c = grep /[^\S]/, @a;
4f890a30 971 is("@b", "@c", $message);
84281c31
A
972
973 @b = grep /\s/, @a;
974 @c = grep /[\s]/, @a;
4f890a30 975 is("@b", "@c", $message);
84281c31
A
976 }
977 {
4f890a30 978 my $message = '\D, [\D], \d, [\d]';
84281c31
A
979 my @a = map chr, 0 .. 255;
980 my @b = grep /\D/, @a;
981 my @c = grep /[^\d]/, @a;
4f890a30 982 is("@b", "@c", $message);
84281c31
A
983
984 @b = grep /\D/, @a;
985 @c = grep /[\D]/, @a;
4f890a30 986 is("@b", "@c", $message);
84281c31
A
987
988 @b = grep /\d/, @a;
989 @c = grep /[^\D]/, @a;
4f890a30 990 is("@b", "@c", $message);
84281c31
A
991
992 @b = grep /\d/, @a;
993 @c = grep /[\d]/, @a;
4f890a30 994 is("@b", "@c", $message);
84281c31
A
995 }
996 {
4f890a30 997 my $message = '\W, [\W], \w, [\w]';
84281c31
A
998 my @a = map chr, 0 .. 255;
999 my @b = grep /\W/, @a;
1000 my @c = grep /[^\w]/, @a;
4f890a30 1001 is("@b", "@c", $message);
84281c31
A
1002
1003 @b = grep /\W/, @a;
1004 @c = grep /[\W]/, @a;
4f890a30 1005 is("@b", "@c", $message);
84281c31
A
1006
1007 @b = grep /\w/, @a;
1008 @c = grep /[^\W]/, @a;
4f890a30 1009 is("@b", "@c", $message);
84281c31
A
1010
1011 @b = grep /\w/, @a;
1012 @c = grep /[\w]/, @a;
4f890a30 1013 is("@b", "@c", $message);
84281c31 1014 }
a4c04bdc 1015
84281c31
A
1016 {
1017 # see if backtracking optimization works correctly
4f890a30
NC
1018 my $message = 'Backtrack optimization';
1019 like("\n\n", qr/\n $ \n/x, $message);
1020 like("\n\n", qr/\n* $ \n/x, $message);
1021 like("\n\n", qr/\n+ $ \n/x, $message);
1022 like("\n\n", qr/\n? $ \n/x, $message);
1023 like("\n\n", qr/\n*? $ \n/x, $message);
1024 like("\n\n", qr/\n+? $ \n/x, $message);
1025 like("\n\n", qr/\n?? $ \n/x, $message);
1026 unlike("\n\n", qr/\n*+ $ \n/x, $message);
1027 unlike("\n\n", qr/\n++ $ \n/x, $message);
1028 like("\n\n", qr/\n?+ $ \n/x, $message);
84281c31 1029 }
a4c04bdc 1030
84281c31
A
1031 {
1032 package S;
1033 use overload '""' => sub {'Object S'};
1034 sub new {bless []}
0f289c68 1035
4f890a30 1036 my $message = "Ref stringification";
5895685f
NC
1037 ::ok(do { \my $v} =~ /^SCALAR/, "Scalar ref stringification") or diag($message);
1038 ::ok(do {\\my $v} =~ /^REF/, "Ref ref stringification") or diag($message);
1039 ::ok([] =~ /^ARRAY/, "Array ref stringification") or diag($message);
1040 ::ok({} =~ /^HASH/, "Hash ref stringification") or diag($message);
1041 ::ok('S' -> new =~ /^Object S/, "Object stringification") or diag($message);
84281c31 1042 }
a4c04bdc 1043
84281c31 1044 {
4f890a30
NC
1045 my $message = "Test result of match used as match";
1046 ok('a1b' =~ ('xyz' =~ /y/), $message);
1047 is($`, 'a', $message);
1048 ok('a1b' =~ ('xyz' =~ /t/), $message);
1049 is($`, 'a', $message);
84281c31 1050 }
a4c04bdc 1051
84281c31 1052 {
d728c370 1053 my $message = '"1" is not \s';
c11a8df3
NC
1054 warning_is(sub {unlike("1\n" x 102, qr/^\s*\n/m, $message)},
1055 undef, "$message (did not warn)");
84281c31 1056 }
a4c04bdc 1057
84281c31 1058 {
4f890a30 1059 my $message = '\s, [[:space:]] and [[:blank:]]';
84281c31
A
1060 my %space = (spc => " ",
1061 tab => "\t",
1062 cr => "\r",
1063 lf => "\n",
1064 ff => "\f",
1065 # There's no \v but the vertical tabulator seems miraculously
1066 # be 11 both in ASCII and EBCDIC.
1067 vt => chr(11),
1068 false => "space");
1069
1070 my @space0 = sort grep {$space {$_} =~ /\s/ } keys %space;
1071 my @space1 = sort grep {$space {$_} =~ /[[:space:]]/} keys %space;
1072 my @space2 = sort grep {$space {$_} =~ /[[:blank:]]/} keys %space;
1073
075b9d7d 1074 is("@space0", "cr ff lf spc tab vt", $message);
4f890a30
NC
1075 is("@space1", "cr ff lf spc tab vt", $message);
1076 is("@space2", "spc tab", $message);
84281c31 1077 }
a4c04bdc 1078
ff3f963a 1079 {
c9415951 1080 my $n= 50;
93f09d7b 1081 # this must be a high number and go from 0 to N, as the bug we are looking for doesn't
c9415951
YO
1082 # seem to be predictable. Slight changes to the test make it fail earlier or later.
1083 foreach my $i (0 .. $n)
1084 {
1085 my $str= "\n" x $i;
93f09d7b 1086 ok $str=~/.*\z/, "implicit MBOL check string disable does not break things length=$i";
c9415951
YO
1087 }
1088 }
92f3d482
YO
1089 {
1090 # we are actually testing that we dont die when executing these patterns
1091 use utf8;
1092 my $e = "Böck";
1093 ok(utf8::is_utf8($e),"got a unicode string - rt75680");
1094
1095 ok($e !~ m/.*?[x]$/, "unicode string against /.*?[x]\$/ - rt75680");
1096 ok($e !~ m/.*?\p{Space}$/i, "unicode string against /.*?\\p{space}\$/i - rt75680");
1097 ok($e !~ m/.*?[xyz]$/, "unicode string against /.*?[xyz]\$/ - rt75680");
1098 ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/, "unicode string against big pattern - rt75680");
1099 }
1100 {
1101 # we are actually testing that we dont die when executing these patterns
6d937e95 1102 my $e = "B" . uni_to_native("\x{f6}") . "ck";
92f3d482
YO
1103 ok(!utf8::is_utf8($e), "got a latin string - rt75680");
1104
1105 ok($e !~ m/.*?[x]$/, "latin string against /.*?[x]\$/ - rt75680");
1106 ok($e !~ m/.*?\p{Space}$/i, "latin string against /.*?\\p{space}\$/i - rt75680");
1107 ok($e !~ m/.*?[xyz]$/,"latin string against /.*?[xyz]\$/ - rt75680");
1108 ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/,"latin string against big pattern - rt75680");
1109 }
c920e018
A
1110
1111 {
1112 #
1113 # Tests for bug 77414.
1114 #
1115
4f890a30 1116 my $message = '\p property after empty * match';
c920e018 1117 {
4f890a30
NC
1118 like("1", qr/\s*\pN/, $message);
1119 like("-", qr/\s*\p{Dash}/, $message);
1120 like(" ", qr/\w*\p{Blank}/, $message);
c920e018
A
1121 }
1122
4f890a30
NC
1123 like("1", qr/\s*\pN+/, $message);
1124 like("-", qr/\s*\p{Dash}{1}/, $message);
1125 like(" ", qr/\w*\p{Blank}{1,4}/, $message);
c920e018
A
1126
1127 }
1128
6d937e95
KW
1129 { # Some constructs with Latin1 characters cause a utf8 string not
1130 # to match itself in non-utf8
1131 my $c = uni_to_native("\xc0");
1132 my $pattern = my $utf8_pattern = qr/(($c)+,?)/;
634c83a2
KW
1133 utf8::upgrade($utf8_pattern);
1134 ok $c =~ $pattern, "\\xc0 =~ $pattern; Neither pattern nor target utf8";
d4e0b827 1135 ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; Neither pattern nor target utf8";
634c83a2 1136 ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; pattern utf8, target not";
d4e0b827 1137 ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; pattern utf8, target not";
634c83a2
KW
1138 utf8::upgrade($c);
1139 ok $c =~ $pattern, "\\xc0 =~ $pattern; target utf8, pattern not";
d4e0b827 1140 ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; target utf8, pattern not";
634c83a2 1141 ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; Both target and pattern utf8";
d4e0b827 1142 ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; Both target and pattern utf8";
634c83a2
KW
1143 }
1144
6d937e95 1145 { # Make sure can override the formatting
8cc86590 1146 use feature 'unicode_strings';
6d937e95
KW
1147 ok uni_to_native("\xc0") =~ /\w/, 'Under unicode_strings: "\xc0" =~ /\w/';
1148 ok uni_to_native("\xc0") !~ /(?d:\w)/, 'Under unicode_strings: "\xc0" !~ /(?d:\w)/';
8cc86590
KW
1149 }
1150
704f71be 1151 {
5b6010b3
YO
1152 my $str= "\x{100}";
1153 chop $str;
1154 my $qr= qr/$str/;
de26e0cc 1155 is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag enabled - Bug #80212");
5b6010b3
YO
1156 $str= "";
1157 $qr= qr/$str/;
de26e0cc 1158 is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag disabled - Bug #80212");
5b6010b3
YO
1159
1160 }
1161
72aa120d 1162 {
04934b6d 1163 local $::TODO = "[perl #38133]";
72aa120d
KW
1164
1165 "A" =~ /(((?:A))?)+/;
1166 my $first = $2;
1167
1168 "A" =~ /(((A))?)+/;
1169 my $second = $2;
1170
de26e0cc 1171 is($first, $second);
72aa120d
KW
1172 }
1173
99ca48e1
DM
1174 {
1175 # RT #3516: \G in a m//g expression causes problems
1176 my $count = 0;
1177 while ("abc" =~ m/(\G[ac])?/g) {
1178 last if $count++ > 10;
1179 }
1180 ok($count < 10, 'RT #3516 A');
1181
1182 $count = 0;
1183 while ("abc" =~ m/(\G|.)[ac]/g) {
1184 last if $count++ > 10;
1185 }
1186 ok($count < 10, 'RT #3516 B');
1187
1188 $count = 0;
1189 while ("abc" =~ m/(\G?[ac])?/g) {
1190 last if $count++ > 10;
1191 }
1192 ok($count < 10, 'RT #3516 C');
1193 }
d774cd11
YO
1194 {
1195 # RT #84294: Is this a bug in the simple Perl regex?
1196 # : Nested buffers and (?{...}) dont play nicely on partial matches
1197 our @got= ();
1198 ok("ab" =~ /((\w+)(?{ push @got, $2 })){2}/,"RT #84294: Pattern should match");
d774cd11
YO
1199 my $want= "'ab', 'a', 'b'";
1200 my $got= join(", ", map { defined($_) ? "'$_'" : "undef" } @got);
1201 is($got,$want,'RT #84294: check that "ab" =~ /((\w+)(?{ push @got, $2 })){2}/ leaves @got in the correct state');
1202 }
1203
0bda3001
KW
1204 {
1205 # Suppress warnings, as the non-unicode one comes out even if turn off
1206 # warnings here (because the execution is done in another scope).
1207 local $SIG{__WARN__} = sub {};
1208 my $str = "\x{110000}";
1209
2d88a86a
KW
1210 unlike($str, qr/\p{ASCII_Hex_Digit=True}/, "Non-Unicode doesn't match \\p{AHEX=True}");
1211 like($str, qr/\p{ASCII_Hex_Digit=False}/, "Non-Unicode matches \\p{AHEX=False}");
1212 like($str, qr/\P{ASCII_Hex_Digit=True}/, "Non-Unicode matches \\P{AHEX=True}");
1213 unlike($str, qr/\P{ASCII_Hex_Digit=False}/, "Non-Unicode matches \\P{AHEX=FALSE}");
0bda3001
KW
1214 }
1215
b289b443 1216 {
c256547e 1217 # Test that IDstart works, but because the author (khw) knows
b289b443
KW
1218 # regexes much better than the rest of the core, it is being done here
1219 # in the context of a regex which relies on buffer names beginng with
1220 # IDStarts.
1221 use utf8;
1222 my $str = "abc";
1223 like($str, qr/(?<a>abc)/, "'a' is legal IDStart");
1224 like($str, qr/(?<_>abc)/, "'_' is legal IDStart");
1225 like($str, qr/(?<ß>abc)/, "U+00DF is legal IDStart");
1226 like($str, qr/(?<ℕ>abc)/, "U+2115' is legal IDStart");
1227
1228 # This test works on Unicode 6.0 in which U+2118 and U+212E are legal
1229 # IDStarts there, but are not Word characters, and therefore Perl
1230 # doesn't allow them to be IDStarts. But there is no guarantee that
1231 # Unicode won't change things around in the future so that at some
1232 # future Unicode revision these tests would need to be revised.
1233 foreach my $char ("%", "×", chr(0x2118), chr(0x212E)) {
1234 my $prog = <<"EOP";
1235use utf8;;
1236"abc" =~ qr/(?<$char>abc)/;
1237EOP
1238 utf8::encode($prog);
20e5bab4 1239 fresh_perl_like($prog, qr!Group name must start with a non-digit word character!, {},
b289b443
KW
1240 sprintf("'U+%04X not legal IDFirst'", ord($char)));
1241 }
1242 }
bbdd8bad
KW
1243
1244 { # [perl #101710]
1245 my $pat = "b";
1246 utf8::upgrade($pat);
1247 like("\xffb", qr/$pat/i, "/i: utf8 pattern, non-utf8 string, latin1-char preceding matching char in string");
1248 }
1249
ba510004
FC
1250 { # Crash with @a =~ // warning
1251 local $SIG{__WARN__} = sub {
1252 pass 'no crash for @a =~ // warning'
1253 };
1254 eval ' sub { my @a =~ // } ';
1255 }
1256
cc88c9aa
FC
1257 { # Concat overloading and qr// thingies
1258 my @refs;
1259 my $qr = qr//;
3bbc3d30
YO
1260 package Cat {
1261 require overload;
1262 overload->import(
cc88c9aa
FC
1263 '""' => sub { ${$_[0]} },
1264 '.' => sub {
1265 push @refs, ref $_[1] if ref $_[1];
1266 bless $_[2] ? \"$_[1]${$_[0]}" : \"${$_[0]}$_[1]"
1267 }
3bbc3d30 1268 );
cc88c9aa
FC
1269 }
1270 my $s = "foo";
1271 my $o = bless \$s, Cat::;
1272 /$o$qr/;
1273 is "@refs", "Regexp", '/$o$qr/ passes qr ref to cat overload meth';
1274 }
1275
21eede78
YO
1276 {
1277 my $count=0;
1278 my $str="\n";
1279 $count++ while $str=~/.*/g;
1280 is $count, 2, 'test that ANCH_MBOL works properly. We should get 2 from $count++ while "\n"=~/.*/g';
1281 my $class_count= 0;
1282 $class_count++ while $str=~/[^\n]*/g;
1283 is $class_count, $count, 'while "\n"=~/.*/g and while "\n"=~/[^\n]*/g should behave the same';
1284 my $anch_count= 0;
1285 $anch_count++ while $str=~/^.*/mg;
1286 is $anch_count, 1, 'while "\n"=~/^.*/mg should match only once';
1287 }
96f54887
KW
1288
1289 { # [perl #111174]
1290 use re '/u';
6d937e95
KW
1291 my $A_grave = uni_to_native("\xc0");
1292 like uni_to_native("\xe0"), qr/(?i:$A_grave)/, "(?i: shouldn't lose the passed in /u";
96f54887
KW
1293 use re '/a';
1294 unlike "\x{100}", qr/(?i:\w)/, "(?i: shouldn't lose the passed in /a";
1295 use re '/aa';
1296 unlike 'k', qr/(?i:\N{KELVIN SIGN})/, "(?i: shouldn't lose the passed in /aa";
1297 }
6ae44cd2
DM
1298
1299 {
1300 # the test for whether the pattern should be re-compiled should
1301 # consider the UTF8ness of the previous and current pattern
1302 # string, as well as the physical bytes of the pattern string
1303
6d937e95 1304 for my $s (byte_utf8a_to_utf8n("\xc4\x80"), "\x{100}") {
6ae44cd2
DM
1305 ok($s =~ /^$s$/, "re-compile check is UTF8-aware");
1306 }
1307 }
1308
e03b874a
DM
1309 # #113682 more overloading and qr//
1310 # when doing /foo$overloaded/, if $overloaded returns
1311 # a qr/(?{})/ via qr or "" overloading, then 'use re 'eval'
1312 # shouldn't be required. Via '.', it still is.
1313 {
1314 package Qr0;
1315 use overload 'qr' => sub { qr/(??{50})/ };
1316
1317 package Qr1;
1318 use overload '""' => sub { qr/(??{51})/ };
1319
1320 package Qr2;
1321 use overload '.' => sub { $_[1] . qr/(??{52})/ };
1322
1323 package Qr3;
1324 use overload '""' => sub { qr/(??{7})/ },
1325 '.' => sub { $_[1] . qr/(??{53})/ };
1326
1327 package Qr_indirect;
1328 use overload '""' => sub { $_[0][0] };
1329
1330 package main;
1331
1332 for my $i (0..3) {
1333 my $o = bless [], "Qr$i";
1334 if ((0,0,1,1)[$i]) {
1335 eval { "A5$i" =~ /^A$o$/ };
1336 like($@, qr/Eval-group not allowed/, "Qr$i");
1337 eval { "5$i" =~ /$o/ };
1338 like($@, ($i == 3 ? qr/^$/ : qr/no method found,/),
1339 "Qr$i bare");
1340 {
1341 use re 'eval';
1342 ok("A5$i" =~ /^A$o$/, "Qr$i - with use re eval");
1343 eval { "5$i" =~ /$o/ };
1344 like($@, ($i == 3 ? qr/^$/ : qr/no method found,/),
1345 "Qr$i bare - with use re eval");
1346 }
1347 }
1348 else {
1349 ok("A5$i" =~ /^A$o$/, "Qr$i");
1350 ok("5$i" =~ /$o/, "Qr$i bare");
1351 }
1352 }
1353
1354 my $o = bless [ bless [], "Qr1" ], 'Qr_indirect';
1355 ok("A51" =~ /^A$o/, "Qr_indirect");
1356 ok("51" =~ /$o/, "Qr_indirect bare");
1357 }
1358
34b39fc9
KW
1359 { # Various flags weren't being set when a [] is optimized into an
1360 # EXACTish node
1361 ;
1362 ;
6d937e95
KW
1363 my $sharp_s = uni_to_native("\xdf");
1364 ok("\x{017F}\x{017F}" =~ qr/^[$sharp_s]?$/i, "[] to EXACTish optimization");
34b39fc9
KW
1365 }
1366
5e4a1da1 1367 {
6d937e95 1368 for my $char (":", uni_to_native("\x{f7}"), "\x{2010}") {
5e4a1da1
KW
1369 my $utf8_char = $char;
1370 utf8::upgrade($utf8_char);
1371 my $display = $char;
1372 $display = display($display);
1373 my $utf8_display = "utf8::upgrade(\"$display\")";
1374
1375 like($char, qr/^$char?$/, "\"$display\" =~ /^$display?\$/");
1376 like($char, qr/^$utf8_char?$/, "my \$p = \"$display\"; utf8::upgrade(\$p); \"$display\" =~ /^\$p?\$/");
1377 like($utf8_char, qr/^$char?$/, "my \$c = \"$display\"; utf8::upgrade(\$c); \"\$c\" =~ /^$display?\$/");
1378 like($utf8_char, qr/^$utf8_char?$/, "my \$c = \"$display\"; utf8::upgrade(\$c); my \$p = \"$display\"; utf8::upgrade(\$p); \"\$c\" =~ /^\$p?\$/");
1379 }
1380 }
1381
4fab19ce
DM
1382 {
1383 # #116148: Pattern utf8ness sticks around globally
1384 # the utf8 in the first match was sticking around for the second
1385 # match
1386
1387 use feature 'unicode_strings';
1388
1389 my $x = "\x{263a}";
1390 $x =~ /$x/;
1391
1392 my $text = "Perl";
1393 ok("Perl" =~ /P.*$/i, '#116148');
1394 }
1395
b8372399
DIM
1396 { # 118297: Mixing up- and down-graded strings in regex
1397 utf8::upgrade(my $u = "\x{e5}");
1398 utf8::downgrade(my $d = "\x{e5}");
1399 my $warned;
1400 local $SIG{__WARN__} = sub { $warned++ if $_[0] =~ /\AMalformed UTF-8/ };
1401 my $re = qr/$u$d/;
1402 ok(!$warned, "no warnings when interpolating mixed up-/downgraded strings in pattern");
1403 my $c = "\x{e5}\x{e5}";
1404 utf8::downgrade($c);
1405 like($c, $re, "mixed up-/downgraded pattern matches downgraded string");
1406 utf8::upgrade($c);
1407 like($c, $re, "mixed up-/downgraded pattern matches upgraded string");
1408 }
4fab19ce 1409
f1e1b256
YO
1410 {
1411 # if we have 87 capture buffers defined then \87 should refer to the 87th.
1412 # test that this is true for 1..100
19438407
S
1413 # Note that this test causes the engine to recurse at runtime, and
1414 # hence use a lot of C stack.
f1e1b256 1415 for my $i (1..100) {
31108f3e
YO
1416 my $capture= "a";
1417 $capture= "($capture)" for 1 .. $i;
1418 for my $mid ("","b") {
1419 my $str= "a${mid}a";
1420 my $backref= "\\$i";
1421 eval {
1422 ok($str=~/$capture$mid$backref/,"\\$i works with $i buffers '$str'=~/...$mid$backref/");
1423 1;
1424 } or do {
1425 is("$@","","\\$i works with $i buffers works with $i buffers '$str'=~/...$mid$backref/");
1426 };
1427 }
f1e1b256
YO
1428 }
1429 }
1430
5411a0e5
DM
1431 # this mixture of readonly (not COWable) and COWable strings
1432 # messed up the capture buffers under COW. The actual test results
1433 # are incidental; the issue is was an AddressSanitizer failure
1434 {
1435 my $c ='AB';
1436 my $res = '';
1437 for ($c, 'C', $c, 'DE') {
1438 ok(/(.)/, "COWable match");
1439 $res .= $1;
1440 }
1441 is($res, "ACAD");
1442 }
1443
1444
61965f1d
FC
1445 {
1446 # RT #45667
1447 # /[#$x]/x didn't interpolate the var $x.
1448 my $b = 'cd';
1449 my $s = 'abcd$%#&';
1450 $s =~ s/[a#$b%]/X/g;
1451 is ($s, 'XbXX$XX&', 'RT #45667 without /x');
1452 $s = 'abcd$%#&';
1453 $s =~ s/[a#$b%]/X/gx;
1454 is ($s, 'XbXX$XX&', 'RT #45667 with /x');
1455 }
1456
0cb43d32
FC
1457 {
1458 no warnings "uninitialized";
1459 my @a;
1460 $a[1]++;
1461 /@a/;
1462 pass('no crash with /@a/ when array has nonexistent elems');
1463 }
1464
475b1e90
FC
1465 {
1466 is runperl(prog => 'delete $::{qq-\cR-}; //; print qq-ok\n-'),
1467 "ok\n",
4a2b275c 1468 'deleting *^R does not result in crashes';
d772ce1c 1469 no warnings 'once';
4a2b275c
FC
1470 *^R = *caretRglobwithnoscalar;
1471 "" =~ /(?{42})/;
1472 is $^R, 42, 'assigning to *^R does not result in a crash';
7e68f152
FC
1473 is runperl(
1474 stderr => 1,
1475 prog => 'eval q|'
1476 .' q-..- =~ /(??{undef *^R;q--})(?{42})/; '
1477 .' print qq-$^R\n-'
1478 .'|'
1479 ),
1480 "42\n",
1481 'undefining *^R within (??{}) does not result in a crash';
475b1e90
FC
1482 }
1483
8498497f
KW
1484 SKIP: { # Test literal range end point special handling
1485 unless ($::IS_EBCDIC) {
423df6e4 1486 skip "Valid only for EBCDIC", 24;
8498497f
KW
1487 }
1488
1489 like("\x89", qr/[i-j]/, '"\x89" should match [i-j]');
1490 unlike("\x8A", qr/[i-j]/, '"\x8A" shouldnt match [i-j]');
1491 unlike("\x90", qr/[i-j]/, '"\x90" shouldnt match [i-j]');
1492 like("\x91", qr/[i-j]/, '"\x91" should match [i-j]');
1493
423df6e4
KW
1494 like("\x89", qr/[i-\N{LATIN SMALL LETTER J}]/, '"\x89" should match [i-\N{LATIN SMALL LETTER J}]');
1495 unlike("\x8A", qr/[i-\N{LATIN SMALL LETTER J}]/, '"\x8A" shouldnt match [i-\N{LATIN SMALL LETTER J}]');
1496 unlike("\x90", qr/[i-\N{LATIN SMALL LETTER J}]/, '"\x90" shouldnt match [i-\N{LATIN SMALL LETTER J}]');
1497 like("\x91", qr/[i-\N{LATIN SMALL LETTER J}]/, '"\x91" should match [i-\N{LATIN SMALL LETTER J}]');
1498
1499 like("\x89", qr/[i-\N{U+6A}]/, '"\x89" should match [i-\N{U+6A}]');
1500 unlike("\x8A", qr/[i-\N{U+6A}]/, '"\x8A" shouldnt match [i-\N{U+6A}]');
1501 unlike("\x90", qr/[i-\N{U+6A}]/, '"\x90" shouldnt match [i-\N{U+6A}]');
1502 like("\x91", qr/[i-\N{U+6A}]/, '"\x91" should match [i-\N{U+6A}]');
1503
1504 like("\x89", qr/[\N{U+69}-\N{U+6A}]/, '"\x89" should match [\N{U+69}-\N{U+6A}]');
1505 unlike("\x8A", qr/[\N{U+69}-\N{U+6A}]/, '"\x8A" shouldnt match [\N{U+69}-\N{U+6A}]');
1506 unlike("\x90", qr/[\N{U+69}-\N{U+6A}]/, '"\x90" shouldnt match [\N{U+69}-\N{U+6A}]');
1507 like("\x91", qr/[\N{U+69}-\N{U+6A}]/, '"\x91" should match [\N{U+69}-\N{U+6A}]');
1508
8498497f
KW
1509 like("\x89", qr/[i-\x{91}]/, '"\x89" should match [i-\x{91}]');
1510 like("\x8A", qr/[i-\x{91}]/, '"\x8A" should match [i-\x{91}]');
1511 like("\x90", qr/[i-\x{91}]/, '"\x90" should match [i-\x{91}]');
1512 like("\x91", qr/[i-\x{91}]/, '"\x91" should match [i-\x{91}]');
1513
1514 # Need to use eval, because tries to compile on ASCII platforms even
1515 # though the tests are skipped, and fails because 0x89-j is an illegal
1516 # range there.
6d937e95
KW
1517 like("\x89", eval 'qr/[\x{89}-j]/', '"\x89" should match [\x{89}-j]');
1518 like("\x8A", eval 'qr/[\x{89}-j]/', '"\x8A" should match [\x{89}-j]');
1519 like("\x90", eval 'qr/[\x{89}-j]/', '"\x90" should match [\x{89}-j]');
1520 like("\x91", eval 'qr/[\x{89}-j]/', '"\x91" should match [\x{89}-j]');
8498497f 1521 }
d6ef1678 1522
749e076f
KW
1523 # These are based on looking at the code in regcomp.c
1524 # We don't look for specific code, just the existence of an SSC
1525 foreach my $re (qw( qr/a?c/
1526 qr/a?c/i
1527 qr/[ab]?c/
1528 qr/\R?c/
1529 qr/\d?c/d
1530 qr/\w?c/l
1531 qr/\s?c/a
b35552de 1532 qr/[[:lower:]]?c/u
749e076f 1533 )) {
d7919c41
FC
1534 SKIP: {
1535 skip "no re-debug under miniperl" if is_miniperl;
749e076f
KW
1536 my $prog = <<"EOP";
1537use re qw(Debug COMPILE);
1538$re;
1539EOP
20e5bab4 1540 fresh_perl_like($prog, qr/synthetic stclass/, { stderr=>1 }, "$re generates a synthetic start class");
d7919c41 1541 }
749e076f
KW
1542 }
1543
86897aa6
KW
1544 {
1545 like "\x{AA}", qr/a?[\W_]/d, "\\W with /d synthetic start class works";
1546 }
1547
6d937e95
KW
1548 SKIP: {
1549 skip("Tests are ASCII-centric, some would fail on EBCDIC", 12) if $::IS_EBCDIC;
1550
e8ea8356
JH
1551 # Verify that the very last Latin-1 U+00FF
1552 # (LATIN SMALL LETTER Y WITH DIAERESIS)
1553 # and its UPPER counterpart (U+0178 which is pure Unicode),
1554 # and likewise for the very first pure Unicode
1555 # (LATIN CAPITAL LETTER A WITH MACRON) fold-match properly,
1556 # and there are no off-by-one logic errors in the transition zone.
1557
1558 ok("\xFF" =~ /\xFF/i, "Y WITH DIAERESIS l =~ l");
1559 ok("\xFF" =~ /\x{178}/i, "Y WITH DIAERESIS l =~ u");
1560 ok("\x{178}" =~ /\xFF/i, "Y WITH DIAERESIS u =~ l");
1561 ok("\x{178}" =~ /\x{178}/i, "Y WITH DIAERESIS u =~ u");
86897aa6 1562
e8ea8356
JH
1563 # U+00FF with U+05D0 (non-casing Hebrew letter).
1564 ok("\xFF\x{5D0}" =~ /\xFF\x{5D0}/i, "Y WITH DIAERESIS l =~ l");
1565 ok("\xFF\x{5D0}" =~ /\x{178}\x{5D0}/i, "Y WITH DIAERESIS l =~ u");
1566 ok("\x{178}\x{5D0}" =~ /\xFF\x{5D0}/i, "Y WITH DIAERESIS u =~ l");
1567 ok("\x{178}\x{5D0}" =~ /\x{178}\x{5D0}/i, "Y WITH DIAERESIS u =~ u");
1568
1569 # U+0100.
1570 ok("\x{100}" =~ /\x{100}/i, "A WITH MACRON u =~ u");
1571 ok("\x{100}" =~ /\x{101}/i, "A WITH MACRON u =~ l");
1572 ok("\x{101}" =~ /\x{100}/i, "A WITH MACRON l =~ u");
1573 ok("\x{101}" =~ /\x{101}/i, "A WITH MACRON l =~ l");
1574 }
0b2c2a84 1575
8373491a
KW
1576 {
1577 use utf8;
1578 ok("abc" =~ /a\85b\85c/x, "NEL is white-space under /x");
1579 }
1580
412f55bb
KW
1581 {
1582 ok('a(b)c' =~ qr(a\(b\)c), "'\\(' is a literal in qr(...)");
1583 ok('a[b]c' =~ qr[a\[b\]c], "'\\[' is a literal in qr[...]");
1584 ok('a{3}c' =~ qr{a\{3\}c}, # Only failed when { could be a meta
1585 "'\\{' is a literal in qr{...}, where it could be a quantifier");
1586
1587 # This one is for completeness
1588 ok('a<b>c' =~ qr<a\<b\>c>, "'\\<' is a literal in qr<...>)");
1589 }
1590
b6e093f2
KW
1591 { # Was getting optimized into EXACT (non-folding node)
1592 my $x = qr/[x]/i;
1593 utf8::upgrade($x);
1594 like("X", qr/$x/, "UTF-8 of /[x]/i matches upper case");
1595 }
1596
fa542f55
FC
1597 { # make sure we get an error when \p{} cannot load Unicode tables
1598 fresh_perl_like(<<' prog that cannot load uni tables',
1599 BEGIN {
1600 @INC = '../lib';
1601 require utf8; require 'utf8_heavy.pl';
1602 @INC = ();
1603 }
1604 $name = 'A B';
1605 if ($name =~ /(\p{IsUpper}) (\p{IsUpper})/){
1606 print "It's good! >$1< >$2<\n";
1607 } else {
1608 print "It's not good...\n";
1609 }
1610 prog that cannot load uni tables
1611 qr/^Can't locate unicore\/Heavy\.pl(?x:
1612 )|^Can't find Unicode property definition/,
1613 undef,
1614 '\p{} should not fail silently when uni tables evanesce');
1615 }
59206727
KW
1616
1617 { # Special handling of literal-ended ranges in [...] was breaking this
1618 use utf8;
1619 like("ÿ", qr/[ÿ-ÿ]/, "\"ÿ\" should match [ÿ-ÿ]");
1620 }
405dffcb
KW
1621
1622 { # [perl #123539]
1623 like("TffffffffffffTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT5TTTTTTTTTTTTTTTTTTTTTTTTT3TTgTTTTTTTTTTTTTTTTTTTTT2TTTTTTTTTTTTTTTTTTTTTTTHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHiHHHHHHHfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff&ffff", qr/TffffffffffffTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT5TTTTTTTTTTTTTTTTTTTTTTTTT3TTgTTTTTTTTTTTTTTTTTTTTT2TTTTTTTTTTTTTTTTTTTTTTTHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHiHHHHHHHfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff&ffff/il, "");
1624 like("TffffffffffffT\x{100}TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT5TTTTTTTTTTTTTTTTTTTTTTTTT3TTgTTTTTTTTTTTTTTTTTTTTT2TTTTTTTTTTTTTTTTTTTTTTTHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHiHHHHHHHfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff&ffff", qr/TffffffffffffT\x{100}TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT5TTTTTTTTTTTTTTTTTTTTTTTTT3TTgTTTTTTTTTTTTTTTTTTTTT2TTTTTTTTTTTTTTTTTTTTTTTHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHiHHHHHHHfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff&ffff/il, "");
1625 }
1626
56f81afc
HS
1627 { # [perl #123604]
1628 my($s, $x, @x) = ('abc', 'a', 'd');
1629 my $long = 'b' x 2000;
1630 my $eval = q{$s =~ m{$x[bbb]c} ? 1 : 0};
1631 $eval =~ s{bbb}{$long};
1632 my $match = eval $eval;
1633 ok(1, "did not crash");
1634 ok($match, "[bbb...] resolved as character class, not subscript");
1635 }
8a6d8ec6
HS
1636
1637 { # [perl #123755]
1638 for my $pat ('(??', '(?P', '(?i-') {
1639 eval qq{ qr/$pat/ };
1640 ok(1, "qr/$pat/ did not crash");
1641 eval qq{ qr/${pat}\x{123}/ };
1642 my $e = $@;
1643 like($e, qr{\x{123}},
1644 "qr/${pat}x/ shows x in error even if it's a wide character");
1645 }
1646 }
b3725d49
HS
1647
1648 {
1649 # Expect one of these sizes to cause overflow and wrap to negative
1650 for my $bits (32, 64) {
1651 my $wrapneg = 2 ** ($bits - 2) * 3;
1652 for my $sign ('', '-') {
1653 my $pat = sprintf "qr/(?%s%u)/", $sign, $wrapneg;
1654 eval $pat;
1655 ok(1, "big backref $pat did not crash");
1656 }
1657 }
1658 }
65651c16 1659 {
65651c16
YO
1660 # Test that we handle qr/\8888888/ and variants without an infinite loop,
1661 # we use a test within a test so we can todo it, and make sure we don't
1662 # infinite loop our tests.
1663 # NOTE - Do not put quotes in the code!
1664 # NOTE - We have to triple escape the backref in the pattern below.
1665 my $code='
1666 BEGIN{require q(test.pl);}
1667 watchdog(3);
1668 for my $len (1 .. 20) {
1669 my $eights= q(8) x $len;
1670 eval qq{ qr/\\\\$eights/ };
1671 }
1672 print q(No infinite loop here!);
1673 ';
1674 fresh_perl_is($code, "No infinite loop here!", {},
1675 "test that we handle things like m/\\888888888/ without infinite loops" );
1676 }
4a0c7558 1677
22b433ef
KW
1678 { # Test that we handle some malformed UTF-8 without looping [perl
1679 # #123562]
1680
1681 my $code='
1682 BEGIN{require q(test.pl);}
1683 use Encode qw(_utf8_on);
1684 my $malformed = "a\x80\n";
1685 _utf8_on($malformed);
1686 watchdog(3);
1687 $malformed =~ /(\n\r|\r)$/;
1688 print q(No infinite loop here!);
1689 ';
1690 fresh_perl_like($code, qr/Malformed UTF-8 character/, {},
1691 "test that we handle some UTF-8 malformations without looping" );
1692 }
1693
4a0c7558
HS
1694 {
1695 # [perl #123843] hits SEGV trying to compile this pattern
1696 my $match;
1697 eval q{ ($match) = ("xxyxxyxy" =~ m{(x+(y(?1))*)}) };
1698 ok(1, "compiled GOSUB in CURLYM ok");
1699 is($match, 'xxyxxyx', "matched GOSUB in CURLYM");
1700 }
e92b41c6
HS
1701
1702 {
1703 # [perl #123852] doesn't avoid all the capture-related work with
1704 # //n, leading to possible memory corruption
1705 eval q{ qr{()(?1)}n };
1706 my $error = $@;
1707 ok(1, "qr{()(?1)}n didn't crash");
1708 like($error, qr{Reference to nonexistent group},
1709 'gave appropriate error for qr{()(?1)}n');
1710 }
84281c31
A
1711} # End of sub run_tests
1712
17131;