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