3 # This is a home for regular expression tests that don't fit into
4 # the format supported by re/regexp.t. If you want to add a test
5 # that does fit that format, add it to re/re_tests, not here.
18 require Config; import Config;
19 require './test.pl'; require './charset_tools.pl';
20 require './loc_tools.pl';
21 set_up_inc('../lib', '.', '../ext/re');
23 skip_all('no re module') unless defined &DynaLoader::boot_DynaLoader;
24 skip_all_without_unicode_tables();
26 plan tests => 800; # Update this when adding/deleting tests.
28 run_tests() unless caller;
37 (my $x_pretty = $x) =~ s/\n/\\n/g;
39 ok $x =~ /^abc/, qq ["$x_pretty" =~ /^abc/];
40 ok $x !~ /^def/, qq ["$x_pretty" !~ /^def/];
42 # used to be a test for $*
43 ok $x =~ /^def/m, qq ["$x_pretty" =~ /^def/m];
45 ok(!($x =~ /^xxx/), qq ["$x_pretty" =~ /^xxx/]);
46 ok(!($x !~ /^abc/), qq ["$x_pretty" !~ /^abc/]);
48 ok $x =~ /def/, qq ["$x_pretty" =~ /def/];
49 ok(!($x !~ /def/), qq ["$x_pretty" !~ /def/]);
51 ok $x !~ /.def/, qq ["$x_pretty" !~ /.def/];
52 ok(!($x =~ /.def/), qq ["$x_pretty" =~ /.def/]);
54 ok $x =~ /\ndef/, qq ["$x_pretty" =~ /\\ndef/];
55 ok(!($x !~ /\ndef/), qq ["$x_pretty" !~ /\\ndef/]);
60 ok /^([0-9][0-9]*)/, qq [\$_ = '$_'; /^([0-9][0-9]*)/];
65 ok /(a*b*)(c*)/ && $1 eq 'aaabbb' && $2 eq 'ccc',
66 qq [\$_ = '$_'; /(a*b*)(c*)/];
67 ok /(a+b+c+)/ && $1 eq 'aaabbbccc', qq [\$_ = '$_'; /(a+b+c+)/];
68 unlike($_, qr/a+b?c+/, qq [\$_ = '$_'; /a+b?c+/]);
71 ok /a+b?c+/, qq [\$_ = '$_'; /a+b?c+/];
72 ok /a*b?c*/, qq [\$_ = '$_'; /a*b?c*/];
75 ok /a*b?c*/, qq [\$_ = '$_'; /a*b?c*/];
76 unlike($_, qr/a*b+c*/, qq [\$_ = '$_'; /a*b+c*/]);
79 ok /bcd|xyz/, qq [\$_ = '$_'; /bcd|xyz/];
80 ok /xyz|bcd/, qq [\$_ = '$_'; /xyz|bcd/];
81 ok m|bc/*d|, qq [\$_ = '$_'; m|bc/*d|];
82 ok /^$_$/, qq [\$_ = '$_'; /^\$_\$/];
86 # used to be a test for $*
87 ok "ab\ncd\n" =~ /^cd/m, q ["ab\ncd\n" =~ /^cd/m];
91 our %XXX = map {($_ => $_)} 123, 234, 345;
93 our @XXX = ('ok 1','not ok 1', 'ok 2','not ok 2','not ok 3');
94 while ($_ = shift(@XXX)) {
95 my $e = index ($_, 'not') >= 0 ? '' : 1;
113 ok !keys %XXX, "%XXX is empty";
119 my $message = "Test empty pattern";
125 is($&, $xyz, $message);
130 is($&, $xyz, $message);
134 no warnings 'uninitialized';
136 is($&, $xyz, $message);
140 is($&, $xyz, $message);
144 my $message = q !Check $`, $&, $'!;
146 /def/; # optimized up to cmd
147 is("$`:$&:$'", 'abc:def:ghi', $message);
150 /cde/ + 0; # optimized only to spat
151 is("$`:$&:$'", 'ab:cde:fghi', $message);
153 /[d][e][f]/; # not optimized
154 is("$`:$&:$'", 'abc:def:ghi', $message);
158 $_ = 'now is the {time for all} good men to come to.';
160 is($1, 'time for all', "Match braces");
164 my $message = "{N,M} quantifier";
165 $_ = 'xxx {3,4} yyy zzz';
166 ok(/( {3,4})/, $message);
167 is($1, ' ', $message);
168 unlike($_, qr/( {4,})/, $message);
169 ok(/( {2,3}.)/, $message);
170 is($1, ' y', $message);
171 ok(/(y{2,3}.)/, $message);
172 is($1, 'yyy ', $message);
173 unlike($_, qr/x {3,4}/, $message);
174 unlike($_, qr/^xxx {3,4}/, $message);
178 my $message = "Test /g";
180 $_ = "now is the time for all good men to come to.";
181 my @words = /(\w+)/g;
182 my $exp = "now:is:the:time:for:all:good:men:to:come:to";
184 is("@words", $exp, $message);
190 is("@words", $exp, $message);
197 is("@words", "to:to", $message);
201 is("@words", "to:to", $message);
217 my $t1 = my $t2 = my $t3 = my $t4 = my $t5 =
218 my $t6 = my $t7 = my $t8 = my $t9 = 0;
220 for my $iter (1 .. 5) {
231 my $x = "$t1$t2$t3$t4$t5$t6$t7$t8$t9";
232 is($x, '505550555', "Test /o");
237 ok "abc" =~ /^abc$|$xyz/, "| after \$";
239 # perl 4.009 says "unmatched ()"
240 my $message = '$ inside ()';
243 eval '"abc" =~ /a(bc$)|$xyz/; $result = "$&:$1"';
244 is($@, "", $message);
245 is($result, "abc:bc", $message);
249 my $message = "Scalar /g";
252 ok( /abc/g && $` eq "", $message);
253 ok( /abc/g && $` eq "abcfoo", $message);
254 ok(!/abc/g, $message);
256 $message = "Scalar /gi";
258 ok( /ABC/gi && $` eq "", $message);
259 ok( /ABC/gi && $` eq "abcfoo", $message);
260 ok(!/ABC/gi, $message);
262 $message = "Scalar /g";
264 ok( /abc/g && $' eq "fooabcbar", $message);
265 ok( /abc/g && $' eq "bar", $message);
269 is(@x, 2, "/g reset after assignment");
273 my $message = '/g, \G and pos';
277 is(pos $_, 2, $message);
279 is(pos $_, undef, $message);
283 my $message = '(?{ })';
285 'abc' =~ m'a(?{ $out = 2 })b';
286 is($out, 2, $message);
289 'abc' =~ m'a(?{ $out = 3 })c';
290 is($out, 1, $message);
294 $_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6';
295 my @out = /(?<!foo)bar./g;
296 is("@out", 'bar2 barf', "Negative lookbehind");
300 my $message = "REG_INFTY tests";
301 # Tests which depend on REG_INFTY
303 # Defaults assumed if this fails
304 eval { require Config; };
305 $::reg_infty = $Config::Config{reg_infty} // 32767;
306 $::reg_infty_m = $::reg_infty - 1;
307 $::reg_infty_p = $::reg_infty + 1;
308 $::reg_infty_m = $::reg_infty_m; # Suppress warning.
310 # As well as failing if the pattern matches do unexpected things, the
311 # next three tests will fail if you should have picked up a lower-than-
312 # default value for $reg_infty from Config.pm, but have not.
314 is(eval q{('aaa' =~ /(a{1,$::reg_infty_m})/)[0]}, 'aaa', $message);
315 is($@, '', $message);
316 is(eval q{('a' x $::reg_infty_m) =~ /a{$::reg_infty_m}/}, 1, $message);
317 is($@, '', $message);
318 isnt(q{('a' x ($::reg_infty_m - 1)) !~ /a{$::reg_infty_m}/}, 1, $message);
319 is($@, '', $message);
321 eval "'aaa' =~ /a{1,$::reg_infty}/";
322 like($@, qr/^\QQuantifier in {,} bigger than/, $message);
323 eval "'aaa' =~ /a{1,$::reg_infty_p}/";
324 like($@, qr/^\QQuantifier in {,} bigger than/, $message);
328 # Poke a couple more parse failures
329 my $context = 'x' x 256;
330 eval qq("${context}y" =~ /(?<=$context)y/);
331 ok $@ =~ /^\QLookbehind longer than 255 not/, "Lookbehind limit";
336 for my $l (125, 140, 250, 270, 300000, 30) { # Ordered to free memory
338 my $message = "Long monster, length = $l";
339 like("ba$a=", qr/a$a=/, $message);
340 unlike("b$a=", qr/a$a=/, $message);
341 like("b$a=", qr/ba+=/, $message);
343 like("ba$a=", qr/b(?:a|b)+=/, $message);
348 # 20000 nodes, each taking 3 words per string, and 1 per branch
349 my $long_constant_len = join '|', 12120 .. 32645;
350 my $long_var_len = join '|', 8120 .. 28645;
351 my %ans = ( 'ax13876y25677lbc' => 1,
352 'ax13876y25677mcb' => 0, # not b.
353 'ax13876y35677nbc' => 0, # Num too big
354 'ax13876y25677y21378obc' => 1,
355 'ax13876y25677y21378zbc' => 0, # Not followed by [k-o]
356 'ax13876y25677y21378y21378kbc' => 1,
357 'ax13876y25677y21378y21378kcb' => 0, # Not b.
358 'ax13876y25677y21378y21378y21378kbc' => 0, # 5 runs
362 my $message = "20000 nodes, const-len '$_'";
363 ok !($ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o), $message;
365 $message = "20000 nodes, var-len '$_'";
366 ok !($ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o,), $message;
371 my $message = "Complicated backtracking";
372 $_ = " a (bla()) and x(y b((l)u((e))) and b(l(e)e)e";
373 my $expect = "(bla()) ((l)u((e))) (l(e)e)";
380 (?{ $c = 1 }) # Initialize
382 (?(?{ $c == 0 }) # PREVIOUS iteration was OK, stop the loop
384 ) # Fail: will unwind one iteration back
387 [^()]+ # Match a big chunk
390 ) # Do not try to match subchunks
398 )+ # This may not match with different subblocks
403 ) # Otherwise the chunk 1 may succeed with $c>0
409 push @ans, $res while $res = matchit;
410 is("@ans", "1 1 1", $message);
413 is("@ans", $expect, $message);
415 $message = "Recursion with (??{ })";
417 $matched = qr/\((?:(?>[^()]+)|(??{$matched}))*\)/;
419 @ans = my @ans1 = ();
420 push (@ans, $res), push (@ans1, $&) while $res = m/$matched/g;
422 is("@ans", "1 1 1", $message);
423 is("@ans1", $expect, $message);
426 is("@ans", $expect, $message);
431 ok "abc" =~ /^(??{"a"})b/, '"abc" =~ /^(??{"a"})b/';
435 my @ans = ('a/b' =~ m%(.*/)?(.*)%); # Stack may be bad
436 is("@ans", 'a/ b', "Stack may be bad");
440 my $message = "Eval-group not allowed at runtime";
441 my $code = '{$blah = 45}';
444 ok($@ && $@ =~ /not allowed at runtime/ && $blah == 12, $message);
447 my $res = eval { "xx" =~ /(?$code)/o };
449 no warnings 'uninitialized';
450 chomp $@; my $message = "$message '$@', '$res', '$blah'";
451 ok($@ && $@ =~ /not allowed at runtime/ && $blah == 12, $message);
456 $res = eval { "xx" =~ /(?$code)/o };
458 no warnings 'uninitialized';
459 my $message = "$message '$@', '$res', '$blah'";
460 ok(!$@ && $res, $message);
463 $code = '{$blah = 45}';
466 is($blah, 45, $message);
470 is($blah, 45, $message);
474 my $message = "Pos checks";
477 is(pos $x, 2, $message);
480 is(pos $x, 2, $message);
488 is(f (pos $x), 4, $message);
492 my $message = 'Checking $^R';
494 'foot' =~ /foo(?{$x = 12; 75})[t]/;
495 is($^R, 75, $message);
498 'foot' =~ /foo(?{$x = 12; 75})[xy]/;
499 ok($^R eq '67' && $x eq '12', $message);
502 'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/;
503 ok($^R eq '79' && $x eq '12', $message);
507 is(qr/\b\v$/i, '(?^i:\b\v$)', 'qr/\b\v$/i');
508 is(qr/\b\v$/s, '(?^s:\b\v$)', 'qr/\b\v$/s');
509 is(qr/\b\v$/m, '(?^m:\b\v$)', 'qr/\b\v$/m');
510 is(qr/\b\v$/x, '(?^x:\b\v$)', 'qr/\b\v$/x');
511 is(qr/\b\v$/xism, '(?^msix:\b\v$)', 'qr/\b\v$/xism');
512 is(qr/\b\v$/, '(?^:\b\v$)', 'qr/\b\v$/');
515 { # Test that charset modifier work, and are interpolated
516 is(qr/\b\v$/, '(?^:\b\v$)', 'Verify no locale, no unicode_strings gives default modifier');
517 is(qr/(?l:\b\v$)/, '(?^:(?l:\b\v$))', 'Verify infix l modifier compiles');
518 is(qr/(?u:\b\v$)/, '(?^:(?u:\b\v$))', 'Verify infix u modifier compiles');
519 is(qr/(?l)\b\v$/, '(?^:(?l)\b\v$)', 'Verify (?l) compiles');
520 is(qr/(?u)\b\v$/, '(?^:(?u)\b\v$)', 'Verify (?u) compiles');
522 my $dual = qr/\b\v$/;
526 skip 'Locales not available', 1 unless locales_enabled('LC_CTYPE');
530 is($locale, '(?^l:\b\v$)', 'Verify has l modifier when compiled under use locale');
534 use feature 'unicode_strings';
535 my $unicode = qr/\b\v$/;
536 is($unicode, '(?^u:\b\v$)', 'Verify has u modifier when compiled under unicode_strings');
537 is(qr/abc$dual/, '(?^u:abc(?^:\b\v$))', 'Verify retains d meaning when interpolated under locale');
540 skip 'Locales not available', 1 unless locales_enabled('LC_CTYPE');
542 is(qr/abc$locale/, '(?^u:abc(?^l:\b\v$))', 'Verify retains l when interpolated under unicode_strings');
545 no feature 'unicode_strings';
547 skip 'Locales not available', 1 unless locales_enabled('LC_CTYPE');
548 is(qr/abc$locale/, '(?^:abc(?^l:\b\v$))', 'Verify retains l when interpolated outside locale and unicode strings');
551 is(qr/def$unicode/, '(?^:def(?^u:\b\v$))', 'Verify retains u when interpolated outside locale and unicode strings');
554 skip 'Locales not available', 2 unless locales_enabled('LC_CTYPE');
557 is(qr/abc$dual/, '(?^l:abc(?^:\b\v$))', 'Verify retains d meaning when interpolated under locale');
558 is(qr/abc$unicode/, '(?^l:abc(?^u:\b\v$))', 'Verify retains u when interpolated under locale');
563 my $message = "Look around";
565 foreach my $ans ('', 'c') {
566 ok(/(?<=(?=a)..)((?=c)|.)/g, $message);
567 is($1, $ans, $message);
572 my $message = "Empty clause";
574 foreach my $ans ('', 'a', '') {
575 ok(/^|a|$/g, $message);
576 is($&, $ans, $message);
582 my $message = "Prefixify";
584 my ($v, $a, $b, $res) = @_;
585 ok($v =~ s/\Q$a\E/$b/, $message);
586 is($v, $res, $message);
590 prefixify ('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch');
591 prefixify ('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch');
597 ok $1 && /$1/, "Capture a quote";
601 no warnings 'closure';
602 my $message = '(?{ $var } refers to package vars';
606 '' =~ /(?{ $c = 4 })/;
607 main::is($c, 4, $message);
608 main::is($::c, 3, $message);
612 is(eval 'q(a:[b]:) =~ /[x[:foo:]]/', undef);
613 like ($@, qr/POSIX class \[:[^:]+:\] unknown in regex/,
614 'POSIX class [: :] must have valid name');
616 for my $d (qw [= .]) {
617 is(eval "/[[${d}foo${d}]]/", undef);
618 like ($@, qr/\QPOSIX syntax [$d $d] is reserved for future extensions/,
619 "POSIX syntax [[$d $d]] is an error");
624 # test if failure of patterns returns empty list
625 my $message = "Failed pattern returns empty list";
628 is("@_", "", $message);
631 is("@_", "", $message);
634 is("@_", "", $message);
637 is("@_", "", $message);
641 my $message = '@- and @+ tests';
644 is($#+, 0, $message);
645 is($#-, 0, $message);
646 is($+ [0], 2, $message);
647 is($- [0], 1, $message);
648 ok(!defined $+ [1] && !defined $- [1] &&
649 !defined $+ [2] && !defined $- [2], $message);
652 is($#+, 2, $message);
653 is($#-, 2, $message);
654 is($+ [0], 3, $message);
655 is($- [0], 0, $message);
656 is($+ [1], 2, $message);
657 is($- [1], 1, $message);
658 is($+ [2], 3, $message);
659 is($- [2], 2, $message);
660 ok(!defined $+ [3] && !defined $- [3] &&
661 !defined $+ [4] && !defined $- [4], $message);
663 # Exists has a special check for @-/@+ - bug 45147
664 ok(exists $-[0], $message);
665 ok(exists $+[0], $message);
666 ok(exists $-[2], $message);
667 ok(exists $+[2], $message);
668 ok(!exists $-[3], $message);
669 ok(!exists $+[3], $message);
670 ok(exists $-[-1], $message);
671 ok(exists $+[-1], $message);
672 ok(exists $-[-3], $message);
673 ok(exists $+[-3], $message);
674 ok(!exists $-[-4], $message);
675 ok(!exists $+[-4], $message);
678 is($#+, 3, $message);
679 is($#-, 3, $message);
680 is($+ [1], 2, $message);
681 is($- [1], 1, $message);
682 is($+ [3], 3, $message);
683 is($- [3], 2, $message);
684 ok(!defined $+ [2] && !defined $- [2] &&
685 !defined $+ [4] && !defined $- [4], $message);
688 is($#+, 1, $message);
689 is($#-, 1, $message);
690 is($+ [0], 2, $message);
691 is($- [0], 0, $message);
692 is($+ [1], 2, $message);
693 is($- [1], 1, $message);
694 ok(!defined $+ [2] && !defined $- [2] &&
695 !defined $+ [3] && !defined $- [3], $message);
698 is($#+, 2, $message);
699 is($#-, 1, $message);
701 # Check that values don’t stick
702 " "=~/()()()(.)(..)/;
703 my($m,$p) = (\$-[5], \$+[5]);
704 () = "$$_" for $m, $p; # FETCH (or eqv.)
706 is $$m, undef, 'values do not stick to @- elements';
707 is $$p, undef, 'values do not stick to @+ elements';
710 foreach ('$+[0] = 13', '$-[0] = 13', '@+ = (7, 6, 5)',
711 '@- = qw (foo bar)', '$^N = 42') {
713 like($@, qr/^Modification of a read-only value attempted/,
714 '$^N, @- and @+ are read-only');
718 my $message = '\G testing';
722 is("@a", "a a", $message);
726 unlike($str, qr/^\G/, $message);
727 unlike($str, qr/^.\G/, $message);
728 like($str, qr/^..\G/, $message);
729 unlike($str, qr/^...\G/, $message);
730 ok($str =~ /\G../ && $& eq 'cd', $message);
731 ok($str =~ /.\G./ && $& eq 'bc', $message);
736 my $message = '\G and intuit and anchoring';
739 ok($_ =~ /\Gabc/, $message);
740 ok($_ =~ /^\Gabc/, $message);
743 ok($_ =~ /\Gdef/, $message);
745 ok($_ =~ /\Gdef$/, $message);
747 ok($_ =~ /abc\Gdef$/, $message);
749 ok($_ =~ /^abc\Gdef$/, $message);
751 ok($_ =~ /c\Gd/, $message);
753 ok($_ =~ /..\GX?def/, $message);
759 my @a = $s =~ /(\d)\G/g; # this infinitely looped up till 5.19.1
760 is("@a", "1", '\G looping');
765 my $message = 'pos inside (?{ })';
768 like($str, qr/b(?{$foo = $_; $bar = pos})c/, $message);
769 is($foo, $str, $message);
770 is($bar, 2, $message);
771 is(pos $str, undef, $message);
776 ok($str =~ /b(?{$foo = $_; $bar = pos})c/g, $message);
777 is($foo, $str, $message);
778 is($bar, 2, $message);
779 is(pos $str, 3, $message);
784 like($_, qr/b(?{$foo = $_; $bar = pos})c/, $message);
785 is($foo, $str, $message);
786 is($bar, 2, $message);
790 ok(/b(?{$foo = $_; $bar = pos})c/g, $message);
791 is($foo, $str, $message);
792 is($bar, 2, $message);
793 is(pos, 3, $message);
798 1 while /b(?{$foo = $_; $bar = pos})c/g;
799 is($foo, $str, $message);
800 is($bar, 2, $message);
801 is(pos, undef, $message);
806 ok(s/b(?{$foo = $_; $bar = pos})c/x/g, $message);
807 is($foo, 'abcde|abcde', $message);
808 is($bar, 8, $message);
809 is($_, 'axde|axde', $message);
814 () = /([ace]).(?{push @res, $1,$2})([ce])(?{push @res, $1,$2})/g;
815 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
816 is("@res", "'a' undef 'a' 'c' 'e' undef 'a' undef 'a' 'c'", $message);
819 () = /([ace]).(?{push @res, $`,$&,$'})([ce])(?{push @res, $`,$&,$'})/g;
820 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
821 is("@res", "'' 'ab' 'cde|abcde' " .
822 "'' 'abc' 'de|abcde' " .
823 "'abcd' 'e|' 'abcde' " .
824 "'abcde|' 'ab' 'cde' " .
825 "'abcde|' 'abc' 'de'", $message);
829 my $message = '\G anchor checks';
830 my $foo = 'aabbccddeeffgg';
833 ok($foo =~ /.\G(..)/g, $message);
834 is($1, 'ab', $message);
837 ok($foo =~ /.\G(..)/g, $message);
838 is($1, 'cc', $message);
841 ok($foo =~ /.\G(..)/g, $message);
842 is($1, 'de', $message);
844 ok($foo =~ /\Gef/g, $message);
847 ok($foo =~ /\G(..)/g, $message);
848 is($1, 'aa', $message);
850 ok($foo =~ /\G(..)/g, $message);
851 is($1, 'bb', $message);
854 ok($foo =~ /\G(..)/g, $message);
855 is($1, 'cd', $message);
859 my $message = 'basic \G floating checks';
860 my $foo = 'aabbccddeeffgg';
863 ok($foo =~ /a+\G(..)/g, "$message: a+\\G");
864 is($1, 'ab', "$message: ab");
867 ok($foo =~ /b+\G(..)/g, "$message: b+\\G");
868 is($1, 'cc', "$message: cc");
871 ok($foo =~ /d+\G(..)/g, "$message: d+\\G");
872 is($1, 'de', "$message: de");
874 ok($foo =~ /\Gef/g, "$message: \\Gef");
878 ok($foo =~ /(?=a+\G)(..)/g, "$message: (?a+\\G)");
879 is($1, 'aa', "$message: aa");
883 ok($foo =~ /a(?=a+\G)(..)/g, "$message: a(?=a+\\G)");
884 is($1, 'ab', "$message: ab");
890 my @res = /(\d*|x)/g;
892 is("@res", "123||x|123|", "0 match in alternation");
896 my $message = "Match against temporaries (created via pp_helem())" .
898 ok({foo => "bar\n" . $^X} -> {foo} =~ /^(.*)\n/g, $message);
899 is($1, "bar", $message);
903 my $message = 'package $i inside (?{ }), ' .
904 'saved substrings and changing $_';
905 our @a = qw [foo bar];
907 s/(\w)(?{push @b, $1})/,$1,/g for @a;
908 is("@b", "f o o b a r", $message);
909 is("@a", ",f,,o,,o, ,b,,a,,r,", $message);
911 $message = 'lexical $i inside (?{ }), ' .
912 'saved substrings and changing $_';
913 no warnings 'closure';
914 my @c = qw [foo bar];
916 s/(\w)(?{push @d, $1})/,$1,/g for @c;
917 is("@d", "f o o b a r", $message);
918 is("@c", ",f,,o,,o, ,b,,a,,r,", $message);
922 my $message = 'Brackets';
925 { (?> [^{}]+ | (??{ $brackets }) )* }
928 ok("{{}" =~ $brackets, $message);
929 is($&, "{}", $message);
930 ok("something { long { and } hairy" =~ $brackets, $message);
931 is($&, "{ and }", $message);
932 ok("something { long { and } hairy" =~ m/((??{ $brackets }))/, $message);
933 is($&, "{ and }", $message);
939 ok(!m/^-.*bb/mg, '$_ = "a-a\nxbb"; m/^-.*bb/mg');
943 my $message = '\G anchor checks';
944 my $text = "aaXbXcc";
946 ok($text !~ /\GXb*X/g, $message);
951 unlike($_, qr/^\s*A/m, '$_ = "xA\n" x 500; /^\s*A/m"');
953 my $text = "abc dbf";
954 my @res = ($text =~ /.*?(b).*?\b/g);
955 is("@res", "b b", '\b is not special');
959 my $message = '\S, [\S], \s, [\s]';
960 my @a = map chr, 0 .. 255;
961 my @b = grep m/\S/, @a;
962 my @c = grep m/[^\s]/, @a;
963 is("@b", "@c", $message);
966 @c = grep /[\S]/, @a;
967 is("@b", "@c", $message);
970 @c = grep /[^\S]/, @a;
971 is("@b", "@c", $message);
974 @c = grep /[\s]/, @a;
975 is("@b", "@c", $message);
977 # Test an inverted posix class with a char also in the class.
978 my $nbsp = chr utf8::unicode_to_native(0xA0);
979 my $non_s = chr utf8::unicode_to_native(0xA1);
980 my $pat_string = "[^\\S ]";
981 unlike(" ", qr/$pat_string/, "Verify ' ' !~ /$pat_string/");
982 like("\t", qr/$pat_string/, "Verify '\\t =~ /$pat_string/");
983 unlike($nbsp, qr/$pat_string/, "Verify non-utf8-NBSP !~ /$pat_string/");
984 utf8::upgrade($nbsp);
985 like($nbsp, qr/$pat_string/, "Verify utf8-NBSP =~ /$pat_string/");
986 unlike($non_s, qr/$pat_string/, "Verify non-utf8-inverted-bang !~ /$pat_string/");
987 utf8::upgrade($non_s);
988 unlike($non_s, qr/$pat_string/, "Verify utf8-inverted-bang !~ /$pat_string/");
991 my $message = '\D, [\D], \d, [\d]';
992 my @a = map chr, 0 .. 255;
993 my @b = grep /\D/, @a;
994 my @c = grep /[^\d]/, @a;
995 is("@b", "@c", $message);
998 @c = grep /[\D]/, @a;
999 is("@b", "@c", $message);
1002 @c = grep /[^\D]/, @a;
1003 is("@b", "@c", $message);
1006 @c = grep /[\d]/, @a;
1007 is("@b", "@c", $message);
1010 my $message = '\W, [\W], \w, [\w]';
1011 my @a = map chr, 0 .. 255;
1012 my @b = grep /\W/, @a;
1013 my @c = grep /[^\w]/, @a;
1014 is("@b", "@c", $message);
1017 @c = grep /[\W]/, @a;
1018 is("@b", "@c", $message);
1021 @c = grep /[^\W]/, @a;
1022 is("@b", "@c", $message);
1025 @c = grep /[\w]/, @a;
1026 is("@b", "@c", $message);
1030 # see if backtracking optimization works correctly
1031 my $message = 'Backtrack optimization';
1032 like("\n\n", qr/\n $ \n/x, $message);
1033 like("\n\n", qr/\n* $ \n/x, $message);
1034 like("\n\n", qr/\n+ $ \n/x, $message);
1035 like("\n\n", qr/\n? $ \n/x, $message);
1036 like("\n\n", qr/\n*? $ \n/x, $message);
1037 like("\n\n", qr/\n+? $ \n/x, $message);
1038 like("\n\n", qr/\n?? $ \n/x, $message);
1039 unlike("\n\n", qr/\n*+ $ \n/x, $message);
1040 unlike("\n\n", qr/\n++ $ \n/x, $message);
1041 like("\n\n", qr/\n?+ $ \n/x, $message);
1046 use overload '""' => sub {'Object S'};
1049 my $message = "Ref stringification";
1050 ::ok(do { \my $v} =~ /^SCALAR/, "Scalar ref stringification") or diag($message);
1051 ::ok(do {\\my $v} =~ /^REF/, "Ref ref stringification") or diag($message);
1052 ::ok([] =~ /^ARRAY/, "Array ref stringification") or diag($message);
1053 ::ok({} =~ /^HASH/, "Hash ref stringification") or diag($message);
1054 ::ok('S' -> new =~ /^Object S/, "Object stringification") or diag($message);
1058 my $message = "Test result of match used as match";
1059 ok('a1b' =~ ('xyz' =~ /y/), $message);
1060 is($`, 'a', $message);
1061 ok('a1b' =~ ('xyz' =~ /t/), $message);
1062 is($`, 'a', $message);
1066 my $message = '"1" is not \s';
1067 warning_is(sub {unlike("1\n" x 102, qr/^\s*\n/m, $message)},
1068 undef, "$message (did not warn)");
1072 my $message = '\s, [[:space:]] and [[:blank:]]';
1073 my %space = (spc => " ",
1078 # There's no \v but the vertical tabulator seems miraculously
1079 # be 11 both in ASCII and EBCDIC.
1083 my @space0 = sort grep {$space {$_} =~ /\s/ } keys %space;
1084 my @space1 = sort grep {$space {$_} =~ /[[:space:]]/} keys %space;
1085 my @space2 = sort grep {$space {$_} =~ /[[:blank:]]/} keys %space;
1087 is("@space0", "cr ff lf spc tab vt", $message);
1088 is("@space1", "cr ff lf spc tab vt", $message);
1089 is("@space2", "spc tab", $message);
1094 # this must be a high number and go from 0 to N, as the bug we are looking for doesn't
1095 # seem to be predictable. Slight changes to the test make it fail earlier or later.
1096 foreach my $i (0 .. $n)
1099 ok $str=~/.*\z/, "implicit MBOL check string disable does not break things length=$i";
1103 # we are actually testing that we dont die when executing these patterns
1106 ok(utf8::is_utf8($e),"got a unicode string - rt75680");
1108 ok($e !~ m/.*?[x]$/, "unicode string against /.*?[x]\$/ - rt75680");
1109 ok($e !~ m/.*?\p{Space}$/i, "unicode string against /.*?\\p{space}\$/i - rt75680");
1110 ok($e !~ m/.*?[xyz]$/, "unicode string against /.*?[xyz]\$/ - rt75680");
1111 ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/, "unicode string against big pattern - rt75680");
1114 # we are actually testing that we dont die when executing these patterns
1115 my $e = "B" . uni_to_native("\x{f6}") . "ck";
1116 ok(!utf8::is_utf8($e), "got a latin string - rt75680");
1118 ok($e !~ m/.*?[x]$/, "latin string against /.*?[x]\$/ - rt75680");
1119 ok($e !~ m/.*?\p{Space}$/i, "latin string against /.*?\\p{space}\$/i - rt75680");
1120 ok($e !~ m/.*?[xyz]$/,"latin string against /.*?[xyz]\$/ - rt75680");
1121 ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/,"latin string against big pattern - rt75680");
1126 # Tests for bug 77414.
1129 my $message = '\p property after empty * match';
1131 like("1", qr/\s*\pN/, $message);
1132 like("-", qr/\s*\p{Dash}/, $message);
1133 like(" ", qr/\w*\p{Blank}/, $message);
1136 like("1", qr/\s*\pN+/, $message);
1137 like("-", qr/\s*\p{Dash}{1}/, $message);
1138 like(" ", qr/\w*\p{Blank}{1,4}/, $message);
1142 { # Some constructs with Latin1 characters cause a utf8 string not
1143 # to match itself in non-utf8
1144 my $c = uni_to_native("\xc0");
1145 my $pattern = my $utf8_pattern = qr/(($c)+,?)/;
1146 utf8::upgrade($utf8_pattern);
1147 ok $c =~ $pattern, "\\xc0 =~ $pattern; Neither pattern nor target utf8";
1148 ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; Neither pattern nor target utf8";
1149 ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; pattern utf8, target not";
1150 ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; pattern utf8, target not";
1152 ok $c =~ $pattern, "\\xc0 =~ $pattern; target utf8, pattern not";
1153 ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; target utf8, pattern not";
1154 ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; Both target and pattern utf8";
1155 ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; Both target and pattern utf8";
1158 { # Make sure can override the formatting
1159 use feature 'unicode_strings';
1160 ok uni_to_native("\xc0") =~ /\w/, 'Under unicode_strings: "\xc0" =~ /\w/';
1161 ok uni_to_native("\xc0") !~ /(?d:\w)/, 'Under unicode_strings: "\xc0" !~ /(?d:\w)/';
1168 is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag enabled - Bug #80212");
1171 is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag disabled - Bug #80212");
1176 local $::TODO = "[perl #38133]";
1178 "A" =~ /(((?:A))?)+/;
1184 is($first, $second);
1188 # RT #3516: \G in a m//g expression causes problems
1190 while ("abc" =~ m/(\G[ac])?/g) {
1191 last if $count++ > 10;
1193 ok($count < 10, 'RT #3516 A');
1196 while ("abc" =~ m/(\G|.)[ac]/g) {
1197 last if $count++ > 10;
1199 ok($count < 10, 'RT #3516 B');
1202 while ("abc" =~ m/(\G?[ac])?/g) {
1203 last if $count++ > 10;
1205 ok($count < 10, 'RT #3516 C');
1208 # RT #84294: Is this a bug in the simple Perl regex?
1209 # : Nested buffers and (?{...}) dont play nicely on partial matches
1211 ok("ab" =~ /((\w+)(?{ push @got, $2 })){2}/,"RT #84294: Pattern should match");
1212 my $want= "'ab', 'a', 'b'";
1213 my $got= join(", ", map { defined($_) ? "'$_'" : "undef" } @got);
1214 is($got,$want,'RT #84294: check that "ab" =~ /((\w+)(?{ push @got, $2 })){2}/ leaves @got in the correct state');
1218 # Suppress warnings, as the non-unicode one comes out even if turn off
1219 # warnings here (because the execution is done in another scope).
1220 local $SIG{__WARN__} = sub {};
1221 my $str = "\x{110000}";
1223 unlike($str, qr/\p{ASCII_Hex_Digit=True}/, "Non-Unicode doesn't match \\p{AHEX=True}");
1224 like($str, qr/\p{ASCII_Hex_Digit=False}/, "Non-Unicode matches \\p{AHEX=False}");
1225 like($str, qr/\P{ASCII_Hex_Digit=True}/, "Non-Unicode matches \\P{AHEX=True}");
1226 unlike($str, qr/\P{ASCII_Hex_Digit=False}/, "Non-Unicode matches \\P{AHEX=FALSE}");
1230 # Test that IDstart works, but because the author (khw) knows
1231 # regexes much better than the rest of the core, it is being done here
1232 # in the context of a regex which relies on buffer names beginng with
1236 like($str, qr/(?<a>abc)/, "'a' is legal IDStart");
1237 like($str, qr/(?<_>abc)/, "'_' is legal IDStart");
1238 like($str, qr/(?<ß>abc)/, "U+00DF is legal IDStart");
1239 like($str, qr/(?<ℕ>abc)/, "U+2115' is legal IDStart");
1241 # This test works on Unicode 6.0 in which U+2118 and U+212E are legal
1242 # IDStarts there, but are not Word characters, and therefore Perl
1243 # doesn't allow them to be IDStarts. But there is no guarantee that
1244 # Unicode won't change things around in the future so that at some
1245 # future Unicode revision these tests would need to be revised.
1246 foreach my $char ("%", "×", chr(0x2118), chr(0x212E)) {
1249 "abc" =~ qr/(?<$char>abc)/;
1251 utf8::encode($prog);
1252 fresh_perl_like($prog, qr!Group name must start with a non-digit word character!, {},
1253 sprintf("'U+%04X not legal IDFirst'", ord($char)));
1259 utf8::upgrade($pat);
1260 like("\xffb", qr/$pat/i, "/i: utf8 pattern, non-utf8 string, latin1-char preceding matching char in string");
1263 { # Crash with @a =~ // warning
1264 local $SIG{__WARN__} = sub {
1265 pass 'no crash for @a =~ // warning'
1267 eval ' sub { my @a =~ // } ';
1270 { # Concat overloading and qr// thingies
1276 '""' => sub { ${$_[0]} },
1278 push @refs, ref $_[1] if ref $_[1];
1279 bless $_[2] ? \"$_[1]${$_[0]}" : \"${$_[0]}$_[1]"
1284 my $o = bless \$s, Cat::;
1286 is "@refs", "Regexp", '/$o$qr/ passes qr ref to cat overload meth';
1292 $count++ while $str=~/.*/g;
1293 is $count, 2, 'test that ANCH_MBOL works properly. We should get 2 from $count++ while "\n"=~/.*/g';
1295 $class_count++ while $str=~/[^\n]*/g;
1296 is $class_count, $count, 'while "\n"=~/.*/g and while "\n"=~/[^\n]*/g should behave the same';
1298 $anch_count++ while $str=~/^.*/mg;
1299 is $anch_count, 1, 'while "\n"=~/^.*/mg should match only once';
1304 my $A_grave = uni_to_native("\xc0");
1305 like uni_to_native("\xe0"), qr/(?i:$A_grave)/, "(?i: shouldn't lose the passed in /u";
1307 unlike "\x{100}", qr/(?i:\w)/, "(?i: shouldn't lose the passed in /a";
1309 unlike 'k', qr/(?i:\N{KELVIN SIGN})/, "(?i: shouldn't lose the passed in /aa";
1313 # the test for whether the pattern should be re-compiled should
1314 # consider the UTF8ness of the previous and current pattern
1315 # string, as well as the physical bytes of the pattern string
1317 for my $s (byte_utf8a_to_utf8n("\xc4\x80"), "\x{100}") {
1318 ok($s =~ /^$s$/, "re-compile check is UTF8-aware");
1322 # #113682 more overloading and qr//
1323 # when doing /foo$overloaded/, if $overloaded returns
1324 # a qr/(?{})/ via qr or "" overloading, then 'use re 'eval'
1325 # shouldn't be required. Via '.', it still is.
1328 use overload 'qr' => sub { qr/(??{50})/ };
1331 use overload '""' => sub { qr/(??{51})/ };
1334 use overload '.' => sub { $_[1] . qr/(??{52})/ };
1337 use overload '""' => sub { qr/(??{7})/ },
1338 '.' => sub { $_[1] . qr/(??{53})/ };
1340 package Qr_indirect;
1341 use overload '""' => sub { $_[0][0] };
1346 my $o = bless [], "Qr$i";
1347 if ((0,0,1,1)[$i]) {
1348 eval { "A5$i" =~ /^A$o$/ };
1349 like($@, qr/Eval-group not allowed/, "Qr$i");
1350 eval { "5$i" =~ /$o/ };
1351 like($@, ($i == 3 ? qr/^$/ : qr/no method found,/),
1355 ok("A5$i" =~ /^A$o$/, "Qr$i - with use re eval");
1356 eval { "5$i" =~ /$o/ };
1357 like($@, ($i == 3 ? qr/^$/ : qr/no method found,/),
1358 "Qr$i bare - with use re eval");
1362 ok("A5$i" =~ /^A$o$/, "Qr$i");
1363 ok("5$i" =~ /$o/, "Qr$i bare");
1367 my $o = bless [ bless [], "Qr1" ], 'Qr_indirect';
1368 ok("A51" =~ /^A$o/, "Qr_indirect");
1369 ok("51" =~ /$o/, "Qr_indirect bare");
1372 { # Various flags weren't being set when a [] is optimized into an
1376 my $sharp_s = uni_to_native("\xdf");
1377 ok("\x{017F}\x{017F}" =~ qr/^[$sharp_s]?$/i, "[] to EXACTish optimization");
1381 for my $char (":", uni_to_native("\x{f7}"), "\x{2010}") {
1382 my $utf8_char = $char;
1383 utf8::upgrade($utf8_char);
1384 my $display = $char;
1385 $display = display($display);
1386 my $utf8_display = "utf8::upgrade(\"$display\")";
1388 like($char, qr/^$char?$/, "\"$display\" =~ /^$display?\$/");
1389 like($char, qr/^$utf8_char?$/, "my \$p = \"$display\"; utf8::upgrade(\$p); \"$display\" =~ /^\$p?\$/");
1390 like($utf8_char, qr/^$char?$/, "my \$c = \"$display\"; utf8::upgrade(\$c); \"\$c\" =~ /^$display?\$/");
1391 like($utf8_char, qr/^$utf8_char?$/, "my \$c = \"$display\"; utf8::upgrade(\$c); my \$p = \"$display\"; utf8::upgrade(\$p); \"\$c\" =~ /^\$p?\$/");
1396 # #116148: Pattern utf8ness sticks around globally
1397 # the utf8 in the first match was sticking around for the second
1400 use feature 'unicode_strings';
1406 ok("Perl" =~ /P.*$/i, '#116148');
1409 { # 118297: Mixing up- and down-graded strings in regex
1410 utf8::upgrade(my $u = "\x{e5}");
1411 utf8::downgrade(my $d = "\x{e5}");
1413 local $SIG{__WARN__} = sub { $warned++ if $_[0] =~ /\AMalformed UTF-8/ };
1415 ok(!$warned, "no warnings when interpolating mixed up-/downgraded strings in pattern");
1416 my $c = "\x{e5}\x{e5}";
1417 utf8::downgrade($c);
1418 like($c, $re, "mixed up-/downgraded pattern matches downgraded string");
1420 like($c, $re, "mixed up-/downgraded pattern matches upgraded string");
1424 # if we have 87 capture buffers defined then \87 should refer to the 87th.
1425 # test that this is true for 1..100
1426 # Note that this test causes the engine to recurse at runtime, and
1427 # hence use a lot of C stack.
1428 for my $i (1..100) {
1430 $capture= "($capture)" for 1 .. $i;
1431 for my $mid ("","b") {
1432 my $str= "a${mid}a";
1433 my $backref= "\\$i";
1435 ok($str=~/$capture$mid$backref/,"\\$i works with $i buffers '$str'=~/...$mid$backref/");
1438 is("$@","","\\$i works with $i buffers works with $i buffers '$str'=~/...$mid$backref/");
1444 # this mixture of readonly (not COWable) and COWable strings
1445 # messed up the capture buffers under COW. The actual test results
1446 # are incidental; the issue is was an AddressSanitizer failure
1450 for ($c, 'C', $c, 'DE') {
1451 ok(/(.)/, "COWable match");
1460 # /[#$x]/x didn't interpolate the var $x.
1463 $s =~ s/[a#$b%]/X/g;
1464 is ($s, 'XbXX$XX&', 'RT #45667 without /x');
1466 $s =~ s/[a#$b%]/X/gx;
1467 is ($s, 'XbXX$XX&', 'RT #45667 with /x');
1471 no warnings "uninitialized";
1475 pass('no crash with /@a/ when array has nonexistent elems');
1479 is runperl(prog => 'delete $::{qq-\cR-}; //; print qq-ok\n-'),
1481 'deleting *^R does not result in crashes';
1483 *^R = *caretRglobwithnoscalar;
1485 is $^R, 42, 'assigning to *^R does not result in a crash';
1489 .' q-..- =~ /(??{undef *^R;q--})(?{42})/; '
1494 'undefining *^R within (??{}) does not result in a crash';
1497 SKIP: { # Test literal range end point special handling
1498 unless ($::IS_EBCDIC) {
1499 skip "Valid only for EBCDIC", 24;
1502 like("\x89", qr/[i-j]/, '"\x89" should match [i-j]');
1503 unlike("\x8A", qr/[i-j]/, '"\x8A" shouldnt match [i-j]');
1504 unlike("\x90", qr/[i-j]/, '"\x90" shouldnt match [i-j]');
1505 like("\x91", qr/[i-j]/, '"\x91" should match [i-j]');
1507 like("\x89", qr/[i-\N{LATIN SMALL LETTER J}]/, '"\x89" should match [i-\N{LATIN SMALL LETTER J}]');
1508 unlike("\x8A", qr/[i-\N{LATIN SMALL LETTER J}]/, '"\x8A" shouldnt match [i-\N{LATIN SMALL LETTER J}]');
1509 unlike("\x90", qr/[i-\N{LATIN SMALL LETTER J}]/, '"\x90" shouldnt match [i-\N{LATIN SMALL LETTER J}]');
1510 like("\x91", qr/[i-\N{LATIN SMALL LETTER J}]/, '"\x91" should match [i-\N{LATIN SMALL LETTER J}]');
1512 like("\x89", qr/[i-\N{U+6A}]/, '"\x89" should match [i-\N{U+6A}]');
1513 unlike("\x8A", qr/[i-\N{U+6A}]/, '"\x8A" shouldnt match [i-\N{U+6A}]');
1514 unlike("\x90", qr/[i-\N{U+6A}]/, '"\x90" shouldnt match [i-\N{U+6A}]');
1515 like("\x91", qr/[i-\N{U+6A}]/, '"\x91" should match [i-\N{U+6A}]');
1517 like("\x89", qr/[\N{U+69}-\N{U+6A}]/, '"\x89" should match [\N{U+69}-\N{U+6A}]');
1518 unlike("\x8A", qr/[\N{U+69}-\N{U+6A}]/, '"\x8A" shouldnt match [\N{U+69}-\N{U+6A}]');
1519 unlike("\x90", qr/[\N{U+69}-\N{U+6A}]/, '"\x90" shouldnt match [\N{U+69}-\N{U+6A}]');
1520 like("\x91", qr/[\N{U+69}-\N{U+6A}]/, '"\x91" should match [\N{U+69}-\N{U+6A}]');
1522 like("\x89", qr/[i-\x{91}]/, '"\x89" should match [i-\x{91}]');
1523 like("\x8A", qr/[i-\x{91}]/, '"\x8A" should match [i-\x{91}]');
1524 like("\x90", qr/[i-\x{91}]/, '"\x90" should match [i-\x{91}]');
1525 like("\x91", qr/[i-\x{91}]/, '"\x91" should match [i-\x{91}]');
1527 # Need to use eval, because tries to compile on ASCII platforms even
1528 # though the tests are skipped, and fails because 0x89-j is an illegal
1530 like("\x89", eval 'qr/[\x{89}-j]/', '"\x89" should match [\x{89}-j]');
1531 like("\x8A", eval 'qr/[\x{89}-j]/', '"\x8A" should match [\x{89}-j]');
1532 like("\x90", eval 'qr/[\x{89}-j]/', '"\x90" should match [\x{89}-j]');
1533 like("\x91", eval 'qr/[\x{89}-j]/', '"\x91" should match [\x{89}-j]');
1536 # These are based on looking at the code in regcomp.c
1537 # We don't look for specific code, just the existence of an SSC
1538 foreach my $re (qw( qr/a?c/
1548 skip "no re-debug under miniperl" if is_miniperl;
1550 use re qw(Debug COMPILE);
1553 fresh_perl_like($prog, qr/synthetic stclass/, { stderr=>1 }, "$re generates a synthetic start class");
1558 like "\x{AA}", qr/a?[\W_]/d, "\\W with /d synthetic start class works";
1562 skip("Tests are ASCII-centric, some would fail on EBCDIC", 12) if $::IS_EBCDIC;
1564 # Verify that the very last Latin-1 U+00FF
1565 # (LATIN SMALL LETTER Y WITH DIAERESIS)
1566 # and its UPPER counterpart (U+0178 which is pure Unicode),
1567 # and likewise for the very first pure Unicode
1568 # (LATIN CAPITAL LETTER A WITH MACRON) fold-match properly,
1569 # and there are no off-by-one logic errors in the transition zone.
1571 ok("\xFF" =~ /\xFF/i, "Y WITH DIAERESIS l =~ l");
1572 ok("\xFF" =~ /\x{178}/i, "Y WITH DIAERESIS l =~ u");
1573 ok("\x{178}" =~ /\xFF/i, "Y WITH DIAERESIS u =~ l");
1574 ok("\x{178}" =~ /\x{178}/i, "Y WITH DIAERESIS u =~ u");
1576 # U+00FF with U+05D0 (non-casing Hebrew letter).
1577 ok("\xFF\x{5D0}" =~ /\xFF\x{5D0}/i, "Y WITH DIAERESIS l =~ l");
1578 ok("\xFF\x{5D0}" =~ /\x{178}\x{5D0}/i, "Y WITH DIAERESIS l =~ u");
1579 ok("\x{178}\x{5D0}" =~ /\xFF\x{5D0}/i, "Y WITH DIAERESIS u =~ l");
1580 ok("\x{178}\x{5D0}" =~ /\x{178}\x{5D0}/i, "Y WITH DIAERESIS u =~ u");
1583 ok("\x{100}" =~ /\x{100}/i, "A WITH MACRON u =~ u");
1584 ok("\x{100}" =~ /\x{101}/i, "A WITH MACRON u =~ l");
1585 ok("\x{101}" =~ /\x{100}/i, "A WITH MACRON l =~ u");
1586 ok("\x{101}" =~ /\x{101}/i, "A WITH MACRON l =~ l");
1591 ok("abc" =~ /a
\85b
\85c/x, "NEL is white-space under /x");
1595 ok('a(b)c' =~ qr(a\(b\)c), "'\\(' is a literal in qr(...)");
1596 ok('a[b]c' =~ qr[a\[b\]c], "'\\[' is a literal in qr[...]");
1597 ok('a{3}c' =~ qr{a\{3\}c}, # Only failed when { could be a meta
1598 "'\\{' is a literal in qr{...}, where it could be a quantifier");
1600 # This one is for completeness
1601 ok('a<b>c' =~ qr<a\<b\>c>, "'\\<' is a literal in qr<...>)");
1604 { # Was getting optimized into EXACT (non-folding node)
1607 like("X", qr/$x/, "UTF-8 of /[x]/i matches upper case");
1610 { # make sure we get an error when \p{} cannot load Unicode tables
1611 fresh_perl_like(<<' prog that cannot load uni tables',
1614 require utf8; require 'utf8_heavy.pl';
1618 if ($name =~ /(\p{IsUpper}) (\p{IsUpper})/){
1619 print "It's good! >$1< >$2<\n";
1621 print "It's not good...\n";
1623 prog that cannot load uni tables
1624 qr/^Can't locate unicore\/Heavy\.pl(?x:
1625 )|^Can't find Unicode property definition/,
1627 '\p{} should not fail silently when uni tables evanesce');
1630 { # Special handling of literal-ended ranges in [...] was breaking this
1632 like("ÿ", qr/[ÿ-ÿ]/, "\"ÿ\" should match [ÿ-ÿ]");
1636 like("TffffffffffffTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT5TTTTTTTTTTTTTTTTTTTTTTTTT3TTgTTTTTTTTTTTTTTTTTTTTT2TTTTTTTTTTTTTTTTTTTTTTTHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHiHHHHHHHfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff&ffff", qr/TffffffffffffTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT5TTTTTTTTTTTTTTTTTTTTTTTTT3TTgTTTTTTTTTTTTTTTTTTTTT2TTTTTTTTTTTTTTTTTTTTTTTHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHiHHHHHHHfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff&ffff/il, "");
1637 like("TffffffffffffT\x{100}TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT5TTTTTTTTTTTTTTTTTTTTTTTTT3TTgTTTTTTTTTTTTTTTTTTTTT2TTTTTTTTTTTTTTTTTTTTTTTHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHiHHHHHHHfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff&ffff", qr/TffffffffffffT\x{100}TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT5TTTTTTTTTTTTTTTTTTTTTTTTT3TTgTTTTTTTTTTTTTTTTTTTTT2TTTTTTTTTTTTTTTTTTTTTTTHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHiHHHHHHHfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff&ffff/il, "");
1641 my($s, $x, @x) = ('abc', 'a', 'd');
1642 my $long = 'b' x 2000;
1643 my $eval = q{$s =~ m{$x[bbb]c} ? 1 : 0};
1644 $eval =~ s{bbb}{$long};
1645 my $match = eval $eval;
1646 ok(1, "did not crash");
1647 ok($match, "[bbb...] resolved as character class, not subscript");
1651 for my $pat ('(??', '(?P', '(?i-') {
1652 eval qq{ qr/$pat/ };
1653 ok(1, "qr/$pat/ did not crash");
1654 eval qq{ qr/${pat}\x{123}/ };
1656 like($e, qr{\x{123}},
1657 "qr/${pat}x/ shows x in error even if it's a wide character");
1662 # Expect one of these sizes to cause overflow and wrap to negative
1663 for my $bits (32, 64) {
1664 my $wrapneg = 2 ** ($bits - 2) * 3;
1665 for my $sign ('', '-') {
1666 my $pat = sprintf "qr/(?%s%u)/", $sign, $wrapneg;
1668 ok(1, "big backref $pat did not crash");
1673 # Test that we handle qr/\8888888/ and variants without an infinite loop,
1674 # we use a test within a test so we can todo it, and make sure we don't
1675 # infinite loop our tests.
1676 # NOTE - Do not put quotes in the code!
1677 # NOTE - We have to triple escape the backref in the pattern below.
1679 BEGIN{require q(test.pl);}
1681 for my $len (1 .. 20) {
1682 my $eights= q(8) x $len;
1683 eval qq{ qr/\\\\$eights/ };
1685 print q(No infinite loop here!);
1687 fresh_perl_is($code, "No infinite loop here!", {},
1688 "test that we handle things like m/\\888888888/ without infinite loops" );
1691 { # Test that we handle some malformed UTF-8 without looping [perl
1695 BEGIN{require q(test.pl);}
1696 use Encode qw(_utf8_on);
1697 # \x80 and \x41 are continuation bytes in their respective
1699 my $malformed = (ord("A") == 65) ? "a\x80\n" : "a\x41\n";
1700 utf8::downgrade($malformed);
1701 _utf8_on($malformed);
1703 $malformed =~ /(\n\r|\r)$/;
1704 print q(No infinite loop here!);
1706 fresh_perl_like($code, qr/Malformed UTF-8 character/, {},
1707 "test that we handle some UTF-8 malformations without looping" );
1711 # [perl #123843] hits SEGV trying to compile this pattern
1713 eval q{ ($match) = ("xxyxxyxy" =~ m{(x+(y(?1))*)}) };
1714 ok(1, "compiled GOSUB in CURLYM ok");
1715 is($match, 'xxyxxyx', "matched GOSUB in CURLYM");
1719 # [perl #123852] doesn't avoid all the capture-related work with
1720 # //n, leading to possible memory corruption
1721 eval q{ qr{()(?1)}n };
1723 ok(1, "qr{()(?1)}n didn't crash");
1724 like($error, qr{Reference to nonexistent group},
1725 'gave appropriate error for qr{()(?1)}n');
1729 # [perl #126406] panic with unmatchable quantifier
1731 no warnings "regexp";
1732 "" =~ m/(.0\N{6,0}0\N{6,0}000000000000000000000000000000000)/;
1734 fresh_perl_is($code, "", {},
1735 "perl [#126406] panic");
1738 my $bug="[perl #126182]"; # test for infinite pattern recursion
1740 [ 'q(a)=~/(.(?2))((?<=(?=(?1)).))/', "died", "look ahead left recursion fails fast" ],
1741 [ 'q(aa)=~/(?R)a/', "died", "left-recursion fails fast", ],
1742 [ 'q(bbaa)=~/(?&x)(?(DEFINE)(?<x>(?&y)*a)(?<y>(?&x)*b))/',
1743 "died", "inter-cyclic optional left recursion dies" ],
1744 [ 'q(abc) =~ /a((?1)?)c/', "died", "optional left recursion dies" ],
1745 [ 'q(abc) =~ /a((?1)??)c/', "died", "min mod left recursion dies" ],
1746 [ 'q(abc) =~ /a((?1)*)c/', "died", "* left recursion dies" ],
1747 [ 'q(abc) =~ /a((?1)+)c/', "died", "+ left recursion dies" ],
1748 [ 'q(abc) =~ /a((?1){0,3})c/', "died", "{0,3} left recursion fails fast" ],
1750 [ 'q(aaabbb)=~/a(?R)?b/', "matched", "optional self recursion works" ],
1751 [ '"((5maa-maa)(maa-3maa))" =~ /(\\\\((?:[^()]++|(?0))*+\\\\))/', "matched",
1752 "recursion and possessive captures", "((5maa-maa)(maa-3maa))"],
1753 [ '"((5maa-maa)(maa-3maa))" =~ /(\\\\((?:[^()]++|(?1))*+\\\\))/', "matched",
1754 "recursion and possessive captures", "((5maa-maa)(maa-3maa))"],
1755 [ '"((5maa-maa)(maa-3maa))" =~ /(\\\\((?:[^()]+|(?0))*\\\\))/', "matched",
1756 "recursion and possessive captures", "((5maa-maa)(maa-3maa))"],
1757 [ '"((5maa-maa)(maa-3maa))" =~ /(\\\\((?:[^()]+|(?1))*\\\\))/', "matched",
1758 "recursion and possessive captures", "((5maa-maa)(maa-3maa))"],
1760 my ($expr, $expect, $test_name, $cap1)= @$tuple;
1761 # avoid quotes in this code!
1763 BEGIN{require q(test.pl);}
1765 my $status= eval(q{ !(' . $expr . ') ? q(failed) : ' .
1766 ($cap1 ? '($1 ne q['.$cap1.']) ? qq(badmatch:$1) : ' : '') .
1768 || ( ( $@ =~ /Infinite recursion/ ) ? qq(died) : q(strange-death) );
1771 fresh_perl_is($code, $expect, {}, "$bug - $test_name" );
1776 BEGIN{require q(test.pl);}
1778 $SIG{ALRM} = sub {print "Timeout\n"; exit(1)};
1780 $_ = "a" x 1000 . "b" x 1000 . "c" x 1000;
1782 ',"Timeout",{},"Test Perl 73464")
1785 { # [perl #128686], crashed the the interpreter
1786 my $AE = chr utf8::unicode_to_native(0xC6);
1787 my $ae = chr utf8::unicode_to_native(0xE6);
1788 my $re = qr/[$ae\s]/i;
1789 ok($AE !~ $re, '/[\xE6\s]/i doesn\'t match \xC6 when not in UTF-8');
1791 ok($AE =~ $re, '/[\xE6\s]/i matches \xC6 when in UTF-8');
1794 { # [perl #126606 crashed the interpreter
1795 no warnings 'deprecated';
1796 like("sS", qr/\N{}Ss|/i, "\N{} with empty branch alternation works");
1800 local $::TODO = "RT #21491: m'' interpolates escape sequences";
1801 is(0+("\n" =~ m'\n'), 0, q|RT #21491: m'\n' should not interpolate|);
1803 } # End of sub run_tests