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