This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
gh16947: avoid mutating regexp program only within GOSUB
[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;
2abbd513 9no warnings 'experimental::vlb';
84281c31
A
10use 5.010;
11
84281c31
A
12sub run_tests;
13
9133bbab 14$| = 1;
3568d838 15
8d37f932 16
e4d48cc9
GS
17BEGIN {
18 chdir 't' if -d 't';
569f7fc5 19 require Config; import Config;
6d937e95 20 require './test.pl'; require './charset_tools.pl';
ef9d5242 21 require './loc_tools.pl';
624c42e2
N
22 set_up_inc('../lib', '.', '../ext/re');
23}
99956112 24
99956112 25skip_all_without_unicode_tables();
84281c31 26
089ad25d 27plan tests => 1019; # Update this when adding/deleting tests.
b7a35066 28
9d45b377 29run_tests() unless caller;
b7a35066 30
84281c31
A
31#
32# Tests start here.
33#
34sub run_tests {
f8fb8615
KW
35 my $sharp_s = uni_to_native("\xdf");
36
84281c31 37 {
84281c31 38 my $x = "abc\ndef\n";
5895685f 39 (my $x_pretty = $x) =~ s/\n/\\n/g;
fd291da9 40
5895685f
NC
41 ok $x =~ /^abc/, qq ["$x_pretty" =~ /^abc/];
42 ok $x !~ /^def/, qq ["$x_pretty" !~ /^def/];
fd291da9 43
84281c31 44 # used to be a test for $*
5895685f 45 ok $x =~ /^def/m, qq ["$x_pretty" =~ /^def/m];
fd291da9 46
b33825c4
NC
47 ok(!($x =~ /^xxx/), qq ["$x_pretty" =~ /^xxx/]);
48 ok(!($x !~ /^abc/), qq ["$x_pretty" !~ /^abc/]);
fd291da9 49
5895685f 50 ok $x =~ /def/, qq ["$x_pretty" =~ /def/];
b33825c4 51 ok(!($x !~ /def/), qq ["$x_pretty" !~ /def/]);
4765795a 52
5895685f 53 ok $x !~ /.def/, qq ["$x_pretty" !~ /.def/];
b33825c4 54 ok(!($x =~ /.def/), qq ["$x_pretty" =~ /.def/]);
4765795a 55
5895685f 56 ok $x =~ /\ndef/, qq ["$x_pretty" =~ /\\ndef/];
b33825c4 57 ok(!($x !~ /\ndef/), qq ["$x_pretty" !~ /\\ndef/]);
84281c31 58 }
4765795a 59
84281c31
A
60 {
61 $_ = '123';
62 ok /^([0-9][0-9]*)/, qq [\$_ = '$_'; /^([0-9][0-9]*)/];
63 }
f9969324 64
84281c31
A
65 {
66 $_ = 'aaabbbccc';
67 ok /(a*b*)(c*)/ && $1 eq 'aaabbb' && $2 eq 'ccc',
68 qq [\$_ = '$_'; /(a*b*)(c*)/];
69 ok /(a+b+c+)/ && $1 eq 'aaabbbccc', qq [\$_ = '$_'; /(a+b+c+)/];
b33825c4 70 unlike($_, qr/a+b?c+/, qq [\$_ = '$_'; /a+b?c+/]);
84281c31
A
71
72 $_ = 'aaabccc';
73 ok /a+b?c+/, qq [\$_ = '$_'; /a+b?c+/];
74 ok /a*b?c*/, qq [\$_ = '$_'; /a*b?c*/];
75
76 $_ = 'aaaccc';
77 ok /a*b?c*/, qq [\$_ = '$_'; /a*b?c*/];
b33825c4 78 unlike($_, qr/a*b+c*/, qq [\$_ = '$_'; /a*b+c*/]);
84281c31
A
79
80 $_ = 'abcdef';
81 ok /bcd|xyz/, qq [\$_ = '$_'; /bcd|xyz/];
82 ok /xyz|bcd/, qq [\$_ = '$_'; /xyz|bcd/];
83 ok m|bc/*d|, qq [\$_ = '$_'; m|bc/*d|];
84 ok /^$_$/, qq [\$_ = '$_'; /^\$_\$/];
4765795a 85 }
4765795a 86
84281c31
A
87 {
88 # used to be a test for $*
5895685f 89 ok "ab\ncd\n" =~ /^cd/m, q ["ab\ncd\n" =~ /^cd/m];
84281c31 90 }
4765795a 91
84281c31
A
92 {
93 our %XXX = map {($_ => $_)} 123, 234, 345;
94
95 our @XXX = ('ok 1','not ok 1', 'ok 2','not ok 2','not ok 3');
96 while ($_ = shift(@XXX)) {
b33825c4 97 my $e = index ($_, 'not') >= 0 ? '' : 1;
725a61d7 98 my $r = m?(.*)?;
b33825c4 99 is($r, $e, "?(.*)?");
84281c31
A
100 /not/ && reset;
101 if (/not ok 2/) {
102 if ($^O eq 'VMS') {
103 $_ = shift(@XXX);
104 }
105 else {
106 reset 'X';
107 }
108 }
109 }
4765795a 110
84281c31
A
111 SKIP: {
112 if ($^O eq 'VMS') {
113 skip "Reset 'X'", 1;
114 }
115 ok !keys %XXX, "%XXX is empty";
116 }
4765795a 117
84281c31 118 }
4765795a 119
84281c31 120 {
4f890a30 121 my $message = "Test empty pattern";
84281c31
A
122 my $xyz = 'xyz';
123 my $cde = 'cde';
124
125 $cde =~ /[^ab]*/;
126 $xyz =~ //;
4f890a30 127 is($&, $xyz, $message);
84281c31
A
128
129 my $foo = '[^ab]*';
130 $cde =~ /$foo/;
131 $xyz =~ //;
4f890a30 132 is($&, $xyz, $message);
84281c31
A
133
134 $cde =~ /$foo/;
135 my $null;
136 no warnings 'uninitialized';
137 $xyz =~ /$null/;
4f890a30 138 is($&, $xyz, $message);
84281c31
A
139
140 $null = "";
141 $xyz =~ /$null/;
4f890a30 142 is($&, $xyz, $message);
3cb4cde3
TC
143
144 # each entry: regexp, match string, $&, //o match success
145 my @tests =
146 (
147 [ "", "xy", "x", 1 ],
148 [ "y", "yz", "y", !1 ],
149 );
150 for my $test (@tests) {
151 my ($re, $str, $matched, $omatch) = @$test;
152 $xyz =~ /x/o;
153 ok($str =~ /$re/, "$str matches /$re/");
154 is($&, $matched, "on $matched");
155 $xyz =~ /x/o;
156 is($str =~ /$re/o, $omatch, "$str matches /$re/o (or not)");
157 }
84281c31 158 }
4765795a 159
84281c31 160 {
4f890a30 161 my $message = q !Check $`, $&, $'!;
84281c31 162 $_ = 'abcdefghi';
0f289c68 163 /def/; # optimized up to cmd
4f890a30 164 is("$`:$&:$'", 'abc:def:ghi', $message);
4765795a 165
84281c31 166 no warnings 'void';
0f289c68 167 /cde/ + 0; # optimized only to spat
4f890a30 168 is("$`:$&:$'", 'ab:cde:fghi', $message);
4765795a 169
0f289c68 170 /[d][e][f]/; # not optimized
4f890a30 171 is("$`:$&:$'", 'abc:def:ghi', $message);
84281c31 172 }
4765795a 173
84281c31
A
174 {
175 $_ = 'now is the {time for all} good men to come to.';
2a53d331 176 / \{([^}]*)}/;
de26e0cc 177 is($1, 'time for all', "Match braces");
84281c31 178 }
4765795a 179
84281c31 180 {
4f890a30 181 my $message = "{N,M} quantifier";
84281c31 182 $_ = 'xxx {3,4} yyy zzz';
4f890a30
NC
183 ok(/( {3,4})/, $message);
184 is($1, ' ', $message);
185 unlike($_, qr/( {4,})/, $message);
186 ok(/( {2,3}.)/, $message);
187 is($1, ' y', $message);
188 ok(/(y{2,3}.)/, $message);
189 is($1, 'yyy ', $message);
190 unlike($_, qr/x {3,4}/, $message);
191 unlike($_, qr/^xxx {3,4}/, $message);
84281c31 192 }
4765795a 193
84281c31 194 {
4f890a30 195 my $message = "Test /g";
84281c31
A
196 local $" = ":";
197 $_ = "now is the time for all good men to come to.";
198 my @words = /(\w+)/g;
199 my $exp = "now:is:the:time:for:all:good:men:to:come:to";
4765795a 200
4f890a30 201 is("@words", $exp, $message);
4765795a 202
84281c31
A
203 @words = ();
204 while (/\w+/g) {
205 push (@words, $&);
206 }
4f890a30 207 is("@words", $exp, $message);
4765795a 208
84281c31
A
209 @words = ();
210 pos = 0;
211 while (/to/g) {
212 push(@words, $&);
213 }
4f890a30 214 is("@words", "to:to", $message);
4765795a 215
84281c31
A
216 pos $_ = 0;
217 @words = /to/g;
4f890a30 218 is("@words", "to:to", $message);
84281c31 219 }
4765795a 220
84281c31
A
221 {
222 $_ = "abcdefghi";
223
224 my $pat1 = 'def';
225 my $pat2 = '^def';
226 my $pat3 = '.def.';
227 my $pat4 = 'abc';
228 my $pat5 = '^abc';
229 my $pat6 = 'abc$';
230 my $pat7 = 'ghi';
231 my $pat8 = '\w*ghi';
232 my $pat9 = 'ghi$';
233
234 my $t1 = my $t2 = my $t3 = my $t4 = my $t5 =
235 my $t6 = my $t7 = my $t8 = my $t9 = 0;
236
237 for my $iter (1 .. 5) {
238 $t1++ if /$pat1/o;
239 $t2++ if /$pat2/o;
240 $t3++ if /$pat3/o;
241 $t4++ if /$pat4/o;
242 $t5++ if /$pat5/o;
243 $t6++ if /$pat6/o;
244 $t7++ if /$pat7/o;
245 $t8++ if /$pat8/o;
246 $t9++ if /$pat9/o;
247 }
248 my $x = "$t1$t2$t3$t4$t5$t6$t7$t8$t9";
de26e0cc 249 is($x, '505550555', "Test /o");
84281c31 250 }
4765795a 251
4f890a30 252 {
84281c31
A
253 my $xyz = 'xyz';
254 ok "abc" =~ /^abc$|$xyz/, "| after \$";
4765795a 255
84281c31 256 # perl 4.009 says "unmatched ()"
4f890a30 257 my $message = '$ inside ()';
4765795a 258
84281c31
A
259 my $result;
260 eval '"abc" =~ /a(bc$)|$xyz/; $result = "$&:$1"';
4f890a30
NC
261 is($@, "", $message);
262 is($result, "abc:bc", $message);
84281c31 263 }
4765795a 264
84281c31 265 {
4f890a30 266 my $message = "Scalar /g";
84281c31
A
267 $_ = "abcfooabcbar";
268
4f890a30
NC
269 ok( /abc/g && $` eq "", $message);
270 ok( /abc/g && $` eq "abcfoo", $message);
271 ok(!/abc/g, $message);
84281c31 272
4f890a30 273 $message = "Scalar /gi";
84281c31 274 pos = 0;
4f890a30
NC
275 ok( /ABC/gi && $` eq "", $message);
276 ok( /ABC/gi && $` eq "abcfoo", $message);
277 ok(!/ABC/gi, $message);
84281c31 278
4f890a30 279 $message = "Scalar /g";
84281c31 280 pos = 0;
4f890a30
NC
281 ok( /abc/g && $' eq "fooabcbar", $message);
282 ok( /abc/g && $' eq "bar", $message);
84281c31
A
283
284 $_ .= '';
285 my @x = /abc/g;
de26e0cc 286 is(@x, 2, "/g reset after assignment");
4765795a 287 }
4765795a 288
84281c31 289 {
4f890a30 290 my $message = '/g, \G and pos';
84281c31
A
291 $_ = "abdc";
292 pos $_ = 2;
293 /\Gc/gc;
4f890a30 294 is(pos $_, 2, $message);
84281c31 295 /\Gc/g;
4f890a30 296 is(pos $_, undef, $message);
84281c31 297 }
4765795a 298
84281c31 299 {
4f890a30 300 my $message = '(?{ })';
84281c31
A
301 our $out = 1;
302 'abc' =~ m'a(?{ $out = 2 })b';
4f890a30 303 is($out, 2, $message);
84281c31
A
304
305 $out = 1;
306 'abc' =~ m'a(?{ $out = 3 })c';
4f890a30 307 is($out, 1, $message);
84281c31 308 }
4765795a 309
84281c31
A
310 {
311 $_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6';
312 my @out = /(?<!foo)bar./g;
de26e0cc 313 is("@out", 'bar2 barf', "Negative lookbehind");
84281c31 314 }
4765795a 315
84281c31 316 {
4f890a30 317 my $message = "REG_INFTY tests";
84281c31 318 # Tests which depend on REG_INFTY
19d6612d
NC
319
320 # Defaults assumed if this fails
321 eval { require Config; };
4288c5b9 322 $::reg_infty = $Config::Config{reg_infty} // 65535;
84281c31
A
323 $::reg_infty_m = $::reg_infty - 1;
324 $::reg_infty_p = $::reg_infty + 1;
93f09d7b 325 $::reg_infty_m = $::reg_infty_m; # Suppress warning.
84281c31
A
326
327 # As well as failing if the pattern matches do unexpected things, the
328 # next three tests will fail if you should have picked up a lower-than-
329 # default value for $reg_infty from Config.pm, but have not.
330
14358a41
NC
331 is(eval q{('aaa' =~ /(a{1,$::reg_infty_m})/)[0]}, 'aaa', $message);
332 is($@, '', $message);
333 is(eval q{('a' x $::reg_infty_m) =~ /a{$::reg_infty_m}/}, 1, $message);
334 is($@, '', $message);
335 isnt(q{('a' x ($::reg_infty_m - 1)) !~ /a{$::reg_infty_m}/}, 1, $message);
336 is($@, '', $message);
337
84281c31 338 eval "'aaa' =~ /a{1,$::reg_infty}/";
224b2e7e 339 like($@, qr/^\QQuantifier in {,} bigger than/, $message);
84281c31 340 eval "'aaa' =~ /a{1,$::reg_infty_p}/";
4f890a30 341 like($@, qr/^\QQuantifier in {,} bigger than/, $message);
cc27c3b3
KW
342
343 # It should be 'a' x 2147483647, but that exhausts memory on
344 # reasonably sized modern machines
345 like('a' x $::reg_infty_p, qr/a{1,}/,
346 "{1,} matches more times than REG_INFTY");
4765795a 347 }
8269fa76 348
84281c31
A
349 {
350 # Poke a couple more parse failures
351 my $context = 'x' x 256;
352 eval qq("${context}y" =~ /(?<=$context)y/);
353 ok $@ =~ /^\QLookbehind longer than 255 not/, "Lookbehind limit";
354 }
8269fa76 355
bca55e15
KW
356 SKIP:
357 { # Long Monsters
358
17266e72 359 my @trials = (125, 140, 250, 270, 300000, 30);
bca55e15 360
17266e72
KW
361 skip('limited memory', @trials * 4) if $ENV{'PERL_SKIP_BIG_MEM_TESTS'};
362
363 for my $l (@trials) { # Ordered to free memory
84281c31 364 my $a = 'a' x $l;
4f890a30
NC
365 my $message = "Long monster, length = $l";
366 like("ba$a=", qr/a$a=/, $message);
367 unlike("b$a=", qr/a$a=/, $message);
368 like("b$a=", qr/ba+=/, $message);
84281c31 369
224b2e7e 370 like("ba$a=", qr/b(?:a|b)+=/, $message);
84281c31
A
371 }
372 }
8269fa76 373
bca55e15
KW
374 SKIP:
375 { # 20000 nodes, each taking 3 words per string, and 1 per branch
376
84281c31
A
377 my %ans = ( 'ax13876y25677lbc' => 1,
378 'ax13876y25677mcb' => 0, # not b.
379 'ax13876y35677nbc' => 0, # Num too big
380 'ax13876y25677y21378obc' => 1,
0f289c68 381 'ax13876y25677y21378zbc' => 0, # Not followed by [k-o]
84281c31
A
382 'ax13876y25677y21378y21378kbc' => 1,
383 'ax13876y25677y21378y21378kcb' => 0, # Not b.
384 'ax13876y25677y21378y21378y21378kbc' => 0, # 5 runs
385 );
386
17266e72
KW
387 skip('limited memory', 2 * scalar keys %ans) if $ENV{'PERL_SKIP_BIG_MEM_TESTS'};
388
389 my $long_constant_len = join '|', 12120 .. 32645;
390 my $long_var_len = join '|', 8120 .. 28645;
391
84281c31 392 for (keys %ans) {
4f890a30
NC
393 my $message = "20000 nodes, const-len '$_'";
394 ok !($ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o), $message;
84281c31 395
4f890a30
NC
396 $message = "20000 nodes, var-len '$_'";
397 ok !($ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o,), $message;
84281c31 398 }
b8ef571c 399 }
209a9bc1 400
84281c31 401 {
4f890a30 402 my $message = "Complicated backtracking";
84281c31
A
403 $_ = " a (bla()) and x(y b((l)u((e))) and b(l(e)e)e";
404 my $expect = "(bla()) ((l)u((e))) (l(e)e)";
405
83461ff8 406 our $c;
84281c31
A
407 sub matchit {
408 m/
409 (
410 \(
0f289c68 411 (?{ $c = 1 }) # Initialize
84281c31
A
412 (?:
413 (?(?{ $c == 0 }) # PREVIOUS iteration was OK, stop the loop
414 (?!
0f289c68
YO
415 ) # Fail: will unwind one iteration back
416 )
84281c31 417 (?:
0f289c68 418 [^()]+ # Match a big chunk
84281c31
A
419 (?=
420 [()]
0f289c68 421 ) # Do not try to match subchunks
84281c31
A
422 |
423 \(
424 (?{ ++$c })
425 |
426 \)
427 (?{ --$c })
428 )
0f289c68 429 )+ # This may not match with different subblocks
84281c31
A
430 )
431 (?(?{ $c != 0 })
432 (?!
0f289c68
YO
433 ) # Fail
434 ) # Otherwise the chunk 1 may succeed with $c>0
84281c31
A
435 /xg;
436 }
3568d838 437
84281c31
A
438 my @ans = ();
439 my $res;
440 push @ans, $res while $res = matchit;
4f890a30 441 is("@ans", "1 1 1", $message);
3568d838 442
84281c31 443 @ans = matchit;
4f890a30 444 is("@ans", $expect, $message);
3568d838 445
4f890a30 446 $message = "Recursion with (??{ })";
84281c31
A
447 our $matched;
448 $matched = qr/\((?:(?>[^()]+)|(??{$matched}))*\)/;
3568d838 449
84281c31
A
450 @ans = my @ans1 = ();
451 push (@ans, $res), push (@ans1, $&) while $res = m/$matched/g;
3568d838 452
4f890a30
NC
453 is("@ans", "1 1 1", $message);
454 is("@ans1", $expect, $message);
3568d838 455
84281c31 456 @ans = m/$matched/g;
4f890a30 457 is("@ans", $expect, $message);
3568d838 458
84281c31 459 }
3568d838 460
84281c31
A
461 {
462 ok "abc" =~ /^(??{"a"})b/, '"abc" =~ /^(??{"a"})b/';
463 }
3568d838 464
84281c31 465 {
0f289c68 466 my @ans = ('a/b' =~ m%(.*/)?(.*)%); # Stack may be bad
de26e0cc 467 is("@ans", 'a/ b', "Stack may be bad");
84281c31 468 }
3568d838 469
84281c31 470 {
4f890a30 471 my $message = "Eval-group not allowed at runtime";
84281c31
A
472 my $code = '{$blah = 45}';
473 our $blah = 12;
474 eval { /(?$code)/ };
4f890a30 475 ok($@ && $@ =~ /not allowed at runtime/ && $blah == 12, $message);
84281c31 476
3044771b
NC
477 $blah = 12;
478 my $res = eval { "xx" =~ /(?$code)/o };
479 {
480 no warnings 'uninitialized';
5895685f 481 chomp $@; my $message = "$message '$@', '$res', '$blah'";
4f890a30 482 ok($@ && $@ =~ /not allowed at runtime/ && $blah == 12, $message);
3044771b
NC
483 }
484
485 $code = '=xx';
486 $blah = 12;
487 $res = eval { "xx" =~ /(?$code)/o };
488 {
489 no warnings 'uninitialized';
4f890a30
NC
490 my $message = "$message '$@', '$res', '$blah'";
491 ok(!$@ && $res, $message);
3044771b 492 }
3568d838 493
84281c31
A
494 $code = '{$blah = 45}';
495 $blah = 12;
496 eval "/(?$code)/";
4f890a30 497 is($blah, 45, $message);
3568d838 498
84281c31
A
499 $blah = 12;
500 /(?{$blah = 45})/;
4f890a30 501 is($blah, 45, $message);
84281c31 502 }
3568d838 503
84281c31 504 {
4f890a30 505 my $message = "Pos checks";
84281c31
A
506 my $x = 'banana';
507 $x =~ /.a/g;
4f890a30 508 is(pos $x, 2, $message);
3568d838 509
84281c31 510 $x =~ /.z/gc;
4f890a30 511 is(pos $x, 2, $message);
3568d838 512
84281c31
A
513 sub f {
514 my $p = $_[0];
515 return $p;
516 }
3568d838 517
84281c31 518 $x =~ /.a/g;
4f890a30 519 is(f (pos $x), 4, $message);
84281c31 520 }
3568d838 521
84281c31 522 {
4f890a30 523 my $message = 'Checking $^R';
84281c31
A
524 our $x = $^R = 67;
525 'foot' =~ /foo(?{$x = 12; 75})[t]/;
4f890a30 526 is($^R, 75, $message);
84281c31
A
527
528 $x = $^R = 67;
529 'foot' =~ /foo(?{$x = 12; 75})[xy]/;
4f890a30 530 ok($^R eq '67' && $x eq '12', $message);
84281c31
A
531
532 $x = $^R = 67;
533 'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/;
4f890a30 534 ok($^R eq '79' && $x eq '12', $message);
84281c31 535 }
3568d838 536
84281c31 537 {
de26e0cc
NC
538 is(qr/\b\v$/i, '(?^i:\b\v$)', 'qr/\b\v$/i');
539 is(qr/\b\v$/s, '(?^s:\b\v$)', 'qr/\b\v$/s');
540 is(qr/\b\v$/m, '(?^m:\b\v$)', 'qr/\b\v$/m');
541 is(qr/\b\v$/x, '(?^x:\b\v$)', 'qr/\b\v$/x');
542 is(qr/\b\v$/xism, '(?^msix:\b\v$)', 'qr/\b\v$/xism');
543 is(qr/\b\v$/, '(?^:\b\v$)', 'qr/\b\v$/');
84281c31 544 }
3568d838 545
9de15fec 546 { # Test that charset modifier work, and are interpolated
de26e0cc
NC
547 is(qr/\b\v$/, '(?^:\b\v$)', 'Verify no locale, no unicode_strings gives default modifier');
548 is(qr/(?l:\b\v$)/, '(?^:(?l:\b\v$))', 'Verify infix l modifier compiles');
549 is(qr/(?u:\b\v$)/, '(?^:(?u:\b\v$))', 'Verify infix u modifier compiles');
550 is(qr/(?l)\b\v$/, '(?^:(?l)\b\v$)', 'Verify (?l) compiles');
551 is(qr/(?u)\b\v$/, '(?^:(?u)\b\v$)', 'Verify (?u) compiles');
9de15fec
KW
552
553 my $dual = qr/\b\v$/;
569f7fc5
JR
554 my $locale;
555
556 SKIP: {
ef9d5242 557 skip 'Locales not available', 1 unless locales_enabled('LC_CTYPE');
569f7fc5 558
5f1269ab 559 use locale;
569f7fc5
JR
560 $locale = qr/\b\v$/;
561 is($locale, '(?^l:\b\v$)', 'Verify has l modifier when compiled under use locale');
562 no locale;
563 }
9de15fec
KW
564
565 use feature 'unicode_strings';
566 my $unicode = qr/\b\v$/;
de26e0cc
NC
567 is($unicode, '(?^u:\b\v$)', 'Verify has u modifier when compiled under unicode_strings');
568 is(qr/abc$dual/, '(?^u:abc(?^:\b\v$))', 'Verify retains d meaning when interpolated under locale');
569f7fc5
JR
569
570 SKIP: {
ef9d5242 571 skip 'Locales not available', 1 unless locales_enabled('LC_CTYPE');
569f7fc5
JR
572
573 is(qr/abc$locale/, '(?^u:abc(?^l:\b\v$))', 'Verify retains l when interpolated under unicode_strings');
574 }
9de15fec
KW
575
576 no feature 'unicode_strings';
569f7fc5 577 SKIP: {
ef9d5242 578 skip 'Locales not available', 1 unless locales_enabled('LC_CTYPE');
569f7fc5
JR
579 is(qr/abc$locale/, '(?^:abc(?^l:\b\v$))', 'Verify retains l when interpolated outside locale and unicode strings');
580 }
581
de26e0cc 582 is(qr/def$unicode/, '(?^:def(?^u:\b\v$))', 'Verify retains u when interpolated outside locale and unicode strings');
9de15fec 583
569f7fc5 584 SKIP: {
ef9d5242 585 skip 'Locales not available', 2 unless locales_enabled('LC_CTYPE');
569f7fc5 586
5f1269ab 587 use locale;
569f7fc5
JR
588 is(qr/abc$dual/, '(?^l:abc(?^:\b\v$))', 'Verify retains d meaning when interpolated under locale');
589 is(qr/abc$unicode/, '(?^l:abc(?^u:\b\v$))', 'Verify retains u when interpolated under locale');
590 }
9de15fec
KW
591 }
592
84281c31 593 {
4f890a30 594 my $message = "Look around";
84281c31 595 $_ = 'xabcx';
84281c31 596 foreach my $ans ('', 'c') {
4f890a30
NC
597 ok(/(?<=(?=a)..)((?=c)|.)/g, $message);
598 is($1, $ans, $message);
84281c31
A
599 }
600 }
3568d838 601
84281c31 602 {
4f890a30 603 my $message = "Empty clause";
84281c31
A
604 $_ = 'a';
605 foreach my $ans ('', 'a', '') {
4f890a30
NC
606 ok(/^|a|$/g, $message);
607 is($&, $ans, $message);
84281c31
A
608 }
609 }
3568d838 610
84281c31 611 {
84281c31 612 sub prefixify {
4f890a30
NC
613 my $message = "Prefixify";
614 {
84281c31 615 my ($v, $a, $b, $res) = @_;
4f890a30
NC
616 ok($v =~ s/\Q$a\E/$b/, $message);
617 is($v, $res, $message);
84281c31
A
618 }
619 }
3568d838 620
84281c31
A
621 prefixify ('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch');
622 prefixify ('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch');
623 }
3568d838 624
84281c31
A
625 {
626 $_ = 'var="foo"';
627 /(\")/;
628 ok $1 && /$1/, "Capture a quote";
629 }
3568d838 630
84281c31 631 {
84281c31 632 no warnings 'closure';
4f890a30 633 my $message = '(?{ $var } refers to package vars';
84281c31
A
634 package aa;
635 our $c = 2;
636 $::c = 3;
637 '' =~ /(?{ $c = 4 })/;
4f890a30
NC
638 main::is($c, 4, $message);
639 main::is($::c, 3, $message);
84281c31 640 }
3568d838 641
84281c31 642 {
cb124425
NC
643 is(eval 'q(a:[b]:) =~ /[x[:foo:]]/', undef);
644 like ($@, qr/POSIX class \[:[^:]+:\] unknown in regex/,
645 'POSIX class [: :] must have valid name');
84281c31
A
646
647 for my $d (qw [= .]) {
cb124425
NC
648 is(eval "/[[${d}foo${d}]]/", undef);
649 like ($@, qr/\QPOSIX syntax [$d $d] is reserved for future extensions/,
650 "POSIX syntax [[$d $d]] is an error");
84281c31
A
651 }
652 }
3568d838 653
84281c31
A
654 {
655 # test if failure of patterns returns empty list
4f890a30 656 my $message = "Failed pattern returns empty list";
84281c31
A
657 $_ = 'aaa';
658 @_ = /bbb/;
4f890a30 659 is("@_", "", $message);
3568d838 660
84281c31 661 @_ = /bbb/g;
4f890a30 662 is("@_", "", $message);
a72deede 663
84281c31 664 @_ = /(bbb)/;
4f890a30 665 is("@_", "", $message);
a72deede 666
84281c31 667 @_ = /(bbb)/g;
4f890a30 668 is("@_", "", $message);
84281c31 669 }
a72deede 670
84281c31 671 {
27deb0cf 672 my $message = '@- and @+ and @{^CAPTURE} tests';
84281c31 673
27deb0cf
YO
674 $_= "ace";
675 /c(?=.$)/;
676 is($#{^CAPTURE}, -1, $message);
4f890a30
NC
677 is($#+, 0, $message);
678 is($#-, 0, $message);
679 is($+ [0], 2, $message);
680 is($- [0], 1, $message);
681 ok(!defined $+ [1] && !defined $- [1] &&
682 !defined $+ [2] && !defined $- [2], $message);
84281c31 683
27deb0cf
YO
684 /a(c)(e)/;
685 is($#{^CAPTURE}, 1, $message); # one less than $#-
4f890a30
NC
686 is($#+, 2, $message);
687 is($#-, 2, $message);
688 is($+ [0], 3, $message);
689 is($- [0], 0, $message);
27deb0cf 690 is(${^CAPTURE}[0], "c", $message);
4f890a30
NC
691 is($+ [1], 2, $message);
692 is($- [1], 1, $message);
27deb0cf 693 is(${^CAPTURE}[1], "e", $message);
4f890a30
NC
694 is($+ [2], 3, $message);
695 is($- [2], 2, $message);
696 ok(!defined $+ [3] && !defined $- [3] &&
27deb0cf 697 !defined ${^CAPTURE}[2] && !defined ${^CAPTURE}[3] &&
4f890a30 698 !defined $+ [4] && !defined $- [4], $message);
84281c31 699
54a4274e 700 # Exists has a special check for @-/@+ - bug 45147
4f890a30
NC
701 ok(exists $-[0], $message);
702 ok(exists $+[0], $message);
27deb0cf
YO
703 ok(exists ${^CAPTURE}[0], $message);
704 ok(exists ${^CAPTURE}[1], $message);
4f890a30
NC
705 ok(exists $-[2], $message);
706 ok(exists $+[2], $message);
27deb0cf 707 ok(!exists ${^CAPTURE}[2], $message);
4f890a30
NC
708 ok(!exists $-[3], $message);
709 ok(!exists $+[3], $message);
27deb0cf
YO
710 ok(exists ${^CAPTURE}[-1], $message);
711 ok(exists ${^CAPTURE}[-2], $message);
4f890a30
NC
712 ok(exists $-[-1], $message);
713 ok(exists $+[-1], $message);
714 ok(exists $-[-3], $message);
715 ok(exists $+[-3], $message);
716 ok(!exists $-[-4], $message);
717 ok(!exists $+[-4], $message);
27deb0cf 718 ok(!exists ${^CAPTURE}[-3], $message);
84281c31 719
27deb0cf
YO
720
721 /.(c)(b)?(e)/;
722 is($#{^CAPTURE}, 2, $message); # one less than $#-
4f890a30
NC
723 is($#+, 3, $message);
724 is($#-, 3, $message);
27deb0cf
YO
725 is(${^CAPTURE}[0], "c", $message);
726 is(${^CAPTURE}[2], "e", $message . "[$1 $3]");
4f890a30
NC
727 is($+ [1], 2, $message);
728 is($- [1], 1, $message);
729 is($+ [3], 3, $message);
730 is($- [3], 2, $message);
731 ok(!defined $+ [2] && !defined $- [2] &&
27deb0cf
YO
732 !defined $+ [4] && !defined $- [4] &&
733 !defined ${^CAPTURE}[1], $message);
84281c31 734
27deb0cf
YO
735 /.(c)/;
736 is($#{^CAPTURE}, 0, $message); # one less than $#-
4f890a30
NC
737 is($#+, 1, $message);
738 is($#-, 1, $message);
27deb0cf 739 is(${^CAPTURE}[0], "c", $message);
4f890a30
NC
740 is($+ [0], 2, $message);
741 is($- [0], 0, $message);
742 is($+ [1], 2, $message);
743 is($- [1], 1, $message);
744 ok(!defined $+ [2] && !defined $- [2] &&
27deb0cf
YO
745 !defined $+ [3] && !defined $- [3] &&
746 !defined ${^CAPTURE}[1], $message);
84281c31 747
27deb0cf
YO
748 /.(c)(ba*)?/;
749 is($#{^CAPTURE}, 0, $message); # one less than $#-
4f890a30
NC
750 is($#+, 2, $message);
751 is($#-, 1, $message);
232af1f8 752
150fa1c5 753 # Check that values don't stick
232af1f8 754 " "=~/()()()(.)(..)/;
27deb0cf
YO
755 my($m,$p,$q) = (\$-[5], \$+[5], \${^CAPTURE}[4]);
756 () = "$$_" for $m, $p, $q; # FETCH (or eqv.)
232af1f8
FC
757 " " =~ /()/;
758 is $$m, undef, 'values do not stick to @- elements';
759 is $$p, undef, 'values do not stick to @+ elements';
27deb0cf 760 is $$q, undef, 'values do not stick to @{^CAPTURE} elements';
84281c31 761 }
a72deede 762
d9bad346 763 foreach ('$+[0] = 13', '$-[0] = 13', '@+ = (7, 6, 5)',
27deb0cf 764 '${^CAPTURE}[0] = 13',
d9bad346 765 '@- = qw (foo bar)', '$^N = 42') {
cb124425
NC
766 is(eval $_, undef);
767 like($@, qr/^Modification of a read-only value attempted/,
d9bad346 768 '$^N, @- and @+ are read-only');
84281c31 769 }
a72deede 770
84281c31 771 {
4f890a30 772 my $message = '\G testing';
84281c31
A
773 $_ = 'aaa';
774 pos = 1;
775 my @a = /\Ga/g;
4f890a30 776 is("@a", "a a", $message);
84281c31
A
777
778 my $str = 'abcde';
779 pos $str = 2;
4f890a30
NC
780 unlike($str, qr/^\G/, $message);
781 unlike($str, qr/^.\G/, $message);
782 like($str, qr/^..\G/, $message);
783 unlike($str, qr/^...\G/, $message);
784 ok($str =~ /\G../ && $& eq 'cd', $message);
4f890a30 785 ok($str =~ /.\G./ && $& eq 'bc', $message);
cf44e600
DM
786
787 }
788
789 {
790 my $message = '\G and intuit and anchoring';
791 $_ = "abcdef";
792 pos = 0;
793 ok($_ =~ /\Gabc/, $message);
794 ok($_ =~ /^\Gabc/, $message);
795
796 pos = 3;
797 ok($_ =~ /\Gdef/, $message);
798 pos = 3;
799 ok($_ =~ /\Gdef$/, $message);
800 pos = 3;
801 ok($_ =~ /abc\Gdef$/, $message);
802 pos = 3;
803 ok($_ =~ /^abc\Gdef$/, $message);
804 pos = 3;
805 ok($_ =~ /c\Gd/, $message);
e0362b86
DM
806 pos = 3;
807 ok($_ =~ /..\GX?def/, $message);
84281c31 808 }
a72deede 809
84281c31 810 {
fe3974be
DM
811 my $s = '123';
812 pos($s) = 1;
813 my @a = $s =~ /(\d)\G/g; # this infinitely looped up till 5.19.1
814 is("@a", "1", '\G looping');
815 }
816
817
818 {
4f890a30 819 my $message = 'pos inside (?{ })';
84281c31
A
820 my $str = 'abcde';
821 our ($foo, $bar);
4f890a30
NC
822 like($str, qr/b(?{$foo = $_; $bar = pos})c/, $message);
823 is($foo, $str, $message);
824 is($bar, 2, $message);
825 is(pos $str, undef, $message);
84281c31
A
826
827 undef $foo;
828 undef $bar;
829 pos $str = undef;
4f890a30
NC
830 ok($str =~ /b(?{$foo = $_; $bar = pos})c/g, $message);
831 is($foo, $str, $message);
832 is($bar, 2, $message);
833 is(pos $str, 3, $message);
84281c31
A
834
835 $_ = $str;
836 undef $foo;
837 undef $bar;
4f890a30
NC
838 like($_, qr/b(?{$foo = $_; $bar = pos})c/, $message);
839 is($foo, $str, $message);
840 is($bar, 2, $message);
84281c31
A
841
842 undef $foo;
843 undef $bar;
4f890a30
NC
844 ok(/b(?{$foo = $_; $bar = pos})c/g, $message);
845 is($foo, $str, $message);
846 is($bar, 2, $message);
847 is(pos, 3, $message);
84281c31
A
848
849 undef $foo;
850 undef $bar;
851 pos = undef;
852 1 while /b(?{$foo = $_; $bar = pos})c/g;
4f890a30
NC
853 is($foo, $str, $message);
854 is($bar, 2, $message);
855 is(pos, undef, $message);
84281c31
A
856
857 undef $foo;
858 undef $bar;
859 $_ = 'abcde|abcde';
4f890a30
NC
860 ok(s/b(?{$foo = $_; $bar = pos})c/x/g, $message);
861 is($foo, 'abcde|abcde', $message);
862 is($bar, 8, $message);
863 is($_, 'axde|axde', $message);
84281c31
A
864
865 # List context:
866 $_ = 'abcde|abcde';
867 our @res;
868 () = /([ace]).(?{push @res, $1,$2})([ce])(?{push @res, $1,$2})/g;
869 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
4f890a30 870 is("@res", "'a' undef 'a' 'c' 'e' undef 'a' undef 'a' 'c'", $message);
84281c31
A
871
872 @res = ();
873 () = /([ace]).(?{push @res, $`,$&,$'})([ce])(?{push @res, $`,$&,$'})/g;
874 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
4f890a30 875 is("@res", "'' 'ab' 'cde|abcde' " .
84281c31
A
876 "'' 'abc' 'de|abcde' " .
877 "'abcd' 'e|' 'abcde' " .
878 "'abcde|' 'ab' 'cde' " .
4f890a30 879 "'abcde|' 'abc' 'de'", $message);
84281c31 880 }
f33976b4 881
84281c31 882 {
4f890a30 883 my $message = '\G anchor checks';
84281c31
A
884 my $foo = 'aabbccddeeffgg';
885 pos ($foo) = 1;
cce850e4 886
98bee633
DM
887 ok($foo =~ /.\G(..)/g, $message);
888 is($1, 'ab', $message);
cce850e4 889
98bee633
DM
890 pos ($foo) += 1;
891 ok($foo =~ /.\G(..)/g, $message);
892 is($1, 'cc', $message);
cce850e4 893
98bee633
DM
894 pos ($foo) += 1;
895 ok($foo =~ /.\G(..)/g, $message);
896 is($1, 'de', $message);
897
898 ok($foo =~ /\Gef/g, $message);
cce850e4 899
84281c31 900 undef pos $foo;
4f890a30
NC
901 ok($foo =~ /\G(..)/g, $message);
902 is($1, 'aa', $message);
cce850e4 903
4f890a30
NC
904 ok($foo =~ /\G(..)/g, $message);
905 is($1, 'bb', $message);
cce850e4 906
84281c31 907 pos ($foo) = 5;
4f890a30
NC
908 ok($foo =~ /\G(..)/g, $message);
909 is($1, 'cd', $message);
84281c31 910 }
cce850e4 911
84281c31 912 {
9c13586e
DM
913 my $message = 'basic \G floating checks';
914 my $foo = 'aabbccddeeffgg';
915 pos ($foo) = 1;
916
917 ok($foo =~ /a+\G(..)/g, "$message: a+\\G");
918 is($1, 'ab', "$message: ab");
919
920 pos ($foo) += 1;
921 ok($foo =~ /b+\G(..)/g, "$message: b+\\G");
922 is($1, 'cc', "$message: cc");
923
924 pos ($foo) += 1;
925 ok($foo =~ /d+\G(..)/g, "$message: d+\\G");
926 is($1, 'de', "$message: de");
927
928 ok($foo =~ /\Gef/g, "$message: \\Gef");
929
930 pos ($foo) = 1;
931
932 ok($foo =~ /(?=a+\G)(..)/g, "$message: (?a+\\G)");
933 is($1, 'aa', "$message: aa");
934
935 pos ($foo) = 2;
936
937 ok($foo =~ /a(?=a+\G)(..)/g, "$message: a(?=a+\\G)");
938 is($1, 'ab', "$message: ab");
939
940 }
941
942 {
84281c31
A
943 $_ = '123x123';
944 my @res = /(\d*|x)/g;
945 local $" = '|';
de26e0cc 946 is("@res", "123||x|123|", "0 match in alternation");
84281c31 947 }
cce850e4 948
84281c31 949 {
4f890a30 950 my $message = "Match against temporaries (created via pp_helem())" .
84281c31 951 " is safe";
4f890a30
NC
952 ok({foo => "bar\n" . $^X} -> {foo} =~ /^(.*)\n/g, $message);
953 is($1, "bar", $message);
84281c31 954 }
75685a94 955
84281c31 956 {
4f890a30 957 my $message = 'package $i inside (?{ }), ' .
84281c31
A
958 'saved substrings and changing $_';
959 our @a = qw [foo bar];
960 our @b = ();
961 s/(\w)(?{push @b, $1})/,$1,/g for @a;
4f890a30
NC
962 is("@b", "f o o b a r", $message);
963 is("@a", ",f,,o,,o, ,b,,a,,r,", $message);
84281c31 964
4f890a30 965 $message = 'lexical $i inside (?{ }), ' .
84281c31
A
966 'saved substrings and changing $_';
967 no warnings 'closure';
968 my @c = qw [foo bar];
969 my @d = ();
970 s/(\w)(?{push @d, $1})/,$1,/g for @c;
4f890a30
NC
971 is("@d", "f o o b a r", $message);
972 is("@c", ",f,,o,,o, ,b,,a,,r,", $message);
d9f424b2
JH
973 }
974
84281c31 975 {
4f890a30 976 my $message = 'Brackets';
84281c31
A
977 our $brackets;
978 $brackets = qr {
979 { (?> [^{}]+ | (??{ $brackets }) )* }
980 }x;
981
4f890a30
NC
982 ok("{{}" =~ $brackets, $message);
983 is($&, "{}", $message);
984 ok("something { long { and } hairy" =~ $brackets, $message);
985 is($&, "{ and }", $message);
986 ok("something { long { and } hairy" =~ m/((??{ $brackets }))/, $message);
987 is($&, "{ and }", $message);
84281c31 988 }
a4c04bdc 989
84281c31
A
990 {
991 $_ = "a-a\nxbb";
992 pos = 1;
b33825c4 993 ok(!m/^-.*bb/mg, '$_ = "a-a\nxbb"; m/^-.*bb/mg');
84281c31 994 }
a4c04bdc 995
84281c31 996 {
4f890a30 997 my $message = '\G anchor checks';
84281c31
A
998 my $text = "aaXbXcc";
999 pos ($text) = 0;
4f890a30 1000 ok($text !~ /\GXb*X/g, $message);
84281c31 1001 }
a4c04bdc 1002
84281c31
A
1003 {
1004 $_ = "xA\n" x 500;
b33825c4 1005 unlike($_, qr/^\s*A/m, '$_ = "xA\n" x 500; /^\s*A/m"');
a4c04bdc 1006
84281c31
A
1007 my $text = "abc dbf";
1008 my @res = ($text =~ /.*?(b).*?\b/g);
de26e0cc 1009 is("@res", "b b", '\b is not special');
987aaf07 1010 }
a4c04bdc 1011
84281c31 1012 {
4f890a30 1013 my $message = '\S, [\S], \s, [\s]';
84281c31 1014 my @a = map chr, 0 .. 255;
9d45b377
YO
1015 my @b = grep m/\S/, @a;
1016 my @c = grep m/[^\s]/, @a;
4f890a30 1017 is("@b", "@c", $message);
84281c31
A
1018
1019 @b = grep /\S/, @a;
1020 @c = grep /[\S]/, @a;
4f890a30 1021 is("@b", "@c", $message);
84281c31
A
1022
1023 @b = grep /\s/, @a;
1024 @c = grep /[^\S]/, @a;
4f890a30 1025 is("@b", "@c", $message);
84281c31
A
1026
1027 @b = grep /\s/, @a;
1028 @c = grep /[\s]/, @a;
4f890a30 1029 is("@b", "@c", $message);
72e868c8
KW
1030
1031 # Test an inverted posix class with a char also in the class.
1032 my $nbsp = chr utf8::unicode_to_native(0xA0);
1033 my $non_s = chr utf8::unicode_to_native(0xA1);
1034 my $pat_string = "[^\\S ]";
1035 unlike(" ", qr/$pat_string/, "Verify ' ' !~ /$pat_string/");
1036 like("\t", qr/$pat_string/, "Verify '\\t =~ /$pat_string/");
1037 unlike($nbsp, qr/$pat_string/, "Verify non-utf8-NBSP !~ /$pat_string/");
1038 utf8::upgrade($nbsp);
1039 like($nbsp, qr/$pat_string/, "Verify utf8-NBSP =~ /$pat_string/");
1040 unlike($non_s, qr/$pat_string/, "Verify non-utf8-inverted-bang !~ /$pat_string/");
1041 utf8::upgrade($non_s);
1042 unlike($non_s, qr/$pat_string/, "Verify utf8-inverted-bang !~ /$pat_string/");
84281c31
A
1043 }
1044 {
4f890a30 1045 my $message = '\D, [\D], \d, [\d]';
84281c31
A
1046 my @a = map chr, 0 .. 255;
1047 my @b = grep /\D/, @a;
1048 my @c = grep /[^\d]/, @a;
4f890a30 1049 is("@b", "@c", $message);
84281c31
A
1050
1051 @b = grep /\D/, @a;
1052 @c = grep /[\D]/, @a;
4f890a30 1053 is("@b", "@c", $message);
84281c31
A
1054
1055 @b = grep /\d/, @a;
1056 @c = grep /[^\D]/, @a;
4f890a30 1057 is("@b", "@c", $message);
84281c31
A
1058
1059 @b = grep /\d/, @a;
1060 @c = grep /[\d]/, @a;
4f890a30 1061 is("@b", "@c", $message);
84281c31
A
1062 }
1063 {
4f890a30 1064 my $message = '\W, [\W], \w, [\w]';
84281c31
A
1065 my @a = map chr, 0 .. 255;
1066 my @b = grep /\W/, @a;
1067 my @c = grep /[^\w]/, @a;
4f890a30 1068 is("@b", "@c", $message);
84281c31
A
1069
1070 @b = grep /\W/, @a;
1071 @c = grep /[\W]/, @a;
4f890a30 1072 is("@b", "@c", $message);
84281c31
A
1073
1074 @b = grep /\w/, @a;
1075 @c = grep /[^\W]/, @a;
4f890a30 1076 is("@b", "@c", $message);
84281c31
A
1077
1078 @b = grep /\w/, @a;
1079 @c = grep /[\w]/, @a;
4f890a30 1080 is("@b", "@c", $message);
84281c31 1081 }
a4c04bdc 1082
84281c31
A
1083 {
1084 # see if backtracking optimization works correctly
4f890a30
NC
1085 my $message = 'Backtrack optimization';
1086 like("\n\n", qr/\n $ \n/x, $message);
1087 like("\n\n", qr/\n* $ \n/x, $message);
1088 like("\n\n", qr/\n+ $ \n/x, $message);
1089 like("\n\n", qr/\n? $ \n/x, $message);
1090 like("\n\n", qr/\n*? $ \n/x, $message);
1091 like("\n\n", qr/\n+? $ \n/x, $message);
1092 like("\n\n", qr/\n?? $ \n/x, $message);
1093 unlike("\n\n", qr/\n*+ $ \n/x, $message);
1094 unlike("\n\n", qr/\n++ $ \n/x, $message);
1095 like("\n\n", qr/\n?+ $ \n/x, $message);
84281c31 1096 }
a4c04bdc 1097
84281c31
A
1098 {
1099 package S;
1100 use overload '""' => sub {'Object S'};
1101 sub new {bless []}
0f289c68 1102
4f890a30 1103 my $message = "Ref stringification";
5895685f
NC
1104 ::ok(do { \my $v} =~ /^SCALAR/, "Scalar ref stringification") or diag($message);
1105 ::ok(do {\\my $v} =~ /^REF/, "Ref ref stringification") or diag($message);
1106 ::ok([] =~ /^ARRAY/, "Array ref stringification") or diag($message);
1107 ::ok({} =~ /^HASH/, "Hash ref stringification") or diag($message);
1108 ::ok('S' -> new =~ /^Object S/, "Object stringification") or diag($message);
84281c31 1109 }
a4c04bdc 1110
84281c31 1111 {
4f890a30
NC
1112 my $message = "Test result of match used as match";
1113 ok('a1b' =~ ('xyz' =~ /y/), $message);
1114 is($`, 'a', $message);
1115 ok('a1b' =~ ('xyz' =~ /t/), $message);
1116 is($`, 'a', $message);
84281c31 1117 }
a4c04bdc 1118
84281c31 1119 {
d728c370 1120 my $message = '"1" is not \s';
c11a8df3
NC
1121 warning_is(sub {unlike("1\n" x 102, qr/^\s*\n/m, $message)},
1122 undef, "$message (did not warn)");
84281c31 1123 }
a4c04bdc 1124
84281c31 1125 {
4f890a30 1126 my $message = '\s, [[:space:]] and [[:blank:]]';
84281c31
A
1127 my %space = (spc => " ",
1128 tab => "\t",
1129 cr => "\r",
1130 lf => "\n",
1131 ff => "\f",
1132 # There's no \v but the vertical tabulator seems miraculously
1133 # be 11 both in ASCII and EBCDIC.
1134 vt => chr(11),
1135 false => "space");
1136
1137 my @space0 = sort grep {$space {$_} =~ /\s/ } keys %space;
1138 my @space1 = sort grep {$space {$_} =~ /[[:space:]]/} keys %space;
1139 my @space2 = sort grep {$space {$_} =~ /[[:blank:]]/} keys %space;
1140
075b9d7d 1141 is("@space0", "cr ff lf spc tab vt", $message);
4f890a30
NC
1142 is("@space1", "cr ff lf spc tab vt", $message);
1143 is("@space2", "spc tab", $message);
84281c31 1144 }
a4c04bdc 1145
ff3f963a 1146 {
c9415951 1147 my $n= 50;
93f09d7b 1148 # this must be a high number and go from 0 to N, as the bug we are looking for doesn't
c9415951
YO
1149 # seem to be predictable. Slight changes to the test make it fail earlier or later.
1150 foreach my $i (0 .. $n)
1151 {
1152 my $str= "\n" x $i;
93f09d7b 1153 ok $str=~/.*\z/, "implicit MBOL check string disable does not break things length=$i";
c9415951
YO
1154 }
1155 }
92f3d482
YO
1156 {
1157 # we are actually testing that we dont die when executing these patterns
1158 use utf8;
1159 my $e = "Böck";
1160 ok(utf8::is_utf8($e),"got a unicode string - rt75680");
1161
1162 ok($e !~ m/.*?[x]$/, "unicode string against /.*?[x]\$/ - rt75680");
1163 ok($e !~ m/.*?\p{Space}$/i, "unicode string against /.*?\\p{space}\$/i - rt75680");
1164 ok($e !~ m/.*?[xyz]$/, "unicode string against /.*?[xyz]\$/ - rt75680");
1165 ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/, "unicode string against big pattern - rt75680");
1166 }
1167 {
1168 # we are actually testing that we dont die when executing these patterns
6d937e95 1169 my $e = "B" . uni_to_native("\x{f6}") . "ck";
92f3d482
YO
1170 ok(!utf8::is_utf8($e), "got a latin string - rt75680");
1171
1172 ok($e !~ m/.*?[x]$/, "latin string against /.*?[x]\$/ - rt75680");
1173 ok($e !~ m/.*?\p{Space}$/i, "latin string against /.*?\\p{space}\$/i - rt75680");
1174 ok($e !~ m/.*?[xyz]$/,"latin string against /.*?[xyz]\$/ - rt75680");
1175 ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/,"latin string against big pattern - rt75680");
1176 }
c920e018
A
1177
1178 {
1179 #
1180 # Tests for bug 77414.
1181 #
1182
4f890a30 1183 my $message = '\p property after empty * match';
c920e018 1184 {
4f890a30
NC
1185 like("1", qr/\s*\pN/, $message);
1186 like("-", qr/\s*\p{Dash}/, $message);
1187 like(" ", qr/\w*\p{Blank}/, $message);
c920e018
A
1188 }
1189
4f890a30
NC
1190 like("1", qr/\s*\pN+/, $message);
1191 like("-", qr/\s*\p{Dash}{1}/, $message);
1192 like(" ", qr/\w*\p{Blank}{1,4}/, $message);
c920e018
A
1193
1194 }
1195
6d937e95
KW
1196 { # Some constructs with Latin1 characters cause a utf8 string not
1197 # to match itself in non-utf8
1198 my $c = uni_to_native("\xc0");
1199 my $pattern = my $utf8_pattern = qr/(($c)+,?)/;
634c83a2
KW
1200 utf8::upgrade($utf8_pattern);
1201 ok $c =~ $pattern, "\\xc0 =~ $pattern; Neither pattern nor target utf8";
d4e0b827 1202 ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; Neither pattern nor target utf8";
634c83a2 1203 ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; pattern utf8, target not";
d4e0b827 1204 ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; pattern utf8, target not";
634c83a2
KW
1205 utf8::upgrade($c);
1206 ok $c =~ $pattern, "\\xc0 =~ $pattern; target utf8, pattern not";
d4e0b827 1207 ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; target utf8, pattern not";
634c83a2 1208 ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; Both target and pattern utf8";
d4e0b827 1209 ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; Both target and pattern utf8";
634c83a2
KW
1210 }
1211
6d937e95 1212 { # Make sure can override the formatting
8cc86590 1213 use feature 'unicode_strings';
6d937e95
KW
1214 ok uni_to_native("\xc0") =~ /\w/, 'Under unicode_strings: "\xc0" =~ /\w/';
1215 ok uni_to_native("\xc0") !~ /(?d:\w)/, 'Under unicode_strings: "\xc0" !~ /(?d:\w)/';
8cc86590
KW
1216 }
1217
704f71be 1218 {
5b6010b3
YO
1219 my $str= "\x{100}";
1220 chop $str;
1221 my $qr= qr/$str/;
de26e0cc 1222 is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag enabled - Bug #80212");
5b6010b3
YO
1223 $str= "";
1224 $qr= qr/$str/;
de26e0cc 1225 is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag disabled - Bug #80212");
5b6010b3
YO
1226
1227 }
1228
72aa120d 1229 {
04934b6d 1230 local $::TODO = "[perl #38133]";
72aa120d
KW
1231
1232 "A" =~ /(((?:A))?)+/;
1233 my $first = $2;
1234
1235 "A" =~ /(((A))?)+/;
1236 my $second = $2;
1237
de26e0cc 1238 is($first, $second);
72aa120d
KW
1239 }
1240
99ca48e1
DM
1241 {
1242 # RT #3516: \G in a m//g expression causes problems
1243 my $count = 0;
1244 while ("abc" =~ m/(\G[ac])?/g) {
1245 last if $count++ > 10;
1246 }
1247 ok($count < 10, 'RT #3516 A');
1248
1249 $count = 0;
1250 while ("abc" =~ m/(\G|.)[ac]/g) {
1251 last if $count++ > 10;
1252 }
1253 ok($count < 10, 'RT #3516 B');
1254
1255 $count = 0;
1256 while ("abc" =~ m/(\G?[ac])?/g) {
1257 last if $count++ > 10;
1258 }
1259 ok($count < 10, 'RT #3516 C');
1260 }
d774cd11
YO
1261 {
1262 # RT #84294: Is this a bug in the simple Perl regex?
1263 # : Nested buffers and (?{...}) dont play nicely on partial matches
1264 our @got= ();
1265 ok("ab" =~ /((\w+)(?{ push @got, $2 })){2}/,"RT #84294: Pattern should match");
d774cd11
YO
1266 my $want= "'ab', 'a', 'b'";
1267 my $got= join(", ", map { defined($_) ? "'$_'" : "undef" } @got);
1268 is($got,$want,'RT #84294: check that "ab" =~ /((\w+)(?{ push @got, $2 })){2}/ leaves @got in the correct state');
1269 }
1270
0bda3001
KW
1271 {
1272 # Suppress warnings, as the non-unicode one comes out even if turn off
1273 # warnings here (because the execution is done in another scope).
1274 local $SIG{__WARN__} = sub {};
1275 my $str = "\x{110000}";
1276
2d88a86a
KW
1277 unlike($str, qr/\p{ASCII_Hex_Digit=True}/, "Non-Unicode doesn't match \\p{AHEX=True}");
1278 like($str, qr/\p{ASCII_Hex_Digit=False}/, "Non-Unicode matches \\p{AHEX=False}");
1279 like($str, qr/\P{ASCII_Hex_Digit=True}/, "Non-Unicode matches \\P{AHEX=True}");
1280 unlike($str, qr/\P{ASCII_Hex_Digit=False}/, "Non-Unicode matches \\P{AHEX=FALSE}");
0bda3001
KW
1281 }
1282
b289b443 1283 {
c256547e 1284 # Test that IDstart works, but because the author (khw) knows
b289b443
KW
1285 # regexes much better than the rest of the core, it is being done here
1286 # in the context of a regex which relies on buffer names beginng with
1287 # IDStarts.
1288 use utf8;
1289 my $str = "abc";
1290 like($str, qr/(?<a>abc)/, "'a' is legal IDStart");
1291 like($str, qr/(?<_>abc)/, "'_' is legal IDStart");
1292 like($str, qr/(?<ß>abc)/, "U+00DF is legal IDStart");
1293 like($str, qr/(?<ℕ>abc)/, "U+2115' is legal IDStart");
1294
1295 # This test works on Unicode 6.0 in which U+2118 and U+212E are legal
1296 # IDStarts there, but are not Word characters, and therefore Perl
1297 # doesn't allow them to be IDStarts. But there is no guarantee that
1298 # Unicode won't change things around in the future so that at some
1299 # future Unicode revision these tests would need to be revised.
1300 foreach my $char ("%", "×", chr(0x2118), chr(0x212E)) {
1301 my $prog = <<"EOP";
1302use utf8;;
1303"abc" =~ qr/(?<$char>abc)/;
1304EOP
1305 utf8::encode($prog);
20e5bab4 1306 fresh_perl_like($prog, qr!Group name must start with a non-digit word character!, {},
b289b443
KW
1307 sprintf("'U+%04X not legal IDFirst'", ord($char)));
1308 }
1309 }
bbdd8bad
KW
1310
1311 { # [perl #101710]
1312 my $pat = "b";
1313 utf8::upgrade($pat);
1314 like("\xffb", qr/$pat/i, "/i: utf8 pattern, non-utf8 string, latin1-char preceding matching char in string");
1315 }
1316
ba510004
FC
1317 { # Crash with @a =~ // warning
1318 local $SIG{__WARN__} = sub {
1319 pass 'no crash for @a =~ // warning'
1320 };
1321 eval ' sub { my @a =~ // } ';
1322 }
1323
cc88c9aa
FC
1324 { # Concat overloading and qr// thingies
1325 my @refs;
1326 my $qr = qr//;
3bbc3d30
YO
1327 package Cat {
1328 require overload;
1329 overload->import(
cc88c9aa
FC
1330 '""' => sub { ${$_[0]} },
1331 '.' => sub {
1332 push @refs, ref $_[1] if ref $_[1];
1333 bless $_[2] ? \"$_[1]${$_[0]}" : \"${$_[0]}$_[1]"
1334 }
3bbc3d30 1335 );
cc88c9aa
FC
1336 }
1337 my $s = "foo";
1338 my $o = bless \$s, Cat::;
1339 /$o$qr/;
1340 is "@refs", "Regexp", '/$o$qr/ passes qr ref to cat overload meth';
1341 }
1342
21eede78
YO
1343 {
1344 my $count=0;
1345 my $str="\n";
1346 $count++ while $str=~/.*/g;
1347 is $count, 2, 'test that ANCH_MBOL works properly. We should get 2 from $count++ while "\n"=~/.*/g';
1348 my $class_count= 0;
1349 $class_count++ while $str=~/[^\n]*/g;
1350 is $class_count, $count, 'while "\n"=~/.*/g and while "\n"=~/[^\n]*/g should behave the same';
1351 my $anch_count= 0;
1352 $anch_count++ while $str=~/^.*/mg;
1353 is $anch_count, 1, 'while "\n"=~/^.*/mg should match only once';
1354 }
96f54887
KW
1355
1356 { # [perl #111174]
1357 use re '/u';
6d937e95
KW
1358 my $A_grave = uni_to_native("\xc0");
1359 like uni_to_native("\xe0"), qr/(?i:$A_grave)/, "(?i: shouldn't lose the passed in /u";
96f54887
KW
1360 use re '/a';
1361 unlike "\x{100}", qr/(?i:\w)/, "(?i: shouldn't lose the passed in /a";
1362 use re '/aa';
1363 unlike 'k', qr/(?i:\N{KELVIN SIGN})/, "(?i: shouldn't lose the passed in /aa";
945961fd 1364 unlike 'k', qr'(?i:\N{KELVIN SIGN})', "(?i: shouldn't lose the passed in /aa";
96f54887 1365 }
6ae44cd2
DM
1366
1367 {
1368 # the test for whether the pattern should be re-compiled should
1369 # consider the UTF8ness of the previous and current pattern
1370 # string, as well as the physical bytes of the pattern string
1371
6d937e95 1372 for my $s (byte_utf8a_to_utf8n("\xc4\x80"), "\x{100}") {
6ae44cd2
DM
1373 ok($s =~ /^$s$/, "re-compile check is UTF8-aware");
1374 }
1375 }
1376
e03b874a
DM
1377 # #113682 more overloading and qr//
1378 # when doing /foo$overloaded/, if $overloaded returns
1379 # a qr/(?{})/ via qr or "" overloading, then 'use re 'eval'
1380 # shouldn't be required. Via '.', it still is.
1381 {
1382 package Qr0;
1383 use overload 'qr' => sub { qr/(??{50})/ };
1384
1385 package Qr1;
1386 use overload '""' => sub { qr/(??{51})/ };
1387
1388 package Qr2;
1389 use overload '.' => sub { $_[1] . qr/(??{52})/ };
1390
1391 package Qr3;
1392 use overload '""' => sub { qr/(??{7})/ },
1393 '.' => sub { $_[1] . qr/(??{53})/ };
1394
1395 package Qr_indirect;
1396 use overload '""' => sub { $_[0][0] };
1397
1398 package main;
1399
1400 for my $i (0..3) {
1401 my $o = bless [], "Qr$i";
1402 if ((0,0,1,1)[$i]) {
1403 eval { "A5$i" =~ /^A$o$/ };
1404 like($@, qr/Eval-group not allowed/, "Qr$i");
1405 eval { "5$i" =~ /$o/ };
1406 like($@, ($i == 3 ? qr/^$/ : qr/no method found,/),
1407 "Qr$i bare");
1408 {
1409 use re 'eval';
1410 ok("A5$i" =~ /^A$o$/, "Qr$i - with use re eval");
1411 eval { "5$i" =~ /$o/ };
1412 like($@, ($i == 3 ? qr/^$/ : qr/no method found,/),
1413 "Qr$i bare - with use re eval");
1414 }
1415 }
1416 else {
1417 ok("A5$i" =~ /^A$o$/, "Qr$i");
1418 ok("5$i" =~ /$o/, "Qr$i bare");
1419 }
1420 }
1421
1422 my $o = bless [ bless [], "Qr1" ], 'Qr_indirect';
1423 ok("A51" =~ /^A$o/, "Qr_indirect");
1424 ok("51" =~ /$o/, "Qr_indirect bare");
1425 }
1426
34b39fc9
KW
1427 { # Various flags weren't being set when a [] is optimized into an
1428 # EXACTish node
6d937e95 1429 ok("\x{017F}\x{017F}" =~ qr/^[$sharp_s]?$/i, "[] to EXACTish optimization");
34b39fc9
KW
1430 }
1431
3363c703
KW
1432 { # Test that it avoids spllitting a multi-char fold across nodes.
1433 # These all fold to things that are like 'ss', which, if split across
1434 # nodes could fail to match a single character that folds to the
95815e9d 1435 # combination. 1F0 byte expands when folded;
3ae8ec47 1436 my $utf8_locale = find_utf8_ctype_locale();
95815e9d 1437 for my $char('F', $sharp_s, "\x{1F0}", "\x{FB00}") {
3ae8ec47
KW
1438 my $length = 260; # Long enough to overflow an EXACTFish regnode
1439 my $p = $char x $length;
95815e9d
KW
1440 my $s = ($char eq $sharp_s) ? 'ss'
1441 : $char eq "\x{1F0}"
1442 ? "j\x{30c}"
1443 : 'ff';
3ae8ec47
KW
1444 $s = $s x $length;
1445 for my $charset (qw(u d l aa)) {
1446 for my $utf8 (0..1) {
3ae8ec47 1447 for my $locale ('C', $utf8_locale) {
36b76730
TC
1448 SKIP:
1449 {
1450 skip "test skipped for non-C locales", 2
3ae8ec47
KW
1451 if $charset ne 'l'
1452 && (! defined $locale || $locale ne 'C');
36b76730
TC
1453 if ($charset eq 'l') {
1454 if (! defined $locale) {
1455 skip "No UTF-8 locale", 2;
1456 }
1457 skip "Can't test in miniperl",2
1458 if is_miniperl();
1459
1460 require POSIX;
1461 POSIX::setlocale(&LC_CTYPE, $locale);
3ae8ec47
KW
1462 }
1463
36b76730
TC
1464 my $pat = $p;
1465 utf8::upgrade($pat) if $utf8;
1466 my $should_pass =
1467 ( $charset eq 'u'
04f3bbde
KW
1468 || ($charset eq 'd' && $utf8)
1469 || ($charset eq 'd' && ( $char =~ /[[:ascii:]]/
1470 || ord $char > 255))
1471 || ($charset eq 'aa' && $char =~ /[[:ascii:]]/)
1472 || ($charset eq 'l' && $locale ne 'C')
1473 || ($charset eq 'l' && $char =~ /[[:ascii:]]/)
1474 );
36b76730
TC
1475 my $name = "(?i$charset), utf8=$utf8, locale=$locale,"
1476 . " char=" . sprintf "%x", ord $char;
1477 no warnings 'locale';
1478 is (eval " '$s' =~ qr/(?i$charset)$pat/;",
1479 $should_pass, $name);
1480 fail "$name: $@" if $@;
1481 is (eval " 'a$s' =~ qr/(?i$charset)a$pat/;",
1482 $should_pass, "extra a, $name");
1483 fail "$name: $@" if $@;
3ae8ec47 1484 }
3ae8ec47
KW
1485 }
1486 }
1487 }
1488 }
1489 }
1490
521a1e96 1491 SKIP:
5e4a1da1 1492 {
521a1e96 1493 skip "no re debug", 5 if is_miniperl;
3363c703
KW
1494 my $s = ("0123456789" x 26214) x 2; # Should fill 2 LEXACTS, plus
1495 # small change
1496 my $pattern_prefix = "use utf8; use re qw(Debug COMPILE)";
1497 my $pattern = "$pattern_prefix; qr/$s/;";
1498 my $result = fresh_perl($pattern);
1499 if ($? != 0) { # Re-run so as to display STDERR.
1500 fail($pattern);
1501 fresh_perl($pattern, { stderr => 0, verbose => 1 });
1502 }
1503 like($result, qr/Final program[^X]*\bLEXACT\b[^X]*\bLEXACT\b[^X]*\bEXACT\b[^X]*\bEND\b/s,
1504 "Check that LEXACT nodes are generated");
1505 like($s, qr/$s/, "Check that LEXACT nodes match");
1506 like("a$s", qr/a$s/, "Previous test preceded by an 'a'");
1507 substr($s, 260000, 1) = "\x{100}";
1508 $pattern = "$pattern_prefix; qr/$s/;";
1509 $result = fresh_perl($pattern, { 'wide_chars' => 1 } );
1510 if ($? != 0) { # Re-run so as to display STDERR.
1511 fail($pattern);
1512 fresh_perl($pattern, { stderr => 0, verbose => 1 });
1513 }
3f2416ae 1514 like($result, qr/Final program[^X]*\bLEXACT_REQ8\b[^X]*\bLEXACT\b[^X]*\bEXACT\b[^X]*\bEND\b/s,
5cd61b66 1515 "Check that an LEXACT_ONLY node is generated with a \\x{100}");
3f2416ae 1516 like($s, qr/$s/, "Check that LEXACT_REQ8 nodes match");
3363c703
KW
1517 }
1518
1519 {
6d937e95 1520 for my $char (":", uni_to_native("\x{f7}"), "\x{2010}") {
5e4a1da1
KW
1521 my $utf8_char = $char;
1522 utf8::upgrade($utf8_char);
1523 my $display = $char;
1524 $display = display($display);
1525 my $utf8_display = "utf8::upgrade(\"$display\")";
1526
1527 like($char, qr/^$char?$/, "\"$display\" =~ /^$display?\$/");
1528 like($char, qr/^$utf8_char?$/, "my \$p = \"$display\"; utf8::upgrade(\$p); \"$display\" =~ /^\$p?\$/");
1529 like($utf8_char, qr/^$char?$/, "my \$c = \"$display\"; utf8::upgrade(\$c); \"\$c\" =~ /^$display?\$/");
1530 like($utf8_char, qr/^$utf8_char?$/, "my \$c = \"$display\"; utf8::upgrade(\$c); my \$p = \"$display\"; utf8::upgrade(\$p); \"\$c\" =~ /^\$p?\$/");
1531 }
1532 }
1533
4fab19ce
DM
1534 {
1535 # #116148: Pattern utf8ness sticks around globally
1536 # the utf8 in the first match was sticking around for the second
1537 # match
1538
1539 use feature 'unicode_strings';
1540
1541 my $x = "\x{263a}";
1542 $x =~ /$x/;
1543
1544 my $text = "Perl";
1545 ok("Perl" =~ /P.*$/i, '#116148');
1546 }
1547
b8372399
DIM
1548 { # 118297: Mixing up- and down-graded strings in regex
1549 utf8::upgrade(my $u = "\x{e5}");
1550 utf8::downgrade(my $d = "\x{e5}");
1551 my $warned;
1552 local $SIG{__WARN__} = sub { $warned++ if $_[0] =~ /\AMalformed UTF-8/ };
1553 my $re = qr/$u$d/;
1554 ok(!$warned, "no warnings when interpolating mixed up-/downgraded strings in pattern");
1555 my $c = "\x{e5}\x{e5}";
1556 utf8::downgrade($c);
1557 like($c, $re, "mixed up-/downgraded pattern matches downgraded string");
1558 utf8::upgrade($c);
1559 like($c, $re, "mixed up-/downgraded pattern matches upgraded string");
1560 }
4fab19ce 1561
f1e1b256
YO
1562 {
1563 # if we have 87 capture buffers defined then \87 should refer to the 87th.
1564 # test that this is true for 1..100
19438407
S
1565 # Note that this test causes the engine to recurse at runtime, and
1566 # hence use a lot of C stack.
6096c8b5
DM
1567
1568 # Compiling for all 100 nested captures blows the stack under
1569 # clang and ASan; reduce.
1570 my $max_captures = $Config{ccflags} =~ /sanitize/ ? 20 : 100;
1571
f1e1b256 1572 for my $i (1..100) {
6096c8b5
DM
1573 if ($i > $max_captures) {
1574 pass("skipping $i buffers under ASan aa");
1575 pass("skipping $i buffers under ASan aba");
1576 next;
1577 }
31108f3e
YO
1578 my $capture= "a";
1579 $capture= "($capture)" for 1 .. $i;
1580 for my $mid ("","b") {
1581 my $str= "a${mid}a";
1582 my $backref= "\\$i";
1583 eval {
1584 ok($str=~/$capture$mid$backref/,"\\$i works with $i buffers '$str'=~/...$mid$backref/");
1585 1;
1586 } or do {
1587 is("$@","","\\$i works with $i buffers works with $i buffers '$str'=~/...$mid$backref/");
1588 };
1589 }
f1e1b256
YO
1590 }
1591 }
1592
5411a0e5
DM
1593 # this mixture of readonly (not COWable) and COWable strings
1594 # messed up the capture buffers under COW. The actual test results
1595 # are incidental; the issue is was an AddressSanitizer failure
1596 {
1597 my $c ='AB';
1598 my $res = '';
1599 for ($c, 'C', $c, 'DE') {
1600 ok(/(.)/, "COWable match");
1601 $res .= $1;
1602 }
1603 is($res, "ACAD");
1604 }
1605
1606
61965f1d
FC
1607 {
1608 # RT #45667
1609 # /[#$x]/x didn't interpolate the var $x.
1610 my $b = 'cd';
1611 my $s = 'abcd$%#&';
1612 $s =~ s/[a#$b%]/X/g;
1613 is ($s, 'XbXX$XX&', 'RT #45667 without /x');
1614 $s = 'abcd$%#&';
1615 $s =~ s/[a#$b%]/X/gx;
1616 is ($s, 'XbXX$XX&', 'RT #45667 with /x');
1617 }
1618
0cb43d32
FC
1619 {
1620 no warnings "uninitialized";
1621 my @a;
1622 $a[1]++;
1623 /@a/;
1624 pass('no crash with /@a/ when array has nonexistent elems');
1625 }
1626
475b1e90
FC
1627 {
1628 is runperl(prog => 'delete $::{qq-\cR-}; //; print qq-ok\n-'),
1629 "ok\n",
4a2b275c 1630 'deleting *^R does not result in crashes';
d772ce1c 1631 no warnings 'once';
4a2b275c
FC
1632 *^R = *caretRglobwithnoscalar;
1633 "" =~ /(?{42})/;
1634 is $^R, 42, 'assigning to *^R does not result in a crash';
7e68f152
FC
1635 is runperl(
1636 stderr => 1,
1637 prog => 'eval q|'
1638 .' q-..- =~ /(??{undef *^R;q--})(?{42})/; '
1639 .' print qq-$^R\n-'
1640 .'|'
1641 ),
1642 "42\n",
1643 'undefining *^R within (??{}) does not result in a crash';
475b1e90
FC
1644 }
1645
8498497f
KW
1646 SKIP: { # Test literal range end point special handling
1647 unless ($::IS_EBCDIC) {
423df6e4 1648 skip "Valid only for EBCDIC", 24;
8498497f
KW
1649 }
1650
1651 like("\x89", qr/[i-j]/, '"\x89" should match [i-j]');
1652 unlike("\x8A", qr/[i-j]/, '"\x8A" shouldnt match [i-j]');
1653 unlike("\x90", qr/[i-j]/, '"\x90" shouldnt match [i-j]');
1654 like("\x91", qr/[i-j]/, '"\x91" should match [i-j]');
1655
423df6e4
KW
1656 like("\x89", qr/[i-\N{LATIN SMALL LETTER J}]/, '"\x89" should match [i-\N{LATIN SMALL LETTER J}]');
1657 unlike("\x8A", qr/[i-\N{LATIN SMALL LETTER J}]/, '"\x8A" shouldnt match [i-\N{LATIN SMALL LETTER J}]');
1658 unlike("\x90", qr/[i-\N{LATIN SMALL LETTER J}]/, '"\x90" shouldnt match [i-\N{LATIN SMALL LETTER J}]');
1659 like("\x91", qr/[i-\N{LATIN SMALL LETTER J}]/, '"\x91" should match [i-\N{LATIN SMALL LETTER J}]');
1660
1661 like("\x89", qr/[i-\N{U+6A}]/, '"\x89" should match [i-\N{U+6A}]');
1662 unlike("\x8A", qr/[i-\N{U+6A}]/, '"\x8A" shouldnt match [i-\N{U+6A}]');
1663 unlike("\x90", qr/[i-\N{U+6A}]/, '"\x90" shouldnt match [i-\N{U+6A}]');
1664 like("\x91", qr/[i-\N{U+6A}]/, '"\x91" should match [i-\N{U+6A}]');
1665
1666 like("\x89", qr/[\N{U+69}-\N{U+6A}]/, '"\x89" should match [\N{U+69}-\N{U+6A}]');
1667 unlike("\x8A", qr/[\N{U+69}-\N{U+6A}]/, '"\x8A" shouldnt match [\N{U+69}-\N{U+6A}]');
1668 unlike("\x90", qr/[\N{U+69}-\N{U+6A}]/, '"\x90" shouldnt match [\N{U+69}-\N{U+6A}]');
1669 like("\x91", qr/[\N{U+69}-\N{U+6A}]/, '"\x91" should match [\N{U+69}-\N{U+6A}]');
1670
8498497f
KW
1671 like("\x89", qr/[i-\x{91}]/, '"\x89" should match [i-\x{91}]');
1672 like("\x8A", qr/[i-\x{91}]/, '"\x8A" should match [i-\x{91}]');
1673 like("\x90", qr/[i-\x{91}]/, '"\x90" should match [i-\x{91}]');
1674 like("\x91", qr/[i-\x{91}]/, '"\x91" should match [i-\x{91}]');
1675
1676 # Need to use eval, because tries to compile on ASCII platforms even
1677 # though the tests are skipped, and fails because 0x89-j is an illegal
1678 # range there.
6d937e95
KW
1679 like("\x89", eval 'qr/[\x{89}-j]/', '"\x89" should match [\x{89}-j]');
1680 like("\x8A", eval 'qr/[\x{89}-j]/', '"\x8A" should match [\x{89}-j]');
1681 like("\x90", eval 'qr/[\x{89}-j]/', '"\x90" should match [\x{89}-j]');
1682 like("\x91", eval 'qr/[\x{89}-j]/', '"\x91" should match [\x{89}-j]');
8498497f 1683 }
d6ef1678 1684
749e076f
KW
1685 # These are based on looking at the code in regcomp.c
1686 # We don't look for specific code, just the existence of an SSC
1687 foreach my $re (qw( qr/a?c/
1688 qr/a?c/i
1689 qr/[ab]?c/
1690 qr/\R?c/
1691 qr/\d?c/d
1692 qr/\w?c/l
1693 qr/\s?c/a
b35552de 1694 qr/[[:lower:]]?c/u
749e076f 1695 )) {
d7919c41
FC
1696 SKIP: {
1697 skip "no re-debug under miniperl" if is_miniperl;
749e076f
KW
1698 my $prog = <<"EOP";
1699use re qw(Debug COMPILE);
1700$re;
1701EOP
20e5bab4 1702 fresh_perl_like($prog, qr/synthetic stclass/, { stderr=>1 }, "$re generates a synthetic start class");
d7919c41 1703 }
749e076f
KW
1704 }
1705
86897aa6
KW
1706 {
1707 like "\x{AA}", qr/a?[\W_]/d, "\\W with /d synthetic start class works";
1708 }
1709
6d937e95
KW
1710 SKIP: {
1711 skip("Tests are ASCII-centric, some would fail on EBCDIC", 12) if $::IS_EBCDIC;
1712
e8ea8356
JH
1713 # Verify that the very last Latin-1 U+00FF
1714 # (LATIN SMALL LETTER Y WITH DIAERESIS)
1715 # and its UPPER counterpart (U+0178 which is pure Unicode),
1716 # and likewise for the very first pure Unicode
1717 # (LATIN CAPITAL LETTER A WITH MACRON) fold-match properly,
1718 # and there are no off-by-one logic errors in the transition zone.
1719
1720 ok("\xFF" =~ /\xFF/i, "Y WITH DIAERESIS l =~ l");
1721 ok("\xFF" =~ /\x{178}/i, "Y WITH DIAERESIS l =~ u");
1722 ok("\x{178}" =~ /\xFF/i, "Y WITH DIAERESIS u =~ l");
1723 ok("\x{178}" =~ /\x{178}/i, "Y WITH DIAERESIS u =~ u");
86897aa6 1724
e8ea8356
JH
1725 # U+00FF with U+05D0 (non-casing Hebrew letter).
1726 ok("\xFF\x{5D0}" =~ /\xFF\x{5D0}/i, "Y WITH DIAERESIS l =~ l");
1727 ok("\xFF\x{5D0}" =~ /\x{178}\x{5D0}/i, "Y WITH DIAERESIS l =~ u");
1728 ok("\x{178}\x{5D0}" =~ /\xFF\x{5D0}/i, "Y WITH DIAERESIS u =~ l");
1729 ok("\x{178}\x{5D0}" =~ /\x{178}\x{5D0}/i, "Y WITH DIAERESIS u =~ u");
1730
1731 # U+0100.
1732 ok("\x{100}" =~ /\x{100}/i, "A WITH MACRON u =~ u");
1733 ok("\x{100}" =~ /\x{101}/i, "A WITH MACRON u =~ l");
1734 ok("\x{101}" =~ /\x{100}/i, "A WITH MACRON l =~ u");
1735 ok("\x{101}" =~ /\x{101}/i, "A WITH MACRON l =~ l");
1736 }
0b2c2a84 1737
8373491a
KW
1738 {
1739 use utf8;
1740 ok("abc" =~ /a\85b\85c/x, "NEL is white-space under /x");
1741 }
1742
412f55bb
KW
1743 {
1744 ok('a(b)c' =~ qr(a\(b\)c), "'\\(' is a literal in qr(...)");
1745 ok('a[b]c' =~ qr[a\[b\]c], "'\\[' is a literal in qr[...]");
1746 ok('a{3}c' =~ qr{a\{3\}c}, # Only failed when { could be a meta
1747 "'\\{' is a literal in qr{...}, where it could be a quantifier");
1748
1749 # This one is for completeness
1750 ok('a<b>c' =~ qr<a\<b\>c>, "'\\<' is a literal in qr<...>)");
1751 }
1752
b6e093f2
KW
1753 { # Was getting optimized into EXACT (non-folding node)
1754 my $x = qr/[x]/i;
1755 utf8::upgrade($x);
1756 like("X", qr/$x/, "UTF-8 of /[x]/i matches upper case");
1757 }
1758
59206727
KW
1759 { # Special handling of literal-ended ranges in [...] was breaking this
1760 use utf8;
1761 like("ÿ", qr/[ÿ-ÿ]/, "\"ÿ\" should match [ÿ-ÿ]");
1762 }
405dffcb
KW
1763
1764 { # [perl #123539]
1765 like("TffffffffffffTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT5TTTTTTTTTTTTTTTTTTTTTTTTT3TTgTTTTTTTTTTTTTTTTTTTTT2TTTTTTTTTTTTTTTTTTTTTTTHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHiHHHHHHHfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff&ffff", qr/TffffffffffffTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT5TTTTTTTTTTTTTTTTTTTTTTTTT3TTgTTTTTTTTTTTTTTTTTTTTT2TTTTTTTTTTTTTTTTTTTTTTTHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHiHHHHHHHfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff&ffff/il, "");
1766 like("TffffffffffffT\x{100}TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT5TTTTTTTTTTTTTTTTTTTTTTTTT3TTgTTTTTTTTTTTTTTTTTTTTT2TTTTTTTTTTTTTTTTTTTTTTTHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHiHHHHHHHfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff&ffff", qr/TffffffffffffT\x{100}TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT5TTTTTTTTTTTTTTTTTTTTTTTTT3TTgTTTTTTTTTTTTTTTTTTTTT2TTTTTTTTTTTTTTTTTTTTTTTHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHiHHHHHHHfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff&ffff/il, "");
1767 }
1768
56f81afc
HS
1769 { # [perl #123604]
1770 my($s, $x, @x) = ('abc', 'a', 'd');
1771 my $long = 'b' x 2000;
1772 my $eval = q{$s =~ m{$x[bbb]c} ? 1 : 0};
1773 $eval =~ s{bbb}{$long};
1774 my $match = eval $eval;
1775 ok(1, "did not crash");
1776 ok($match, "[bbb...] resolved as character class, not subscript");
1777 }
8a6d8ec6
HS
1778
1779 { # [perl #123755]
1780 for my $pat ('(??', '(?P', '(?i-') {
1781 eval qq{ qr/$pat/ };
1782 ok(1, "qr/$pat/ did not crash");
1783 eval qq{ qr/${pat}\x{123}/ };
1784 my $e = $@;
1785 like($e, qr{\x{123}},
1786 "qr/${pat}x/ shows x in error even if it's a wide character");
1787 }
1788 }
b3725d49
HS
1789
1790 {
1791 # Expect one of these sizes to cause overflow and wrap to negative
1792 for my $bits (32, 64) {
1793 my $wrapneg = 2 ** ($bits - 2) * 3;
1794 for my $sign ('', '-') {
1795 my $pat = sprintf "qr/(?%s%u)/", $sign, $wrapneg;
1796 eval $pat;
1797 ok(1, "big backref $pat did not crash");
1798 }
1799 }
1800 }
65651c16 1801 {
65651c16
YO
1802 # Test that we handle qr/\8888888/ and variants without an infinite loop,
1803 # we use a test within a test so we can todo it, and make sure we don't
1804 # infinite loop our tests.
1805 # NOTE - Do not put quotes in the code!
1806 # NOTE - We have to triple escape the backref in the pattern below.
1807 my $code='
3d7c117d 1808 BEGIN{require q(./test.pl);}
65651c16
YO
1809 watchdog(3);
1810 for my $len (1 .. 20) {
1811 my $eights= q(8) x $len;
1812 eval qq{ qr/\\\\$eights/ };
1813 }
1814 print q(No infinite loop here!);
1815 ';
1816 fresh_perl_is($code, "No infinite loop here!", {},
1817 "test that we handle things like m/\\888888888/ without infinite loops" );
1818 }
4a0c7558 1819
521a1e96 1820 SKIP:
22b433ef
KW
1821 { # Test that we handle some malformed UTF-8 without looping [perl
1822 # #123562]
521a1e96 1823 skip "no Encode", 1 if is_miniperl;
22b433ef 1824 my $code='
3d7c117d 1825 BEGIN{require q(./test.pl);}
22b433ef 1826 use Encode qw(_utf8_on);
95c6a212
KW
1827 # \x80 and \x41 are continuation bytes in their respective
1828 # character sets
1829 my $malformed = (ord("A") == 65) ? "a\x80\n" : "a\x41\n";
1830 utf8::downgrade($malformed);
22b433ef
KW
1831 _utf8_on($malformed);
1832 watchdog(3);
1833 $malformed =~ /(\n\r|\r)$/;
1834 print q(No infinite loop here!);
1835 ';
1836 fresh_perl_like($code, qr/Malformed UTF-8 character/, {},
1837 "test that we handle some UTF-8 malformations without looping" );
1838 }
1839
4a0c7558
HS
1840 {
1841 # [perl #123843] hits SEGV trying to compile this pattern
1842 my $match;
1843 eval q{ ($match) = ("xxyxxyxy" =~ m{(x+(y(?1))*)}) };
1844 ok(1, "compiled GOSUB in CURLYM ok");
1845 is($match, 'xxyxxyx', "matched GOSUB in CURLYM");
1846 }
e92b41c6
HS
1847
1848 {
1849 # [perl #123852] doesn't avoid all the capture-related work with
1850 # //n, leading to possible memory corruption
1851 eval q{ qr{()(?1)}n };
1852 my $error = $@;
1853 ok(1, "qr{()(?1)}n didn't crash");
1854 like($error, qr{Reference to nonexistent group},
1855 'gave appropriate error for qr{()(?1)}n');
1856 }
9457bb3f
KW
1857
1858 {
1859 # [perl #126406] panic with unmatchable quantifier
1860 my $code='
1861 no warnings "regexp";
1862 "" =~ m/(.0\N{6,0}0\N{6,0}000000000000000000000000000000000)/;
1863 ';
1864 fresh_perl_is($code, "", {},
1865 "perl [#126406] panic");
1866 }
ba6840fb 1867 {
401a8022
YO
1868 my $bug="[perl #126182]"; # test for infinite pattern recursion
1869 for my $tuple (
401a8022
YO
1870 [ 'q(a)=~/(.(?2))((?<=(?=(?1)).))/', "died", "look ahead left recursion fails fast" ],
1871 [ 'q(aa)=~/(?R)a/', "died", "left-recursion fails fast", ],
1872 [ 'q(bbaa)=~/(?&x)(?(DEFINE)(?<x>(?&y)*a)(?<y>(?&x)*b))/',
1873 "died", "inter-cyclic optional left recursion dies" ],
1874 [ 'q(abc) =~ /a((?1)?)c/', "died", "optional left recursion dies" ],
1875 [ 'q(abc) =~ /a((?1)??)c/', "died", "min mod left recursion dies" ],
1876 [ 'q(abc) =~ /a((?1)*)c/', "died", "* left recursion dies" ],
1877 [ 'q(abc) =~ /a((?1)+)c/', "died", "+ left recursion dies" ],
1878 [ 'q(abc) =~ /a((?1){0,3})c/', "died", "{0,3} left recursion fails fast" ],
1879
1880 [ 'q(aaabbb)=~/a(?R)?b/', "matched", "optional self recursion works" ],
ce12e254
YO
1881 [ '"((5maa-maa)(maa-3maa))" =~ /(\\\\((?:[^()]++|(?0))*+\\\\))/', "matched",
1882 "recursion and possessive captures", "((5maa-maa)(maa-3maa))"],
1883 [ '"((5maa-maa)(maa-3maa))" =~ /(\\\\((?:[^()]++|(?1))*+\\\\))/', "matched",
1884 "recursion and possessive captures", "((5maa-maa)(maa-3maa))"],
1885 [ '"((5maa-maa)(maa-3maa))" =~ /(\\\\((?:[^()]+|(?0))*\\\\))/', "matched",
1886 "recursion and possessive captures", "((5maa-maa)(maa-3maa))"],
1887 [ '"((5maa-maa)(maa-3maa))" =~ /(\\\\((?:[^()]+|(?1))*\\\\))/', "matched",
1888 "recursion and possessive captures", "((5maa-maa)(maa-3maa))"],
401a8022 1889 ) {
ce12e254 1890 my ($expr, $expect, $test_name, $cap1)= @$tuple;
401a8022
YO
1891 # avoid quotes in this code!
1892 my $code='
3d7c117d 1893 BEGIN{require q(./test.pl);}
401a8022 1894 watchdog(3);
ce12e254
YO
1895 my $status= eval(q{ !(' . $expr . ') ? q(failed) : ' .
1896 ($cap1 ? '($1 ne q['.$cap1.']) ? qq(badmatch:$1) : ' : '') .
1897 ' q(matched) })
1898 || ( ( $@ =~ /Infinite recursion/ ) ? qq(died) : q(strange-death) );
401a8022
YO
1899 print $status;
1900 ';
1901 fresh_perl_is($code, $expect, {}, "$bug - $test_name" );
1902 }
ba6840fb 1903 }
8df928d2
YO
1904 {
1905 fresh_perl_is('
1906 BEGIN{require q(test.pl);}
1907 watchdog(3);
1908 $SIG{ALRM} = sub {print "Timeout\n"; exit(1)};
1909 alarm 1;
1910 $_ = "a" x 1000 . "b" x 1000 . "c" x 1000;
1911 /.*a.*b.*c.*[de]/;
1912 ',"Timeout",{},"Test Perl 73464")
1913 }
33bc5d3d
KW
1914
1915 { # [perl #128686], crashed the the interpreter
1916 my $AE = chr utf8::unicode_to_native(0xC6);
1917 my $ae = chr utf8::unicode_to_native(0xE6);
1918 my $re = qr/[$ae\s]/i;
1919 ok($AE !~ $re, '/[\xE6\s]/i doesn\'t match \xC6 when not in UTF-8');
1920 utf8::upgrade $AE;
1921 ok($AE =~ $re, '/[\xE6\s]/i matches \xC6 when in UTF-8');
1922 }
1923
6d314683
YO
1924 {
1925 is(0+("\n" =~ m'\n'), 1, q|m'\n' should interpolate escapes|);
da42332b
YO
1926 }
1927
1928 {
2c2284d8
KW
1929 my $str = "a\xB6";
1930 ok( $str =~ m{^(a|a\x{b6})$}, "fix [perl #129950] - latin1 case" );
da42332b 1931 utf8::upgrade($str);
2c2284d8 1932 ok( $str =~ m{^(a|a\x{b6})$}, "fix [perl #129950] - utf8 case" );
da42332b 1933 }
5585e758
YO
1934 {
1935 my $got= run_perl( switches => [ '-l' ], prog => <<'EOF_CODE' );
1936 my $died= !eval {
1937 $_=qq(ab);
1938 print;
1939 my $p=qr/(?{ s!!x! })/;
1940 /$p/;
1941 print;
1942 /a/;
1943 /$p/;
1944 print;
1945 /b/;
1946 /$p/;
1947 print;
1948 //;
1949 1;
1950 };
1951 $error = $died ? ($@ || qq(Zombie)) : qq(none);
1952 print $died ? qq(died) : qq(lived);
1953 print qq(Error: $@);
1954EOF_CODE
1955 my @got= split /\n/, $got;
1956 is($got[0],"ab","empty pattern in regex codeblock: got expected start string");
1957 is($got[1],"xab",
1958 "empty pattern in regex codeblock: first subst with no last-match worked right");
1959 is($got[2],"xxb","empty pattern in regex codeblock: second subst worked right");
1960 is($got[3],"xxx","empty pattern in regex codeblock: third subst worked right");
1961 is($got[4],"died","empty pattern in regex codeblock: died as expected");
1962 like($got[5],qr/Error: Infinite recursion via empty pattern/,
1963 "empty pattern in regex codeblock: produced the right exception message" );
1964 }
7d79ca09
DM
1965
1966 # This test is based on the one directly above, which happened to
1967 # leak. Repeat the test, but stripped down to the bare essentials
1968 # of the leak, which is to die while executing a regex which is
1969 # already the current regex, thus causing the saved outer set of
1970 # capture offsets to leak. The test itself doesn't do anything
1971 # except sit around hoping not to be triggered by ASan
1972 {
1973 eval {
1974 my $s = "abcd";
1975 $s =~ m{([abcd]) (?{ die if $1 eq 'd'; })}gx;
1976 $s =~ //g;
1977 $s =~ //g;
1978 $s =~ //g;
1979 };
1980 pass("call to current regex doesn't leak");
1981 }
1982
1983
1984
8faf4f30
HS
1985 {
1986 # [perl #130495] /x comment skipping stopped a byte short, leading
1987 # to assertion failure or 'malformed utf-8 character" warning
1988 fresh_perl_is(
98c155b5 1989 "use utf8; m{a#\x{124}}x", '', {wide_chars => 1},
8faf4f30
HS
1990 '[perl #130495] utf-8 character at end of /x comment should not misparse',
1991 );
1992 }
871e132c
HS
1993 {
1994 # [perl #130522] causes out-of-bounds read detected by clang with
1995 # address=sanitized when length of the STCLASS string is greater than
1996 # length of target string.
1997 my $re = qr{(?=\0z)\0?z?$}i;
1998 my($yes, $no) = (1, "");
1999 for my $test (
2000 [ $no, undef, '<undef>' ],
2001 [ $no, '', '' ],
2002 [ $no, "\0", '\0' ],
2003 [ $yes, "\0z", '\0z' ],
2004 [ $no, "\0z\0", '\0z\0' ],
2005 [ $yes, "\0z\n", '\0z\n' ],
2006 ) {
2007 my($result, $target, $disp) = @$test;
2008 no warnings qw/uninitialized/;
2009 is($target =~ $re, $result, "[perl #130522] with target '$disp'");
2010 }
2011 }
2dfc11ec
HS
2012 {
2013 # [perl #129377] backref to an unmatched capture should not cause
2014 # reading before start of string.
2015 SKIP: {
2016 skip "no re-debug under miniperl" if is_miniperl;
2017 my $prog = <<'EOP';
2018use re qw(Debug EXECUTE);
2019"x" =~ m{ () y | () \1 }x;
2020EOP
2021 fresh_perl_like($prog, qr{
2022 \A (?! .* ^ \s+ - )
2023 }msx, { stderr => 1 }, "Offsets in debug output are not negative");
2024 }
2025 }
dc0dad9b
TC
2026 {
2027 # buffer overflow
495a482d
DM
2028
2029 # This test also used to leak - fixed by the commit which added
2030 # this line.
2031
dc0dad9b
TC
2032 fresh_perl_is("BEGIN{\$^H=0x200000}\ns/[(?{//xx",
2033 "Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE (?{/ at (eval 1) line 1.\n",
2034 {}, "buffer overflow for regexp component");
2035 }
d2ba660a
TC
2036 {
2037 # [perl #129281] buffer write overflow, detected by ASAN, valgrind
d2ba660a
TC
2038 fresh_perl_is('/0(?0)|^*0(?0)|^*(^*())0|/', '', {}, "don't bump whilem_c too much");
2039 }
6c4f4eb1
TC
2040 {
2041 # RT #131893 - fails with ASAN -fsanitize=undefined
2042 fresh_perl_is('qr/0(0?(0||00*))|/', '', {}, "integer overflow during compilation");
2043 }
bb152a4b
DM
2044
2045 {
2046 # RT #131575 intuit skipping back from the end to find the highest
2047 # possible start point, was potentially hopping back beyond pos()
2048 # and crashing by calling fbm_instr with a negative length
2049
2050 my $text = "=t=\x{5000}";
2051 pos($text) = 3;
2052 ok(scalar($text !~ m{(~*=[a-z]=)}g), "RT #131575");
2053 }
4dc12118
YO
2054 {
2055 fresh_perl_is('"AA" =~ m/AA{1,0}/','',{},"handle OPFAIL insert properly");
2056 }
12453e29
YO
2057 {
2058 fresh_perl_is('$_="0\x{1000000}";/^000?\0000/','',{},"dont throw assert errors trying to fbm past end of string");
2059 }
f8fb8615
KW
2060 { # [perl $132227]
2061 fresh_perl_is("('0ba' . ('ss' x 300)) =~ m/0B\\N{U+41}" . $sharp_s x 150 . '/i and print "1\n"', 1,{},"Use of sharp s under /di that changes to /ui");
f6cb9e97
KW
2062
2063 # A variation, but as far as khw knows not part of 132227
2064 fresh_perl_is("'0bssa' =~ m/0B" . $sharp_s . "\\N{U+41}" . '/i and print "1\n"', 1,{},"Use of sharp s under /di that changes to /ui");
f8fb8615 2065 }
5e20fb07
KW
2066 { # [perl $132164]
2067 fresh_perl_is('m m0*0+\Rm', "",{},"Undefined behavior in address sanitizer");
2068 }
d0d8d0cb 2069 { # [perl #133642]
2abbd513
KW
2070 fresh_perl_is('no warnings "experimental::vlb";
2071 m/((?<=(0?)))/', "",{},"Was getting 'Double free'");
d0d8d0cb 2072 }
d4c456e3
TC
2073 { # [perl #133782]
2074 # this would panic on DEBUGGING builds
2075 fresh_perl_is(<<'CODE', "ok\nok\n",{}, 'Bad length magic was left on $^R');
2076while( "\N{U+100}bc" =~ /(..?)(?{$^N})/g ) {
2077 print "ok\n" if length($^R)==length("$^R");
2078}
2079CODE
2080 }
e0814905 2081 { # [perl #133871], ASAN/valgrind out-of-bounds access
e0814905
KW
2082 fresh_perl_like('qr/(?|(())|())|//', qr/syntax error/, {}, "[perl #133871]");
2083 }
81d11450
KW
2084 { # [perl #133871], ASAN/valgrind out-of-bounds access
2085 fresh_perl_like('qr/\p{nv:NAnq}/', qr/Can't find Unicode property definition/, {}, "GH #17367");
2086 }
9f16475a
KW
2087 { # GH #17370, ASAN/valgrind out-of-bounds access
2088 fresh_perl_like('qr/\p{nv:qnan}/', qr/Can't find Unicode property definition/, {}, "GH #17370");
2089 }
aff71c57
KW
2090 { # GH #17371, segfault
2091 fresh_perl_like('qr/\p{nv=\\\\\}(?0)|\337ss|\337ss//', qr/Unicode property wildcard not terminated/, {}, "GH #17371");
2092 }
bf18a246
KW
2093 { # GH #17384, ASAN/valgrind out-of-bounds access
2094 fresh_perl_like('"q0" =~ /\p{__::Is0}/', qr/Unknown user-defined property name \\p\{__::Is0}/, {}, "GH #17384");
2095 }
81d11450 2096
f912df25 2097 SKIP:
bfa9f5ee 2098 { # [perl #133921], segfault
f912df25
KW
2099 skip "Not valid for EBCDIC", 5 if $::IS_EBCDIC;
2100
bfa9f5ee
KW
2101 fresh_perl_is('qr\ 40||ß+p00000F00000ù\Q00000ÿ00000x00000x0c0e0\Qx0\Qx0\x{0c!}\;\;î0\x\0ÿÿÿþ\0\0\0ù\Q`\Qx`\0\ 1{0c!}\ 1e;\0\0\0ù\ò`\Qm`\x{0c!}\;\;îçÿ \0\7fç\0\0\0!\0F\ 5\0\0/;îçÿù\Q\0\ 1\0\0x\10ÿÿÿÿ\0\0\0ù\0\0\0\7f`x{0c!}\ 1e;\0\0\0ù\Q`\Qx`\x{c!}\;\;îç!}\;îçÿù\Q\87 \x\0ÿÿÿÿ\0\0>=\Qx`\Qx`\0\0ù\ò`\Qx`\x{0c!};\;îçÿ \0F\ 5\0n0t0\0c \0\80\ 1d;t \0\0\0ù \0\7fç\80\0\0!00000000000000000000000m/00000000000000000000\ e00000000000m/\10\10\10\10\x{){} \10\10\10\10)|\10\10\ 4i', "", {}, "[perl #133921]");
2102 fresh_perl_is('\ 4|ß+W0ü0r0\Qx0\Qx0x0c0G00000000000000000O000000000x0x0x0c!}\;îçÿù\Q0 \x\0ÿÿÿÿ\0\0\0ù\Q`\Qx`\0\ 1{0d ;\0\0\0ù\ò`\Qm`\x{0c!}\;\;îçÿ \0\7fç\0\0\0!\0F\ 5\0\0/;îçÿù\Q\0\ 1\0\0x\10ÿÿÿÿ\0\0\0ù\0\0\0\7f`x{0c!}\ 1;\0\0\0ù\Q`\Qq`\x{c!}\;\;îç!}\;îçÿù\Q\87 \x\0ÿÿÿÿ\0\0>=\Qx`\Qx`\0\0ù\ò`\Qx`\x{0c!};\;îçÿ \00000000F\ 5\0m0t0\0c \0\80\ 1d;t \0\0\0ù \0\7fç\80\0\0!00000000000000000000000m/00000000000000000000\ e00000000000m/\10\10\10\10\x{){} \10\10\10\10)|\10\10\ 4\ 4i', "", {}, "[perl #133921]");
bf848a12
KW
2103
2104fresh_perl_is('s\ 4|ß+W0ü0f0\Qx0\Qx0x0c0G0xgive0000000000000O0h\8d000x0 \xòÿÿÿ\0\0ù\Q`\Q
2105
2106\1a
2107
2108
2109 ç
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122\ 5
2123
2124
2125x{0c!}\;\;çÿ \0\7fq0/i0/!\0F\ 5\0\0/;îçÿù\Q\0\ 1\0\0x\10ÿÿÿÿ\0\0\0ù\0\0\0\7f`x{0c!}\ 1e;\0\0\0ù\Q`\Qx`\x{0c!}\;ÿÿÿÿ!}\;îçÿù\Q\87\ 1\x\0ÿÿÿÿ\0\0>=\Qx`\Qx`\0\0ù\ò`ÿ\0\0>=\Qx`\Qx`\0\0ù\ò`\Qx`\x{0c!};\;îçÿ \0u00000F\ 5\0000t0\0p \0\80\ 1d? \0\0\0ù \0\7fç\80\0\0!00000000000000000000000m/00000000000000000000\ e00000000000m/0\0\10\10\10\\0\0\ 1\0} \10\10\10\10)|\10\10\ 4\ 4i', "", {}, "[perl #133921]");
2126
2127 fresh_perl_is('\ 4a aú\0\0úv sWtrt\10\0\ó||ß+W\ eü\16ef\0ù\Qx`\Qx`\x{1c!gGnuc given1111111111111O1111each\8d111\jx` \x\0òÿÿÿ\0\0\0ù\Qx`\Q
2128
2129
2130
2131
2132
2133 ç
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146\ 5
2147
2148
2149x{1c!}\;\;îçÿp \0\7fqr/elsif/!\0eF\ 5\0\0/;îçÿù\Q\0\ 1\0\0x\10ÿÿÿÿ\0\0\0ùHQx\0\0\0\7f`Lx{1c!}\ 1e;\0\0\0ù\Qx`\Qx`\x{1c!}\;ÿÿÿÿc!}\;îçÿù\Qx\87\ 1\x\0ÿÿÿÿ\0\0>=\Qx`\Qx`\0\0ù\òx`ÿ\0\0>=\Qx`\Qx`\0\0ù\òx`\Qx`\x{1c!}8;\;îçÿp \0unshifteF\ 5\0normat0\0cmp \0\80\ 1d?not \0\0\0ùp \0\7fç\80\0\0!0000000000000000000000000m/000000000000000000000\ e00000000000m/0R\0\10\10\10\\0\0\ 1\0} \10\10\10\10)|\10\10\ 4\10\10\10\10\10\ 1\aï||K??\8fp\80¿ÿÿfúd{\\ e{\ 4gri\ 4\ 4{\x{1x/}\0 ð¹NuntiÀh', "", {}, "[perl #133921]");
88b5a12c
KW
2150
2151 fresh_perl_is('s\ 4|ß+W0ü0f0\Qx0\Qx0x0c0g0c 000n0000000000000O0h\8d000x0 \xòÿÿÿ\0\0ù\Q`\Q
2152
2153
2154
2155
2156
2157 ç
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170\ 5
2171
2172
2173x{0c!}\;\;îçÿ \0\7f/0f/!\0F\ 5\0\0/;îçÿù\Q\0\ 1\0\0x\10ÿÿÿÿ\0\0\0ù\0\0\0\7f`x{0c!}\ 1;\0\0\0ù\Q`\Qx`\x{0c!}\;ÿÿÿÿ!}\;îçÿù\Q\87\ 1\x\0ÿÿÿÿ\0\0>=\Qx`\Qx`\0\0ù\ò`ÿ\0\0>=\Qx`\Qx`\0\0ù\ò`\Qx`\x{0c!};\;îçÿ \0000t0F\ 5\0000t0\0p \0\80\ 1d?n \0\0\0ù \0\7fç\80\0\0!00000000000000000000000m/00000000000000000000\ e00000000000m/\0\10\10\10\\0\0\ 1\0} \10\10\10\10)|\10\10\ 4\ 4i', "", {}, "[perl #133933]");
bfa9f5ee 2174 }
bb152a4b 2175
96d9b6ff
KW
2176 { # perl #133998]
2177 fresh_perl_is('print "\x{110000}" =~ qr/(?l)|[^\S\pC\s]/', 1, {},
2178 '/[\S\s]/l works');
2179 }
2180
0a5ed81e
KW
2181 { # perl #133995]
2182 use utf8;
3fb2d820
KW
2183 fresh_perl_is('"έδωσαν ελληνικήვე" =~ m/[^0](?=0)0?/', "",
2184 {wide_chars => 1},
2185 '[^0] doesnt crash on UTF-8 target string');
0a5ed81e
KW
2186 }
2187
f339d50e
KW
2188 { # [perl #133992] This is a tokenizer bug of parsing a pattern
2189 fresh_perl_is(q:$z = do {
2190 use utf8;
2191 "q!Ñ\82еÑ\81Ñ\82! =~ m'"
2192 };
2193 $z .= 'è(?#\84';
2194 $z .= "'";
2195 eval $z;:, "", {}, 'foo');
2196 }
2197
439a3bfe
KW
2198 { # [perl #134325]
2199 my $quote="\\Q";
2200 my $back="\\\\";
2201 my $ff="\xff";
2202 my $s = sprintf "/\\1|(|%s)%s%s /i",
2203 $quote x 8 . $back x 69,
2204 $quote x 5 . $back x 4,
2205 $ff x 48;
d2b59011 2206 like(fresh_perl("$s", { stderr => 1, }), qr/Unmatched \(/);
439a3bfe
KW
2207 }
2208
f58502b2
KW
2209 { # GitHub #17196, caused assertion failure
2210 fresh_perl_like('("0" x 258) =~ /(?l)0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000/',
2211 qr/^$/,
2212 {},
2213 "Assertion failure with /l exact string longer than a single node");
2214 }
2215
2df0dd21
KW
2216SKIP:
2217 { # [perl #134334], Assertion failure
2218 my $utf8_locale = find_utf8_ctype_locale();
2219 skip "no UTF-8 locale available" unless $utf8_locale;
2220 fresh_perl_like("use POSIX; POSIX::setlocale(&LC_CTYPE, '$utf8_locale'); 'ssss' =~ /\xDF+?sX/il;",
2221 qr/^$/,
2222 {},
2223 "Assertion failure matching /il on single char folding to multi");
2224 }
2225
34924db0
KW
2226 { # Test ANYOFHs
2227 my $pat = qr/[\x{4000001}\x{4000003}\x{4000005}]+/;
2228 unlike("\x{4000000}", $pat, "4000000 isn't in pattern");
2229 like("\x{4000001}", $pat, "4000001 is in pattern");
2230 unlike("\x{4000002}", $pat, "4000002 isn't in pattern");
2231 like("\x{4000003}", $pat, "4000003 is in pattern");
2232 unlike("\x{4000004}", $pat, "4000004 isn't in pattern");
2233 like("\x{4000005}", $pat, "4000005 is in pattern");
2234 unlike("\x{4000006}", $pat, "4000006 isn't in pattern");
53d42e43
KW
2235
2236 # gh #17319
2237 $pat = qr/[\N{U+200D}\N{U+2000}]()/;
2238 unlike("\x{1FFF}", $pat, "1FFF isn't in pattern");
2239 like("\x{2000}", $pat, "2000 is in pattern");
2240 unlike("\x{2001}", $pat, "2001 isn't in pattern");
2241 unlike("\x{200C}", $pat, "200C isn't in pattern");
2242 like("\x{200D}", $pat, "200 is in pattern");
2243 unlike("\x{200E}", $pat, "200E isn't in pattern");
34924db0
KW
2244 }
2245
af6880c9
HS
2246 # gh17490: test recursion check
2247 {
2248 my $eval = '(?{1})';
2249 my $re = sprintf '(?&FOO)(?(DEFINE)(?<FOO>%sfoo))', $eval x 20;
2250 my $result = eval qq{"foo" =~ /$re/};
2251 is($@ // '', '', "many evals did not die");
2252 ok($result, "regexp correctly matched");
2253 }
2254
089ad25d 2255 # gh16947: test regexp corruption (GOSUB)
3bc2a780
HS
2256 {
2257 fresh_perl_is(q{
2258 'xy' =~ /x(?0)|x(?|y|y)/ && print 'ok'
089ad25d
HS
2259 }, 'ok', {}, 'gh16947: test regexp corruption (GOSUB)');
2260 }
2261 # gh16947: test fix doesn't break SUSPEND
2262 {
2263 fresh_perl_is(q{ 'sx' =~ m{ss++}i; print 'ok' },
2264 'ok', {}, "gh16947: test fix doesn't break SUSPEND");
3bc2a780
HS
2265 }
2266
84281c31
A
2267} # End of sub run_tests
2268
22691;
10ef0e30
HS
2270
2271#
2272# ex: set ts=8 sts=4 sw=4 et:
2273#