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