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.
19 @INC = ('../lib','.');
21 skip_all_if_miniperl("miniperl can't load Tie::Hash::NamedCapture, need for %+ and %-");
25 plan tests => 2527; # Update this when adding/deleting tests.
27 run_tests() unless caller;
34 like("A \x{263a} B z C", qr/A . B (??{ "z" }) C/,
35 "Match UTF-8 char in presence of (??{ }); Bug 20000731.001");
38 no warnings 'uninitialized';
39 ok(undef =~ /^([^\/]*)(.*)$/, "Used to cause a SEGV; Bug 20001021.005");
43 my $message = 'bug id 20001008.001';
45 my @x = ("stra\337e 138", "stra\337e 138");
47 ok(s/(\d+)\s*([\w\-]+)/$1 . uc $2/e, $message);
48 ok(my ($latin) = /^(.+)(?:\s+\d)/, $message);
49 is($latin, "stra\337e", $message);
50 ok($latin =~ s/stra\337e/straße/, $message);
52 # Previous code follows, but outcommented - there were no tests.
54 # $latin =~ s/stra\337e/straße/; # \303\237 after the 2nd a
55 # use utf8; # needed for the raw UTF-8
56 # $latin =~ s!(s)tr(?:aß|s+e)!$1tr.!; # \303\237 after the a
61 # Fist half of the bug.
62 my $message = 'HEBREW ACCENT QADMA matched by .*; Bug 20001028.003';
64 ok(my ($Y) = $X =~ /(.*)/, $message);
65 is($Y, v1448, $message);
66 is(length $Y, 1, $message);
68 # Second half of the bug.
69 $message = 'HEBREW ACCENT QADMA in replacement; Bug 20001028.003';
71 $X =~ s/^/chr(1488)/e;
72 is(length $X, 1, $message);
73 is(ord $X, 1488, $message);
77 my $message = 'Repeated s///; Bug 20001108.001';
78 my $X = "Szab\x{f3},Bal\x{e1}zs";
80 $Y =~ s/(B)/$1/ for 0 .. 3;
82 is($X, "Szab\x{f3},Bal\x{e1}zs", $message);
86 my $message = 's/// on UTF-8 string; Bug 20000517.001';
89 is($x, "\x{100}B", $message);
90 is(length $x, 2, $message);
94 my $message = '\C and É; Bug 20001230.002';
95 ok("École" =~ /^\C\C(.)/ && $1 eq 'c', $message);
96 like("École", qr/^\C\C(c)/, $message);
100 # The original bug report had 'no utf8' here but that was irrelevant.
102 my $message = "Don't dump core; Bug 20010306.008";
104 like($a, qr/\w/, $message); # used to core dump.
108 my $message = '/g in scalar context; Bug 20010410.006';
109 for my $rx ('/(.*?)\{(.*?)\}/csg',
110 '/(.*?)\{(.*?)\}/cg',
111 '/(.*?)\{(.*?)\}/sg',
113 '/(.+?)\{(.+?)\}/csg',) {
115 my $input = "a{b}c{d}";
117 while (eval \$input =~ $rx) {
126 # Amazingly vertical tabulator is the same in ASCII and EBCDIC.
127 for ("\n", "\t", "\014", "\r") {
128 unlike($_, qr/[[:print:]]/, sprintf "\\%03o not in [[:print:]]; Bug 20010619.003", ord $_);
131 like($_, qr/[[:print:]]/, "'$_' in [[:print:]]; Bug 20010619.003");
136 # [ID 20010814.004] pos() doesn't work when using =~m// in list context
139 my $a = join ":", /b./gc;
140 my $b = join ":", /a./gc;
142 is("$a $b $c", 'ba:ba ad:ae 10', "pos() works with () = m//; Bug 20010814.004");
146 # [ID 20010407.006] matching utf8 return values from
147 # functions does not work
149 my $message = 'UTF-8 return values from functions; Bug 20010407.006';
150 package ID_20010407_006;
154 ::ok($x =~ /(..)/, $message);
156 ::ok(length ($y) == 2 && $y eq $x, $message);
157 ::ok(x =~ /(..)/, $message);
159 ::ok(length ($y) == 2 && $y eq $x, $message);
163 # High bit bug -- japhy
165 ok $x =~ /.*?\200/, "High bit fine";
169 my $message = 'UTF-8 hash keys and /$/';
170 # http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters
171 # /2002-01/msg01327.html
174 my $v = substr ($u, 0, 1);
175 my $w = substr ($u, 1, 1);
176 my %u = ($u => $u, $v => $v, $w => $w);
178 my $m1 = /^\w*$/ ? 1 : 0;
179 my $m2 = $u {$_} =~ /^\w*$/ ? 1 : 0;
180 is($m1, $m2, $message);
185 my $message = "s///eg [change 13f46d054db22cf4]; Bug 20020124.005";
187 for my $char ("a", "\x{df}", "\x{100}") {
188 my $x = "$char b $char";
193 is(substr ($x, 0, 1), substr ($x, -1, 1), $message);
198 my $message = "Correct pmop flags checked when empty pattern; Bug 20020412.005";
200 # Requires reuse of last successful pattern.
205 ok($match != $_, $message)
206 or diag(sprintf "'match one' %s on %s iteration" =>
207 $match ? 'succeeded' : 'failed',
208 $_ ? 'second' : 'first');
211 my $result = join "" => $num =~ //g;
212 is($result, $num, $message);
216 my $message = 'UTF-8 regex matches above 32k; Bug 20020630.002';
217 for (['byte', "\x{ff}"], ['utf8', "\x{1ff}"]) {
218 my ($type, $char) = @$_;
219 for my $len (32000, 32768, 33000) {
220 my $s = $char . "f" x $len;
221 my $r = $s =~ /$char([f]*)/gc;
222 ok($r, $message) or diag("<$type x $len>");
223 ok(!$r || pos ($s) == $len + 1, $message)
224 or diag("<$type x $len>; pos = @{[pos $s]}");
230 my $s = "\x{100}" x 5;
231 my $ok = $s =~ /(\x{100}{4})/;
232 my ($ord, $len) = (ord $1, length $1);
233 ok $ok && $ord == 0x100 && $len == 4, "No panic: end_shift [change 0e933229fa758625]";
238 chop $a; # Leaves the UTF-8 flag
239 $a .= "y"; # 1 byte before 'y'.
241 like($a, qr/^\C/, 'match one \C on 1-byte UTF-8; Bug 15763');
242 like($a, qr/^\C{1}/, 'match \C{1}; Bug 15763');
244 like($a, qr/^\Cy/, 'match \Cy; Bug 15763');
245 like($a, qr/^\C{1}y/, 'match \C{1}y; Bug 15763');
247 unlike($a, qr/^\C\Cy/, q {don't match two \Cy; Bug 15763});
248 unlike($a, qr/^\C{2}y/, q {don't match \C{2}y; Bug 15763});
250 $a = "\x{100}y"; # 2 bytes before "y"
252 like($a, qr/^\C/, 'match one \C on 2-byte UTF-8; Bug 15763');
253 like($a, qr/^\C{1}/, 'match \C{1}; Bug 15763');
254 like($a, qr/^\C\C/, 'match two \C; Bug 15763');
255 like($a, qr/^\C{2}/, 'match \C{2}; Bug 15763');
257 like($a, qr/^\C\C\C/, 'match three \C on 2-byte UTF-8 and a byte; Bug 15763');
258 like($a, qr/^\C{3}/, 'match \C{3}; Bug 15763');
260 like($a, qr/^\C\Cy/, 'match two \C; Bug 15763');
261 like($a, qr/^\C{2}y/, 'match \C{2}; Bug 15763');
263 unlike($a, qr/^\C\C\Cy/, q {don't match three \Cy; Bug 15763});
264 unlike($a, qr/^\C{2}\Cy/, q {don't match \C{2}\Cy; Bug 15763});
265 unlike($a, qr/^\C{3}y/, q {don't match \C{3}y; Bug 15763});
267 $a = "\x{1000}y"; # 3 bytes before "y"
269 like($a, qr/^\C/, 'match one \C on three-byte UTF-8; Bug 15763');
270 like($a, qr/^\C{1}/, 'match \C{1}; Bug 15763');
271 like($a, qr/^\C\C/, 'match two \C; Bug 15763');
272 like($a, qr/^\C{2}/, 'match \C{2}; Bug 15763');
273 like($a, qr/^\C\C\C/, 'match three \C; Bug 15763');
274 like($a, qr/^\C{3}/, 'match \C{3}; Bug 15763');
276 like($a, qr/^\C\C\C\C/, 'match four \C on three-byte UTF-8 and a byte; Bug 15763');
277 like($a, qr/^\C{4}/, 'match \C{4}; Bug 15763');
279 like($a, qr/^\C\C\Cy/, 'match three \Cy; Bug 15763');
280 like($a, qr/^\C{3}y/, 'match \C{3}y; Bug 15763');
282 unlike($a, qr/^\C\C\C\Cy/, q {don't match four \Cy; Bug 15763});
283 unlike($a, qr/^\C{4}y/, q {don't match \C{4}y; Bug 15763});
288 my $message = 'UTF-8 matching; Bug 15397';
289 like("\x{100}", qr/\x{100}/, $message);
290 like("\x{100}", qr/(\x{100})/, $message);
291 like("\x{100}", qr/(\x{100}){1}/, $message);
292 like("\x{100}\x{100}", qr/(\x{100}){2}/, $message);
293 like("\x{100}\x{100}", qr/(\x{100})(\x{100})/, $message);
297 my $message = 'Neither ()* nor ()*? sets $1 when matched 0 times; Bug 7471';
299 ok(/(AB)*?CD/ && !defined $1, $message);
300 ok(/(AB)*CD/ && !defined $1, $message);
304 my $message = "Caching shouldn't prevent match; Bug 3547";
305 my $pattern = "^(b+?|a){1,2}c";
306 ok("bac" =~ /$pattern/ && $1 eq 'a', $message);
307 ok("bbac" =~ /$pattern/ && $1 eq 'a', $message);
308 ok("bbbac" =~ /$pattern/ && $1 eq 'a', $message);
309 ok("bbbbac" =~ /$pattern/ && $1 eq 'a', $message);
313 ok("\x{100}" =~ /(.)/, '$1 should keep UTF-8 ness; Bug 18232');
314 is($1, "\x{100}", '$1 is UTF-8; Bug 18232');
316 is($1, "\x{100}", '$1 is still UTF-8; Bug 18232');
317 isnt($1, "\xC4\x80", '$1 is not non-UTF-8; Bug 18232');
321 my $message = "Optimizer doesn't prematurely reject match; Bug 19767";
325 my $NormalChar = qr /[\p{IsDigit}\p{IsLower}\p{IsUpper}]/;
326 my $NormalWord = qr /${NormalChar}+?/;
327 my $PredNameHyphen = qr /^${NormalWord}(\-${NormalWord})*?$/;
330 like($attr, $PredNameHyphen, $message); # Original test.
333 like("0", qr/\p{N}+\z/, $message); # Variant.
337 my $message = "(??{ }) doesn't return stale values; Bug 20683";
339 foreach (1, 2, 3, 4) {
340 $p ++ if /(??{ $p })/
347 sub TIESCALAR {bless []}
351 foreach (1, 2, 3, 4) {
358 # Subject: Odd regexp behavior
359 # From: Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>
360 # Date: Wed, 26 Feb 2003 16:53:12 +0000
361 # Message-Id: <E18o4nw-0008Ly-00@wisbech.cl.cam.ac.uk>
362 # To: perl-unicode@perl.org
364 my $message = 'Markus Kuhn 2003-02-26';
366 my $x = "\x{2019}\nk";
367 ok($x =~ s/(\S)\n(\S)/$1 $2/sg, $message);
368 is($x, "\x{2019} k", $message);
371 ok($x =~ s/(\S)\n(\S)/$1 $2/sg, $message);
372 is($x, "b k", $message);
374 like("\x{2019}", qr/\S/, $message);
378 my $message = "(??{ .. }) in split doesn't corrupt its stack; Bug 21411";
380 is('-1-3-5-', join('', split /((??{$i++}))/, '-1-3-5-'), $message);
381 no warnings 'syntax';
382 @_ = split /(?{'WOW'})/, 'abc';
384 is("@_", "a|b|c", $message);
387 is(join('-', split /(?{ split "" })/, "abc"), 'a-b-c', 'nested split');
390 $_ = "code: 'x' { '...' }\n"; study;
391 my @x; push @x, $& while m/'[^\']*'/gx;
393 is("@x", "'x':'...'", "Parse::RecDescent triggered infinite loop; Bug 17757");
398 ok("a\nb" !~ /^b/, "Propagated modifier; $_[0]; Bug 22354");
399 ok("a\nb" =~ /^b/m, "Propagated modifier; $_[0] - with /m; Bug 22354");
402 $_ = "x"; s/x/func "in subst"/e;
403 $_ = "x"; s/x/func "in multiline subst"/em;
404 $_ = "x"; /x(?{func "in regexp"})/;
405 $_ = "x"; /x(?{func "in multiline regexp"})/m;
411 is("abcde", $`, 'Global match sets $`; Bug 19049');
415 # [perl #23769] Unicode regex broken on simple example
416 # regrepeat() didn't handle UTF-8 EXACT case right.
418 my $Mess = 'regrepeat() handles UTF-8 EXACT case right';
419 my $message = "$Mess; Bug 23769";
421 my $s = "\x{a0}\x{a0}\x{a0}\x{100}"; chop $s;
423 like($s, qr/\x{a0}/, $message);
424 like($s, qr/\x{a0}+/, $message);
425 like($s, qr/\x{a0}\x{a0}/, $message);
427 $message = "$Mess (easy variant); Bug 23769";
428 ok("aaa\x{100}" =~ /(a+)/, $message);
429 is($1, "aaa", $message);
431 $message = "$Mess (easy invariant); Bug 23769";
432 ok("aaa\x{100} " =~ /(a+?)/, $message);
433 is($1, "a", $message);
435 $message = "$Mess (regrepeat variant); Bug 23769";
436 ok("\xa0\xa0\xa0\x{100} " =~ /(\xa0+?)/, $message);
437 is($1, "\xa0", $message);
439 $message = "$Mess (regrepeat invariant); Bug 23769";
440 ok("\xa0\xa0\xa0\x{100}" =~ /(\xa0+)/, $message);
441 is($1, "\xa0\xa0\xa0", $message);
443 $message = "$Mess (hard variant); Bug 23769";
444 ok("\xa0\xa1\xa0\xa1\xa0\xa1\x{100}" =~ /((?:\xa0\xa1)+?)/, $message);
445 is($1, "\xa0\xa1", $message);
447 $message = "$Mess (hard invariant); Bug 23769";
448 ok("ababab\x{100} " =~ /((?:ab)+)/, $message);
449 is($1, 'ababab', $message);
451 ok("\xa0\xa1\xa0\xa1\xa0\xa1\x{100}" =~ /((?:\xa0\xa1)+)/, $message);
452 is($1, "\xa0\xa1\xa0\xa1\xa0\xa1", $message);
454 ok("ababab\x{100} " =~ /((?:ab)+?)/, $message);
455 is($1, "ab", $message);
457 $message = "Don't match first byte of UTF-8 representation; Bug 23769";
458 unlike("\xc4\xc4\xc4", qr/(\x{100}+)/, $message);
459 unlike("\xc4\xc4\xc4", qr/(\x{100}+?)/, $message);
460 unlike("\xc4\xc4\xc4", qr/(\x{100}++)/, $message);
464 # perl panic: pp_match start/end pointers
466 is(eval {my ($x, $y) = "bca" =~ /^(?=.*(a)).*(bc)/; "$x-$y"}, "a-bc",
467 'Captures can move backwards in string; Bug 25269');
471 # \cA not recognized in character classes
472 like("a\cAb", qr/\cA/, '\cA in pattern; Bug 27940');
473 like("a\cAb", qr/[\cA]/, '\cA in character class; Bug 27940');
474 like("a\cAb", qr/[\cA-\cB]/, '\cA in character class range; Bug 27940');
475 like("abc", qr/[^\cA-\cB]/, '\cA in negated character class range; Bug 27940');
476 like("a\cBb", qr/[\cA-\cC]/, '\cB in character class range; Bug 27940');
477 like("a\cCbc", qr/[^\cA-\cB]/, '\cC in negated character class range; Bug 27940');
478 like("a\cAb", qr/(??{"\cA"})/, '\cA in ??{} pattern; Bug 27940');
479 unlike("ab", qr/a\cIb/x, '\cI in pattern; Bug 27940');
483 # perl #28532: optional zero-width match at end of string is ignored
485 ok("abc" =~ /^abc(\z)?/ && defined($1),
486 'Optional zero-width match at end of string; Bug 28532');
487 ok("abc" =~ /^abc(\z)??/ && !defined($1),
488 'Optional zero-width match at end of string; Bug 28532');
492 my $utf8 = "\xe9\x{100}"; chop $utf8;
495 like($utf8, qr/\xe9/i, "utf8/latin; Bug 36207");
496 like($utf8, qr/$latin1/i, "utf8/latin runtime; Bug 36207");
497 like($utf8, qr/(abc|\xe9)/i, "utf8/latin trie; Bug 36207");
498 like($utf8, qr/(abc|$latin1)/i, "utf8/latin trie runtime; Bug 36207");
500 like("\xe9", qr/$utf8/i, "latin/utf8; Bug 36207");
501 like("\xe9", qr/(abc|$utf8)/i, "latin/utf8 trie; Bug 36207");
502 like($latin1, qr/$utf8/i, "latin/utf8 runtime; Bug 36207");
503 like($latin1, qr/(abc|$utf8)/i, "latin/utf8 trie runtime; Bug 36207");
512 "Assigning to original string does not corrupt match vars; Bug 37038");
520 my $aeek = bless {} => 'wooosh';
521 is(do {$aeek -> gloople () =~ /(.)/g}, 1,
522 "//g match against return value of sub [change e26a497577f3ce7b]");
525 is(do{gloople () =~ /(.)/g}, 1,
526 "change e26a497577f3ce7b didn't affect sub calls for some reason");
531 # See changes 26925-26928, which reverted change 26410
535 sub variable : lvalue {$var}
537 my $o = bless [] => 'lv';
541 $f .= $1 if $o -> variable =~ /(.)/g;
546 is($f, "ab", "pos() retained between calls");
549 ok 0, "Code failed: $@";
553 sub variable : lvalue {$var}
557 $g .= $1 if variable =~ /(.)/g;
562 is($g, "ab", "pos() retained between calls");
565 ok 0, "Code failed: $@";
571 skip "In EBCDIC" if $::IS_EBCDIC;
573 $_ = pack 'U0C2', 0xa2, 0xf8; # Ill-formed UTF-8
575 is(do {!($ret = s/[\0]+//g)}, 1,
576 "Ill-formed UTF-8 doesn't match NUL in class; Bug 37836");
580 # chr(65535) should be allowed in regexes
582 no warnings 'utf8'; # To allow non-characters
587 is($c, "", "U+FFFF, parsed as atom; Bug 38293");
592 is($c, "", "U+FFFF backslashed, parsed as atom; Bug 38293");
596 is($c, "", "U+FFFF, parsed in class; Bug 38293");
601 is($c, "", "U+FFFF backslashed, parsed in class; Bug 38293");
605 is($s, "AB", "U+FFFF, EXACTF; Bug 38293");
609 is($s, "\x{ffff}", "U+FFFF, BOUND; Bug 38293");
613 is($s, "\x{ffff}", "U+FFFF, NBOUND; Bug 38293");
618 # The printing characters
619 my @chars = ("A" .. "Z");
621 my $size = 32771 - 4;
624 # Create some random junk. Inefficient, but it works.
625 for (my $i = 0; $i < $size; $ i++) {
626 $str .= $chars [rand @chars];
629 $str .= ($delim x 4);
632 ok($str =~ s/^(.*?)${delim}{4}//s, "Pattern matches; Bug 39583");
633 is($str, "", "Empty string; Bug 39583");
634 ok(defined $1 && length ($1) == $size, '$1 is correct size; Bug 39583');
638 like("\0-A", qr/\c@-A/, '@- should not be interpolated in a pattern; Bug 27940');
639 like("\0\0A", qr/\c@+A/, '@+ should not be interpolated in a pattern; Bug 27940');
640 like("X\@-A", qr/X@-A/, '@- should not be interpolated in a pattern; Bug 27940');
641 like("X\@\@A", qr/X@+A/, '@+ should not be interpolated in a pattern; Bug 27940');
643 like("X\0A", qr/X\c@?A/, '\c@?; Bug 27940');
644 like("X\0A", qr/X\c@*A/, '\c@*; Bug 27940');
645 like("X\0A", qr/X\c@(A)/, '\c@(; Bug 27940');
646 like("X\0A", qr/X(\c@)A/, '\c@); Bug 27940');
647 like("X\0A", qr/X\c@|ZA/, '\c@|; Bug 27940');
649 like("X\@A", qr/X@?A/, '@?; Bug 27940');
650 like("X\@A", qr/X@*A/, '@*; Bug 27940');
651 like("X\@A", qr/X@(A)/, '@(; Bug 27940');
652 like("X\@A", qr/X(@)A/, '@); Bug 27940');
653 like("X\@A", qr/X@|ZA/, '@|; Bug 27940');
655 local $" = ','; # non-whitespace and non-RE-specific
656 like('abc', qr/(.)(.)(.)/, 'The last successful match is bogus; Bug 27940');
657 like("A@+B", qr/A@{+}B/, 'Interpolation of @+ in /@{+}/; Bug 27940');
658 like("A@-B", qr/A@{-}B/, 'Interpolation of @- in /@{-}/; Bug 27940');
659 like("A@+B", qr/A@{+}B/x, 'Interpolation of @+ in /@{+}/x; Bug 27940');
660 like("A@-B", qr/A@{-}B/x, 'Interpolation of @- in /@{-}/x; Bug 27940');
664 my $s = 'foo bar baz';
665 my (@k, @v, @fetch, $res);
667 my @names = qw ($+{A} $+{B} $+{C});
668 if ($s =~ /(?<A>foo)\s+(?<B>bar)?\s+(?<C>baz)/) {
669 while (my ($k, $v) = each (%+)) {
673 @v = sort values (%+);
683 is($fetch[$_][0], $fetch[$_][1], "$names[$_]; Bug 50496");
688 is($res, 1, "'$s' =~ /(?<A>foo)\\s+(?<B>bar)?\\s+(?<C>baz)/; Bug 50496");
689 is($count, 3, "Got 3 keys in %+ via each; Bug 50496");
690 is(0 + @k, 3, "Got 3 keys in %+ via keys; Bug 50496");
691 is("@k", "A B C", "Got expected keys; Bug 50496");
692 is("@v", "bar baz foo", "Got expected values; Bug 50496");
694 no warnings "uninitialized";
695 print for $+ {this_key_doesnt_exist};
697 is($@, '', 'lvalue $+ {...} should not throw an exception; Bug 50496');
702 # Almost the same as the block above, except that the capture is nested.
705 my $s = 'foo bar baz';
706 my (@k, @v, @fetch, $res);
708 my @names = qw ($+{A} $+{B} $+{C} $+{D});
709 if ($s =~ /(?<D>(?<A>foo)\s+(?<B>bar)?\s+(?<C>baz))/) {
710 while (my ($k,$v) = each(%+)) {
714 @v = sort values (%+);
725 is($fetch[$_][0], $fetch[$_][1], "$names[$_]; Bug 50496");
730 is($res, 1, "'$s' =~ /(?<D>(?<A>foo)\\s+(?<B>bar)?\\s+(?<C>baz))/; Bug 50496");
731 is($count, 4, "Got 4 keys in %+ via each; Bug 50496");
732 is(@k, 4, "Got 4 keys in %+ via keys; Bug 50496");
733 is("@k", "A B C D", "Got expected keys; Bug 50496");
734 is("@v", "bar baz foo foo bar baz", "Got expected values; Bug 50496");
736 no warnings "uninitialized";
737 print for $+ {this_key_doesnt_exist};
739 is($@, '', 'lvalue $+ {...} should not throw an exception; Bug 50496');
747 while ($str =~ /b/g) {$mval = $#-; $pval = $#+; $count ++}
748 is($mval, 0, '@- should be empty; Bug 36046');
749 is($pval, 0, '@+ should be empty; Bug 36046');
750 is($count, 1, 'Should have matched once only; Bug 36046');
754 my $message = '/m in precompiled regexp; Bug 40684';
756 my $rex = qr'^abc$'m;
757 ok($s =~ m/$rex/, $message);
758 ok($s =~ m/^abc$/m, $message);
762 my $message = '(?: ... )? should not lose $^R; Bug 36909';
766 ok('x foofoo y' =~ m {
767 (foo) # $^R correctly set
768 (?{ "last regexp code result" })
770 is($^R, 'last regexp code result', $message);
772 is($^R, 'Nothing', $message);
777 ok('x foofoo y' =~ m {
778 (?:foo|bar)+ # $^R correctly set
779 (?{ "last regexp code result" })
781 is($^R, 'last regexp code result', $message);
783 is($^R, 'Nothing', $message);
787 ok('x foofoo y' =~ m {
788 (foo|bar)\1+ # $^R undefined
789 (?{ "last regexp code result" })
791 is($^R, 'last regexp code result', $message);
793 is($^R, 'Nothing', $message);
797 ok('x foofoo y' =~ m {
798 (foo|bar)\1 # This time without the +
799 (?{"last regexp code result"})
801 is($^R, 'last regexp code result', $message);
803 is($^R, 'Nothing', $message);
807 my $message = 'Match is linear, not quadratic; Bug 22395';
809 for my $l (10, 100, 1000) {
811 ('a' x $l) =~ /(.*)(?{$count++})[bc]/;
812 local $::TODO = "Should be L+1 not L*(L+3)/2 (L=$l)";
813 is($count, $l + 1, $message);
818 my $message = '@-/@+ should not have undefined values; Bug 22614';
821 /(.){1,}(?{push @len,0+@-})(.){1,}(?{})^/;
822 is("@len", "2 2 2", $message);
826 my $message = '$& set on s///; Bug 18209';
827 my $text = ' word1 word2 word3 word4 word5 word6 ';
829 my @words = ('word1', 'word3', 'word5');
831 foreach my $word (@words) {
832 $text =~ s/$word\s//gi; # Leave a space to separate words
833 # in the resultant str.
834 # The following block is not working.
840 is($count, 3, $message);
841 is($text, ' word2 word4 word6 ', $message);
847 local $_ = qq (A\nB\nC\n);
849 while (m#(\G|\n)([^\n]*)\n#gsx) {
853 is("@res", "A B C", "/g pattern shouldn't infinite loop; Bug 6893");
858 my @tails = ('', '(?(1))', '(|)', '()?');
859 my @quants = ('*','+');
863 for my $pat (@$pats) {
864 for my $quant (@quants) {
865 for my $tail (@tails) {
866 my $re = "($pat$quant\$)$tail";
867 ok(/$re/ && $1 eq $_, "'$_' =~ /$re/; Bug 41010");
868 ok(/$re/m && $1 eq $_, "'$_' =~ /$re/m; Bug 41010");
877 '(1|[23]|4|[56]|[78]|[90])',
878 '(?:1|[23]|4|[56]|[78]|[90])',
879 '(1|2|3|4|5|6|7|8|9|0)',
880 '(?:1|2|3|4|5|6|7|8|9|0)');
881 my @spats = ('[ ]', ' ', '( |\t)', '(?: |\t)', '[ \t]', '\s');
883 my @dstrs = ('12345');
884 $doit -> (\@spats, @sstrs);
885 $doit -> (\@dpats, @dstrs);
889 # [perl #45605] Regexp failure with utf8-flagged and byte-flagged string
891 my $utf_8 = "\xd6schel";
892 utf8::upgrade ($utf_8);
893 $utf_8 =~ m {(\xd6|Ö)schel};
894 is($1, "\xd6", "Upgrade error; Bug 45605");
898 # Regardless of utf8ness any character matches itself when
899 # doing a case insensitive match. See also [perl #36207]
901 for my $o (0 .. 255) {
902 my @ch = (chr ($o), chr ($o));
903 utf8::upgrade ($ch [1]);
904 for my $u_str (0, 1) {
905 for my $u_pat (0, 1) {
906 like($ch[$u_str], qr/\Q$ch[$u_pat]\E/i,
907 "\$c =~ /\$c/i : chr ($o) : u_str = $u_str u_pat = $u_pat; Bug 36207");
908 like($ch[$u_str], qr/\Q$ch[$u_pat]\E|xyz/i,
909 "\$c=~/\$c|xyz/i : chr($o) : u_str = $u_str u_pat = $u_pat; Bug 36207");
916 my $message = '$REGMARK in replacement; Bug 49190';
919 ok(s/(*:B)A/$REGMARK/, $message);
920 is($_, "B", $message);
922 ok(s/(*:X)A+|(*:Y)B+|(*:Z)C+/$REGMARK/g, $message);
923 is($_, "ZYX", $message);
924 # Use a longer name to force reallocation of $REGMARK.
926 ok(s/(*:X)A+|(*:YYYYYYYYYYYYYYYY)B+|(*:Z)C+/$REGMARK/g, $message);
927 is($_, "ZYYYYYYYYYYYYYYYYX", $message);
931 my $message = 'Substitution evaluation in list context; Bug 52658';
933 my @te = ($reg =~ m{^(/?(?:\.\./)*)},
934 $reg =~ s/(x)/'b'/eg > 1 ? '##' : '++');
935 is($reg, '../bbb/', $message);
936 is($te [0], '../', $message);
940 my $a = "xyzt" x 8192;
941 like($a, qr/\A(?>[a-z])*\z/,
942 '(?>) does not cause wrongness on long string; Bug 60034');
943 my $b = $a . chr 256;
945 is($a, $b, 'Bug 60034');
946 like($b, qr/\A(?>[a-z])*\z/,
947 '(?>) does not cause wrongness on long string with UTF-8; Bug 60034');
951 # Keep the following tests last -- they may crash perl
953 print "# Tests that follow may crash perl\n";
956 my $message = 'Pattern in a loop, failure should not ' .
957 'affect previous success; Bug 19049/38869';
959 'ab cdef', # Matches regex
960 ('e' x 40000 ) .'ab c' # Matches not, but 'ab c' matches part of it
965 m/ab(.+)cd/i; # The ignore-case seems to be important
966 $y = $1; # Use $1, which might not be from the last match!
967 $x = substr ($list [0], $- [0], $+ [0] - $- [0]);
969 is($y, ' ', $message);
970 is($x, 'ab cd', $message);
974 ok (("a" x (2 ** 15 - 10)) =~ /^()(a|bb)*$/, "Recursive stack cracker; Bug 24274");
975 ok ((q(a)x 100) =~ /^(??{'(.)'x 100})/,
976 "Regexp /^(??{'(.)'x 100})/ crashes older perls; Bug 24274");
980 # [perl #45337] utf8 + "[a]a{2}" + /$.../ = panic: sv_len_utf8 cache
982 local ${^UTF8CACHE} = -1;
983 my $message = "Shouldn't panic; Bug 45337";
986 like("aaa", qr/$s/, $message);
989 my $message = "Check if tree logic breaks \$^R; Bug 57042";
997 for my $line ("(A)","(B)") {
998 if ($line =~ m/$cond_re/) {
999 push @res, $^R ? "#$^R" : "UNDEF";
1002 is("@res","#1 #2", $message);
1005 no warnings 'closure';
1006 my $re = qr/A(??{"1"})/;
1007 ok "A1B" =~ m/^((??{ $re }))((??{"B"}))$/;
1012 # This only works under -DEBUGGING because it relies on an assert().
1014 # Check capture offset re-entrancy of utf8 code.
1016 sub fswash { $_[0] =~ s/([>X])//g; }
1018 my $k1 = "." x 4 . ">>";
1021 my $k2 = "\x{f1}\x{2022}";
1022 $k2 =~ s/([\360-\362])/>/g;
1025 is($k2, "\x{2022}", "utf8::SWASHNEW doesn't cause capture leaks; Bug 60508");
1029 # minimal CURLYM limited to 32767 matches
1031 qr{a(x|y)*b}, # CURLYM
1032 qr{a(x|y)*?b}, # .. with minmod
1033 qr{a([wx]|[yz])*b}, # .. and without tries
1034 qr{a([wx]|[yz])*?b},
1037 my $s = join '', 'a', 'x' x $len, 'b';
1038 for my $pat (@pat) {
1039 like($s, $pat, "$pat; Bug 65372");
1044 local $::TODO = "[perl #38133]";
1046 "A" =~ /(((?:A))?)+/;
1052 is($first, $second);
1057 = 'utf8 =~ /trie/ where trie matches a continuation octet; Bug 70998';
1061 local $SIG{__WARN__} = sub { $w .= shift };
1063 # This bug can be reduced to
1064 qq{\x{30ab}} =~ /\xab|\xa9/;
1065 # but it's nice to have a more 'real-world' test. The original test
1066 # case from the RT ticket follows:
1072 my $conv_rx = '(' . join('|', map { quotemeta } keys %conv) . ')';
1073 $conv_rx = qr{$conv_rx};
1076 = qq{\x{3042}\x{304b}\x{3055}\x{305f}\x{306a}\x{306f}\x{307e}}
1077 . qq{\x{3084}\x{3089}\x{308f}\x{3093}\x{3042}\x{304b}\x{3055}}
1078 . qq{\x{305f}\x{306a}\x{306f}\x{307e}\x{3084}\x{3089}\x{308f}}
1079 . qq{\x{3093}\x{30a2}\x{30ab}\x{30b5}\x{30bf}\x{30ca}\x{30cf}}
1080 . qq{\x{30de}\x{30e4}\x{30e9}\x{30ef}\x{30f3}\x{30a2}\x{30ab}}
1081 . qq{\x{30b5}\x{30bf}\x{30ca}\x{30cf}\x{30de}\x{30e4}\x{30e9}}
1082 . qq{\x{30ef}\x{30f3}\x{30a2}\x{30ab}\x{30b5}\x{30bf}\x{30ca}}
1083 . qq{\x{30cf}\x{30de}\x{30e4}\x{30e9}\x{30ef}\x{30f3}};
1085 $x =~ s{$conv_rx}{$conv{$1}}eg;
1087 is($w, undef, $message);
1091 # minimal CURLYM limited to 32767 matches
1093 is(join("-", " abc def " =~ /(?=(\S+))/g), "abc-bc-c-def-ef-f",
1094 'stclass optimisation does not break + inside (?=); Bug 68564');
1098 use charnames ":full";
1099 # Delayed interpolation of \N'
1100 my $r1 = qr/\N{THAI CHARACTER SARA I}/;
1101 my $s1 = "\x{E34}\x{E34}\x{E34}\x{E34}";
1104 ok $s1 =~ /$r1+/, 'my $r1 = qr/\N{THAI CHARACTER SARA I}/; my $s1 = "\x{E34}\x{E34}\x{E34}\x{E34}; $s1 =~ /$r1+/';
1107 ok "${s1}A" =~ m/$s1\N{LATIN CAPITAL LETTER A}/, '"${s1}A" =~ m/$s1\N{LATIN CAPITAL LETTER A}/';
1109 ok "abbbbc" =~ m/\N{1}/ && $& eq "a", '"abbbbc" =~ m/\N{1}/ && $& eq "a"';
1110 ok "abbbbc" =~ m/\N{3,4}/ && $& eq "abbb", '"abbbbc" =~ m/\N{3,4}/ && $& eq "abbb"';
1114 use charnames ":full";
1115 my $message = '[perl #74982] Period coming after \N{}';
1116 ok("\x{ff08}." =~ m/\N{FULLWIDTH LEFT PARENTHESIS}./ && $& eq "\x{ff08}.", $message);
1117 ok("\x{ff08}." =~ m/[\N{FULLWIDTH LEFT PARENTHESIS}]./ && $& eq "\x{ff08}.", $message);
1121 ######## "Segfault using HTML::Entities", Richard Jolly <richardjolly@mac.com>, <A3C7D27E-C9F4-11D8-B294-003065AE00B6@mac.com> in perl-unicode@perl.org
1123 skip('Perl configured without Encode module', 1)
1124 unless $Config{extensions} =~ / Encode /;
1126 # Test case cut down by jhi
1127 fresh_perl_like(<<'EOP', qr!Malformed UTF-8 character \(unexpected end of string\) in substitution \(s///\) at!, 'Segfault using HTML::Entities');
1129 my $t = ord('A') == 193 ? "\xEA" : "\xE9";
1130 Encode::_utf8_on($t);
1136 # pattern must be compiled late or we can break the test file
1137 my $message = '[perl #115050] repeated nothings in a trie can cause panic';
1139 $pattern = '[xyz]|||';
1140 ok("blah blah" =~ /$pattern/, $message);
1141 ok("blah blah" =~ /(?:$pattern)h/, $message);
1142 $pattern = '|||[xyz]';
1143 ok("blah blah" =~ /$pattern/, $message);
1144 ok("blah blah" =~ /(?:$pattern)h/, $message);
1146 } # End of sub run_tests