fix bug #57042 - preserve $^R across TRIE matches
[p5sagit/p5-mst-13.2.git] / t / op / pat.t
1 #!./perl
2 #
3 # This is a home for regular expression tests that don't fit into
4 # the format supported by op/regexp.t.  If you want to add a test
5 # that does fit that format, add it to op/re_tests, not here.
6
7 use strict;
8 use warnings;
9 use 5.010;
10
11
12 sub run_tests;
13
14 $| = 1;
15
16 my $EXPECTED_TESTS = 3865;  # Update this when adding/deleting tests.
17
18 BEGIN {
19     chdir 't' if -d 't';
20     @INC = '../lib';
21 }
22 our $TODO;
23 our $Message = "Noname test";
24 our $Error;
25 our $DiePattern;
26 our $WarnPattern;
27 our $BugId;
28 our $PatchId;
29 our $running_as_thread;
30
31 my $ordA = ord ('A');  # This defines ASCII/UTF-8 vs EBCDIC/UTF-EBCDIC
32 # This defined the platform.
33 my $IS_ASCII  = $ordA ==  65;
34 my $IS_EBCDIC = $ordA == 193;
35
36 use vars '%Config';
37 eval 'use Config';          #  Defaults assumed if this fails
38
39 my $test = 0;
40
41 print "1..$EXPECTED_TESTS\n";
42
43 run_tests unless caller ();
44
45 END {
46 }
47
48 sub pretty {
49     my ($mess) = @_;
50     $mess =~ s/\n/\\n/g;
51     $mess =~ s/\r/\\r/g;
52     $mess =~ s/\t/\\t/g;
53     $mess =~ s/([\00-\37\177])/sprintf '\%03o', ord $1/eg;
54     $mess =~ s/#/\\#/g;
55     $mess;
56 }
57
58 sub safe_globals {
59     defined($_) and s/#/\\#/g for $BugId, $PatchId, $TODO;
60 }
61
62 sub _ok {
63     my ($ok, $mess, $error) = @_;
64     safe_globals();
65     $mess    = pretty ($mess // $Message);
66     $mess   .= "; Bug $BugId"     if defined $BugId;
67     $mess   .= "; Patch $PatchId" if defined $PatchId;
68     $mess   .= " # TODO $TODO"     if defined $TODO;
69
70     my $line_nr = (caller(1)) [2];
71
72     printf "%sok %d - %s\n",
73               ($ok ? "" : "not "),
74               ++ $test,
75               "$mess\tLine $line_nr";
76
77     unless ($ok) {
78         print "# Failed test at line $line_nr\n" unless defined $TODO;
79         if ($error //= $Error) {
80             no warnings 'utf8';
81             chomp $error;
82             $error = join "\n#", map {pretty $_} split /\n\h*#/ => $error;
83             $error = "# $error" unless $error =~ /^\h*#/;
84             print $error, "\n";
85         }
86     }
87
88     return $ok;
89 }
90
91 # Force scalar context on the pattern match
92 sub  ok ($;$$) {_ok  $_ [0], $_ [1], $_ [2]}
93 sub nok ($;$$) {_ok !$_ [0], "Failed: " . ($_ [1] // $Message), $_ [2]}
94
95
96 sub skip {
97     my $why = shift;
98     safe_globals();
99     $why =~ s/\n.*//s;
100     $why .= "; Bug $BugId" if defined $BugId;
101     # seems like the new harness code doesnt like todo and skip to be mixed.
102     # which seems like a bug in the harness to me. -- dmq
103     #$why .= " # TODO $TODO" if defined $TODO;
104     
105     my $n = shift // 1;
106     my $line_nr = (caller(0)) [2];
107     for (1 .. $n) {
108         ++ $test;
109         #print "not " if defined $TODO;
110         print "ok $test # skip $why\tLine $line_nr\n";
111     }
112     no warnings "exiting";
113     last SKIP;
114 }
115
116 sub iseq ($$;$) { 
117     my ($got, $expect, $name) = @_;
118     
119     $_ = defined ($_) ? "'$_'" : "undef" for $got, $expect;
120         
121     my $ok    = $got eq $expect;
122     my $error = "# expected: $expect\n" .
123                 "#   result: $got";
124
125     _ok $ok, $name, $error;
126 }   
127
128 sub isneq ($$;$) { 
129     my ($got, $expect, $name) = @_;
130     my $todo = $TODO ? " # TODO $TODO" : '';
131     
132     $_ = defined ($_) ? "'$_'" : "undef" for $got, $expect;
133         
134     my $ok    = $got ne $expect;
135     my $error = "# results are equal ($got)";
136
137     _ok $ok, $name, $error;
138 }   
139
140
141 sub eval_ok ($;$) {
142     my ($code, $name) = @_;
143     local $@;
144     if (ref $code) {
145         _ok eval {&$code} && !$@, $name;
146     }
147     else {
148         _ok eval  ($code) && !$@, $name;
149     }
150 }
151
152 sub must_die {
153     my ($code, $pattern, $name) = @_;
154     $pattern //= $DiePattern;
155     undef $@;
156     ref $code ? &$code : eval $code;
157     my  $r = $@ && $@ =~ /$pattern/;
158     _ok $r, $name // $Message // "\$\@ =~ /$pattern/";
159 }
160
161 sub must_warn {
162     my ($code, $pattern, $name) = @_;
163     $pattern //= $WarnPattern;
164     my $w;
165     local $SIG {__WARN__} = sub {$w .= join "" => @_};
166     use warnings 'all';
167     ref $code ? &$code : eval $code;
168     my $r = $w && $w =~ /$pattern/;
169     $w //= "UNDEF";
170     _ok $r, $name // $Message // "Got warning /$pattern/",
171             "# expected: /$pattern/\n" .
172             "#   result: $w";
173 }
174
175 sub may_not_warn {
176     my ($code, $name) = @_;
177     my $w;
178     local $SIG {__WARN__} = sub {$w .= join "" => @_};
179     use warnings 'all';
180     ref $code ? &$code : eval $code;
181     _ok !$w, $name // ($Message ? "$Message (did not warn)"
182                                 : "Did not warn"),
183              "Got warning '$w'";
184 }
185
186
187 #
188 # Tests start here.
189 #
190 sub run_tests {
191
192     {
193
194         my $x = "abc\ndef\n";
195
196         ok $x =~ /^abc/,  qq ["$x" =~ /^abc/];
197         ok $x !~ /^def/,  qq ["$x" !~ /^def/];
198
199         # used to be a test for $*
200         ok $x =~ /^def/m, qq ["$x" =~ /^def/m];
201
202         nok $x =~ /^xxx/, qq ["$x" =~ /^xxx/];
203         nok $x !~ /^abc/, qq ["$x" !~ /^abc/];
204
205          ok $x =~ /def/, qq ["$x" =~ /def/];
206         nok $x !~ /def/, qq ["$x" !~ /def/];
207
208          ok $x !~ /.def/, qq ["$x" !~ /.def/];
209         nok $x =~ /.def/, qq ["$x" =~ /.def/];
210
211          ok $x =~ /\ndef/, qq ["$x" =~ /\ndef/];
212         nok $x !~ /\ndef/, qq ["$x" !~ /\ndef/];
213     }
214
215     {
216         $_ = '123';
217         ok /^([0-9][0-9]*)/, qq [\$_ = '$_'; /^([0-9][0-9]*)/];
218     }
219
220     {
221         $_ = 'aaabbbccc';
222          ok /(a*b*)(c*)/ && $1 eq 'aaabbb' && $2 eq 'ccc',
223                                              qq [\$_ = '$_'; /(a*b*)(c*)/];
224          ok /(a+b+c+)/ && $1 eq 'aaabbbccc', qq [\$_ = '$_'; /(a+b+c+)/];
225         nok /a+b?c+/,                        qq [\$_ = '$_'; /a+b?c+/];
226
227         $_ = 'aaabccc';
228          ok /a+b?c+/, qq [\$_ = '$_'; /a+b?c+/];
229          ok /a*b?c*/, qq [\$_ = '$_'; /a*b?c*/];
230
231         $_ = 'aaaccc';
232          ok /a*b?c*/, qq [\$_ = '$_'; /a*b?c*/];
233         nok /a*b+c*/, qq [\$_ = '$_'; /a*b+c*/];
234
235         $_ = 'abcdef';
236          ok /bcd|xyz/, qq [\$_ = '$_'; /bcd|xyz/];
237          ok /xyz|bcd/, qq [\$_ = '$_'; /xyz|bcd/];
238          ok m|bc/*d|,  qq [\$_ = '$_'; m|bc/*d|];
239          ok /^$_$/,    qq [\$_ = '$_'; /^\$_\$/];
240     }
241
242     {
243         # used to be a test for $*
244         ok "ab\ncd\n" =~ /^cd/m, qq ["ab\ncd\n" =~ /^cd/m];
245     }
246
247     {
248         our %XXX = map {($_ => $_)} 123, 234, 345;
249
250         our @XXX = ('ok 1','not ok 1', 'ok 2','not ok 2','not ok 3');
251         while ($_ = shift(@XXX)) {
252             my $f = index ($_, 'not') >= 0 ? \&nok : \&ok;
253             my $r = ?(.*)?;
254             &$f ($r, "?(.*)?");
255             /not/ && reset;
256             if (/not ok 2/) {
257                 if ($^O eq 'VMS') {
258                     $_ = shift(@XXX);
259                 }
260                 else {
261                     reset 'X';
262                 }
263             }
264         }
265
266         SKIP: {
267             if ($^O eq 'VMS') {
268                 skip "Reset 'X'", 1;
269             }
270             ok !keys %XXX, "%XXX is empty";
271         }
272
273     }
274
275     {
276         local $Message = "Test empty pattern";
277         my $xyz = 'xyz';
278         my $cde = 'cde';
279
280         $cde =~ /[^ab]*/;
281         $xyz =~ //;
282         iseq $&, $xyz;
283
284         my $foo = '[^ab]*';
285         $cde =~ /$foo/;
286         $xyz =~ //;
287         iseq $&, $xyz;
288
289         $cde =~ /$foo/;
290         my $null;
291         no warnings 'uninitialized';
292         $xyz =~ /$null/;
293         iseq $&, $xyz;
294
295         $null = "";
296         $xyz =~ /$null/;
297         iseq $&, $xyz;
298     }
299
300     {
301         local $Message = q !Check $`, $&, $'!;
302         $_ = 'abcdefghi';
303         /def/;          # optimized up to cmd
304         iseq "$`:$&:$'", 'abc:def:ghi';
305
306         no warnings 'void';
307         /cde/ + 0;      # optimized only to spat
308         iseq "$`:$&:$'", 'ab:cde:fghi';
309
310         /[d][e][f]/;    # not optimized
311         iseq "$`:$&:$'", 'abc:def:ghi';
312     }
313
314     {
315         $_ = 'now is the {time for all} good men to come to.';
316         / {([^}]*)}/;
317         iseq $1, 'time for all', "Match braces";
318     }
319
320     {
321         local $Message = "{N,M} quantifier";
322         $_ = 'xxx {3,4}  yyy   zzz';
323         ok /( {3,4})/;
324         iseq $1, '   ';
325         ok !/( {4,})/;
326         ok /( {2,3}.)/;
327         iseq $1, '  y';
328         ok /(y{2,3}.)/;
329         iseq $1, 'yyy ';
330         ok !/x {3,4}/;
331         ok !/^xxx {3,4}/;
332     }
333
334     {
335         local $Message = "Test /g";
336         local $" = ":";
337         $_ = "now is the time for all good men to come to.";
338         my @words = /(\w+)/g;
339         my $exp   = "now:is:the:time:for:all:good:men:to:come:to";
340
341         iseq "@words", $exp;
342
343         @words = ();
344         while (/\w+/g) {
345             push (@words, $&);
346         }
347         iseq "@words", $exp;
348
349         @words = ();
350         pos = 0;
351         while (/to/g) {
352             push(@words, $&);
353         }
354         iseq "@words", "to:to";
355
356         pos $_ = 0;
357         @words = /to/g;
358         iseq "@words", "to:to";
359     }
360
361     {
362         $_ = "abcdefghi";
363
364         my $pat1 = 'def';
365         my $pat2 = '^def';
366         my $pat3 = '.def.';
367         my $pat4 = 'abc';
368         my $pat5 = '^abc';
369         my $pat6 = 'abc$';
370         my $pat7 = 'ghi';
371         my $pat8 = '\w*ghi';
372         my $pat9 = 'ghi$';
373
374         my $t1 = my $t2 = my $t3 = my $t4 = my $t5 =
375         my $t6 = my $t7 = my $t8 = my $t9 = 0;
376
377         for my $iter (1 .. 5) {
378             $t1++ if /$pat1/o;
379             $t2++ if /$pat2/o;
380             $t3++ if /$pat3/o;
381             $t4++ if /$pat4/o;
382             $t5++ if /$pat5/o;
383             $t6++ if /$pat6/o;
384             $t7++ if /$pat7/o;
385             $t8++ if /$pat8/o;
386             $t9++ if /$pat9/o;
387         }
388         my $x = "$t1$t2$t3$t4$t5$t6$t7$t8$t9";
389         iseq $x, '505550555', "Test /o";
390     }
391
392
393     SKIP: {
394         my $xyz = 'xyz';
395         ok "abc" =~ /^abc$|$xyz/, "| after \$";
396
397         # perl 4.009 says "unmatched ()"
398         local $Message = '$ inside ()';
399
400         my $result;
401         eval '"abc" =~ /a(bc$)|$xyz/; $result = "$&:$1"';
402         iseq $@, "" or skip "eval failed", 1;
403         iseq $result, "abc:bc";
404     }
405
406
407     {
408         local $Message = "Scalar /g";
409         $_ = "abcfooabcbar";
410
411         ok  /abc/g && $` eq "";
412         ok  /abc/g && $` eq "abcfoo";
413         ok !/abc/g;
414
415         local $Message = "Scalar /gi";
416         pos = 0;
417         ok  /ABC/gi && $` eq "";
418         ok  /ABC/gi && $` eq "abcfoo";
419         ok !/ABC/gi;
420
421         local $Message = "Scalar /g";
422         pos = 0;
423         ok  /abc/g && $' eq "fooabcbar";
424         ok  /abc/g && $' eq "bar";
425
426         $_ .= '';
427         my @x = /abc/g;
428         iseq @x, 2, "/g reset after assignment";
429     }
430
431     {
432         local $Message = '/g, \G and pos';
433         $_ = "abdc";
434         pos $_ = 2;
435         /\Gc/gc;
436         iseq pos $_, 2;
437         /\Gc/g;
438         ok !defined pos $_;
439     }
440
441     {
442         local $Message = '(?{ })';
443         our $out = 1;
444         'abc' =~ m'a(?{ $out = 2 })b';
445         iseq $out, 2;
446
447         $out = 1;
448         'abc' =~ m'a(?{ $out = 3 })c';
449         iseq $out, 1;
450     }
451
452
453     {
454         $_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6';
455         my @out = /(?<!foo)bar./g;
456         iseq "@out", 'bar2 barf', "Negative lookbehind";
457     }
458
459     {
460         local $Message = "REG_INFTY tests";
461         # Tests which depend on REG_INFTY
462         $::reg_infty   = $Config {reg_infty} // 32767;
463         $::reg_infty_m = $::reg_infty - 1;
464         $::reg_infty_p = $::reg_infty + 1;
465         $::reg_infty_m = $::reg_infty_m;   # Surpress warning.
466
467         # As well as failing if the pattern matches do unexpected things, the
468         # next three tests will fail if you should have picked up a lower-than-
469         # default value for $reg_infty from Config.pm, but have not.
470
471         eval_ok q (('aaa' =~ /(a{1,$::reg_infty_m})/)[0] eq 'aaa');
472         eval_ok q (('a' x $::reg_infty_m) =~ /a{$::reg_infty_m}/);
473         eval_ok q (('a' x ($::reg_infty_m - 1)) !~ /a{$::reg_infty_m}/);
474         eval "'aaa' =~ /a{1,$::reg_infty}/";
475         ok $@ =~ /^\QQuantifier in {,} bigger than/;
476         eval "'aaa' =~ /a{1,$::reg_infty_p}/";
477         ok $@ =~ /^\QQuantifier in {,} bigger than/;
478     }
479
480     {
481         # Poke a couple more parse failures
482         my $context = 'x' x 256;
483         eval qq("${context}y" =~ /(?<=$context)y/);
484         ok $@ =~ /^\QLookbehind longer than 255 not/, "Lookbehind limit";
485     }
486
487     {
488         # Long Monsters
489         local $Message = "Long monster";
490         for my $l (125, 140, 250, 270, 300000, 30) { # Ordered to free memory
491             my $a = 'a' x $l;
492             local $Error = "length = $l";
493              ok "ba$a=" =~ /a$a=/;
494             nok "b$a="  =~ /a$a=/;
495              ok "b$a="  =~ /ba+=/;
496
497             local $TODO = "See bug 60464" if $l > 32767;
498              ok "ba$a=" =~ /b(?:a|b)+=/;
499         }
500     }
501
502
503     {
504         # 20000 nodes, each taking 3 words per string, and 1 per branch
505         my $long_constant_len = join '|', 12120 .. 32645;
506         my $long_var_len = join '|', 8120 .. 28645;
507         my %ans = ( 'ax13876y25677lbc' => 1,
508                     'ax13876y25677mcb' => 0, # not b.
509                     'ax13876y35677nbc' => 0, # Num too big
510                     'ax13876y25677y21378obc' => 1,
511                     'ax13876y25677y21378zbc' => 0,      # Not followed by [k-o]
512                     'ax13876y25677y21378y21378kbc' => 1,
513                     'ax13876y25677y21378y21378kcb' => 0, # Not b.
514                     'ax13876y25677y21378y21378y21378kbc' => 0, # 5 runs
515                   );
516
517         local $Message = "20000 nodes";
518         for (keys %ans) {
519             local $Error = "const-len '$_'";
520             ok !($ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o);
521
522             local $Error = "var-len '$_'";
523             ok !($ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o);
524         }
525     }
526
527     {
528         local $Message = "Complicated backtracking";
529         $_ = " a (bla()) and x(y b((l)u((e))) and b(l(e)e)e";
530         my $expect = "(bla()) ((l)u((e))) (l(e)e)";
531
532         use vars '$c';
533         sub matchit {
534           m/
535              (
536                \(
537                (?{ $c = 1 })    # Initialize
538                (?:
539                  (?(?{ $c == 0 })   # PREVIOUS iteration was OK, stop the loop
540                    (?!
541                    )            # Fail: will unwind one iteration back
542                  )      
543                  (?:
544                    [^()]+               # Match a big chunk
545                    (?=
546                      [()]
547                    )            # Do not try to match subchunks
548                  |
549                    \(
550                    (?{ ++$c })
551                  |
552                    \)
553                    (?{ --$c })
554                  )
555                )+               # This may not match with different subblocks
556              )
557              (?(?{ $c != 0 })
558                (?!
559                )                # Fail
560              )                  # Otherwise the chunk 1 may succeed with $c>0
561            /xg;
562         }
563
564         my @ans = ();
565         my $res;
566         push @ans, $res while $res = matchit;
567         iseq "@ans", "1 1 1";
568
569         @ans = matchit;
570         iseq "@ans", $expect;
571
572         local $Message = "Recursion with (??{ })";
573         our $matched;
574         $matched = qr/\((?:(?>[^()]+)|(??{$matched}))*\)/;
575
576         @ans = my @ans1 = ();
577         push (@ans, $res), push (@ans1, $&) while $res = m/$matched/g;
578
579         iseq "@ans", "1 1 1";
580         iseq "@ans1", $expect;
581
582         @ans = m/$matched/g;
583         iseq "@ans", $expect;
584
585     }
586
587     {
588         ok "abc" =~ /^(??{"a"})b/, '"abc" =~ /^(??{"a"})b/';
589     }
590
591     {
592         my @ans = ('a/b' =~ m%(.*/)?(.*)%);     # Stack may be bad
593         iseq "@ans", 'a/ b', "Stack may be bad";
594     }
595
596     {
597         local $Message = "Eval-group not allowed at runtime";
598         my $code = '{$blah = 45}';
599         our $blah = 12;
600         eval { /(?$code)/ };
601         ok $@ && $@ =~ /not allowed at runtime/ && $blah == 12;
602
603         for $code ('{$blah = 45}','=xx') {
604             $blah = 12;
605             my $res = eval { "xx" =~ /(?$code)/o };
606             no warnings 'uninitialized';
607             local $Error = "'$@', '$res', '$blah'";
608             if ($code eq '=xx') {
609                 ok !$@ && $res;
610             }
611             else {
612                 ok $@ && $@ =~ /not allowed at runtime/ && $blah == 12;
613             }
614         }
615
616         $code = '{$blah = 45}';
617         $blah = 12;
618         eval "/(?$code)/";
619         iseq $blah, 45;
620
621         $blah = 12;
622         /(?{$blah = 45})/;
623         iseq $blah, 45;
624     }
625
626     {
627         local $Message = "Pos checks";
628         my $x = 'banana';
629         $x =~ /.a/g;
630         iseq pos ($x), 2;
631
632         $x =~ /.z/gc;
633         iseq pos ($x), 2;
634
635         sub f {
636             my $p = $_[0];
637             return $p;
638         }
639
640         $x =~ /.a/g;
641         iseq f (pos ($x)), 4;
642     }
643
644     {
645         local $Message = 'Checking $^R';
646         our $x = $^R = 67;
647         'foot' =~ /foo(?{$x = 12; 75})[t]/;
648         iseq $^R, 75;
649
650         $x = $^R = 67;
651         'foot' =~ /foo(?{$x = 12; 75})[xy]/;
652         ok $^R eq '67' && $x eq '12';
653
654         $x = $^R = 67;
655         'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/;
656         ok $^R eq '79' && $x eq '12';
657     }
658
659     {
660         iseq qr/\b\v$/i,    '(?i-xsm:\b\v$)', 'qr/\b\v$/i';
661         iseq qr/\b\v$/s,    '(?s-xim:\b\v$)', 'qr/\b\v$/s';
662         iseq qr/\b\v$/m,    '(?m-xis:\b\v$)', 'qr/\b\v$/m';
663         iseq qr/\b\v$/x,    '(?x-ism:\b\v$)', 'qr/\b\v$/x';
664         iseq qr/\b\v$/xism, '(?msix:\b\v$)',  'qr/\b\v$/xism';
665         iseq qr/\b\v$/,     '(?-xism:\b\v$)', 'qr/\b\v$/';
666     }
667
668
669     {
670         local $Message = "Look around";
671         $_ = 'xabcx';
672       SKIP:
673         foreach my $ans ('', 'c') {
674             ok /(?<=(?=a)..)((?=c)|.)/g or skip "Match failed", 1;
675             iseq $1, $ans;
676         }
677     }
678
679     {
680         local $Message = "Empty clause";
681         $_ = 'a';
682         foreach my $ans ('', 'a', '') {
683             ok /^|a|$/g or skip "Match failed", 1;
684             iseq $&, $ans;
685         }
686     }
687
688     {
689         local $Message = "Prefixify";
690         sub prefixify {
691             SKIP: {
692                 my ($v, $a, $b, $res) = @_;
693                 ok $v =~ s/\Q$a\E/$b/ or skip "Match failed", 1;
694                 iseq $v, $res;
695             }
696         }
697
698         prefixify ('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch');
699         prefixify ('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch');
700     }
701
702     {
703         $_ = 'var="foo"';
704         /(\")/;
705         ok $1 && /$1/, "Capture a quote";
706     }
707
708     {
709         local $Message =  "Call code from qr //";
710         $a = qr/(?{++$b})/;
711         $b = 7;
712         ok /$a$a/ && $b eq '9';
713
714         $c="$a";
715         ok /$a$a/ && $b eq '11';
716
717         undef $@;
718         eval {/$c/};
719         ok $@ && $@ =~ /not allowed at runtime/;
720
721         use re "eval";
722         /$a$c$a/;
723         iseq $b, '14';
724
725         our $lex_a = 43;
726         our $lex_b = 17;
727         our $lex_c = 27;
728         my $lex_res = ($lex_b =~ qr/$lex_b(?{ $lex_c = $lex_a++ })/);
729
730         iseq $lex_res, 1;
731         iseq $lex_a, 44;
732         iseq $lex_c, 43;
733
734         no re "eval";
735         undef $@;
736         my $match = eval { /$a$c$a/ };
737         ok $@ && $@ =~ /Eval-group not allowed/ && !$match;
738         iseq $b, '14';
739      
740         $lex_a = 2;
741         $lex_a = 43;
742         $lex_b = 17;
743         $lex_c = 27;
744         $lex_res = ($lex_b =~ qr/17(?{ $lex_c = $lex_a++ })/);
745
746         iseq $lex_res, 1;
747         iseq $lex_a, 44;
748         iseq $lex_c, 43;
749
750     }
751
752
753     {
754         no warnings 'closure';
755         local $Message = '(?{ $var } refers to package vars';
756         package aa;
757         our $c = 2;
758         $::c = 3;
759         '' =~ /(?{ $c = 4 })/;
760         main::iseq $c, 4;
761         main::iseq $::c, 3;
762     }
763
764
765     {
766         must_die 'q(a:[b]:) =~ /[x[:foo:]]/',
767                  'POSIX class \[:[^:]+:\] unknown in regex',
768                  'POSIX class [: :] must have valid name';
769
770         for my $d (qw [= .]) {
771             must_die "/[[${d}foo${d}]]/",
772                      "\QPOSIX syntax [$d $d] is reserved for future extensions",
773                      "POSIX syntax [[$d $d]] is an error";
774         }
775     }
776
777
778     {
779         # test if failure of patterns returns empty list
780         local $Message = "Failed pattern returns empty list";
781         $_ = 'aaa';
782         @_ = /bbb/;
783         iseq "@_", "";
784
785         @_ = /bbb/g;
786         iseq "@_", "";
787
788         @_ = /(bbb)/;
789         iseq "@_", "";
790
791         @_ = /(bbb)/g;
792         iseq "@_", "";
793     }
794
795     
796     {
797         local $Message = '@- and @+ tests';
798
799         /a(?=.$)/;
800         iseq $#+, 0;
801         iseq $#-, 0;
802         iseq $+ [0], 2;
803         iseq $- [0], 1;
804         ok !defined $+ [1] && !defined $- [1] &&
805            !defined $+ [2] && !defined $- [2];
806
807         /a(a)(a)/;
808         iseq $#+, 2;
809         iseq $#-, 2;
810         iseq $+ [0], 3;
811         iseq $- [0], 0;
812         iseq $+ [1], 2;
813         iseq $- [1], 1;
814         iseq $+ [2], 3;
815         iseq $- [2], 2;
816         ok !defined $+ [3] && !defined $- [3] &&
817            !defined $+ [4] && !defined $- [4];
818
819
820         /.(a)(b)?(a)/;
821         iseq $#+, 3;
822         iseq $#-, 3;
823         iseq $+ [1], 2;
824         iseq $- [1], 1;
825         iseq $+ [3], 3;
826         iseq $- [3], 2;
827         ok !defined $+ [2] && !defined $- [2] &&
828            !defined $+ [4] && !defined $- [4];
829
830
831         /.(a)/;
832         iseq $#+, 1;
833         iseq $#-, 1;
834         iseq $+ [0], 2;
835         iseq $- [0], 0;
836         iseq $+ [1], 2;
837         iseq $- [1], 1;
838         ok !defined $+ [2] && !defined $- [2] &&
839            !defined $+ [3] && !defined $- [3];
840
841         /.(a)(ba*)?/;
842         iseq $#+, 2;
843         iseq $#-, 1;
844     }
845
846
847     {
848         local $DiePattern = '^Modification of a read-only value attempted';
849         local $Message    = 'Elements of @- and @+ are read-only';
850         must_die '$+[0] = 13';
851         must_die '$-[0] = 13';
852         must_die '@+ = (7, 6, 5)';
853         must_die '@- = qw (foo bar)';
854     }
855
856
857     {
858         local $Message = '\G testing';
859         $_ = 'aaa';
860         pos = 1;
861         my @a = /\Ga/g;
862         iseq "@a", "a a";
863
864         my $str = 'abcde';
865         pos $str = 2;
866         ok $str !~ /^\G/;
867         ok $str !~ /^.\G/;
868         ok $str =~ /^..\G/;
869         ok $str !~ /^...\G/;
870         ok $str =~ /\G../ && $& eq 'cd';
871
872         local $TODO = $running_as_thread;
873         ok $str =~ /.\G./ && $& eq 'bc';
874     }
875
876
877     {
878         local $Message = 'pos inside (?{ })';
879         my $str = 'abcde';
880         our ($foo, $bar);
881         ok $str =~ /b(?{$foo = $_; $bar = pos})c/;
882         iseq $foo, $str;
883         iseq $bar, 2;
884         ok !defined pos ($str);
885
886         undef $foo;
887         undef $bar;
888         pos $str = undef;
889         ok $str =~ /b(?{$foo = $_; $bar = pos})c/g;
890         iseq $foo, $str;
891         iseq $bar, 2;
892         iseq pos ($str), 3;
893
894         $_ = $str;
895         undef $foo;
896         undef $bar;
897         ok /b(?{$foo = $_; $bar = pos})c/;
898         iseq $foo, $str;
899         iseq $bar, 2;
900
901         undef $foo;
902         undef $bar;
903         ok /b(?{$foo = $_; $bar = pos})c/g;
904         iseq $foo, $str;
905         iseq $bar, 2;
906         iseq pos, 3;
907
908         undef $foo;
909         undef $bar;
910         pos = undef;
911         1 while /b(?{$foo = $_; $bar = pos})c/g;
912         iseq $foo, $str;
913         iseq $bar, 2;
914         ok !defined pos;
915
916         undef $foo;
917         undef $bar;
918         $_ = 'abcde|abcde';
919         ok s/b(?{$foo = $_; $bar = pos})c/x/g;
920         iseq $foo, 'abcde|abcde';
921         iseq $bar, 8;
922         iseq $_, 'axde|axde';
923
924         # List context:
925         $_ = 'abcde|abcde';
926         our @res;
927         () = /([ace]).(?{push @res, $1,$2})([ce])(?{push @res, $1,$2})/g;
928         @res = map {defined $_ ? "'$_'" : 'undef'} @res;
929         iseq "@res", "'a' undef 'a' 'c' 'e' undef 'a' undef 'a' 'c'";
930
931         @res = ();
932         () = /([ace]).(?{push @res, $`,$&,$'})([ce])(?{push @res, $`,$&,$'})/g;
933         @res = map {defined $_ ? "'$_'" : 'undef'} @res;
934         iseq "@res", "'' 'ab' 'cde|abcde' " .
935                      "'' 'abc' 'de|abcde' " .
936                      "'abcd' 'e|' 'abcde' " .
937                      "'abcde|' 'ab' 'cde' " .
938                      "'abcde|' 'abc' 'de'" ;
939     }
940
941
942     {
943         local $Message = '\G anchor checks';
944         my $foo = 'aabbccddeeffgg';
945         pos ($foo) = 1;
946         {
947             local $TODO = $running_as_thread;
948             no warnings 'uninitialized';
949             ok $foo =~ /.\G(..)/g;
950             iseq $1, 'ab';
951
952             pos ($foo) += 1;
953             ok $foo =~ /.\G(..)/g;
954             iseq $1, 'cc';
955
956             pos ($foo) += 1;
957             ok $foo =~ /.\G(..)/g;
958             iseq $1, 'de';
959
960             ok $foo =~ /\Gef/g;
961         }
962
963         undef pos $foo;
964         ok $foo =~ /\G(..)/g;
965         iseq $1, 'aa';
966
967         ok $foo =~ /\G(..)/g;
968         iseq $1, 'bb';
969
970         pos ($foo) = 5;
971         ok $foo =~ /\G(..)/g;
972         iseq $1, 'cd';
973     }
974
975
976     {
977         $_ = '123x123';
978         my @res = /(\d*|x)/g;
979         local $" = '|';
980         iseq "@res", "123||x|123|", "0 match in alternation";
981     }
982
983
984     {
985         local $Message = "Match against temporaries (created via pp_helem())" .
986                          " is safe";
987         ok {foo => "bar\n" . $^X} -> {foo} =~ /^(.*)\n/g;
988         iseq $1, "bar";
989     }
990
991
992     {
993         local $Message = 'package $i inside (?{ }), ' .
994                          'saved substrings and changing $_';
995         our @a = qw [foo bar];
996         our @b = ();
997         s/(\w)(?{push @b, $1})/,$1,/g for @a;
998         iseq "@b", "f o o b a r";
999         iseq "@a", ",f,,o,,o, ,b,,a,,r,";
1000
1001         local $Message = 'lexical $i inside (?{ }), ' .
1002                          'saved substrings and changing $_';
1003         no warnings 'closure';
1004         my @c = qw [foo bar];
1005         my @d = ();
1006         s/(\w)(?{push @d, $1})/,$1,/g for @c;
1007         iseq "@d", "f o o b a r";
1008         iseq "@c", ",f,,o,,o, ,b,,a,,r,";
1009     }
1010
1011
1012     {
1013         local $Message = 'Brackets';
1014         our $brackets;
1015         $brackets = qr {
1016             {  (?> [^{}]+ | (??{ $brackets }) )* }
1017         }x;
1018
1019         ok "{{}" =~ $brackets;
1020         iseq $&, "{}";
1021         ok "something { long { and } hairy" =~ $brackets;
1022         iseq $&, "{ and }";
1023         ok "something { long { and } hairy" =~ m/((??{ $brackets }))/;
1024         iseq $&, "{ and }";
1025     }
1026
1027
1028     {
1029         $_ = "a-a\nxbb";
1030         pos = 1;
1031         nok m/^-.*bb/mg, '$_ = "a-a\nxbb"; m/^-.*bb/mg';
1032     }
1033
1034
1035     {
1036         local $Message = '\G anchor checks';
1037         my $text = "aaXbXcc";
1038         pos ($text) = 0;
1039         ok $text !~ /\GXb*X/g;
1040     }
1041
1042
1043     {
1044         $_ = "xA\n" x 500;
1045         nok /^\s*A/m, '$_ = "xA\n" x 500; /^\s*A/m"';
1046
1047         my $text = "abc dbf";
1048         my @res = ($text =~ /.*?(b).*?\b/g);
1049         iseq "@res", "b b", '\b is not special';
1050     }
1051
1052
1053     {
1054         local $Message = '\S, [\S], \s, [\s]';
1055         my @a = map chr, 0 .. 255;
1056         my @b = grep /\S/, @a;
1057         my @c = grep /[^\s]/, @a;
1058         iseq "@b", "@c";
1059
1060         @b = grep /\S/, @a;
1061         @c = grep /[\S]/, @a;
1062         iseq "@b", "@c";
1063
1064         @b = grep /\s/, @a;
1065         @c = grep /[^\S]/, @a;
1066         iseq "@b", "@c";
1067
1068         @b = grep /\s/, @a;
1069         @c = grep /[\s]/, @a;
1070         iseq "@b", "@c";
1071     }
1072     {
1073         local $Message = '\D, [\D], \d, [\d]';
1074         my @a = map chr, 0 .. 255;
1075         my @b = grep /\D/, @a;
1076         my @c = grep /[^\d]/, @a;
1077         iseq "@b", "@c";
1078
1079         @b = grep /\D/, @a;
1080         @c = grep /[\D]/, @a;
1081         iseq "@b", "@c";
1082
1083         @b = grep /\d/, @a;
1084         @c = grep /[^\D]/, @a;
1085         iseq "@b", "@c";
1086
1087         @b = grep /\d/, @a;
1088         @c = grep /[\d]/, @a;
1089         iseq "@b", "@c";
1090     }
1091     {
1092         local $Message = '\W, [\W], \w, [\w]';
1093         my @a = map chr, 0 .. 255;
1094         my @b = grep /\W/, @a;
1095         my @c = grep /[^\w]/, @a;
1096         iseq "@b", "@c";
1097
1098         @b = grep /\W/, @a;
1099         @c = grep /[\W]/, @a;
1100         iseq "@b", "@c";
1101
1102         @b = grep /\w/, @a;
1103         @c = grep /[^\W]/, @a;
1104         iseq "@b", "@c";
1105
1106         @b = grep /\w/, @a;
1107         @c = grep /[\w]/, @a;
1108         iseq "@b", "@c";
1109     }
1110
1111
1112     {
1113         # see if backtracking optimization works correctly
1114         local $Message = 'Backtrack optimization';
1115         ok "\n\n" =~ /\n   $ \n/x;
1116         ok "\n\n" =~ /\n*  $ \n/x;
1117         ok "\n\n" =~ /\n+  $ \n/x;
1118         ok "\n\n" =~ /\n?  $ \n/x;
1119         ok "\n\n" =~ /\n*? $ \n/x;
1120         ok "\n\n" =~ /\n+? $ \n/x;
1121         ok "\n\n" =~ /\n?? $ \n/x;
1122         ok "\n\n" !~ /\n*+ $ \n/x;
1123         ok "\n\n" !~ /\n++ $ \n/x;
1124         ok "\n\n" =~ /\n?+ $ \n/x;
1125     }
1126
1127
1128     {
1129         package S;
1130         use overload '""' => sub {'Object S'};
1131         sub new {bless []}
1132      
1133         local $Message  = "Ref stringification";
1134       ::ok do { \my $v} =~ /^SCALAR/,   "Scalar ref stringification";
1135       ::ok do {\\my $v} =~ /^REF/,      "Ref ref stringification";
1136       ::ok []           =~ /^ARRAY/,    "Array ref stringification";
1137       ::ok {}           =~ /^HASH/,     "Hash ref stringification";
1138       ::ok 'S' -> new   =~ /^Object S/, "Object stringification";
1139     }
1140
1141
1142     {
1143         local $Message = "Test result of match used as match";
1144         ok 'a1b' =~ ('xyz' =~ /y/);
1145         iseq $`, 'a';
1146         ok 'a1b' =~ ('xyz' =~ /t/);
1147         iseq $`, 'a';
1148     }
1149
1150
1151     {
1152         local $Message = '"1" is not \s';
1153         may_not_warn sub {ok ("1\n" x 102) !~ /^\s*\n/m};
1154     }
1155
1156
1157     {
1158         local $Message = '\s, [[:space:]] and [[:blank:]]';
1159         my %space = (spc   => " ",
1160                      tab   => "\t",
1161                      cr    => "\r",
1162                      lf    => "\n",
1163                      ff    => "\f",
1164         # There's no \v but the vertical tabulator seems miraculously
1165         # be 11 both in ASCII and EBCDIC.
1166                      vt    => chr(11),
1167                      false => "space");
1168
1169         my @space0 = sort grep {$space {$_} =~ /\s/         } keys %space;
1170         my @space1 = sort grep {$space {$_} =~ /[[:space:]]/} keys %space;
1171         my @space2 = sort grep {$space {$_} =~ /[[:blank:]]/} keys %space;
1172
1173         iseq "@space0", "cr ff lf spc tab";
1174         iseq "@space1", "cr ff lf spc tab vt";
1175         iseq "@space2", "spc tab";
1176     }
1177
1178
1179     {
1180         local $BugId = '20000731.001';
1181         ok "A \x{263a} B z C" =~ /A . B (??{ "z" }) C/,
1182            "Match UTF-8 char in presense of (??{ })";
1183     }
1184
1185
1186     {
1187         local $BugId = '20001021.005';
1188         no warnings 'uninitialized';
1189         ok undef =~ /^([^\/]*)(.*)$/, "Used to cause a SEGV";
1190     }
1191
1192
1193   SKIP:
1194     {
1195         local $Message = '\C matches octet';
1196         $_ = "a\x{100}b";
1197         ok /(.)(\C)(\C)(.)/ or skip q [\C doesn't match], 4;
1198         iseq $1, "a";
1199         if ($IS_ASCII) {     # ASCII (or equivalent), should be UTF-8
1200             iseq $2, "\xC4";
1201             iseq $3, "\x80";
1202         }
1203         elsif ($IS_EBCDIC) { # EBCDIC (or equivalent), should be UTF-EBCDIC
1204             iseq $2, "\x8C";
1205             iseq $3, "\x41";
1206         }
1207         else {
1208             SKIP: {
1209                 ok 0, "Unexpected platform", "ord ('A') = $ordA";
1210                 skip "Unexpected platform";
1211             }
1212         }
1213         iseq $4, "b";
1214     }
1215
1216
1217   SKIP:
1218     {
1219         local $Message = '\C matches octet';
1220         $_ = "\x{100}";
1221         ok /(\C)/g or skip q [\C doesn't match], 2;
1222         if ($IS_ASCII) {
1223             iseq $1, "\xC4";
1224         }
1225         elsif ($IS_EBCDIC) {
1226             iseq $1, "\x8C";
1227         }
1228         else {
1229             ok 0, "Unexpected platform", "ord ('A') = $ordA";
1230         }
1231         ok /(\C)/g or skip q [\C doesn't match];
1232         if ($IS_ASCII) {
1233             iseq $1, "\x80";
1234         }
1235         elsif ($IS_EBCDIC) {
1236             iseq $1, "\x41";
1237         }
1238         else {
1239             ok 0, "Unexpected platform", "ord ('A') = $ordA";
1240         }
1241     }
1242
1243
1244     {
1245         # Japhy -- added 03/03/2001
1246         () = (my $str = "abc") =~ /(...)/;
1247         $str = "def";
1248         iseq $1, "abc", 'Changing subject does not modify $1';
1249     }
1250
1251
1252   SKIP:
1253     {
1254         # The trick is that in EBCDIC the explicit numeric range should
1255         # match (as also in non-EBCDIC) but the explicit alphabetic range
1256         # should not match.
1257         ok "\x8e" =~ /[\x89-\x91]/, '"\x8e" =~ /[\x89-\x91]/';
1258         ok "\xce" =~ /[\xc9-\xd1]/, '"\xce" =~ /[\xc9-\xd1]/';
1259
1260         skip "Not an EBCDIC platform", 2 unless ord ('i') == 0x89 &&
1261                                                 ord ('J') == 0xd1;
1262
1263         # In most places these tests would succeed since \x8e does not
1264         # in most character sets match 'i' or 'j' nor would \xce match
1265         # 'I' or 'J', but strictly speaking these tests are here for
1266         # the good of EBCDIC, so let's test these only there.
1267         nok "\x8e" !~ /[i-j]/, '"\x8e" !~ /[i-j]/';
1268         nok "\xce" !~ /[I-J]/, '"\xce" !~ /[I-J]/';
1269     }
1270
1271
1272     {
1273         ok "\x{ab}"   =~ /\x{ab}/,   '"\x{ab}"   =~ /\x{ab}/  ';
1274         ok "\x{abcd}" =~ /\x{abcd}/, '"\x{abcd}" =~ /\x{abcd}/';
1275     }
1276
1277
1278     {
1279         local $Message = 'bug id 20001008.001';
1280
1281         my @x = ("stra\337e 138", "stra\337e 138");
1282         for (@x) {
1283             ok s/(\d+)\s*([\w\-]+)/$1 . uc $2/e;
1284             ok my ($latin) = /^(.+)(?:\s+\d)/;
1285             iseq $latin, "stra\337e";
1286             ok $latin =~ s/stra\337e/straße/;
1287             #
1288             # Previous code follows, but outcommented - there were no tests.
1289             #
1290             # $latin =~ s/stra\337e/straße/; # \303\237 after the 2nd a
1291             # use utf8; # needed for the raw UTF-8
1292             # $latin =~ s!(s)tr(?:aß|s+e)!$1tr.!; # \303\237 after the a
1293         }
1294     }
1295
1296
1297     {
1298         local $Message = 'Test \x escapes';
1299         ok "ba\xd4c" =~ /([a\xd4]+)/ && $1 eq "a\xd4";
1300         ok "ba\xd4c" =~ /([a\xd4]+)/ && $1 eq "a\x{d4}";
1301         ok "ba\x{d4}c" =~ /([a\xd4]+)/ && $1 eq "a\x{d4}";
1302         ok "ba\x{d4}c" =~ /([a\xd4]+)/ && $1 eq "a\xd4";
1303         ok "ba\xd4c" =~ /([a\x{d4}]+)/ && $1 eq "a\xd4";
1304         ok "ba\xd4c" =~ /([a\x{d4}]+)/ && $1 eq "a\x{d4}";
1305         ok "ba\x{d4}c" =~ /([a\x{d4}]+)/ && $1 eq "a\x{d4}";
1306         ok "ba\x{d4}c" =~ /([a\x{d4}]+)/ && $1 eq "a\xd4";
1307     }
1308
1309
1310     {
1311         local $BugId   = '20001028.003';
1312
1313         # Fist half of the bug.
1314         local $Message = 'HEBREW ACCENT QADMA matched by .*';
1315         my $X = chr (1448);
1316         ok my ($Y) = $X =~ /(.*)/;
1317         iseq $Y, v1448;
1318         iseq length ($Y), 1;
1319
1320         # Second half of the bug.
1321         $Message = 'HEBREW ACCENT QADMA in replacement';
1322         $X = '';
1323         $X =~ s/^/chr(1488)/e;
1324         iseq length $X, 1;
1325         iseq ord ($X), 1488;
1326     }
1327
1328
1329     {   
1330         local $BugId   = '20001108.001';
1331         local $Message = 'Repeated s///';
1332         my $X = "Szab\x{f3},Bal\x{e1}zs";
1333         my $Y = $X;
1334         $Y =~ s/(B)/$1/ for 0 .. 3;
1335         iseq $Y, $X;
1336         iseq $X, "Szab\x{f3},Bal\x{e1}zs";
1337     }
1338
1339
1340     {
1341         local $BugId   = '20000517.001';
1342         local $Message = 's/// on UTF-8 string';
1343         my $x = "\x{100}A";
1344         $x =~ s/A/B/;
1345         iseq $x, "\x{100}B";
1346         iseq length $x, 2;
1347     }
1348
1349
1350     {
1351         local $BugId   = '20001230.002';
1352         local $Message = '\C and É';
1353         ok "École" =~ /^\C\C(.)/ && $1 eq 'c';
1354         ok "École" =~ /^\C\C(c)/;
1355     }
1356
1357
1358   SKIP:
1359     {
1360         local $Message = 'Match code points > 255';
1361         $_ = "abc\x{100}\x{200}\x{300}\x{380}\x{400}defg";
1362         ok /(.\x{300})./ or skip "No match", 4;
1363         ok $` eq "abc\x{100}"            && length ($`) == 4;
1364         ok $& eq "\x{200}\x{300}\x{380}" && length ($&) == 3;
1365         ok $' eq "\x{400}defg"           && length ($') == 5;
1366         ok $1 eq "\x{200}\x{300}"        && length ($1) == 2;
1367     }
1368
1369
1370     {
1371         # The original bug report had 'no utf8' here but that was irrelevant.
1372         local $BugId   = '20010306.008';
1373         local $Message = "Don't dump core";
1374         my $a = "a\x{1234}";
1375         ok $a =~ m/\w/;  # used to core dump.
1376     }
1377
1378
1379     {
1380         local $BugId = '20010410.006';
1381         local $Message = '/g in scalar context';
1382         for my $rx ('/(.*?)\{(.*?)\}/csg',
1383                     '/(.*?)\{(.*?)\}/cg',
1384                     '/(.*?)\{(.*?)\}/sg',
1385                     '/(.*?)\{(.*?)\}/g',
1386                     '/(.+?)\{(.+?)\}/csg',) {
1387             my $i = 0;
1388             my $input = "a{b}c{d}";
1389             eval <<"            --";
1390                 while (eval \$input =~ $rx) {
1391                     \$i ++;
1392                 }
1393             --
1394             iseq $i, 2;
1395         }
1396     }
1397
1398
1399     {
1400         my $x = "\x{10FFFD}";
1401         $x =~ s/(.)/$1/g;
1402         ok ord($x) == 0x10FFFD && length($x) == 1, "From Robin Houston";
1403     }
1404
1405
1406     {
1407         my %d = (
1408             "7f" => [0, 0, 0],
1409             "80" => [1, 1, 0],
1410             "ff" => [1, 1, 0],
1411            "100" => [0, 1, 1],
1412         );
1413       SKIP:
1414         while (my ($code, $match) = each %d) {
1415             local $Message = "Properties of \\x$code";
1416             my $char = eval qq ["\\x{$code}"];
1417             my $i = 0;
1418             ok (($char =~ /[\x80-\xff]/)            xor !$$match [$i ++]);
1419             ok (($char =~ /[\x80-\x{100}]/)         xor !$$match [$i ++]);
1420             ok (($char =~ /[\x{100}]/)              xor !$$match [$i ++]);
1421         }
1422     }
1423
1424
1425     {
1426         # From Japhy
1427         local $Message;
1428         must_warn 'qr/(?c)/',    '^Useless \(\?c\)';
1429         must_warn 'qr/(?-c)/',   '^Useless \(\?-c\)';
1430         must_warn 'qr/(?g)/',    '^Useless \(\?g\)';
1431         must_warn 'qr/(?-g)/',   '^Useless \(\?-g\)';
1432         must_warn 'qr/(?o)/',    '^Useless \(\?o\)';
1433         must_warn 'qr/(?-o)/',   '^Useless \(\?-o\)';
1434
1435         # Now test multi-error regexes
1436         must_warn 'qr/(?g-o)/',  '^Useless \(\?g\).*\nUseless \(\?-o\)';
1437         must_warn 'qr/(?g-c)/',  '^Useless \(\?g\).*\nUseless \(\?-c\)';
1438         # (?c) means (?g) error won't be thrown
1439         must_warn 'qr/(?o-cg)/', '^Useless \(\?o\).*\nUseless \(\?-c\)';
1440         must_warn 'qr/(?ogc)/',  '^Useless \(\?o\).*\nUseless \(\?g\).*\n' .
1441                                   'Useless \(\?c\)';
1442     }
1443
1444
1445     {
1446         local $Message = "/x tests";
1447         $_ = "foo";
1448         eval_ok <<"        --";
1449           /f
1450            o\r
1451            o
1452            \$
1453           /x
1454         --
1455         eval_ok <<"        --";
1456           /f
1457            o
1458            o
1459            \$\r
1460           /x
1461         --
1462     }
1463
1464
1465     {
1466         local $Message = "/o feature";
1467         sub test_o {$_ [0] =~ /$_[1]/o; return $1}
1468         iseq test_o ('abc', '(.)..'), 'a';
1469         iseq test_o ('abc', '..(.)'), 'a';
1470     }
1471
1472
1473     {
1474         local $BugId = "20010619.003";
1475         # Amazingly vertical tabulator is the same in ASCII and EBCDIC.
1476         for ("\n", "\t", "\014", "\r") {
1477             ok !/[[:print:]]/, "'$_' not in [[:print:]]";
1478         }
1479         for (" ") {
1480             ok  /[[:print:]]/, "'$_' in [[:print:]]";
1481         }
1482     }
1483
1484
1485     {
1486         # Test basic $^N usage outside of a regex
1487         local $Message = '$^N usage outside of a regex';
1488         my $x = "abcdef";
1489         ok ($x =~ /cde/                  and !defined $^N);
1490         ok ($x =~ /(cde)/                and $^N eq "cde");
1491         ok ($x =~ /(c)(d)(e)/            and $^N eq   "e");
1492         ok ($x =~ /(c(d)e)/              and $^N eq "cde");
1493         ok ($x =~ /(foo)|(c(d)e)/        and $^N eq "cde");
1494         ok ($x =~ /(c(d)e)|(foo)/        and $^N eq "cde");
1495         ok ($x =~ /(c(d)e)|(abc)/        and $^N eq "abc");
1496         ok ($x =~ /(c(d)e)|(abc)x/       and $^N eq "cde");
1497         ok ($x =~ /(c(d)e)(abc)?/        and $^N eq "cde");
1498         ok ($x =~ /(?:c(d)e)/            and $^N eq   "d");
1499         ok ($x =~ /(?:c(d)e)(?:f)/       and $^N eq   "d");
1500         ok ($x =~ /(?:([abc])|([def]))*/ and $^N eq   "f");
1501         ok ($x =~ /(?:([ace])|([bdf]))*/ and $^N eq   "f");
1502         ok ($x =~ /(([ace])|([bd]))*/    and $^N eq   "e");
1503        {ok ($x =~ /(([ace])|([bdf]))*/   and $^N eq   "f");}
1504         ## Test to see if $^N is automatically localized -- it should now
1505         ## have the value set in the previous test.
1506         iseq $^N, "e", '$^N is automatically localized';
1507
1508         # Now test inside (?{ ... })
1509         local $Message = '$^N usage inside (?{ ... })';
1510         our ($y, $z);
1511         ok ($x =~ /a([abc])(?{$y=$^N})c/                    and $y eq  "b");
1512         ok ($x =~ /a([abc]+)(?{$y=$^N})d/                   and $y eq  "bc");
1513         ok ($x =~ /a([abcdefg]+)(?{$y=$^N})d/               and $y eq  "bc");
1514         ok ($x =~ /(a([abcdefg]+)(?{$y=$^N})d)(?{$z=$^N})e/ and $y eq  "bc"
1515                                                             and $z eq "abcd");
1516         ok ($x =~ /(a([abcdefg]+)(?{$y=$^N})de)(?{$z=$^N})/ and $y eq  "bc"
1517                                                             and $z eq "abcde");
1518
1519     }
1520
1521
1522   SKIP:
1523     {
1524         ## Should probably put in tests for all the POSIX stuff,
1525         ## but not sure how to guarantee a specific locale......
1526
1527         skip "Not an ASCII platform", 2 unless $IS_ASCII;
1528         local $Message = 'Test [[:cntrl:]]';
1529         my $AllBytes = join "" => map {chr} 0 .. 255;
1530         (my $x = $AllBytes) =~ s/[[:cntrl:]]//g;
1531         iseq $x, join "", map {chr} 0x20 .. 0x7E, 0x80 .. 0xFF;
1532
1533         ($x = $AllBytes) =~ s/[^[:cntrl:]]//g;
1534         iseq $x, join "", map {chr} 0x00 .. 0x1F, 0x7F;
1535     }
1536
1537
1538     {
1539         # With /s modifier UTF8 chars were interpreted as bytes
1540         local $Message = "UTF-8 chars aren't bytes";
1541         my $a = "Hello \x{263A} World";
1542         my @a = ($a =~ /./gs);
1543         iseq $#a, 12;
1544     }
1545
1546
1547     {
1548         local $Message = '. matches \n with /s';
1549         my $str1 = "foo\nbar";
1550         my $str2 = "foo\n\x{100}bar";
1551         my ($a, $b) = map {chr} $IS_ASCII ? (0xc4, 0x80) : (0x8c, 0x41);
1552         my @a;
1553         @a = $str1 =~ /./g;   iseq @a, 6; iseq "@a", "f o o b a r";
1554         @a = $str1 =~ /./gs;  iseq @a, 7; iseq "@a", "f o o \n b a r";
1555         @a = $str1 =~ /\C/g;  iseq @a, 7; iseq "@a", "f o o \n b a r";
1556         @a = $str1 =~ /\C/gs; iseq @a, 7; iseq "@a", "f o o \n b a r";
1557         @a = $str2 =~ /./g;   iseq @a, 7; iseq "@a", "f o o \x{100} b a r";
1558         @a = $str2 =~ /./gs;  iseq @a, 8; iseq "@a", "f o o \n \x{100} b a r";
1559         @a = $str2 =~ /\C/g;  iseq @a, 9; iseq "@a", "f o o \n $a $b b a r";
1560         @a = $str2 =~ /\C/gs; iseq @a, 9; iseq "@a", "f o o \n $a $b b a r";
1561     }
1562
1563
1564     {
1565         # [ID 20010814.004] pos() doesn't work when using =~m// in list context
1566         local $BugId = '20010814.004';
1567         $_ = "ababacadaea";
1568         my $a = join ":", /b./gc;
1569         my $b = join ":", /a./gc;
1570         my $c = pos;
1571         iseq "$a $b $c", 'ba:ba ad:ae 10', "pos() works with () = m//";
1572     }
1573
1574
1575     {
1576         # [ID 20010407.006] matching utf8 return values from
1577         # functions does not work
1578         local $BugId   = '20010407.006';
1579         local $Message = 'UTF-8 return values from functions';
1580         package ID_20010407_006;
1581         sub x {"a\x{1234}"}
1582         my $x = x;
1583         my $y;
1584       ::ok $x =~ /(..)/;
1585         $y = $1;
1586       ::ok length ($y) == 2 && $y eq $x;
1587       ::ok x =~ /(..)/;
1588         $y = $1;
1589       ::ok length ($y) == 2 && $y eq $x;
1590     }
1591
1592
1593     {
1594         no warnings 'digit';
1595         # Check that \x## works. 5.6.1 and 5.005_03 fail some of these.
1596         my $x;
1597         $x = "\x4e" . "E";
1598         ok ($x =~ /^\x4EE$/, "Check only 2 bytes of hex are matched.");
1599
1600         $x = "\x4e" . "i";
1601         ok ($x =~ /^\x4Ei$/, "Check that invalid hex digit stops it (2)");
1602
1603         $x = "\x4" . "j";
1604         ok ($x =~ /^\x4j$/,  "Check that invalid hex digit stops it (1)");
1605
1606         $x = "\x0" . "k";
1607         ok ($x =~ /^\xk$/,   "Check that invalid hex digit stops it (0)");
1608
1609         $x = "\x0" . "x";
1610         ok ($x =~ /^\xx$/, "\\xx isn't to be treated as \\0");
1611
1612         $x = "\x0" . "xa";
1613         ok ($x =~ /^\xxa$/, "\\xxa isn't to be treated as \\xa");
1614
1615         $x = "\x9" . "_b";
1616         ok ($x =~ /^\x9_b$/, "\\x9_b isn't to be treated as \\x9b");
1617
1618         # and now again in [] ranges
1619
1620         $x = "\x4e" . "E";
1621         ok ($x =~ /^[\x4EE]{2}$/, "Check only 2 bytes of hex are matched.");
1622
1623         $x = "\x4e" . "i";
1624         ok ($x =~ /^[\x4Ei]{2}$/, "Check that invalid hex digit stops it (2)");
1625
1626         $x = "\x4" . "j";
1627         ok ($x =~ /^[\x4j]{2}$/,  "Check that invalid hex digit stops it (1)");
1628
1629         $x = "\x0" . "k";
1630         ok ($x =~ /^[\xk]{2}$/,   "Check that invalid hex digit stops it (0)");
1631
1632         $x = "\x0" . "x";
1633         ok ($x =~ /^[\xx]{2}$/, "\\xx isn't to be treated as \\0");
1634
1635         $x = "\x0" . "xa";
1636         ok ($x =~ /^[\xxa]{3}$/, "\\xxa isn't to be treated as \\xa");
1637
1638         $x = "\x9" . "_b";
1639         ok ($x =~ /^[\x9_b]{3}$/, "\\x9_b isn't to be treated as \\x9b");
1640
1641         # Check that \x{##} works. 5.6.1 fails quite a few of these.
1642
1643         $x = "\x9b";
1644         ok ($x =~ /^\x{9_b}$/, "\\x{9_b} is to be treated as \\x9b");
1645
1646         $x = "\x9b" . "y";
1647         ok ($x =~ /^\x{9_b}y$/, "\\x{9_b} is to be treated as \\x9b (again)");
1648
1649         $x = "\x9b" . "y";
1650         ok ($x =~ /^\x{9b_}y$/, "\\x{9b_} is to be treated as \\x9b");
1651
1652         $x = "\x9b" . "y";
1653         ok ($x =~ /^\x{9_bq}y$/, "\\x{9_bc} is to be treated as \\x9b");
1654
1655         $x = "\x0" . "y";
1656         ok ($x =~ /^\x{x9b}y$/, "\\x{x9b} is to be treated as \\x0");
1657
1658         $x = "\x0" . "y";
1659         ok ($x =~ /^\x{0x9b}y$/, "\\x{0x9b} is to be treated as \\x0");
1660
1661         $x = "\x9b" . "y";
1662         ok ($x =~ /^\x{09b}y$/, "\\x{09b} is to be treated as \\x9b");
1663
1664         $x = "\x9b";
1665         ok ($x =~ /^[\x{9_b}]$/, "\\x{9_b} is to be treated as \\x9b");
1666
1667         $x = "\x9b" . "y";
1668         ok ($x =~ /^[\x{9_b}y]{2}$/,
1669                                  "\\x{9_b} is to be treated as \\x9b (again)");
1670
1671         $x = "\x9b" . "y";
1672         ok ($x =~ /^[\x{9b_}y]{2}$/, "\\x{9b_} is to be treated as \\x9b");
1673
1674         $x = "\x9b" . "y";
1675         ok ($x =~ /^[\x{9_bq}y]{2}$/, "\\x{9_bc} is to be treated as \\x9b");
1676
1677         $x = "\x0" . "y";
1678         ok ($x =~ /^[\x{x9b}y]{2}$/, "\\x{x9b} is to be treated as \\x0");
1679
1680         $x = "\x0" . "y";
1681         ok ($x =~ /^[\x{0x9b}y]{2}$/, "\\x{0x9b} is to be treated as \\x0");
1682
1683         $x = "\x9b" . "y";
1684         ok ($x =~ /^[\x{09b}y]{2}$/, "\\x{09b} is to be treated as \\x9b");
1685
1686     }
1687
1688
1689     {
1690         # High bit bug -- japhy
1691         my $x = "ab\200d";
1692         ok $x =~ /.*?\200/, "High bit fine";
1693     }
1694
1695
1696     {
1697         # The basic character classes and Unicode
1698         ok "\x{0100}" =~ /\w/, 'LATIN CAPITAL LETTER A WITH MACRON in /\w/';
1699         ok "\x{0660}" =~ /\d/, 'ARABIC-INDIC DIGIT ZERO in /\d/';
1700         ok "\x{1680}" =~ /\s/, 'OGHAM SPACE MARK in /\s/';
1701     }
1702
1703
1704     {
1705         local $Message = "Folding matches and Unicode";
1706         ok "a\x{100}" =~ /A/i;
1707         ok "A\x{100}" =~ /a/i;
1708         ok "a\x{100}" =~ /a/i;
1709         ok "A\x{100}" =~ /A/i;
1710         ok "\x{101}a" =~ /\x{100}/i;
1711         ok "\x{100}a" =~ /\x{100}/i;
1712         ok "\x{101}a" =~ /\x{101}/i;
1713         ok "\x{100}a" =~ /\x{101}/i;
1714         ok "a\x{100}" =~ /A\x{100}/i;
1715         ok "A\x{100}" =~ /a\x{100}/i;
1716         ok "a\x{100}" =~ /a\x{100}/i;
1717         ok "A\x{100}" =~ /A\x{100}/i;
1718         ok "a\x{100}" =~ /[A]/i;
1719         ok "A\x{100}" =~ /[a]/i;
1720         ok "a\x{100}" =~ /[a]/i;
1721         ok "A\x{100}" =~ /[A]/i;
1722         ok "\x{101}a" =~ /[\x{100}]/i;
1723         ok "\x{100}a" =~ /[\x{100}]/i;
1724         ok "\x{101}a" =~ /[\x{101}]/i;
1725         ok "\x{100}a" =~ /[\x{101}]/i;
1726     }
1727
1728
1729     {
1730         use charnames ':full';
1731         local $Message = "Folding 'LATIN LETTER A WITH GRAVE'";
1732
1733         my $lower = "\N{LATIN SMALL LETTER A WITH GRAVE}";
1734         my $UPPER = "\N{LATIN CAPITAL LETTER A WITH GRAVE}";
1735         
1736         ok $lower =~ m/$UPPER/i;
1737         ok $UPPER =~ m/$lower/i;
1738         ok $lower =~ m/[$UPPER]/i;
1739         ok $UPPER =~ m/[$lower]/i;
1740
1741         local $Message = "Folding 'GREEK LETTER ALPHA WITH VRACHY'";
1742
1743         $lower = "\N{GREEK CAPITAL LETTER ALPHA WITH VRACHY}";
1744         $UPPER = "\N{GREEK SMALL LETTER ALPHA WITH VRACHY}";
1745
1746         ok $lower =~ m/$UPPER/i;
1747         ok $UPPER =~ m/$lower/i;
1748         ok $lower =~ m/[$UPPER]/i;
1749         ok $UPPER =~ m/[$lower]/i;
1750
1751         local $Message = "Folding 'LATIN LETTER Y WITH DIAERESIS'";
1752
1753         $lower = "\N{LATIN SMALL LETTER Y WITH DIAERESIS}";
1754         $UPPER = "\N{LATIN CAPITAL LETTER Y WITH DIAERESIS}";
1755
1756         ok $lower =~ m/$UPPER/i;
1757         ok $UPPER =~ m/$lower/i;
1758         ok $lower =~ m/[$UPPER]/i;
1759         ok $UPPER =~ m/[$lower]/i;
1760     }
1761
1762
1763     {
1764         use charnames ':full';
1765         local $PatchId = "13843";
1766         local $Message = "GREEK CAPITAL LETTER SIGMA vs " .
1767                          "COMBINING GREEK PERISPOMENI";
1768
1769         my $SIGMA = "\N{GREEK CAPITAL LETTER SIGMA}";
1770         my $char  = "\N{COMBINING GREEK PERISPOMENI}";
1771
1772         may_not_warn sub {ok "_:$char:_" !~ m/_:$SIGMA:_/i};
1773     }
1774
1775
1776     {
1777         local $Message = '\X';
1778         use charnames ':full';
1779
1780         ok "a!"                          =~ /^(\X)!/ && $1 eq "a";
1781         ok "\xDF!"                       =~ /^(\X)!/ && $1 eq "\xDF";
1782         ok "\x{100}!"                    =~ /^(\X)!/ && $1 eq "\x{100}";
1783         ok "\x{100}\x{300}!"             =~ /^(\X)!/ && $1 eq "\x{100}\x{300}";
1784         ok "\N{LATIN CAPITAL LETTER E}!" =~ /^(\X)!/ &&
1785                $1 eq "\N{LATIN CAPITAL LETTER E}";
1786         ok "\N{LATIN CAPITAL LETTER E}\N{COMBINING GRAVE ACCENT}!"
1787                                          =~ /^(\X)!/ &&
1788                $1 eq "\N{LATIN CAPITAL LETTER E}\N{COMBINING GRAVE ACCENT}";
1789
1790         local $Message = '\C and \X';
1791         ok "!abc!" =~ /a\Cc/;
1792         ok "!abc!" =~ /a\Xc/;
1793     }
1794
1795
1796     {
1797         local $Message = "Final Sigma";
1798
1799         my $SIGMA = "\x{03A3}"; # CAPITAL
1800         my $Sigma = "\x{03C2}"; # SMALL FINAL
1801         my $sigma = "\x{03C3}"; # SMALL
1802
1803         ok $SIGMA =~ /$SIGMA/i;
1804         ok $SIGMA =~ /$Sigma/i;
1805         ok $SIGMA =~ /$sigma/i;
1806
1807         ok $Sigma =~ /$SIGMA/i;
1808         ok $Sigma =~ /$Sigma/i;
1809         ok $Sigma =~ /$sigma/i;
1810
1811         ok $sigma =~ /$SIGMA/i;
1812         ok $sigma =~ /$Sigma/i;
1813         ok $sigma =~ /$sigma/i;
1814         
1815         ok $SIGMA =~ /[$SIGMA]/i;
1816         ok $SIGMA =~ /[$Sigma]/i;
1817         ok $SIGMA =~ /[$sigma]/i;
1818
1819         ok $Sigma =~ /[$SIGMA]/i;
1820         ok $Sigma =~ /[$Sigma]/i;
1821         ok $Sigma =~ /[$sigma]/i;
1822
1823         ok $sigma =~ /[$SIGMA]/i;
1824         ok $sigma =~ /[$Sigma]/i;
1825         ok $sigma =~ /[$sigma]/i;
1826
1827         local $Message = "More final Sigma";
1828
1829         my $S3 = "$SIGMA$Sigma$sigma";
1830
1831         ok ":$S3:" =~ /:(($SIGMA)+):/i   && $1 eq $S3 && $2 eq $sigma;
1832         ok ":$S3:" =~ /:(($Sigma)+):/i   && $1 eq $S3 && $2 eq $sigma;
1833         ok ":$S3:" =~ /:(($sigma)+):/i   && $1 eq $S3 && $2 eq $sigma;
1834
1835         ok ":$S3:" =~ /:(([$SIGMA])+):/i && $1 eq $S3 && $2 eq $sigma;
1836         ok ":$S3:" =~ /:(([$Sigma])+):/i && $1 eq $S3 && $2 eq $sigma;
1837         ok ":$S3:" =~ /:(([$sigma])+):/i && $1 eq $S3 && $2 eq $sigma;
1838     }
1839
1840
1841     {
1842         use charnames ':full';
1843         local $Message = "Parlez-Vous " .
1844                          "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais?";
1845
1846         ok "Fran\N{LATIN SMALL LETTER C}ais" =~ /Fran.ais/ &&
1847             $& eq "Francais";
1848         ok "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" =~ /Fran.ais/ &&
1849             $& eq "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais";
1850         ok "Fran\N{LATIN SMALL LETTER C}ais" =~ /Fran\Cais/ &&
1851             $& eq "Francais";
1852         # COMBINING CEDILLA is two bytes when encoded
1853         ok "Franc\N{COMBINING CEDILLA}ais" =~ /Franc\C\Cais/;
1854         ok "Fran\N{LATIN SMALL LETTER C}ais" =~ /Fran\Xais/ &&
1855             $& eq "Francais";
1856         ok "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" =~ /Fran\Xais/  &&
1857             $& eq "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais";
1858         ok "Franc\N{COMBINING CEDILLA}ais" =~ /Fran\Xais/ &&
1859             $& eq "Franc\N{COMBINING CEDILLA}ais";
1860         ok "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" =~
1861            /Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais/  &&
1862             $& eq "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais";
1863         ok "Franc\N{COMBINING CEDILLA}ais" =~ /Franc\N{COMBINING CEDILLA}ais/ &&
1864             $& eq "Franc\N{COMBINING CEDILLA}ais";
1865
1866         my @f = (
1867             ["Fran\N{LATIN SMALL LETTER C}ais",                    "Francais"],
1868             ["Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais",
1869                                "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais"],
1870             ["Franc\N{COMBINING CEDILLA}ais", "Franc\N{COMBINING CEDILLA}ais"],
1871         );
1872         foreach my $entry (@f) {
1873             my ($subject, $match) = @$entry;
1874             ok $subject =~ /Fran(?:c\N{COMBINING CEDILLA}?|
1875                     \N{LATIN SMALL LETTER C WITH CEDILLA})ais/x &&
1876                $& eq $match;
1877         }
1878     }
1879
1880
1881     {
1882         local $Message = "Lingering (and useless) UTF8 flag doesn't mess up /i";
1883         my $pat = "ABcde";
1884         my $str = "abcDE\x{100}";
1885         chop $str;
1886         ok $str =~ /$pat/i;
1887
1888         $pat = "ABcde\x{100}";
1889         $str = "abcDE";
1890         chop $pat;
1891         ok $str =~ /$pat/i;
1892
1893         $pat = "ABcde\x{100}";
1894         $str = "abcDE\x{100}";
1895         chop $pat;
1896         chop $str;
1897         ok $str =~ /$pat/i;
1898     }
1899
1900
1901     {
1902         use charnames ':full';
1903         local $Message = "LATIN SMALL LETTER SHARP S " .
1904                          "(\N{LATIN SMALL LETTER SHARP S})";
1905
1906         ok "\N{LATIN SMALL LETTER SHARP S}" =~
1907                                             /\N{LATIN SMALL LETTER SHARP S}/;
1908         ok "\N{LATIN SMALL LETTER SHARP S}" =~
1909                                             /\N{LATIN SMALL LETTER SHARP S}/i;
1910         ok "\N{LATIN SMALL LETTER SHARP S}" =~
1911                                            /[\N{LATIN SMALL LETTER SHARP S}]/;
1912         ok "\N{LATIN SMALL LETTER SHARP S}" =~
1913                                            /[\N{LATIN SMALL LETTER SHARP S}]/i;
1914
1915         ok "ss" =~  /\N{LATIN SMALL LETTER SHARP S}/i;
1916         ok "SS" =~  /\N{LATIN SMALL LETTER SHARP S}/i;
1917         ok "ss" =~ /[\N{LATIN SMALL LETTER SHARP S}]/i;
1918         ok "SS" =~ /[\N{LATIN SMALL LETTER SHARP S}]/i;
1919
1920         ok "\N{LATIN SMALL LETTER SHARP S}" =~ /ss/i;
1921         ok "\N{LATIN SMALL LETTER SHARP S}" =~ /SS/i;
1922  
1923         local $Message = "Unoptimized named sequence in class";
1924         ok "ss" =~ /[\N{LATIN SMALL LETTER SHARP S}x]/i;
1925         ok "SS" =~ /[\N{LATIN SMALL LETTER SHARP S}x]/i;
1926         ok "\N{LATIN SMALL LETTER SHARP S}" =~
1927           /[\N{LATIN SMALL LETTER SHARP S}x]/;
1928         ok "\N{LATIN SMALL LETTER SHARP S}" =~
1929           /[\N{LATIN SMALL LETTER SHARP S}x]/i;
1930     }
1931
1932
1933     {
1934         # More whitespace: U+0085, U+2028, U+2029\n";
1935
1936         # U+0085, U+00A0 need to be forced to be Unicode, the \x{100} does that.
1937       SKIP: {
1938           skip "EBCDIC platform", 4 if $IS_EBCDIC;
1939           # Do \x{0015} and \x{0041} match \s in EBCDIC?
1940           ok "<\x{100}\x{0085}>" =~ /<\x{100}\s>/, '\x{0085} in \s';
1941           ok        "<\x{0085}>" =~        /<\v>/, '\x{0085} in \v';
1942           ok "<\x{100}\x{00A0}>" =~ /<\x{100}\s>/, '\x{00A0} in \s';
1943           ok        "<\x{00A0}>" =~        /<\h>/, '\x{00A0} in \h';
1944         }
1945         my @h = map {sprintf "%05x" => $_} 0x01680, 0x0180E, 0x02000 .. 0x0200A,
1946                                            0x0202F, 0x0205F, 0x03000;
1947         my @v = map {sprintf "%05x" => $_} 0x02028, 0x02029;
1948
1949         my @H = map {sprintf "%05x" => $_} 0x01361,   0x0200B, 0x02408, 0x02420,
1950                                            0x0303F,   0xE0020;
1951         my @V = map {sprintf "%05x" => $_} 0x0008A .. 0x0008D, 0x00348, 0x10100,
1952                                            0xE005F,   0xE007C;
1953
1954         for my $hex (@h) {
1955             my $str = eval qq ["<\\x{$hex}>"];
1956             ok $str =~ /<\s>/, "\\x{$hex} in \\s";
1957             ok $str =~ /<\h>/, "\\x{$hex} in \\h";
1958             ok $str !~ /<\v>/, "\\x{$hex} not in \\v";
1959         }
1960
1961         for my $hex (@v) {
1962             my $str = eval qq ["<\\x{$hex}>"];
1963             ok $str =~ /<\s>/, "\\x{$hex} in \\s";
1964             ok $str =~ /<\v>/, "\\x{$hex} in \\v";
1965             ok $str !~ /<\h>/, "\\x{$hex} not in \\h";
1966         }
1967
1968         for my $hex (@H) {
1969             my $str = eval qq ["<\\x{$hex}>"];
1970             ok $str =~ /<\S>/, "\\x{$hex} in \\S";
1971             ok $str =~ /<\H>/, "\\x{$hex} in \\H";
1972         }
1973
1974         for my $hex (@V) {
1975             my $str = eval qq ["<\\x{$hex}>"];
1976             ok $str =~ /<\S>/, "\\x{$hex} in \\S";
1977             ok $str =~ /<\V>/, "\\x{$hex} in \\V";
1978         }
1979     }
1980
1981
1982     {
1983         # . with /s should work on characters, as opposed to bytes
1984         local $Message = ". with /s works on characters, not bytes";
1985
1986         my $s = "\x{e4}\x{100}";
1987         # This is not expected to match: the point is that
1988         # neither should we get "Malformed UTF-8" warnings.
1989         may_not_warn sub {$s =~ /\G(.+?)\n/gcs}, "No 'Malformed UTF-8' warning";
1990
1991         my @c;
1992         push @c => $1 while $s =~ /\G(.)/gs;
1993
1994         local $" = "";
1995         iseq "@c", $s;
1996
1997         # Test only chars < 256
1998         my $t1 = "Q003\n\n\x{e4}\x{f6}\n\nQ004\n\n\x{e7}";
1999         my $r1 = "";
2000         while ($t1 =~ / \G ( .+? ) \n\s+ ( .+? ) ( $ | \n\s+ ) /xgcs) {
2001             $r1 .= $1 . $2;
2002         }
2003
2004         my $t2 = $t1 . "\x{100}"; # Repeat with a larger char
2005         my $r2 = "";
2006         while ($t2 =~ / \G ( .+? ) \n\s+ ( .+? ) ( $ | \n\s+ ) /xgcs) {
2007             $r2 .= $1 . $2;
2008         }
2009         $r2 =~ s/\x{100}//;
2010
2011         iseq $r1, $r2;
2012     }
2013
2014
2015     {
2016         local $Message = "Unicode lookbehind";
2017         ok "A\x{100}B"        =~ /(?<=A.)B/;
2018         ok "A\x{200}\x{300}B" =~ /(?<=A..)B/;
2019         ok "\x{400}AB"        =~ /(?<=\x{400}.)B/;
2020         ok "\x{500}\x{600}B"  =~ /(?<=\x{500}.)B/;
2021
2022         # Original code also contained:
2023         # ok "\x{500\x{600}}B"  =~ /(?<=\x{500}.)B/;
2024         # but that looks like a typo.
2025     }
2026
2027
2028     {
2029         local $Message = 'UTF-8 hash keys and /$/';
2030         # http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters
2031         #                                         /2002-01/msg01327.html
2032
2033         my $u = "a\x{100}";
2034         my $v = substr ($u, 0, 1);
2035         my $w = substr ($u, 1, 1);
2036         my %u = ($u => $u, $v => $v, $w => $w);
2037         for (keys %u) {
2038             my $m1 =            /^\w*$/ ? 1 : 0;
2039             my $m2 = $u {$_} =~ /^\w*$/ ? 1 : 0;
2040             iseq $m1, $m2;
2041         }
2042     }
2043
2044
2045     {
2046         local $BugId   = "20020124.005";
2047         local $PatchId = "14795";
2048         local $Message = "s///eg";
2049
2050         for my $char ("a", "\x{df}", "\x{100}") {
2051             my $x = "$char b $char";
2052             $x =~ s{($char)}{
2053                   "c" =~ /c/;
2054                   "x";
2055             }ge;
2056             iseq substr ($x, 0, 1), substr ($x, -1, 1);
2057         }
2058     }
2059
2060
2061     {
2062         local $Message = "No SEGV in s/// and UTF-8";
2063         my $s = "s#\x{100}" x 4;
2064         ok $s =~ s/[^\w]/ /g;
2065         if ($ENV {REAL_POSIX_CC}) {
2066             iseq $s, "s  " x 4;
2067         }
2068         else {
2069             iseq $s, "s \x{100}" x 4;
2070         }
2071     }
2072
2073
2074     {
2075         local $Message = "UTF-8 bug (maybe already known?)";
2076         my $u = "foo";
2077         $u =~ s/./\x{100}/g;
2078         iseq $u, "\x{100}\x{100}\x{100}";
2079
2080         $u = "foobar";
2081         $u =~ s/[ao]/\x{100}/g;
2082         iseq $u, "f\x{100}\x{100}b\x{100}r";
2083
2084         $u =~ s/\x{100}/e/g;
2085         iseq $u, "feeber";
2086     }
2087
2088
2089     {
2090         local $Message = "UTF-8 bug with s///";
2091         # check utf8/non-utf8 mixtures
2092         # try to force all float/anchored check combinations
2093
2094         my $c = "\x{100}";
2095         my $subst;
2096         for my $re ("xx.*$c", "x.*$c$c", "$c.*xx", "$c$c.*x",
2097                     "xx.*(?=$c)", "(?=$c).*xx",) {
2098             ok "xxx" !~ /$re/;
2099             ok +($subst = "xxx") !~ s/$re//;
2100         }
2101         for my $re ("xx.*$c*", "$c*.*xx") {
2102             ok "xxx" =~ /$re/;
2103             ok +($subst = "xxx") =~ s/$re//;
2104             iseq $subst, "";
2105         }
2106         for my $re ("xxy*", "y*xx") {
2107             ok "xx$c" =~ /$re/;
2108             ok +($subst = "xx$c") =~ s/$re//;
2109             iseq $subst, $c;
2110             ok "xy$c" !~ /$re/;
2111             ok +($subst = "xy$c") !~ s/$re//;
2112         }
2113         for my $re ("xy$c*z", "x$c*yz") {
2114             ok "xyz" =~ /$re/;
2115             ok +($subst = "xyz") =~ s/$re//;
2116             iseq $subst, "";
2117         }
2118     }
2119
2120
2121     {
2122         local $Message = "qr /.../x";
2123         my $R = qr / A B C # D E/x;
2124         ok "ABCDE" =~    $R   && $& eq "ABC";
2125         ok "ABCDE" =~   /$R/  && $& eq "ABC";
2126         ok "ABCDE" =~  m/$R/  && $& eq "ABC";
2127         ok "ABCDE" =~  /($R)/ && $1 eq "ABC";
2128         ok "ABCDE" =~ m/($R)/ && $1 eq "ABC";
2129     }
2130
2131
2132     {
2133         local $BugId = "20020412.005";
2134         local $Message = "Correct pmop flags checked when empty pattern";
2135
2136         # Requires reuse of last successful pattern.
2137         my $num = 123;
2138         $num =~ /\d/;
2139         for (0 .. 1) {
2140             my $match = ?? + 0;
2141             ok $match != $_, $Message, 
2142                 sprintf "'match one' %s on %s iteration" =>
2143                                $match ? 'succeeded' : 'failed',
2144                                $_     ? 'second'    : 'first';
2145         }
2146         $num =~ /(\d)/;
2147         my $result = join "" => $num =~ //g;
2148         iseq $result, $num;
2149     }
2150
2151
2152     {
2153         local $BugId   = '20020630.002';
2154         local $Message = 'UTF-8 regex matches above 32k';
2155         for (['byte', "\x{ff}"], ['utf8', "\x{1ff}"]) {
2156             my ($type, $char) = @$_;
2157             for my $len (32000, 32768, 33000) {
2158                 my  $s = $char . "f" x $len;
2159                 my  $r = $s =~ /$char([f]*)/gc;
2160                 ok  $r, $Message, "<$type x $len>";
2161                 ok !$r || pos ($s) == $len + 1, $Message,
2162                         "<$type x $len>; pos = @{[pos $s]}";
2163             }
2164         }
2165     }
2166
2167
2168     {
2169         our $a = bless qr /foo/ => 'Foo';
2170         ok 'goodfood' =~ $a,     "Reblessed qr // matches";
2171         iseq $a, '(?-xism:foo)', "Reblessed qr // stringifies";
2172         my $x = "\x{3fe}";
2173         my $z = my $y = "\317\276";  # Byte representation of $x
2174         $a = qr /$x/;
2175         ok $x =~ $a, "UTF-8 interpolation in qr //";
2176         ok "a$a" =~ $x, "Stringified qr // preserves UTF-8";
2177         ok "a$x" =~ /^a$a\z/, "Interpolated qr // preserves UTF-8";
2178         ok "a$x" =~ /^a(??{$a})\z/,
2179                         "Postponed interpolation of qr // preserves UTF-8";
2180         {
2181             local $BugId = '17776';
2182             iseq length qr /##/x, 12, "## in qr // doesn't corrupt memory";
2183         }
2184         {
2185             use re 'eval';
2186             ok "$x$x" =~ /^$x(??{$x})\z/,
2187                "Postponed UTF-8 string in UTF-8 re matches UTF-8";
2188             ok "$y$x" =~ /^$y(??{$x})\z/, 
2189                "Postponed UTF-8 string in non-UTF-8 re matches UTF-8";
2190             ok "$y$x" !~ /^$y(??{$y})\z/,
2191                "Postponed non-UTF-8 string in non-UTF-8 re doesn't match UTF-8";
2192             ok "$x$x" !~ /^$x(??{$y})\z/,
2193                "Postponed non-UTF-8 string in UTF-8 re doesn't match UTF-8";
2194             ok "$y$y" =~ /^$y(??{$y})\z/,
2195                "Postponed non-UTF-8 string in non-UTF-8 re matches non-UTF8";
2196             ok "$x$y" =~ /^$x(??{$y})\z/,
2197                "Postponed non-UTF-8 string in UTF-8 re matches non-UTF8";
2198
2199             $y = $z;  # Reset $y after upgrade.
2200             ok "$x$y" !~ /^$x(??{$x})\z/,
2201                "Postponed UTF-8 string in UTF-8 re doesn't match non-UTF-8";
2202             ok "$y$y" !~ /^$y(??{$x})\z/,
2203                "Postponed UTF-8 string in non-UTF-8 re doesn't match non-UTF-8";
2204         }
2205     }
2206
2207
2208     {
2209         local $PatchId = '18179';
2210         my $s = "\x{100}" x 5;
2211         my $ok = $s =~ /(\x{100}{4})/;
2212         my ($ord, $len) = (ord $1, length $1);
2213         ok $ok && $ord == 0x100 && $len == 4, "No panic: end_shift";
2214     }
2215
2216
2217     {
2218         local $BugId = '15763';
2219         our $a = "x\x{100}";
2220         chop $a;    # Leaves the UTF-8 flag
2221         $a .= "y";  # 1 byte before 'y'.
2222
2223         ok $a =~ /^\C/,        'match one \C on 1-byte UTF-8';
2224         ok $a =~ /^\C{1}/,     'match \C{1}';
2225
2226         ok $a =~ /^\Cy/,       'match \Cy';
2227         ok $a =~ /^\C{1}y/,    'match \C{1}y';
2228
2229         ok $a !~ /^\C\Cy/,     q {don't match two \Cy};
2230         ok $a !~ /^\C{2}y/,    q {don't match \C{2}y};
2231
2232         $a = "\x{100}y"; # 2 bytes before "y"
2233
2234         ok $a =~ /^\C/,        'match one \C on 2-byte UTF-8';
2235         ok $a =~ /^\C{1}/,     'match \C{1}';
2236         ok $a =~ /^\C\C/,      'match two \C';
2237         ok $a =~ /^\C{2}/,     'match \C{2}';
2238
2239         ok $a =~ /^\C\C\C/,    'match three \C on 2-byte UTF-8 and a byte';
2240         ok $a =~ /^\C{3}/,     'match \C{3}';
2241
2242         ok $a =~ /^\C\Cy/,     'match two \C';
2243         ok $a =~ /^\C{2}y/,    'match \C{2}';
2244
2245         ok $a !~ /^\C\C\Cy/,   q {don't match three \Cy};
2246         ok $a !~ /^\C{2}\Cy/,  q {don't match \C{2}\Cy};
2247         ok $a !~ /^\C{3}y/,    q {don't match \C{3}y};
2248
2249         $a = "\x{1000}y"; # 3 bytes before "y"
2250
2251         ok $a =~ /^\C/,        'match one \C on three-byte UTF-8';
2252         ok $a =~ /^\C{1}/,     'match \C{1}';
2253         ok $a =~ /^\C\C/,      'match two \C';
2254         ok $a =~ /^\C{2}/,     'match \C{2}';
2255         ok $a =~ /^\C\C\C/,    'match three \C';
2256         ok $a =~ /^\C{3}/,     'match \C{3}';
2257
2258         ok $a =~ /^\C\C\C\C/,  'match four \C on three-byte UTF-8 and a byte';
2259         ok $a =~ /^\C{4}/,     'match \C{4}';
2260
2261         ok $a =~ /^\C\C\Cy/,   'match three \Cy';
2262         ok $a =~ /^\C{3}y/,    'match \C{3}y';
2263
2264         ok $a !~ /^\C\C\C\Cy/, q {don't match four \Cy};
2265         ok $a !~ /^\C{4}y/,    q {don't match \C{4}y};
2266     }
2267
2268     
2269     {
2270         local $\;
2271         $_ = 'aaaaaaaaaa';
2272         utf8::upgrade($_); chop $_; $\="\n";
2273         ok /[^\s]+/, 'm/[^\s]/ utf8';
2274         ok /[^\d]+/, 'm/[^\d]/ utf8';
2275         ok +($a = $_, $_ =~ s/[^\s]+/./g), 's/[^\s]/ utf8';
2276         ok +($a = $_, $a =~ s/[^\d]+/./g), 's/[^\s]/ utf8';
2277     }
2278
2279
2280     {
2281         local $BugId   = '15397';
2282         local $Message = 'UTF-8 matching';
2283         ok "\x{100}" =~ /\x{100}/;
2284         ok "\x{100}" =~ /(\x{100})/;
2285         ok "\x{100}" =~ /(\x{100}){1}/;
2286         ok "\x{100}\x{100}" =~ /(\x{100}){2}/;
2287         ok "\x{100}\x{100}" =~ /(\x{100})(\x{100})/;
2288     }
2289
2290
2291     {
2292         local $BugId   = '7471';
2293         local $Message = 'Neither ()* nor ()*? sets $1 when matched 0 times';
2294         local $_       = 'CD';
2295         ok /(AB)*?CD/ && !defined $1;
2296         ok /(AB)*CD/  && !defined $1;
2297     }
2298
2299
2300     {
2301         local $BugId   = '3547';
2302         local $Message = "Caching shouldn't prevent match";
2303         my $pattern = "^(b+?|a){1,2}c";
2304         ok "bac"    =~ /$pattern/ && $1 eq 'a';
2305         ok "bbac"   =~ /$pattern/ && $1 eq 'a';
2306         ok "bbbac"  =~ /$pattern/ && $1 eq 'a';
2307         ok "bbbbac" =~ /$pattern/ && $1 eq 'a';
2308     }
2309
2310
2311
2312     {
2313         local $BugId   = '18232';
2314         local $Message = '$1 should keep UTF-8 ness';
2315         ok "\x{100}" =~ /(.)/;
2316         iseq  $1, "\x{100}",  '$1 is UTF-8';
2317         { 'a' =~ /./; }
2318         iseq  $1, "\x{100}",  '$1 is still UTF-8';
2319         isneq $1, "\xC4\x80", '$1 is not non-UTF-8';
2320     }
2321
2322
2323     {
2324         local $BugId   = '19767';
2325         local $Message = "Optimizer doesn't prematurely reject match";
2326         use utf8;
2327
2328         my $attr = 'Name-1';
2329         my $NormalChar      = qr /[\p{IsDigit}\p{IsLower}\p{IsUpper}]/;
2330         my $NormalWord      = qr /${NormalChar}+?/;
2331         my $PredNameHyphen  = qr /^${NormalWord}(\-${NormalWord})*?$/;
2332
2333         $attr =~ /^$/;
2334         ok $attr =~ $PredNameHyphen;  # Original test.
2335
2336         "a" =~ m/[b]/;
2337         ok "0" =~ /\p{N}+\z/;         # Variant.
2338     }
2339
2340
2341     {
2342         local $BugId   = '20683';
2343         local $Message = "(??{ }) doesn't return stale values";
2344         our $p = 1;
2345         foreach (1, 2, 3, 4) {
2346             $p ++ if /(??{ $p })/
2347         }
2348         iseq $p, 5;
2349
2350         {
2351             package P;
2352             $a = 1;
2353             sub TIESCALAR {bless []}
2354             sub FETCH     {$a ++}
2355         }
2356         tie $p, "P";
2357         foreach (1, 2, 3, 4) {
2358             /(??{ $p })/
2359         }
2360         iseq $p, 5;
2361     }
2362
2363
2364     {
2365         # Subject: Odd regexp behavior
2366         # From: Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>
2367         # Date: Wed, 26 Feb 2003 16:53:12 +0000
2368         # Message-Id: <E18o4nw-0008Ly-00@wisbech.cl.cam.ac.uk>
2369         # To: perl-unicode@perl.org
2370
2371         local $Message = 'Markus Kuhn 2003-02-26';
2372     
2373         my $x = "\x{2019}\nk";
2374         ok $x =~ s/(\S)\n(\S)/$1 $2/sg;
2375         ok $x eq "\x{2019} k";
2376
2377         $x = "b\nk";
2378         ok $x =~ s/(\S)\n(\S)/$1 $2/sg;
2379         ok $x eq "b k";
2380
2381         ok "\x{2019}" =~ /\S/;
2382     }
2383
2384
2385     {
2386         local $BugId = '21411';
2387         local $Message = "(??{ .. }) in split doesn't corrupt its stack";
2388         our $i;
2389         ok '-1-3-5-' eq join '', split /((??{$i++}))/, '-1-3-5-';
2390         no warnings 'deprecated', 'syntax';
2391         split /(?{'WOW'})/, 'abc';
2392         local $" = "|";
2393         iseq "@_", "a|b|c";
2394     }
2395
2396
2397     {
2398         # XXX DAPM 13-Apr-06. Recursive split is still broken. It's only luck it
2399         # hasn't been crashing. Disable this test until it is fixed properly.
2400         # XXX also check what it returns rather than just doing ok(1,...)
2401         # split /(?{ split "" })/, "abc";
2402         local $TODO = "Recursive split is still broken";
2403         ok 0, 'cache_re & "(?{": it dumps core in 5.6.1 & 5.8.0';
2404     }
2405
2406
2407     {
2408         ok "\x{100}\n" =~ /\x{100}\n$/, "UTF-8 length cache and fbm_compile";
2409     }
2410
2411
2412     {
2413         package Str;
2414         use overload q /""/ => sub {${$_ [0]};};
2415         sub new {my ($c, $v) = @_; bless \$v, $c;}
2416
2417         package main;
2418         $_ = Str -> new ("a\x{100}/\x{100}b");
2419         ok join (":", /\b(.)\x{100}/g) eq "a:/", "re_intuit_start and PL_bostr";
2420     }
2421
2422
2423     {
2424         local $BugId = '17757';
2425         $_ = "code:   'x' { '...' }\n"; study;
2426         my @x; push @x, $& while m/'[^\']*'/gx;
2427         local $" = ":";
2428         iseq "@x", "'x':'...'", "Parse::RecDescent triggered infinite loop";
2429     }
2430
2431
2432     {
2433         my $re = qq /^([^X]*)X/;
2434         utf8::upgrade ($re);
2435         ok "\x{100}X" =~ /$re/, "S_cl_and ANYOF_UNICODE & ANYOF_INVERTED";
2436     }
2437
2438
2439     {
2440         local $BugId = '22354';
2441         sub func ($) {
2442             ok "a\nb" !~ /^b/,  "Propagated modifier; $_[0]";
2443             ok "a\nb" =~ /^b/m, "Propagated modifier; $_[0] - with /m";
2444         }
2445         func "standalone";
2446         $_ = "x"; s/x/func "in subst"/e;
2447         $_ = "x"; s/x/func "in multiline subst"/em;
2448
2449         #
2450         # Next two give 'panic: malloc'.
2451         # Outcommented, using two TODOs.
2452         #
2453         local $TODO    = 'panic: malloc';
2454         local $Message = 'Postponed regexp and propaged modifier';
2455       # ok 0 for 1 .. 2;
2456       SKIP: {
2457             skip "panic: malloc", 2;
2458             $_ = "x"; /x(?{func "in regexp"})/;
2459             $_ = "x"; /x(?{func "in multiline regexp"})/m;
2460         }
2461     }
2462
2463
2464     {
2465         local $BugId = '19049';
2466         $_    = "abcdef\n";
2467         my @x = m/./g;
2468         iseq "abcde", $`, 'Global match sets $`';
2469     }
2470
2471
2472     {
2473         ok "123\x{100}" =~ /^.*1.*23\x{100}$/,
2474            'UTF-8 + multiple floating substr';
2475     }
2476
2477
2478     {
2479         local $Message = '<20030808193656.5109.1@llama.ni-s.u-net.com>';
2480
2481         # LATIN SMALL/CAPITAL LETTER A WITH MACRON
2482         ok "  \x{101}" =~ qr/\x{100}/i;
2483
2484         # LATIN SMALL/CAPITAL LETTER A WITH RING BELOW
2485         ok "  \x{1E01}" =~ qr/\x{1E00}/i;
2486
2487         # DESERET SMALL/CAPITAL LETTER LONG I
2488         ok "  \x{10428}" =~ qr/\x{10400}/i;
2489
2490         # LATIN SMALL/CAPITAL LETTER A WITH RING BELOW + 'X'
2491         ok "  \x{1E01}x" =~ qr/\x{1E00}X/i;
2492     }
2493
2494
2495     {
2496         # [perl #23769] Unicode regex broken on simple example
2497         # regrepeat() didn't handle UTF-8 EXACT case right.
2498         local $BugId   = '23769';
2499         my $Mess       = 'regrepeat() handles UTF-8 EXACT case right';
2500         local $Message = $Mess;
2501
2502         my $s = "\x{a0}\x{a0}\x{a0}\x{100}"; chop $s;
2503
2504         ok $s =~ /\x{a0}/;
2505         ok $s =~ /\x{a0}+/;
2506         ok $s =~ /\x{a0}\x{a0}/;
2507
2508         $Message = "$Mess (easy variant)";
2509         ok "aaa\x{100}" =~ /(a+)/;
2510         iseq $1, "aaa";
2511
2512         $Message = "$Mess (easy invariant)";
2513         ok "aaa\x{100}     " =~ /(a+?)/;
2514         iseq $1, "a";
2515
2516         $Message = "$Mess (regrepeat variant)";
2517         ok "\xa0\xa0\xa0\x{100}    " =~ /(\xa0+?)/;
2518         iseq $1, "\xa0";
2519
2520         $Message = "$Mess (regrepeat invariant)";
2521         ok "\xa0\xa0\xa0\x{100}" =~ /(\xa0+)/;
2522         iseq $1, "\xa0\xa0\xa0";
2523
2524         $Message = "$Mess (hard variant)";
2525         ok "\xa0\xa1\xa0\xa1\xa0\xa1\x{100}" =~ /((?:\xa0\xa1)+?)/;
2526         iseq $1, "\xa0\xa1";
2527
2528         $Message = "$Mess (hard invariant)";
2529         ok "ababab\x{100}  " =~ /((?:ab)+)/;
2530         iseq $1, 'ababab';
2531
2532         ok "\xa0\xa1\xa0\xa1\xa0\xa1\x{100}" =~ /((?:\xa0\xa1)+)/;
2533         iseq $1, "\xa0\xa1\xa0\xa1\xa0\xa1";
2534
2535         ok "ababab\x{100}  " =~ /((?:ab)+?)/;
2536         iseq $1, "ab";
2537
2538         $Message = "Don't match first byte of UTF-8 representation";
2539         ok "\xc4\xc4\xc4" !~ /(\x{100}+)/;
2540         ok "\xc4\xc4\xc4" !~ /(\x{100}+?)/;
2541         ok "\xc4\xc4\xc4" !~ /(\x{100}++)/;
2542     }
2543
2544
2545     {
2546         for (120 .. 130) {
2547             my $head = 'x' x $_;
2548             local $Message = q [Don't misparse \x{...} in regexp ] .
2549                              q [near 127 char EXACT limit];
2550             for my $tail ('\x{0061}', '\x{1234}', '\x61') {
2551                 eval_ok qq ["$head$tail" =~ /$head$tail/];
2552             }
2553             local $Message = q [Don't misparse \N{...} in regexp ] .
2554                              q [near 127 char EXACT limit];
2555             for my $tail ('\N{SNOWFLAKE}') {
2556                 eval_ok qq [use charnames ':full';
2557                            "$head$tail" =~ /$head$tail/];
2558             }
2559         }
2560     }
2561
2562
2563     {
2564         # perl panic: pp_match start/end pointers
2565         local $BugId = '25269';
2566         iseq "a-bc", eval {my ($x, $y) = "bca" =~ /^(?=.*(a)).*(bc)/; "$x-$y"},
2567              'Captures can move backwards in string';
2568     }
2569
2570
2571     {
2572         local $BugId   = '27940'; # \cA not recognized in character classes
2573         ok "a\cAb" =~ /\cA/, '\cA in pattern';
2574         ok "a\cAb" =~ /[\cA]/, '\cA in character class';
2575         ok "a\cAb" =~ /[\cA-\cB]/, '\cA in character class range';
2576         ok "abc" =~ /[^\cA-\cB]/, '\cA in negated character class range';
2577         ok "a\cBb" =~ /[\cA-\cC]/, '\cB in character class range';
2578         ok "a\cCbc" =~ /[^\cA-\cB]/, '\cC in negated character class range';
2579         ok "a\cAb" =~ /(??{"\cA"})/, '\cA in ??{} pattern';
2580         ok "ab" !~ /a\cIb/x, '\cI in pattern';
2581     }
2582
2583
2584     {
2585         # perl #28532: optional zero-width match at end of string is ignored
2586         local $BugId = '28532';
2587         ok "abc" =~ /^abc(\z)?/ && defined($1),
2588            'Optional zero-width match at end of string';
2589         ok "abc" =~ /^abc(\z)??/ && !defined($1),
2590            'Optional zero-width match at end of string';
2591     }
2592
2593
2594
2595     {   # TRIE related
2596         our @got = ();
2597         "words" =~ /(word|word|word)(?{push @got, $1})s$/;
2598         iseq @got, 1, "TRIE optimation";
2599
2600         @got = ();
2601         "words" =~ /(word|word|word)(?{push @got,$1})s$/i;
2602         iseq @got, 1,"TRIEF optimisation";
2603
2604         my @nums = map {int rand 1000} 1 .. 100;
2605         my $re = "(" . (join "|", @nums) . ")";
2606         $re = qr/\b$re\b/;
2607
2608         foreach (@nums) {
2609             ok $_ =~ /$re/, "Trie nums";
2610         }
2611
2612         $_ = join " ", @nums;
2613         @got = ();
2614         push @got, $1 while /$re/g;
2615
2616         my %count;
2617         $count {$_} ++ for @got;
2618         my $ok = 1;
2619         for (@nums) {
2620             $ok = 0 if --$count {$_} < 0;
2621         }
2622         ok $ok, "Trie min count matches";
2623     }
2624
2625
2626     {
2627         # TRIE related
2628         # LATIN SMALL/CAPITAL LETTER A WITH MACRON
2629         ok "foba  \x{101}foo" =~ qr/(foo|\x{100}foo|bar)/i &&
2630            $1 eq "\x{101}foo",
2631            "TRIEF + LATIN SMALL/CAPITAL LETTER A WITH MACRON";
2632
2633         # LATIN SMALL/CAPITAL LETTER A WITH RING BELOW
2634         ok "foba  \x{1E01}foo" =~ qr/(foo|\x{1E00}foo|bar)/i &&
2635            $1 eq "\x{1E01}foo",
2636            "TRIEF + LATIN SMALL/CAPITAL LETTER A WITH RING BELOW";
2637
2638         # DESERET SMALL/CAPITAL LETTER LONG I
2639         ok "foba  \x{10428}foo" =~ qr/(foo|\x{10400}foo|bar)/i &&
2640            $1 eq "\x{10428}foo",
2641            "TRIEF + DESERET SMALL/CAPITAL LETTER LONG I";
2642
2643         # LATIN SMALL/CAPITAL LETTER A WITH RING BELOW + 'X'
2644         ok "foba  \x{1E01}xfoo" =~ qr/(foo|\x{1E00}Xfoo|bar)/i &&
2645            $1 eq "\x{1E01}xfoo",
2646            "TRIEF + LATIN SMALL/CAPITAL LETTER A WITH RING BELOW + 'X'";
2647
2648         use charnames ':full';
2649
2650         my $s = "\N{LATIN SMALL LETTER SHARP S}";
2651         ok "foba  ba$s" =~ qr/(foo|Ba$s|bar)/i &&  $1 eq "ba$s",
2652            "TRIEF + LATIN SMALL LETTER SHARP S =~ ss";
2653         ok "foba  ba$s" =~ qr/(Ba$s|foo|bar)/i &&  $1 eq "ba$s",
2654            "TRIEF + LATIN SMALL LETTER SHARP S =~ ss";
2655         ok "foba  ba$s" =~ qr/(foo|bar|Ba$s)/i &&  $1 eq "ba$s",
2656            "TRIEF + LATIN SMALL LETTER SHARP S =~ ss";
2657
2658         ok "foba  ba$s" =~ qr/(foo|Bass|bar)/i &&  $1 eq "ba$s",
2659            "TRIEF + LATIN SMALL LETTER SHARP S =~ ss";
2660
2661         ok "foba  ba$s" =~ qr/(foo|BaSS|bar)/i &&  $1 eq "ba$s",
2662            "TRIEF + LATIN SMALL LETTER SHARP S =~ SS";
2663
2664         ok "foba  ba${s}pxySS$s$s" =~ qr/(b(?:a${s}t|a${s}f|a${s}p)[xy]+$s*)/i
2665             &&  $1 eq "ba${s}pxySS$s$s",
2666            "COMMON PREFIX TRIEF + LATIN SMALL LETTER SHARP S";
2667     }
2668
2669
2670   SKIP:
2671     {
2672         print "# Set PERL_SKIP_PSYCHO_TEST to skip this test\n";
2673         my @normal = qw [the are some normal words];
2674
2675         skip "Skipped Psycho", 2 * @normal if $ENV {PERL_SKIP_PSYCHO_TEST};
2676
2677         local $" = "|";
2678
2679         my @psycho = (@normal, map chr $_, 255 .. 20000);
2680         my $psycho1 = "@psycho";
2681         for (my $i = @psycho; -- $i;) {
2682             my $j = int rand (1 + $i);
2683             @psycho [$i, $j] = @psycho [$j, $i];
2684         }
2685         my $psycho2 = "@psycho";
2686
2687         foreach my $word (@normal) {
2688             ok $word =~ /($psycho1)/ && $1 eq $word, 'Psycho';
2689             ok $word =~ /($psycho2)/ && $1 eq $word, 'Psycho';
2690         }
2691     }
2692
2693
2694     {
2695         local $BugId = '36207';
2696         my $utf8 = "\xe9\x{100}"; chop $utf8;
2697         my $latin1 = "\xe9";
2698
2699         ok $utf8 =~ /\xe9/i, "utf8/latin";
2700         ok $utf8 =~ /$latin1/i, "utf8/latin runtime";
2701         ok $utf8 =~ /(abc|\xe9)/i, "utf8/latin trie";
2702         ok $utf8 =~ /(abc|$latin1)/i, "utf8/latin trie runtime";
2703
2704         ok "\xe9" =~ /$utf8/i, "latin/utf8";
2705         ok "\xe9" =~ /(abc|$utf8)/i, "latin/utf8 trie";
2706         ok $latin1 =~ /$utf8/i, "latin/utf8 runtime";
2707         ok $latin1 =~ /(abc|$utf8)/i, "latin/utf8 trie runtime";
2708     }
2709
2710
2711     {
2712         local $BugId = '37038';
2713         my $s = "abcd";
2714         $s =~ /(..)(..)/g;
2715         $s = $1;
2716         $s = $2;
2717         iseq $2, 'cd',
2718              "Assigning to original string does not corrupt match vars";
2719     }
2720
2721
2722     {
2723         {
2724             package wooosh;
2725             sub gloople {"!"}
2726         }
2727         my $aeek = bless {} => 'wooosh';
2728         eval_ok sub {$aeek -> gloople () =~ /(.)/g},
2729                "//g match against return value of sub";
2730
2731         sub gloople {"!"}
2732         eval_ok sub {gloople () =~ /(.)/g},
2733                "26410 didn't affect sub calls for some reason";
2734     }
2735
2736
2737     {
2738         local $TODO = "See changes 26925-26928, which reverted change 26410";
2739         {
2740             package lv;
2741             our $var = "abc";
2742             sub variable : lvalue {$var}
2743         }
2744         my $o = bless [] => 'lv';
2745         my $f = "";
2746         my $r = eval {
2747             for (1 .. 2) {
2748                 $f .= $1 if $o -> variable =~ /(.)/g;
2749             }
2750             1;
2751         };
2752         if ($r) {
2753             iseq $f, "ab", "pos() retained between calls";
2754         }
2755         else {
2756             local $TODO;
2757             ok 0, "Code failed: $@";
2758         }
2759
2760         our $var = "abc";
2761         sub variable : lvalue {$var}
2762         my $g = "";
2763         my $s = eval {
2764             for (1 .. 2) {
2765                 $g .= $1 if variable =~ /(.)/g;
2766             }
2767             1;
2768         };
2769         if ($s) {
2770             iseq $g, "ab", "pos() retained between calls";
2771         }
2772         else {
2773             local $TODO;
2774             ok 0, "Code failed: $@";
2775         }
2776     }
2777
2778
2779   SKIP:
2780     {
2781         local $BugId = '37836';
2782         skip "In EBCDIC" if $IS_EBCDIC;
2783         no warnings 'utf8';
2784         $_ = pack 'U0C2', 0xa2, 0xf8;  # Ill-formed UTF-8
2785         my $ret = 0;
2786         eval_ok sub {!($ret = s/[\0]+//g)},
2787                 "Ill-formed UTF-8 doesn't match NUL in class";
2788     }
2789
2790
2791     {
2792         # chr(65535) should be allowed in regexes
2793         local $BugId = '38293';
2794         no warnings 'utf8'; # To allow non-characters
2795         my ($c, $r, $s);
2796
2797         $c = chr 0xffff;
2798         $c =~ s/$c//g;
2799         ok $c eq "", "U+FFFF, parsed as atom";
2800
2801         $c = chr 0xffff;
2802         $r = "\\$c";
2803         $c =~ s/$r//g;
2804         ok $c eq "", "U+FFFF backslashed, parsed as atom";
2805
2806         $c = chr 0xffff;
2807         $c =~ s/[$c]//g;
2808         ok $c eq "", "U+FFFF, parsed in class";
2809
2810         $c = chr 0xffff;
2811         $r = "[\\$c]";
2812         $c =~ s/$r//g;
2813         ok $c eq "", "U+FFFF backslashed, parsed in class";
2814
2815         $s = "A\x{ffff}B";
2816         $s =~ s/\x{ffff}//i;
2817         ok $s eq "AB", "U+FFFF, EXACTF";
2818
2819         $s = "\x{ffff}A";
2820         $s =~ s/\bA//;
2821         ok $s eq "\x{ffff}", "U+FFFF, BOUND";
2822
2823         $s = "\x{ffff}!";
2824         $s =~ s/\B!//;
2825         ok $s eq "\x{ffff}", "U+FFFF, NBOUND";
2826     }
2827
2828
2829     {
2830         local $BugId = '39583';
2831         
2832         # The printing characters
2833         my @chars = ("A" .. "Z");
2834         my $delim = ",";
2835         my $size = 32771 - 4;
2836         my $str = '';
2837
2838         # Create some random junk. Inefficient, but it works.
2839         for (my $i = 0; $i < $size; $ i++) {
2840             $str .= $chars [rand @chars];
2841         }
2842
2843         $str .= ($delim x 4);
2844         my $res;
2845         my $matched;
2846         ok $str =~ s/^(.*?)${delim}{4}//s, "Pattern matches";
2847         iseq $str, "", "Empty string";
2848         ok defined $1 && length ($1) == $size, '$1 is correct size';
2849     }
2850
2851
2852     {
2853         local $BugId = '27940';
2854         ok "\0-A"  =~ /\c@-A/, '@- should not be interpolated in a pattern';
2855         ok "\0\0A" =~ /\c@+A/, '@+ should not be interpolated in a pattern';
2856         ok "X\@-A"  =~ /X@-A/, '@- should not be interpolated in a pattern';
2857         ok "X\@\@A" =~ /X@+A/, '@+ should not be interpolated in a pattern';
2858
2859         ok "X\0A" =~ /X\c@?A/,  '\c@?';
2860         ok "X\0A" =~ /X\c@*A/,  '\c@*';
2861         ok "X\0A" =~ /X\c@(A)/, '\c@(';
2862         ok "X\0A" =~ /X(\c@)A/, '\c@)';
2863         ok "X\0A" =~ /X\c@|ZA/, '\c@|';
2864
2865         ok "X\@A" =~ /X@?A/,  '@?';
2866         ok "X\@A" =~ /X@*A/,  '@*';
2867         ok "X\@A" =~ /X@(A)/, '@(';
2868         ok "X\@A" =~ /X(@)A/, '@)';
2869         ok "X\@A" =~ /X@|ZA/, '@|';
2870
2871         local $" = ','; # non-whitespace and non-RE-specific
2872         ok 'abc' =~ /(.)(.)(.)/, 'The last successful match is bogus';
2873         ok "A@+B"  =~ /A@{+}B/,  'Interpolation of @+ in /@{+}/';
2874         ok "A@-B"  =~ /A@{-}B/,  'Interpolation of @- in /@{-}/';
2875         ok "A@+B"  =~ /A@{+}B/x, 'Interpolation of @+ in /@{+}/x';
2876         ok "A@-B"  =~ /A@{-}B/x, 'Interpolation of @- in /@{-}/x';
2877     }
2878
2879
2880     {
2881         use lib 'lib';
2882         use Cname;
2883         
2884         ok 'fooB'  =~ /\N{foo}[\N{B}\N{b}]/, "Passthrough charname";
2885         my $test   = 1233;
2886         #
2887         # Why doesn't must_warn work here?
2888         #
2889         my $w;
2890         local $SIG {__WARN__} = sub {$w .= "@_"};
2891         eval 'q(xxWxx) =~ /[\N{WARN}]/';
2892         ok $w && $w =~ /^Ignoring excess chars from/,
2893                  "Ignoring excess chars warning";
2894
2895         undef $w;
2896         eval q [ok "\0" !~ /[\N{EMPTY-STR}XY]/,
2897                    "Zerolength charname in charclass doesn't match \\0"];
2898         ok $w && $w =~ /^Ignoring zero length/,
2899                  'Ignoring zero length \N{%} in character class warning';
2900
2901         ok 'AB'  =~ /(\N{EVIL})/ && $1 eq 'A', 'Charname caching $1';
2902         ok 'ABC' =~ /(\N{EVIL})/,              'Charname caching $1';
2903         ok 'xy'  =~ /x\N{EMPTY-STR}y/,
2904                     'Empty string charname produces NOTHING node';
2905         ok ''    =~ /\N{EMPTY-STR}/,
2906                     'Empty string charname produces NOTHING node';
2907             
2908     }
2909
2910
2911     {
2912         use charnames ':full';
2913
2914         ok 'aabc' !~ /a\N{PLUS SIGN}b/, '/a\N{PLUS SIGN}b/ against aabc';
2915         ok 'a+bc' =~ /a\N{PLUS SIGN}b/, '/a\N{PLUS SIGN}b/ against a+bc';
2916
2917         ok ' A B' =~ /\N{SPACE}\N{U+0041}\N{SPACE}\N{U+0042}/,
2918             'Intermixed named and unicode escapes';
2919         ok "\N{SPACE}\N{U+0041}\N{SPACE}\N{U+0042}" =~
2920            /\N{SPACE}\N{U+0041}\N{SPACE}\N{U+0042}/,
2921             'Intermixed named and unicode escapes';
2922         ok "\N{SPACE}\N{U+0041}\N{SPACE}\N{U+0042}" =~
2923            /[\N{SPACE}\N{U+0041}][\N{SPACE}\N{U+0042}]/,
2924             'Intermixed named and unicode escapes';     
2925     }
2926
2927
2928     {
2929         our $brackets;
2930         $brackets = qr{
2931             {  (?> [^{}]+ | (??{ $brackets }) )* }
2932         }x;
2933
2934         ok "{b{c}d" !~ m/^((??{ $brackets }))/, "Bracket mismatch";
2935
2936         SKIP: {
2937             our @stack = ();
2938             my @expect = qw(
2939                 stuff1
2940                 stuff2
2941                 <stuff1>and<stuff2>
2942                 right
2943                 <right>
2944                 <<right>>
2945                 <<<right>>>
2946                 <<stuff1>and<stuff2>><<<<right>>>>
2947             );
2948
2949             local $_ = '<<<stuff1>and<stuff2>><<<<right>>>>>';
2950             ok /^(<((?:(?>[^<>]+)|(?1))*)>(?{push @stack, $2 }))$/,
2951                 "Recursion matches";
2952             iseq @stack, @expect, "Right amount of matches"
2953                  or skip "Won't test individual results as count isn't equal",
2954                           0 + @expect;
2955             my $idx = 0;
2956             foreach my $expect (@expect) {
2957                 iseq $stack [$idx], $expect,
2958                     "Expecting '$expect' at stack pos #$idx";
2959                 $idx ++;
2960             }
2961         }
2962     }
2963
2964
2965     {
2966         my $s = '123453456';
2967         $s =~ s/(?<digits>\d+)\k<digits>/$+{digits}/;
2968         ok $s eq '123456', 'Named capture (angle brackets) s///';
2969         $s = '123453456';
2970         $s =~ s/(?'digits'\d+)\k'digits'/$+{digits}/;
2971         ok $s eq '123456', 'Named capture (single quotes) s///';    
2972     }
2973
2974
2975     {
2976         my @ary = (
2977             pack('U', 0x00F1),            # n-tilde
2978             '_'.pack('U', 0x00F1),        # _ + n-tilde
2979             'c'.pack('U', 0x0327),        # c + cedilla
2980             pack('U*', 0x00F1, 0x0327),   # n-tilde + cedilla
2981             'a'.pack('U', 0x00B2),        # a + superscript two
2982             pack('U', 0x0391),            # ALPHA
2983             pack('U', 0x0391).'2',        # ALPHA + 2
2984             pack('U', 0x0391).'_',        # ALPHA + _
2985         );
2986
2987         for my $uni (@ary) {
2988             my ($r1, $c1, $r2, $c2) = eval qq {
2989                 use utf8;
2990                 scalar ("..foo foo.." =~ /(?'${uni}'foo) \\k'${uni}'/),
2991                         \$+{${uni}},
2992                 scalar ("..bar bar.." =~ /(?<${uni}>bar) \\k<${uni}>/),
2993                         \$+{${uni}};
2994             };
2995             ok $r1,                         "Named capture UTF (?'')";
2996             ok defined $c1 && $c1 eq 'foo', "Named capture UTF \%+";
2997             ok $r2,                         "Named capture UTF (?<>)";
2998             ok defined $c2 && $c2 eq 'bar', "Named capture UTF \%+";
2999         }
3000     }
3001
3002
3003     {
3004         my $s = 'foo bar baz';
3005         my (@k, @v, @fetch, $res);
3006         my $count = 0;
3007         my @names = qw ($+{A} $+{B} $+{C});
3008         if ($s =~ /(?<A>foo)\s+(?<B>bar)?\s+(?<C>baz)/) {
3009             while (my ($k, $v) = each (%+)) {
3010                 $count++;
3011             }
3012             @k = sort keys   (%+);
3013             @v = sort values (%+);
3014             $res = 1;
3015             push @fetch,
3016                 ["$+{A}", "$1"],
3017                 ["$+{B}", "$2"],
3018                 ["$+{C}", "$3"],
3019             ;
3020         } 
3021         foreach (0 .. 2) {
3022             if ($fetch [$_]) {
3023                 iseq $fetch [$_] [0], $fetch [$_] [1], $names [$_];
3024             } else {
3025                 ok 0, $names[$_];
3026             }
3027         }
3028         iseq $res, 1, "'$s' =~ /(?<A>foo)\\s+(?<B>bar)?\\s+(?<C>baz)/";
3029         iseq $count, 3, "Got 3 keys in %+ via each";
3030         iseq 0 + @k, 3, 'Got 3 keys in %+ via keys';
3031         iseq "@k", "A B C", "Got expected keys";
3032         iseq "@v", "bar baz foo", "Got expected values";
3033         eval '
3034             no warnings "uninitialized";
3035             print for $+ {this_key_doesnt_exist};
3036         ';
3037         ok !$@, 'lvalue $+ {...} should not throw an exception';
3038     }
3039
3040
3041     {
3042         #
3043         # Almost the same as the block above, except that the capture is nested.
3044         #
3045         local $BugId = '50496';
3046         my $s = 'foo bar baz';
3047         my (@k, @v, @fetch, $res);
3048         my $count = 0;
3049         my @names = qw ($+{A} $+{B} $+{C} $+{D});
3050         if ($s =~ /(?<D>(?<A>foo)\s+(?<B>bar)?\s+(?<C>baz))/) {
3051             while (my ($k,$v) = each(%+)) {
3052                 $count++;
3053             }
3054             @k = sort keys   (%+);
3055             @v = sort values (%+);
3056             $res = 1;
3057             push @fetch,
3058                 ["$+{A}", "$2"],
3059                 ["$+{B}", "$3"],
3060                 ["$+{C}", "$4"],
3061                 ["$+{D}", "$1"],
3062             ;
3063         }
3064         foreach (0 .. 3) {
3065             if ($fetch [$_]) {
3066                 iseq $fetch [$_] [0], $fetch [$_] [1], $names [$_];
3067             } else {
3068                 ok 0, $names [$_];
3069             }
3070         }
3071         iseq $res, 1, "'$s' =~ /(?<D>(?<A>foo)\\s+(?<B>bar)?\\s+(?<C>baz))/";
3072         iseq $count, 4, "Got 4 keys in %+ via each";
3073         iseq @k, 4, 'Got 4 keys in %+ via keys';
3074         iseq "@k", "A B C D", "Got expected keys";
3075         iseq "@v", "bar baz foo foo bar baz", "Got expected values";
3076         eval '
3077             no warnings "uninitialized";
3078             print for $+ {this_key_doesnt_exist};
3079         ';
3080         ok !$@,'lvalue $+ {...} should not throw an exception';
3081     }
3082
3083
3084     {
3085         my $s = 'foo bar baz';
3086         my @res;
3087         if ('1234' =~ /(?<A>1)(?<B>2)(?<A>3)(?<B>4)/) {
3088             foreach my $name (sort keys(%-)) {
3089                 my $ary = $- {$name};
3090                 foreach my $idx (0 .. $#$ary) {
3091                     push @res, "$name:$idx:$ary->[$idx]";
3092                 }
3093             }
3094         }
3095         my @expect = qw (A:0:1 A:1:3 B:0:2 B:1:4);
3096         iseq "@res", "@expect", "Check %-";
3097         eval'
3098             no warnings "uninitialized";
3099             print for $- {this_key_doesnt_exist};
3100         ';
3101         ok !$@,'lvalue $- {...} should not throw an exception';
3102     }
3103
3104
3105   SKIP:
3106     {
3107         # stress test CURLYX/WHILEM.
3108         #
3109         # This test includes varying levels of nesting, and according to
3110         # profiling done against build 28905, exercises every code line in the
3111         # CURLYX and WHILEM blocks, except those related to LONGJMP, the
3112         # super-linear cache and warnings. It executes about 0.5M regexes
3113
3114         skip "No psycho tests" if $ENV {PERL_SKIP_PSYCHO_TEST};
3115         print "# Set PERL_SKIP_PSYCHO_TEST to skip this test\n";
3116         my $r = qr/^
3117                     (?:
3118                         ( (?:a|z+)+ )
3119                         (?:
3120                             ( (?:b|z+){3,}? )
3121                             (
3122                                 (?:
3123                                     (?:
3124                                         (?:c|z+){1,1}?z
3125                                     )?
3126                                     (?:c|z+){1,1}
3127                                 )*
3128                             )
3129                             (?:z*){2,}
3130                             ( (?:z+|d)+ )
3131                             (?:
3132                                 ( (?:e|z+)+ )
3133                             )*
3134                             ( (?:f|z+)+ )
3135                         )*
3136                         ( (?:z+|g)+ )
3137                         (?:
3138                             ( (?:h|z+)+ )
3139                         )*
3140                         ( (?:i|z+)+ )
3141                     )+
3142                     ( (?:j|z+)+ )
3143                     (?:
3144                         ( (?:k|z+)+ )
3145                     )*
3146                     ( (?:l|z+)+ )
3147               $/x;
3148           
3149         my $ok = 1;
3150         my $msg = "CURLYX stress test";
3151         OUTER:
3152           for my $a ("x","a","aa") {
3153             for my $b ("x","bbb","bbbb") {
3154               my $bs = $a.$b;
3155               for my $c ("x","c","cc") {
3156                 my $cs = $bs.$c;
3157                 for my $d ("x","d","dd") {
3158                   my $ds = $cs.$d;
3159                   for my $e ("x","e","ee") {
3160                     my $es = $ds.$e;
3161                     for my $f ("x","f","ff") {
3162                       my $fs = $es.$f;
3163                       for my $g ("x","g","gg") {
3164                         my $gs = $fs.$g;
3165                         for my $h ("x","h","hh") {
3166                           my $hs = $gs.$h;
3167                           for my $i ("x","i","ii") {
3168                             my $is = $hs.$i;
3169                             for my $j ("x","j","jj") {
3170                               my $js = $is.$j;
3171                               for my $k ("x","k","kk") {
3172                                 my $ks = $js.$k;
3173                                 for my $l ("x","l","ll") {
3174                                   my $ls = $ks.$l;
3175                                   if ($ls =~ $r) {
3176                                     if ($ls =~ /x/) {
3177                                       $msg .= ": unexpected match for [$ls]";
3178                                       $ok = 0;
3179                                       last OUTER;
3180                                     }
3181                                     my $cap = "$1$2$3$4$5$6$7$8$9$10$11$12";
3182                                     unless ($ls eq $cap) {
3183                                       $msg .= ": capture: [$ls], got [$cap]";
3184                                       $ok = 0;
3185                                       last OUTER;
3186                                     }
3187                                   }
3188                                   else {
3189                                     unless ($ls =~ /x/) {
3190                                       $msg = ": failed for [$ls]";
3191                                       $ok = 0;
3192                                       last OUTER;
3193                                     }
3194                                   }
3195                                 }
3196                               }
3197                             }
3198                           }
3199                         }
3200                       }
3201                     }
3202                   }
3203                 }
3204               }
3205             }
3206         }
3207         ok($ok, $msg);
3208     }
3209
3210
3211     {
3212         # \, breaks {3,4}
3213         ok "xaaay"    !~ /xa{3\,4}y/, '\, in a pattern';
3214         ok "xa{3,4}y" =~ /xa{3\,4}y/, '\, in a pattern';
3215
3216         # \c\ followed by _
3217         ok "x\c_y"    !~ /x\c\_y/,    '\_ in a pattern';
3218         ok "x\c\_y"   =~ /x\c\_y/,    '\_ in a pattern';
3219
3220         # \c\ followed by other characters
3221         for my $c ("z", "\0", "!", chr(254), chr(256)) {
3222             my $targ = "a\034$c";
3223             my $reg  = "a\\c\\$c";
3224             ok eval ("qq/$targ/ =~ /$reg/"), "\\c\\ in pattern";
3225         }
3226     }
3227
3228
3229     {
3230         local $BugId = '36046';
3231         my $str = 'abc'; 
3232         my $count = 0;
3233         my $mval = 0;
3234         my $pval = 0;
3235         while ($str =~ /b/g) {$mval = $#-; $pval = $#+; $count ++}
3236         iseq $mval,  0, '@- should be empty';
3237         iseq $pval,  0, '@+ should be empty';
3238         iseq $count, 1, 'Should have matched once only';
3239     }
3240
3241
3242     {   # Test the (*PRUNE) pattern
3243         our $count = 0;
3244         'aaab' =~ /a+b?(?{$count++})(*FAIL)/;
3245         iseq $count, 9, "Expect 9 for no (*PRUNE)";
3246         $count = 0;
3247         'aaab' =~ /a+b?(*PRUNE)(?{$count++})(*FAIL)/;
3248         iseq $count, 3, "Expect 3 with (*PRUNE)";
3249         local $_ = 'aaab';
3250         $count = 0;
3251         1 while /.(*PRUNE)(?{$count++})(*FAIL)/g;
3252         iseq $count, 4, "/.(*PRUNE)/";
3253         $count = 0;
3254         'aaab' =~ /a+b?(??{'(*PRUNE)'})(?{$count++})(*FAIL)/;
3255         iseq $count, 3, "Expect 3 with (*PRUNE)";
3256         local $_ = 'aaab';
3257         $count = 0;
3258         1 while /.(??{'(*PRUNE)'})(?{$count++})(*FAIL)/g;
3259         iseq $count, 4, "/.(*PRUNE)/";
3260     }
3261
3262
3263     {   # Test the (*SKIP) pattern
3264         our $count = 0;
3265         'aaab' =~ /a+b?(*SKIP)(?{$count++})(*FAIL)/;
3266         iseq $count, 1, "Expect 1 with (*SKIP)";
3267         local $_ = 'aaab';
3268         $count = 0;
3269         1 while /.(*SKIP)(?{$count++})(*FAIL)/g;
3270         iseq $count, 4, "/.(*SKIP)/";
3271         $_ = 'aaabaaab';
3272         $count = 0;
3273         our @res = ();
3274         1 while /(a+b?)(*SKIP)(?{$count++; push @res,$1})(*FAIL)/g;
3275         iseq $count, 2, "Expect 2 with (*SKIP)";
3276         iseq "@res", "aaab aaab", "Adjacent (*SKIP) works as expected";
3277     }
3278
3279
3280     {   # Test the (*SKIP) pattern
3281         our $count = 0;
3282         'aaab' =~ /a+b?(*MARK:foo)(*SKIP)(?{$count++})(*FAIL)/;
3283         iseq $count, 1, "Expect 1 with (*SKIP)";
3284         local $_ = 'aaab';
3285         $count = 0;
3286         1 while /.(*MARK:foo)(*SKIP)(?{$count++})(*FAIL)/g;
3287         iseq $count, 4, "/.(*SKIP)/";
3288         $_ = 'aaabaaab';
3289         $count = 0;
3290         our @res = ();
3291         1 while /(a+b?)(*MARK:foo)(*SKIP)(?{$count++; push @res,$1})(*FAIL)/g;
3292         iseq $count, 2, "Expect 2 with (*SKIP)";
3293         iseq "@res", "aaab aaab", "Adjacent (*SKIP) works as expected";
3294     }
3295
3296
3297     {   # Test the (*SKIP) pattern
3298         our $count = 0;
3299         'aaab' =~ /a*(*MARK:a)b?(*MARK:b)(*SKIP:a)(?{$count++})(*FAIL)/;
3300         iseq $count, 3, "Expect 3 with *MARK:a)b?(*MARK:b)(*SKIP:a)";
3301         local $_ = 'aaabaaab';
3302         $count = 0;
3303         our @res = ();
3304         1 while
3305         /(a*(*MARK:a)b?)(*MARK:x)(*SKIP:a)(?{$count++; push @res,$1})(*FAIL)/g;
3306         iseq $count, 5, "Expect 5 with (*MARK:a)b?)(*MARK:x)(*SKIP:a)";
3307         iseq "@res", "aaab b aaab b ",
3308              "Adjacent (*MARK:a)b?)(*MARK:x)(*SKIP:a) works as expected";
3309     }
3310
3311
3312     {   # Test the (*COMMIT) pattern
3313         our $count = 0;
3314         'aaabaaab' =~ /a+b?(*COMMIT)(?{$count++})(*FAIL)/;
3315         iseq $count, 1, "Expect 1 with (*COMMIT)";
3316         local $_ = 'aaab';
3317         $count = 0;
3318         1 while /.(*COMMIT)(?{$count++})(*FAIL)/g;
3319         iseq $count, 1, "/.(*COMMIT)/";
3320         $_ = 'aaabaaab';
3321         $count = 0;
3322         our @res = ();
3323         1 while /(a+b?)(*COMMIT)(?{$count++; push @res,$1})(*FAIL)/g;
3324         iseq $count, 1, "Expect 1 with (*COMMIT)";
3325         iseq "@res", "aaab", "Adjacent (*COMMIT) works as expected";
3326     }
3327
3328
3329     {
3330         # Test named commits and the $REGERROR var
3331         our $REGERROR;
3332         for my $name ('', ':foo') {
3333             for my $pat ("(*PRUNE$name)",
3334                          ($name ? "(*MARK$name)" : "") . "(*SKIP$name)",
3335                          "(*COMMIT$name)") {                         
3336                 for my $suffix ('(*FAIL)', '') {
3337                     'aaaab' =~ /a+b$pat$suffix/;
3338                     iseq $REGERROR,
3339                          ($suffix ? ($name ? 'foo' : "1") : ""),
3340                         "Test $pat and \$REGERROR $suffix";
3341                 }
3342             }
3343         }
3344     }
3345
3346
3347     {
3348         # Test named commits and the $REGERROR var
3349         package Fnorble;
3350         our $REGERROR;
3351         for my $name ('', ':foo') {
3352             for my $pat ("(*PRUNE$name)",
3353                          ($name ? "(*MARK$name)" : "") . "(*SKIP$name)",
3354                          "(*COMMIT$name)") {                         
3355                 for my $suffix ('(*FAIL)','') {
3356                     'aaaab' =~ /a+b$pat$suffix/;
3357                   ::iseq $REGERROR,
3358                          ($suffix ? ($name ? 'foo' : "1") : ""),
3359                         "Test $pat and \$REGERROR $suffix";
3360                 }
3361             }
3362         }      
3363     }    
3364
3365
3366     {
3367         # Test named commits and the $REGERROR var
3368         local $Message = '$REGERROR';
3369         our $REGERROR;
3370         for my $word (qw (bar baz bop)) {
3371             $REGERROR = "";
3372             "aaaaa$word" =~
3373               /a+(?:bar(*COMMIT:bar)|baz(*COMMIT:baz)|bop(*COMMIT:bop))(*FAIL)/;
3374             iseq $REGERROR, $word;
3375         }    
3376     }
3377
3378
3379     {
3380         local $BugId = '40684';
3381         local $Message = '/m in precompiled regexp';
3382         my $s = "abc\ndef";
3383         my $rex = qr'^abc$'m;
3384         ok $s =~ m/$rex/;
3385         ok $s =~ m/^abc$/m;
3386     }
3387
3388
3389     {
3390         #Mindnumbingly simple test of (*THEN)
3391         for ("ABC","BAX") {
3392             ok /A (*THEN) X | B (*THEN) C/x, "Simple (*THEN) test";
3393         }
3394     }
3395
3396
3397     {
3398         local $Message = "Relative Recursion";
3399         my $parens = qr/(\((?:[^()]++|(?-1))*+\))/;
3400         local $_ = 'foo((2*3)+4-3) + bar(2*(3+4)-1*(2-3))';
3401         my ($all, $one, $two) = ('', '', '');
3402         ok /foo $parens \s* \+ \s* bar $parens/x;
3403         iseq $1, '((2*3)+4-3)';
3404         iseq $2, '(2*(3+4)-1*(2-3))';
3405         iseq $&, 'foo((2*3)+4-3) + bar(2*(3+4)-1*(2-3))';
3406         iseq $&, $_;
3407     }
3408
3409     {
3410         my $spaces="      ";
3411         local $_ = join 'bar', $spaces, $spaces;
3412         our $count = 0;
3413         s/(?>\s+bar)(?{$count++})//g;
3414         iseq $_, $spaces, "SUSPEND final string";
3415         iseq $count, 1, "Optimiser should have prevented more than one match";
3416     }
3417
3418     {
3419         local $BugId   = '36909';
3420         local $Message = '(?: ... )? should not lose $^R';
3421         $^R = 'Nothing';
3422         {
3423             local $^R = "Bad";
3424             ok 'x foofoo y' =~ m {
3425                       (foo) # $^R correctly set
3426                       (?{ "last regexp code result" })
3427             }x;
3428             iseq $^R, 'last regexp code result';
3429         }
3430         iseq $^R, 'Nothing';
3431
3432         {
3433             local $^R = "Bad";
3434
3435             ok 'x foofoo y' =~ m {
3436                       (?:foo|bar)+ # $^R correctly set
3437                       (?{ "last regexp code result" })
3438             }x;
3439             iseq $^R, 'last regexp code result';
3440         }
3441         iseq $^R, 'Nothing';
3442
3443         {
3444             local $^R = "Bad";
3445             ok 'x foofoo y' =~ m {
3446                       (foo|bar)\1+ # $^R undefined
3447                       (?{ "last regexp code result" })
3448             }x;
3449             iseq $^R, 'last regexp code result';
3450         }
3451         iseq $^R, 'Nothing';
3452
3453         {
3454             local $^R = "Bad";
3455             ok 'x foofoo y' =~ m {
3456                       (foo|bar)\1 # This time without the +
3457                       (?{"last regexp code result"})
3458             }x;
3459             iseq $^R, 'last regexp code result';
3460         }
3461         iseq $^R, 'Nothing';
3462     }
3463
3464
3465     {
3466         local $BugId   = '22395';
3467         local $Message = 'Match is linear, not quadratic';
3468         our $count;
3469         for my $l (10, 100, 1000) {
3470             $count = 0;
3471             ('a' x $l) =~ /(.*)(?{$count++})[bc]/;
3472             local $TODO = "Should be L+1 not L*(L+3)/2 (L=$l)";
3473             iseq $count, $l + 1;
3474         }
3475     }
3476
3477
3478     {
3479         local $BugId   = '22614';
3480         local $Message = '@-/@+ should not have undefined values';
3481         local $_ = 'ab';
3482         our @len = ();
3483         /(.){1,}(?{push @len,0+@-})(.){1,}(?{})^/;
3484         iseq "@len", "2 2 2";
3485     }
3486
3487
3488     {
3489         local $BugId   = '18209';
3490         local $Message = '$& set on s///';
3491         my $text = ' word1 word2 word3 word4 word5 word6 ';
3492
3493         my @words = ('word1', 'word3', 'word5');
3494         my $count;
3495         foreach my $word (@words) {
3496             $text =~ s/$word\s//gi; # Leave a space to seperate words
3497                                     # in the resultant str.
3498             # The following block is not working.
3499             if ($&) {
3500                 $count ++;
3501             }
3502             # End bad block
3503         }
3504         iseq $count, 3;
3505         iseq $text, ' word2 word4 word6 ';
3506     }
3507
3508
3509     {
3510         # RT#6893
3511         local $BugId = '6893';
3512         local $_ = qq (A\nB\nC\n); 
3513         my @res;
3514         while (m#(\G|\n)([^\n]*)\n#gsx) { 
3515             push @res, "$2"; 
3516             last if @res > 3;
3517         }
3518         iseq "@res", "A B C", "/g pattern shouldn't infinite loop";
3519     }
3520
3521
3522     {
3523         # From Message-ID: <877ixs6oa6.fsf@k75.linux.bogus>
3524         my $dow_name = "nada";
3525         my $parser = "(\$dow_name) = \$time_string =~ /(D\x{e9}\\ " .
3526                      "C\x{e9}adaoin|D\x{e9}\\ Sathairn|\\w+|\x{100})/";
3527         my $time_string = "D\x{e9} C\x{e9}adaoin";
3528         eval $parser;
3529         ok !$@, "Test Eval worked";
3530         iseq $dow_name, $time_string, "UTF-8 trie common prefix extraction";
3531     }
3532
3533
3534     {
3535         my $v;
3536         ($v = 'bar') =~ /(\w+)/g;
3537         $v = 'foo';
3538         iseq "$1", 'bar', '$1 is safe after /g - may fail due ' .
3539                           'to specialized config in pp_hot.c'
3540     }
3541
3542
3543     {
3544         local $Message = "http://nntp.perl.org/group/perl.perl5.porters/118663";
3545         my $qr_barR1 = qr/(bar)\g-1/;
3546         ok "foobarbarxyz" =~ $qr_barR1;
3547         ok "foobarbarxyz" =~ qr/foo${qr_barR1}xyz/;
3548         ok "foobarbarxyz" =~ qr/(foo)${qr_barR1}xyz/;
3549         ok "foobarbarxyz" =~ qr/(foo)(bar)\g{-1}xyz/;
3550         ok "foobarbarxyz" =~ qr/(foo${qr_barR1})xyz/;
3551         ok "foobarbarxyz" =~ qr/(foo(bar)\g{-1})xyz/;
3552     } 
3553
3554
3555     {
3556         local $BugId   = '41010';
3557         local $Message = 'No optimizer bug';
3558         my @tails  = ('', '(?(1))', '(|)', '()?');    
3559         my @quants = ('*','+');
3560         my $doit = sub {
3561             my $pats = shift;
3562             for (@_) {
3563                 for my $pat (@$pats) {
3564                     for my $quant (@quants) {
3565                         for my $tail (@tails) {
3566                             my $re = "($pat$quant\$)$tail";
3567                             ok /$re/  && $1 eq $_, "'$_' =~ /$re/";
3568                             ok /$re/m && $1 eq $_, "'$_' =~ /$re/m";
3569                         }
3570                     }
3571                 }
3572             }
3573         };    
3574         
3575         my @dpats = ('\d',
3576                      '[1234567890]',
3577                      '(1|[23]|4|[56]|[78]|[90])',
3578                      '(?:1|[23]|4|[56]|[78]|[90])',
3579                      '(1|2|3|4|5|6|7|8|9|0)',
3580                      '(?:1|2|3|4|5|6|7|8|9|0)');
3581         my @spats = ('[ ]', ' ', '( |\t)', '(?: |\t)', '[ \t]', '\s');
3582         my @sstrs = ('  ');
3583         my @dstrs = ('12345');
3584         $doit -> (\@spats, @sstrs);
3585         $doit -> (\@dpats, @dstrs);
3586     }
3587
3588
3589     {
3590         local $Message = '$REGMARK';
3591         our @r = ();
3592         our ($REGMARK, $REGERROR);
3593         ok 'foofoo' =~ /foo (*MARK:foo) (?{push @r,$REGMARK}) /x;
3594         iseq "@r","foo";           
3595         iseq $REGMARK, "foo";
3596         ok 'foofoo' !~ /foo (*MARK:foo) (*FAIL) /x;
3597         ok !$REGMARK;
3598         iseq $REGERROR, 'foo';
3599     }
3600
3601
3602     {
3603         local $Message = '\K test';
3604         my $x;
3605         $x = "abc.def.ghi.jkl";
3606         $x =~ s/.*\K\..*//;
3607         iseq $x, "abc.def.ghi";
3608         
3609         $x = "one two three four";
3610         $x =~ s/o+ \Kthree//g;
3611         iseq $x, "one two  four";
3612         
3613         $x = "abcde";
3614         $x =~ s/(.)\K/$1/g;
3615         iseq $x, "aabbccddee";
3616     }
3617
3618
3619     {
3620         sub kt {
3621             return '4' if $_[0] eq '09028623';
3622         }
3623         # Nested EVAL using PL_curpm (via $1 or friends)
3624         my $re;
3625         our $grabit = qr/ ([0-6][0-9]{7}) (??{ kt $1 }) [890] /x;
3626         $re = qr/^ ( (??{ $grabit }) ) $ /x;
3627         my @res = '0902862349' =~ $re;
3628         iseq join ("-", @res), "0902862349",
3629             'PL_curpm is set properly on nested eval';
3630
3631         our $qr = qr/ (o) (??{ $1 }) /x;
3632         ok 'boob'=~/( b (??{ $qr }) b )/x && 1, "PL_curpm, nested eval";
3633     }
3634
3635
3636     {
3637         use charnames ":full";
3638         ok "\N{ROMAN NUMERAL ONE}" =~ /\p{Alphabetic}/, "I =~ Alphabetic";
3639         ok "\N{ROMAN NUMERAL ONE}" =~ /\p{Uppercase}/,  "I =~ Uppercase";
3640         ok "\N{ROMAN NUMERAL ONE}" !~ /\p{Lowercase}/,  "I !~ Lowercase";
3641         ok "\N{ROMAN NUMERAL ONE}" =~ /\p{IDStart}/,    "I =~ ID_Start";
3642         ok "\N{ROMAN NUMERAL ONE}" =~ /\p{IDContinue}/, "I =~ ID_Continue";
3643         ok "\N{SMALL ROMAN NUMERAL ONE}" =~ /\p{Alphabetic}/, "i =~ Alphabetic";
3644         ok "\N{SMALL ROMAN NUMERAL ONE}" !~ /\p{Uppercase}/,  "i !~ Uppercase";
3645         ok "\N{SMALL ROMAN NUMERAL ONE}" =~ /\p{Lowercase}/,  "i =~ Lowercase";
3646         ok "\N{SMALL ROMAN NUMERAL ONE}" =~ /\p{IDStart}/,    "i =~ ID_Start";
3647         ok "\N{SMALL ROMAN NUMERAL ONE}" =~ /\p{IDContinue}/, "i =~ ID_Continue"
3648     }
3649
3650
3651     {
3652         # requirement of Unicode Technical Standard #18, 1.7 Code Points
3653         # cf. http://www.unicode.org/reports/tr18/#Supplementary_Characters
3654         for my $u (0x7FF, 0x800, 0xFFFF, 0x10000) {
3655             no warnings 'utf8'; # oops
3656             my $c = chr $u;
3657             my $x = sprintf '%04X', $u;
3658             ok "A${c}B" =~ /A[\0-\x{10000}]B/, "Unicode range - $x";
3659         }
3660     }
3661
3662
3663     {
3664         my $res="";
3665
3666         if ('1' =~ /(?|(?<digit>1)|(?<digit>2))/) {
3667             $res = "@{$- {digit}}";
3668         }
3669         iseq $res, "1",
3670             "Check that (?|...) doesnt cause dupe entries in the names array";
3671         
3672         $res = "";
3673         if ('11' =~ /(?|(?<digit>1)|(?<digit>2))(?&digit)/) {
3674             $res = "@{$- {digit}}";
3675         }
3676         iseq $res, "1", "Check that (?&..) to a buffer inside " .
3677                         "a (?|...) goes to the leftmost";
3678     }
3679
3680
3681     {
3682         use warnings;
3683         local $Message = "ASCII pattern that really is UTF-8";
3684         my @w;
3685         local $SIG {__WARN__} = sub {push @w, "@_"};
3686         my $c = qq (\x{DF}); 
3687         ok $c =~ /${c}|\x{100}/;
3688         ok @w == 0;
3689     }    
3690
3691
3692     {
3693         local $Message = "Corruption of match results of qr// across scopes";
3694         my $qr = qr/(fo+)(ba+r)/;
3695         'foobar' =~ /$qr/;
3696         iseq "$1$2", "foobar";
3697         {
3698             'foooooobaaaaar' =~ /$qr/;
3699             iseq "$1$2", 'foooooobaaaaar';    
3700         }
3701         iseq "$1$2", "foobar";
3702     }
3703
3704
3705     {
3706         local $Message = "HORIZWS";
3707         local $_ = "\t \r\n \n \t".chr(11)."\n";
3708         s/\H/H/g;
3709         s/\h/h/g;
3710         iseq $_, "hhHHhHhhHH";
3711         $_ = "\t \r\n \n \t" . chr (11) . "\n";
3712         utf8::upgrade ($_);
3713         s/\H/H/g;
3714         s/\h/h/g;
3715         iseq $_, "hhHHhHhhHH";
3716     }    
3717
3718
3719     {
3720         local $Message = "Various whitespace special patterns";
3721         my @h = map {chr $_}   0x09,   0x20,   0xa0, 0x1680, 0x180e, 0x2000,
3722                              0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006,
3723                              0x2007, 0x2008, 0x2009, 0x200a, 0x202f, 0x205f,
3724                              0x3000;
3725         my @v = map {chr $_}   0x0a,   0x0b,   0x0c,   0x0d,   0x85, 0x2028,
3726                              0x2029;
3727         my @lb = ("\x0D\x0A", map {chr $_} 0x0A .. 0x0D, 0x85, 0x2028, 0x2029);
3728         foreach my $t ([\@h,  qr/\h/, qr/\h+/],
3729                        [\@v,  qr/\v/, qr/\v+/],
3730                        [\@lb, qr/\R/, qr/\R+/],) {
3731             my $ary = shift @$t;
3732             foreach my $pat (@$t) {
3733                 foreach my $str (@$ary) {
3734                     ok $str =~ /($pat)/, $pat;
3735                     iseq $1, $str, $pat;
3736                     utf8::upgrade ($str);
3737                     ok $str =~ /($pat)/, "Upgraded string - $pat";
3738                     iseq $1, $str, "Upgraded string - $pat";
3739                 }
3740             }
3741         }
3742     }
3743
3744
3745     {
3746         local $Message = "Check that \\xDF match properly in its various forms";
3747         # Test that \xDF matches properly. this is pretty hacky stuff,
3748         # but its actually needed. The malarky with '-' is to prevent
3749         # compilation caching from playing any role in the test.
3750         my @df = (chr (0xDF), '-', chr (0xDF));
3751         utf8::upgrade ($df [2]);
3752         my @strs = ('ss', 'sS', 'Ss', 'SS', chr (0xDF));
3753         my @ss = map {("$_", "$_")} @strs;
3754         utf8::upgrade ($ss [$_ * 2 + 1]) for 0 .. $#strs;
3755
3756         for my $ssi (0 .. $#ss) {
3757             for my $dfi (0 .. $#df) {
3758                 my $pat = $df [$dfi];
3759                 my $str = $ss [$ssi];
3760                 my $utf_df = ($dfi > 1) ? 'utf8' : '';
3761                 my $utf_ss = ($ssi % 2) ? 'utf8' : '';
3762                 (my $sstr = $str) =~ s/\xDF/\\xDF/;
3763
3764                 if ($utf_df || $utf_ss || length ($ss [$ssi]) == 1) {
3765                     my $ret = $str =~ /$pat/i;
3766                     next if $pat eq '-';
3767                     ok $ret, "\"$sstr\" =~ /\\xDF/i " .
3768                              "(str is @{[$utf_ss||'latin']}, pat is " .
3769                              "@{[$utf_df||'latin']})";
3770                 }
3771                 else {
3772                     my $ret = $str !~ /$pat/i;
3773                     next if $pat eq '-';
3774                     ok $ret, "\"$sstr\" !~ /\\xDF/i " .
3775                              "(str is @{[$utf_ss||'latin']}, pat is " .
3776                              "@{[$utf_df||'latin']})";
3777                 }
3778             }
3779         }
3780     }
3781
3782
3783     {
3784         local $Message = "BBC(Bleadperl Breaks CPAN) Today: String::Multibyte";
3785         my $re  = qr/(?:[\x00-\xFF]{4})/;
3786         my $hyp = "\0\0\0-";
3787         my $esc = "\0\0\0\\";
3788
3789         my $str = "$esc$hyp$hyp$esc$esc";
3790         my @a = ($str =~ /\G(?:\Q$esc$esc\E|\Q$esc$hyp\E|$re)/g);
3791
3792         iseq @a,3;
3793         local $" = "=";
3794         iseq "@a","$esc$hyp=$hyp=$esc$esc";
3795     }
3796
3797
3798     {
3799         # Test for keys in %+ and %-
3800         local $Message = 'Test keys in %+ and %-';
3801         no warnings 'uninitialized';
3802         my $_ = "abcdef";
3803         /(?<foo>a)|(?<foo>b)/;
3804         iseq ((join ",", sort keys %+), "foo");
3805         iseq ((join ",", sort keys %-), "foo");
3806         iseq ((join ",", sort values %+), "a");
3807         iseq ((join ",", sort map "@$_", values %-), "a ");
3808         /(?<bar>a)(?<bar>b)(?<quux>.)/;
3809         iseq ((join ",", sort keys %+), "bar,quux");
3810         iseq ((join ",", sort keys %-), "bar,quux");
3811         iseq ((join ",", sort values %+), "a,c"); # leftmost
3812         iseq ((join ",", sort map "@$_", values %-), "a b,c");
3813         /(?<un>a)(?<deux>c)?/; # second buffer won't capture
3814         iseq ((join ",", sort keys %+), "un");
3815         iseq ((join ",", sort keys %-), "deux,un");
3816         iseq ((join ",", sort values %+), "a");
3817         iseq ((join ",", sort map "@$_", values %-), ",a");
3818     }
3819
3820
3821     {
3822         # length() on captures, the numbered ones end up in Perl_magic_len
3823         my $_ = "aoeu \xe6var ook";
3824         /^ \w+ \s (?<eek>\S+)/x;
3825
3826         iseq length ($`),      0, q[length $`];
3827         iseq length ($'),      4, q[length $'];
3828         iseq length ($&),      9, q[length $&];
3829         iseq length ($1),      4, q[length $1];
3830         iseq length ($+{eek}), 4, q[length $+{eek} == length $1];
3831     }
3832
3833
3834     {
3835         my $ok = -1;
3836
3837         $ok = exists ($-{x}) ? 1 : 0 if 'bar' =~ /(?<x>foo)|bar/;
3838         iseq $ok, 1, '$-{x} exists after "bar"=~/(?<x>foo)|bar/';
3839         iseq scalar (%+), 0, 'scalar %+ == 0 after "bar"=~/(?<x>foo)|bar/';
3840         iseq scalar (%-), 1, 'scalar %- == 1 after "bar"=~/(?<x>foo)|bar/';
3841
3842         $ok = -1;
3843         $ok = exists ($+{x}) ? 1 : 0 if 'bar' =~ /(?<x>foo)|bar/;
3844         iseq $ok, 0, '$+{x} not exists after "bar"=~/(?<x>foo)|bar/';
3845         iseq scalar (%+), 0, 'scalar %+ == 0 after "bar"=~/(?<x>foo)|bar/';
3846         iseq scalar (%-), 1, 'scalar %- == 1 after "bar"=~/(?<x>foo)|bar/';
3847
3848         $ok = -1;
3849         $ok = exists ($-{x}) ? 1 : 0 if 'foo' =~ /(?<x>foo)|bar/;
3850         iseq $ok, 1, '$-{x} exists after "foo"=~/(?<x>foo)|bar/';
3851         iseq scalar (%+), 1, 'scalar %+ == 1 after "foo"=~/(?<x>foo)|bar/';
3852         iseq scalar (%-), 1, 'scalar %- == 1 after "foo"=~/(?<x>foo)|bar/';
3853
3854         $ok = -1;
3855         $ok = exists ($+{x}) ? 1 : 0 if 'foo'=~/(?<x>foo)|bar/;
3856         iseq $ok, 1, '$+{x} exists after "foo"=~/(?<x>foo)|bar/';
3857     }
3858
3859
3860     {
3861         local $_;
3862         ($_ = 'abc') =~ /(abc)/g;
3863         $_ = '123'; 
3864         iseq "$1", 'abc', "/g leads to unsafe match vars: $1";
3865     }
3866
3867
3868     {
3869         local $Message = 'Message-ID: <20070818091501.7eff4831@r2d2>';
3870         my $str = "";
3871         for (0 .. 5) {
3872             my @x;
3873             $str .= "@x"; # this should ALWAYS be the empty string
3874             'a' =~ /(a|)/;
3875             push @x, 1;
3876         }
3877         iseq length ($str), 0, "Trie scope error, string should be empty";
3878         $str = "";
3879         my @foo = ('a') x 5;
3880         for (@foo) {
3881             my @bar;
3882             $str .= "@bar";
3883             s/a|/push @bar, 1/e;
3884         }
3885         iseq length ($str), 0, "Trie scope error, string should be empty";
3886     }
3887
3888
3889     {
3890         local $BugId = '45605';
3891         # [perl #45605] Regexp failure with utf8-flagged and byte-flagged string
3892
3893         my $utf_8 = "\xd6schel";
3894         utf8::upgrade ($utf_8);
3895         $utf_8 =~ m {(\xd6|&Ouml;)schel};
3896         iseq $1, "\xd6", "Upgrade error";
3897     }
3898
3899
3900     {
3901         # Regardless of utf8ness any character matches itself when 
3902         # doing a case insensitive match. See also [perl #36207] 
3903         local $BugId = '36207';
3904         for my $o (0 .. 255) {
3905             my @ch = (chr ($o), chr ($o));
3906             utf8::upgrade ($ch [1]);
3907             for my $u_str (0, 1) {
3908                 for my $u_pat (0, 1) {
3909                     ok $ch [$u_str] =~ /\Q$ch[$u_pat]\E/i,
3910                     "\$c =~ /\$c/i : chr ($o) : u_str = $u_str u_pat = $u_pat";
3911                     ok $ch [$u_str] =~ /\Q$ch[$u_pat]\E|xyz/i,
3912                     "\$c=~/\$c|xyz/i : chr($o) : u_str = $u_str u_pat = $u_pat";
3913                 }
3914             }
3915         }
3916     }
3917
3918
3919     {
3920         our $a = 3; "" =~ /(??{ $a })/;
3921         our $b = $a;
3922         iseq $b, $a, "Copy of scalar used for postponed subexpression";
3923     }
3924
3925
3926     {
3927          local $BugId   = '49190';
3928          local $Message = '$REGMARK in replacement';
3929          our $REGMARK;
3930          my $_ = "A";
3931          ok s/(*:B)A/$REGMARK/;
3932          iseq $_, "B";
3933          $_ = "CCCCBAA";
3934          ok s/(*:X)A+|(*:Y)B+|(*:Z)C+/$REGMARK/g;
3935          iseq $_, "ZYX";
3936     }
3937
3938
3939     {
3940         our @ctl_n = ();
3941         our @plus = ();
3942         our $nested_tags;
3943         $nested_tags = qr{
3944             <
3945                (\w+)
3946                (?{
3947                        push @ctl_n,$^N;
3948                        push @plus,$+;
3949                })
3950             >
3951             (??{$nested_tags})*
3952             </\s* \w+ \s*>
3953         }x;
3954
3955         my $match = '<bla><blubb></blubb></bla>' =~ m/^$nested_tags$/;
3956         ok $match, 'nested construct matches';
3957         iseq "@ctl_n", "bla blubb", '$^N inside of (?{}) works as expected';
3958         iseq "@plus",  "bla blubb", '$+  inside of (?{}) works as expected';
3959     }
3960
3961
3962     {
3963         local $BugId   = '52658';
3964         local $Message = 'Substitution evaluation in list context';
3965         my $reg = '../xxx/';
3966         my @te  = ($reg =~ m{^(/?(?:\.\./)*)},
3967                    $reg =~ s/(x)/'b'/eg > 1 ? '##' : '++');
3968         iseq $reg, '../bbb/';
3969         iseq $te [0], '../';
3970     }
3971
3972
3973     SKIP: {
3974         # XXX: This set of tests is essentially broken, POSIX character classes
3975         # should not have differing definitions under Unicode. 
3976         # There are property names for that.
3977         skip "Tests assume ASCII", 4 unless $IS_ASCII;
3978
3979         my @notIsPunct = grep {/[[:punct:]]/ and not /\p{IsPunct}/}
3980                                 map {chr} 0x20 .. 0x7f;
3981         iseq join ('', @notIsPunct), '$+<=>^`|~',
3982             '[:punct:] disagress with IsPunct on Symbols';
3983
3984         my @isPrint = grep {not /[[:print:]]/ and /\p{IsPrint}/}
3985                             map {chr} 0 .. 0x1f, 0x7f .. 0x9f;
3986         iseq join ('', @isPrint), "\x09\x0a\x0b\x0c\x0d\x85",
3987             'IsPrint disagrees with [:print:] on control characters';
3988
3989         my @isPunct = grep {/[[:punct:]]/ != /\p{IsPunct}/}
3990                             map {chr} 0x80 .. 0xff;
3991         iseq join ('', @isPunct), "\xa1\xab\xb7\xbb\xbf",       # ¡ « · » ¿
3992             'IsPunct disagrees with [:punct:] outside ASCII';
3993
3994         my @isPunctLatin1 = eval q {
3995             use encoding 'latin1';
3996             grep {/[[:punct:]]/ != /\p{IsPunct}/} map {chr} 0x80 .. 0xff;
3997         };
3998         skip "Eval failed ($@)", 1 if $@;
3999         skip "PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS set to 0", 1
4000               if $ENV {REAL_POSIX_CC};
4001         iseq join ('', @isPunctLatin1), '', 
4002             'IsPunct agrees with [:punct:] with explicit Latin1';
4003     } 
4004
4005
4006     {
4007         local $BugId =  '60034';
4008         local $TODO  = "See bug 60034";
4009         my $a = "xyzt" x 8192;
4010         ok $a =~ /\A(?>[a-z])*\z/,
4011                 '(?>) does not cause wrongness on long string';
4012         my $b = $a . chr 256;
4013         chop $b;
4014         {
4015             local $TODO;
4016             iseq $a, $b;
4017         }
4018         ok $b =~ /\A(?>[a-z])*\z/,
4019            '(?>) does not cause wrongness on long string with UTF-8';
4020     }
4021
4022
4023     #
4024     # Keep the following tests last -- they may crash perl
4025     #
4026     print "# Tests that follow may crash perl\n";
4027     {   
4028         local $BugId   = '19049/38869';
4029         local $Message = 'Pattern in a loop, failure should not ' .
4030                          'affect previous success';
4031         my @list = (
4032             'ab cdef',             # Matches regex
4033             ('e' x 40000 ) .'ab c' # Matches not, but 'ab c' matches part of it
4034         );
4035         my $y;
4036         my $x;
4037         foreach (@list) {
4038             m/ab(.+)cd/i; # The ignore-case seems to be important
4039             $y = $1;      # Use $1, which might not be from the last match!
4040             $x = substr ($list [0], $- [0], $+ [0] - $- [0]);
4041         }
4042         iseq $y, ' ';
4043         iseq $x, 'ab cd';
4044     }
4045
4046
4047     {
4048         local $BugId = '24274';
4049
4050         ok (("a" x (2 ** 15 - 10)) =~ /^()(a|bb)*$/, "Recursive stack cracker");
4051         ok ((q(a)x 100) =~ /^(??{'(.)'x 100})/, 
4052             "Regexp /^(??{'(.)'x 100})/ crashes older perls");
4053     }
4054
4055
4056     {
4057         eval '/\k/';
4058         ok $@ =~ /\QSequence \k... not terminated in regex;\E/,
4059            'Lone \k not allowed';
4060     }
4061
4062
4063     {
4064         local $Message = "Substitution with lookahead (possible segv)";
4065         $_ = "ns1ns1ns1";
4066         s/ns(?=\d)/ns_/g;
4067         iseq $_, "ns_1ns_1ns_1";
4068         $_ = "ns1";
4069         s/ns(?=\d)/ns_/;
4070         iseq $_, "ns_1";
4071         $_ = "123";
4072         s/(?=\d+)|(?<=\d)/!Bang!/g;
4073         iseq $_, "!Bang!1!Bang!2!Bang!3!Bang!";
4074     }
4075
4076
4077     {
4078         # [perl #45337] utf8 + "[a]a{2}" + /$.../ = panic: sv_len_utf8 cache
4079         local $BugId = '45337';
4080         local ${^UTF8CACHE} = -1;
4081         local $Message = "Shouldn't panic";
4082         my $s = "[a]a{2}";
4083         utf8::upgrade $s;
4084         ok "aaa" =~ /$s/;
4085     }
4086     {
4087         local $BugId = '57042';
4088         local $Message = "Check if tree logic breaks \$^R";
4089         my $cond_re = qr/\s*
4090             \s* (?:
4091                    \( \s* A  (?{1})
4092                  | \( \s* B  (?{2})
4093                )
4094            /x;
4095         my @res;
4096         for my $line ("(A)","(B)") {
4097            if ($line =~ m/$cond_re/) {
4098                push @res, $^R ? "#$^R" : "UNDEF";
4099            }
4100         }
4101         iseq "@res","#1 #2";
4102     }
4103     #
4104     # This should be the last test.
4105     #
4106     iseq $test + 1, $EXPECTED_TESTS, "Got the right number of tests!";
4107
4108 } # End of sub run_tests
4109
4110 1;