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