Re: [PATCH]: fix: [perl #39583] Pattern Match fails for specific length string
[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 # please update note at bottom of file when you change this
10 print "1..1211\n"; 
11
12 BEGIN {
13     chdir 't' if -d 't';
14     @INC = '../lib';
15 }
16
17 eval 'use Config';          #  Defaults assumed if this fails
18
19 $x = "abc\ndef\n";
20
21 if ($x =~ /^abc/) {print "ok 1\n";} else {print "not ok 1\n";}
22 if ($x !~ /^def/) {print "ok 2\n";} else {print "not ok 2\n";}
23
24 # used to be a test for $*
25 if ($x =~ /^def/m) {print "ok 3\n";} else {print "not ok 3\n";}
26
27 $_ = '123';
28 if (/^([0-9][0-9]*)/) {print "ok 4\n";} else {print "not ok 4\n";}
29
30 if ($x =~ /^xxx/) {print "not ok 5\n";} else {print "ok 5\n";}
31 if ($x !~ /^abc/) {print "not ok 6\n";} else {print "ok 6\n";}
32
33 if ($x =~ /def/) {print "ok 7\n";} else {print "not ok 7\n";}
34 if ($x !~ /def/) {print "not ok 8\n";} else {print "ok 8\n";}
35
36 if ($x !~ /.def/) {print "ok 9\n";} else {print "not ok 9\n";}
37 if ($x =~ /.def/) {print "not ok 10\n";} else {print "ok 10\n";}
38
39 if ($x =~ /\ndef/) {print "ok 11\n";} else {print "not ok 11\n";}
40 if ($x !~ /\ndef/) {print "not ok 12\n";} else {print "ok 12\n";}
41
42 $_ = 'aaabbbccc';
43 if (/(a*b*)(c*)/ && $1 eq 'aaabbb' && $2 eq 'ccc') {
44         print "ok 13\n";
45 } else {
46         print "not ok 13\n";
47 }
48 if (/(a+b+c+)/ && $1 eq 'aaabbbccc') {
49         print "ok 14\n";
50 } else {
51         print "not ok 14\n";
52 }
53
54 if (/a+b?c+/) {print "not ok 15\n";} else {print "ok 15\n";}
55
56 $_ = 'aaabccc';
57 if (/a+b?c+/) {print "ok 16\n";} else {print "not ok 16\n";}
58 if (/a*b+c*/) {print "ok 17\n";} else {print "not ok 17\n";}
59
60 $_ = 'aaaccc';
61 if (/a*b?c*/) {print "ok 18\n";} else {print "not ok 18\n";}
62 if (/a*b+c*/) {print "not ok 19\n";} else {print "ok 19\n";}
63
64 $_ = 'abcdef';
65 if (/bcd|xyz/) {print "ok 20\n";} else {print "not ok 20\n";}
66 if (/xyz|bcd/) {print "ok 21\n";} else {print "not ok 21\n";}
67
68 if (m|bc/*d|) {print "ok 22\n";} else {print "not ok 22\n";}
69
70 if (/^$_$/) {print "ok 23\n";} else {print "not ok 23\n";}
71
72 # used to be a test for $*
73 if ("ab\ncd\n" =~ /^cd/m) {print "ok 24\n";} else {print "not ok 24\n";}
74
75 $XXX{123} = 123;
76 $XXX{234} = 234;
77 $XXX{345} = 345;
78
79 @XXX = ('ok 25','not ok 25', 'ok 26','not ok 26','not ok 27');
80 while ($_ = shift(@XXX)) {
81     ?(.*)? && (print $1,"\n");
82     /not/ && reset;
83     if (/not ok 26/) {
84       if ($^O eq 'VMS') {
85         $_ = shift(@XXX);
86       }
87       else {
88         reset 'X';
89       }
90    }
91 }
92
93 if ($^O ne 'VMS') {
94   while (($key,$val) = each(%XXX)) {
95     print "not ok 27\n";
96     exit;
97   }
98 }
99
100 print "ok 27\n";
101
102 'cde' =~ /[^ab]*/;
103 'xyz' =~ //;
104 if ($& eq 'xyz') {print "ok 28\n";} else {print "not ok 28\n";}
105
106 $foo = '[^ab]*';
107 'cde' =~ /$foo/;
108 'xyz' =~ //;
109 if ($& eq 'xyz') {print "ok 29\n";} else {print "not ok 29\n";}
110
111 $foo = '[^ab]*';
112 'cde' =~ /$foo/;
113 'xyz' =~ /$null/;
114 if ($& eq 'xyz') {print "ok 30\n";} else {print "not ok 30\n";}
115
116 $_ = 'abcdefghi';
117 /def/;          # optimized up to cmd
118 if ("$`:$&:$'" eq 'abc:def:ghi') {print "ok 31\n";} else {print "not ok 31\n";}
119
120 /cde/ + 0;      # optimized only to spat
121 if ("$`:$&:$'" eq 'ab:cde:fghi') {print "ok 32\n";} else {print "not ok 32\n";}
122
123 /[d][e][f]/;    # not optimized
124 if ("$`:$&:$'" eq 'abc:def:ghi') {print "ok 33\n";} else {print "not ok 33\n";}
125
126 $_ = 'now is the {time for all} good men to come to.';
127 / {([^}]*)}/;
128 if ($1 eq 'time for all') {print "ok 34\n";} else {print "not ok 34 $1\n";}
129
130 $_ = 'xxx {3,4}  yyy   zzz';
131 print /( {3,4})/ ? "ok 35\n" : "not ok 35\n";
132 print $1 eq '   ' ? "ok 36\n" : "not ok 36\n";
133 print /( {4,})/ ? "not ok 37\n" : "ok 37\n";
134 print /( {2,3}.)/ ? "ok 38\n" : "not ok 38\n";
135 print $1 eq '  y' ? "ok 39\n" : "not ok 39\n";
136 print /(y{2,3}.)/ ? "ok 40\n" : "not ok 40\n";
137 print $1 eq 'yyy ' ? "ok 41\n" : "not ok 41\n";
138 print /x {3,4}/ ? "not ok 42\n" : "ok 42\n";
139 print /^xxx {3,4}/ ? "not ok 43\n" : "ok 43\n";
140
141 $_ = "now is the time for all good men to come to.";
142 @words = /(\w+)/g;
143 print join(':',@words) eq "now:is:the:time:for:all:good:men:to:come:to"
144     ? "ok 44\n"
145     : "not ok 44\n";
146
147 @words = ();
148 while (/\w+/g) {
149     push(@words, $&);
150 }
151 print join(':',@words) eq "now:is:the:time:for:all:good:men:to:come:to"
152     ? "ok 45\n"
153     : "not ok 45\n";
154
155 @words = ();
156 pos = 0;
157 while (/to/g) {
158     push(@words, $&);
159 }
160 print join(':',@words) eq "to:to"
161     ? "ok 46\n"
162     : "not ok 46 `@words'\n";
163
164 pos $_ = 0;
165 @words = /to/g;
166 print join(':',@words) eq "to:to"
167     ? "ok 47\n"
168     : "not ok 47 `@words'\n";
169
170 $_ = "abcdefghi";
171
172 $pat1 = 'def';
173 $pat2 = '^def';
174 $pat3 = '.def.';
175 $pat4 = 'abc';
176 $pat5 = '^abc';
177 $pat6 = 'abc$';
178 $pat7 = 'ghi';
179 $pat8 = '\w*ghi';
180 $pat9 = 'ghi$';
181
182 $t1=$t2=$t3=$t4=$t5=$t6=$t7=$t8=$t9=0;
183
184 for $iter (1..5) {
185     $t1++ if /$pat1/o;
186     $t2++ if /$pat2/o;
187     $t3++ if /$pat3/o;
188     $t4++ if /$pat4/o;
189     $t5++ if /$pat5/o;
190     $t6++ if /$pat6/o;
191     $t7++ if /$pat7/o;
192     $t8++ if /$pat8/o;
193     $t9++ if /$pat9/o;
194 }
195
196 $x = "$t1$t2$t3$t4$t5$t6$t7$t8$t9";
197 print $x eq '505550555' ? "ok 48\n" : "not ok 48 $x\n";
198
199 $xyz = 'xyz';
200 print "abc" =~ /^abc$|$xyz/ ? "ok 49\n" : "not ok 49\n";
201
202 # perl 4.009 says "unmatched ()"
203 eval '"abc" =~ /a(bc$)|$xyz/; $result = "$&:$1"';
204 print $@ eq "" ? "ok 50\n" : "not ok 50\n";
205 print $result eq "abc:bc" ? "ok 51\n" : "not ok 51\n";
206
207
208 $_="abcfooabcbar";
209 $x=/abc/g;
210 print $` eq "" ? "ok 52\n" : "not ok 52\n" if $x;
211 $x=/abc/g;
212 print $` eq "abcfoo" ? "ok 53\n" : "not ok 53\n" if $x;
213 $x=/abc/g;
214 print $x == 0 ? "ok 54\n" : "not ok 54\n";
215 pos = 0;
216 $x=/ABC/gi;
217 print $` eq "" ? "ok 55\n" : "not ok 55\n" if $x;
218 $x=/ABC/gi;
219 print $` eq "abcfoo" ? "ok 56\n" : "not ok 56\n" if $x;
220 $x=/ABC/gi;
221 print $x == 0 ? "ok 57\n" : "not ok 57\n";
222 pos = 0;
223 $x=/abc/g;
224 print $' eq "fooabcbar" ? "ok 58\n" : "not ok 58\n" if $x;
225 $x=/abc/g;
226 print $' eq "bar" ? "ok 59\n" : "not ok 59\n" if $x;
227 $_ .= '';
228 @x=/abc/g;
229 print scalar @x == 2 ? "ok 60\n" : "not ok 60\n";
230
231 $_ = "abdc";
232 pos $_ = 2;
233 /\Gc/gc;
234 print "not " if (pos $_) != 2;
235 print "ok 61\n";
236 /\Gc/g;
237 print "not " if defined pos $_;
238 print "ok 62\n";
239
240 $out = 1;
241 'abc' =~ m'a(?{ $out = 2 })b';
242 print "not " if $out != 2;
243 print "ok 63\n";
244
245 $out = 1;
246 'abc' =~ m'a(?{ $out = 3 })c';
247 print "not " if $out != 1;
248 print "ok 64\n";
249
250 $_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6';
251 @out = /(?<!foo)bar./g;
252 print "not " if "@out" ne 'bar2 barf';
253 print "ok 65\n";
254
255 # Tests which depend on REG_INFTY
256 $reg_infty = defined $Config{reg_infty} ? $Config{reg_infty} : 32767;
257 $reg_infty_m = $reg_infty - 1; $reg_infty_p = $reg_infty + 1;
258
259 # As well as failing if the pattern matches do unexpected things, the
260 # next three tests will fail if you should have picked up a lower-than-
261 # default value for $reg_infty from Config.pm, but have not.
262
263 undef $@;
264 print "not " if eval q(('aaa' =~ /(a{1,$reg_infty_m})/)[0] ne 'aaa') || $@;
265 print "ok 66\n";
266
267 undef $@;
268 print "not " if eval q(('a' x $reg_infty_m) !~ /a{$reg_infty_m}/) || $@;
269 print "ok 67\n";
270
271 undef $@;
272 print "not " if eval q(('a' x ($reg_infty_m - 1)) =~ /a{$reg_infty_m}/) || $@;
273 print "ok 68\n";
274
275 undef $@;
276 eval "'aaa' =~ /a{1,$reg_infty}/";
277 print "not " if $@ !~ m%^\QQuantifier in {,} bigger than%;
278 print "ok 69\n";
279
280 eval "'aaa' =~ /a{1,$reg_infty_p}/";
281 print "not "
282         if $@ !~ m%^\QQuantifier in {,} bigger than%;
283 print "ok 70\n";
284 undef $@;
285
286 # Poke a couple more parse failures
287
288 $context = 'x' x 256;
289 eval qq("${context}y" =~ /(?<=$context)y/);
290 print "not " if $@ !~ m%^\QLookbehind longer than 255 not%;
291 print "ok 71\n";
292
293 # removed test
294 print "ok 72\n";
295
296 # Long Monsters
297 $test = 73;
298 for $l (125, 140, 250, 270, 300000, 30) { # Ordered to free memory
299   $a = 'a' x $l;
300   print "# length=$l\nnot " unless "ba$a=" =~ /a$a=/;
301   print "ok $test\n";
302   $test++;
303
304   print "not " if "b$a=" =~ /a$a=/;
305   print "ok $test\n";
306   $test++;
307 }
308
309 # 20000 nodes, each taking 3 words per string, and 1 per branch
310 $long_constant_len = join '|', 12120 .. 32645;
311 $long_var_len = join '|', 8120 .. 28645;
312 %ans = ( 'ax13876y25677lbc' => 1,
313          'ax13876y25677mcb' => 0, # not b.
314          'ax13876y35677nbc' => 0, # Num too big
315          'ax13876y25677y21378obc' => 1,
316          'ax13876y25677y21378zbc' => 0, # Not followed by [k-o]
317          'ax13876y25677y21378y21378kbc' => 1,
318          'ax13876y25677y21378y21378kcb' => 0, # Not b.
319          'ax13876y25677y21378y21378y21378kbc' => 0, # 5 runs
320        );
321
322 for ( keys %ans ) {
323   print "# const-len `$_' not =>  $ans{$_}\nnot "
324     if $ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o;
325   print "ok $test\n";
326   $test++;
327   print "# var-len   `$_' not =>  $ans{$_}\nnot "
328     if $ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o;
329   print "ok $test\n";
330   $test++;
331 }
332
333 $_ = " a (bla()) and x(y b((l)u((e))) and b(l(e)e)e";
334 $expect = "(bla()) ((l)u((e))) (l(e)e)";
335
336 sub matchit {
337   m/
338      (
339        \(
340        (?{ $c = 1 })            # Initialize
341        (?:
342          (?(?{ $c == 0 })       # PREVIOUS iteration was OK, stop the loop
343            (?!
344            )                    # Fail: will unwind one iteration back
345          )      
346          (?:
347            [^()]+               # Match a big chunk
348            (?=
349              [()]
350            )                    # Do not try to match subchunks
351          |
352            \(
353            (?{ ++$c })
354          |
355            \)
356            (?{ --$c })
357          )
358        )+                       # This may not match with different subblocks
359      )
360      (?(?{ $c != 0 })
361        (?!
362        )                        # Fail
363      )                          # Otherwise the chunk 1 may succeed with $c>0
364    /xg;
365 }
366
367 @ans = ();
368 push @ans, $res while $res = matchit;
369
370 print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne "1 1 1";
371 print "ok $test\n";
372 $test++;
373
374 @ans = matchit;
375
376 print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne $expect;
377 print "ok $test\n";
378 $test++;
379
380 print "not " unless "abc" =~ /^(??{"a"})b/;
381 print "ok $test\n";
382 $test++;
383
384 my $matched;
385 $matched = qr/\((?:(?>[^()]+)|(??{$matched}))*\)/;
386
387 @ans = @ans1 = ();
388 push(@ans, $res), push(@ans1, $&) while $res = m/$matched/g;
389
390 print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne "1 1 1";
391 print "ok $test\n";
392 $test++;
393
394 print "# ans1='@ans1'\n# expect='$expect'\nnot " if "@ans1" ne $expect;
395 print "ok $test\n";
396 $test++;
397
398 @ans = m/$matched/g;
399
400 print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne $expect;
401 print "ok $test\n";
402 $test++;
403
404 @ans = ('a/b' =~ m%(.*/)?(.*)%);        # Stack may be bad
405 print "not " if "@ans" ne 'a/ b';
406 print "ok $test\n";
407 $test++;
408
409 $code = '{$blah = 45}';
410 $blah = 12;
411 eval { /(?$code)/ };
412 print "not " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12;
413 print "ok $test\n";
414 $test++;
415
416 for $code ('{$blah = 45}','=xx') {
417   $blah = 12;
418   $res = eval { "xx" =~ /(?$code)/o };
419   if ($code eq '=xx') {
420     print "#'$@','$res','$blah'\nnot " unless not $@ and $res;
421   } else {
422     print "#'$@','$res','$blah'\nnot " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12;
423   }
424   print "ok $test\n";
425   $test++;
426 }
427
428 $code = '{$blah = 45}';
429 $blah = 12;
430 eval "/(?$code)/";                      
431 print "not " if $blah != 45;
432 print "ok $test\n";
433 $test++;
434
435 $blah = 12;
436 /(?{$blah = 45})/;                      
437 print "not " if $blah != 45;
438 print "ok $test\n";
439 $test++;
440
441 $x = 'banana';
442 $x =~ /.a/g;
443 print "not " unless pos($x) == 2;
444 print "ok $test\n";
445 $test++;
446
447 $x =~ /.z/gc;
448 print "not " unless pos($x) == 2;
449 print "ok $test\n";
450 $test++;
451
452 sub f {
453     my $p = $_[0];
454     return $p;
455 }
456
457 $x =~ /.a/g;
458 print "not " unless f(pos($x)) == 4;
459 print "ok $test\n";
460 $test++;
461
462 $x = $^R = 67;
463 'foot' =~ /foo(?{$x = 12; 75})[t]/;
464 print "not " unless $^R eq '75';
465 print "ok $test\n";
466 $test++;
467
468 $x = $^R = 67;
469 'foot' =~ /foo(?{$x = 12; 75})[xy]/;
470 print "not " unless $^R eq '67' and $x eq '12';
471 print "ok $test\n";
472 $test++;
473
474 $x = $^R = 67;
475 'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/;
476 print "not " unless $^R eq '79' and $x eq '12';
477 print "ok $test\n";
478 $test++;
479
480 print "not " unless qr/\b\v$/i eq '(?i-xsm:\bv$)';
481 print "ok $test\n";
482 $test++;
483
484 print "not " unless qr/\b\v$/s eq '(?s-xim:\bv$)';
485 print "ok $test\n";
486 $test++;
487
488 print "not " unless qr/\b\v$/m eq '(?m-xis:\bv$)';
489 print "ok $test\n";
490 $test++;
491
492 print "not " unless qr/\b\v$/x eq '(?x-ism:\bv$)';
493 print "ok $test\n";
494 $test++;
495
496 print "not " unless qr/\b\v$/xism eq '(?msix:\bv$)';
497 print "ok $test\n";
498 $test++;
499
500 print "not " unless qr/\b\v$/ eq '(?-xism:\bv$)';
501 print "ok $test\n";
502 $test++;
503
504 $_ = 'xabcx';
505 foreach $ans ('', 'c') {
506   /(?<=(?=a)..)((?=c)|.)/g;
507   print "# \$1  ='$1'\n# \$ans='$ans'\nnot " unless $1 eq $ans;
508   print "ok $test\n";
509   $test++;
510 }
511
512 $_ = 'a';
513 foreach $ans ('', 'a', '') {
514   /^|a|$/g;
515   print "# \$&  ='$&'\n# \$ans='$ans'\nnot " unless $& eq $ans;
516   print "ok $test\n";
517   $test++;
518 }
519
520 sub prefixify {
521   my($v,$a,$b,$res) = @_;
522   $v =~ s/\Q$a\E/$b/;
523   print "not " unless $res eq $v;
524   print "ok $test\n";
525   $test++;
526 }
527 prefixify('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch');
528 prefixify('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch');
529
530 $_ = 'var="foo"';
531 /(\")/;
532 print "not " unless $1 and /$1/;
533 print "ok $test\n";
534 $test++;
535
536 $a=qr/(?{++$b})/;
537 $b = 7;
538 /$a$a/;
539 print "not " unless $b eq '9';
540 print "ok $test\n";
541 $test++;
542
543 $c="$a";
544 /$a$a/;
545 print "not " unless $b eq '11';
546 print "ok $test\n";
547 $test++;
548
549 {
550   use re "eval";
551   /$a$c$a/;
552   print "not " unless $b eq '14';
553   print "ok $test\n";
554   $test++;
555
556   local $lex_a = 2;
557   my $lex_a = 43;
558   my $lex_b = 17;
559   my $lex_c = 27;
560   my $lex_res = ($lex_b =~ qr/$lex_b(?{ $lex_c = $lex_a++ })/);
561   print "not " unless $lex_res eq '1';
562   print "ok $test\n";
563   $test++;
564   print "not " unless $lex_a eq '44';
565   print "ok $test\n";
566   $test++;
567   print "not " unless $lex_c eq '43';
568   print "ok $test\n";
569   $test++;
570
571
572   no re "eval";
573   $match = eval { /$a$c$a/ };
574   print "not "
575     unless $b eq '14' and $@ =~ /Eval-group not allowed/ and not $match;
576   print "ok $test\n";
577   $test++;
578 }
579
580 {
581   local $lex_a = 2;
582   my $lex_a = 43;
583   my $lex_b = 17;
584   my $lex_c = 27;
585   my $lex_res = ($lex_b =~ qr/17(?{ $lex_c = $lex_a++ })/);
586   print "not " unless $lex_res eq '1';
587   print "ok $test\n";
588   $test++;
589   print "not " unless $lex_a eq '44';
590   print "ok $test\n";
591   $test++;
592   print "not " unless $lex_c eq '43';
593   print "ok $test\n";
594   $test++;
595 }
596
597 {
598   package aa;
599   $c = 2;
600   $::c = 3;
601   '' =~ /(?{ $c = 4 })/;
602   print "not " unless $c == 4;
603 }
604 print "ok $test\n";
605 $test++;
606 print "not " unless $c == 3;
607 print "ok $test\n";
608 $test++;
609
610 sub must_warn_pat {
611     my $warn_pat = shift;
612     return sub { print "not " unless $_[0] =~ /$warn_pat/ }
613 }
614
615 sub must_warn {
616     my ($warn_pat, $code) = @_;
617     local %SIG;
618     eval 'BEGIN { use warnings; $SIG{__WARN__} = $warn_pat };' . $code;
619     print "ok $test\n";
620     $test++;
621 }
622
623
624 sub make_must_warn {
625     my $warn_pat = shift;
626     return sub { must_warn(must_warn_pat($warn_pat)) }
627 }
628
629 my $for_future = make_must_warn('reserved for future extensions');
630
631 &$for_future('q(a:[b]:) =~ /[x[:foo:]]/');
632
633 #&$for_future('q(a=[b]=) =~ /[x[=foo=]]/');
634 print "ok $test\n"; $test++; # now a fatal croak
635
636 #&$for_future('q(a.[b].) =~ /[x[.foo.]]/');
637 print "ok $test\n"; $test++; # now a fatal croak
638
639 # test if failure of patterns returns empty list
640 $_ = 'aaa';
641 @_ = /bbb/;
642 print "not " if @_;
643 print "ok $test\n";
644 $test++;
645
646 @_ = /bbb/g;
647 print "not " if @_;
648 print "ok $test\n";
649 $test++;
650
651 @_ = /(bbb)/;
652 print "not " if @_;
653 print "ok $test\n";
654 $test++;
655
656 @_ = /(bbb)/g;
657 print "not " if @_;
658 print "ok $test\n";
659 $test++;
660
661 /a(?=.$)/;
662 print "not " if $#+ != 0 or $#- != 0;
663 print "ok $test\n";
664 $test++;
665
666 print "not " if $+[0] != 2 or $-[0] != 1;
667 print "ok $test\n";
668 $test++;
669
670 print "not "
671    if defined $+[1] or defined $-[1] or defined $+[2] or defined $-[2];
672 print "ok $test\n";
673 $test++;
674
675 /a(a)(a)/;
676 print "not " if $#+ != 2 or $#- != 2;
677 print "ok $test\n";
678 $test++;
679
680 print "not " if $+[0] != 3 or $-[0] != 0;
681 print "ok $test\n";
682 $test++;
683
684 print "not " if $+[1] != 2 or $-[1] != 1;
685 print "ok $test\n";
686 $test++;
687
688 print "not " if $+[2] != 3 or $-[2] != 2;
689 print "ok $test\n";
690 $test++;
691
692 print "not "
693    if defined $+[3] or defined $-[3] or defined $+[4] or defined $-[4];
694 print "ok $test\n";
695 $test++;
696
697 /.(a)(b)?(a)/;
698 print "not " if $#+ != 3 or $#- != 3;
699 print "ok $test\n";
700 $test++;
701
702 print "not " if $+[0] != 3 or $-[0] != 0;
703 print "ok $test\n";
704 $test++;
705
706 print "not " if $+[1] != 2 or $-[1] != 1;
707 print "ok $test\n";
708 $test++;
709
710 print "not " if $+[3] != 3 or $-[3] != 2;
711 print "ok $test\n";
712 $test++;
713
714 print "not "
715    if defined $+[2] or defined $-[2] or defined $+[4] or defined $-[4];
716 print "ok $test\n";
717 $test++;
718
719 /.(a)/;
720 print "not " if $#+ != 1 or $#- != 1;
721 print "ok $test\n";
722 $test++;
723
724 print "not " if $+[0] != 2 or $-[0] != 0;
725 print "ok $test\n";
726 $test++;
727
728 print "not " if $+[1] != 2 or $-[1] != 1;
729 print "ok $test\n";
730 $test++;
731
732 print "not "
733    if defined $+[2] or defined $-[2] or defined $+[3] or defined $-[3];
734 print "ok $test\n";
735 $test++;
736
737 eval { $+[0] = 13; };
738 print "not "
739    if $@ !~ /^Modification of a read-only value attempted/;
740 print "ok $test\n";
741 $test++;
742
743 eval { $-[0] = 13; };
744 print "not "
745    if $@ !~ /^Modification of a read-only value attempted/;
746 print "ok $test\n";
747 $test++;
748
749 eval { @+ = (7, 6, 5); };
750 print "not "
751    if $@ !~ /^Modification of a read-only value attempted/;
752 print "ok $test\n";
753 $test++;
754
755 eval { @- = qw(foo bar); };
756 print "not "
757    if $@ !~ /^Modification of a read-only value attempted/;
758 print "ok $test\n";
759 $test++;
760
761 /.(a)(ba*)?/;
762 print "#$#-..$#+\nnot " if $#+ != 2 or $#- != 1;
763 print "ok $test\n";
764 $test++;
765
766 $_ = 'aaa';
767 pos = 1;
768 @a = /\Ga/g;
769 print "not " unless "@a" eq "a a";
770 print "ok $test\n";
771 $test++;
772
773 $str = 'abcde';
774 pos $str = 2;
775
776 print "not " if $str =~ /^\G/;
777 print "ok $test\n";
778 $test++;
779
780 print "not " if $str =~ /^.\G/;
781 print "ok $test\n";
782 $test++;
783
784 print "not " unless $str =~ /^..\G/;
785 print "ok $test\n";
786 $test++;
787
788 print "not " if $str =~ /^...\G/;
789 print "ok $test\n";
790 $test++;
791
792 print "not " unless $str =~ /.\G./ and $& eq 'bc';
793 print "ok $test\n";
794 $test++;
795
796 print "not " unless $str =~ /\G../ and $& eq 'cd';
797 print "ok $test\n";
798 $test++;
799
800 undef $foo; undef $bar;
801 print "#'$str','$foo','$bar'\nnot "
802     unless $str =~ /b(?{$foo = $_; $bar = pos})c/
803         and $foo eq 'abcde' and $bar eq 2;
804 print "ok $test\n";
805 $test++;
806
807 undef $foo; undef $bar;
808 pos $str = undef;
809 print "#'$str','$foo','$bar'\nnot "
810     unless $str =~ /b(?{$foo = $_; $bar = pos})c/g
811         and $foo eq 'abcde' and $bar eq 2 and pos $str eq 3;
812 print "ok $test\n";
813 $test++;
814
815 $_ = $str;
816
817 undef $foo; undef $bar;
818 print "#'$str','$foo','$bar'\nnot "
819     unless /b(?{$foo = $_; $bar = pos})c/
820         and $foo eq 'abcde' and $bar eq 2;
821 print "ok $test\n";
822 $test++;
823
824 undef $foo; undef $bar;
825 print "#'$str','$foo','$bar'\nnot "
826     unless /b(?{$foo = $_; $bar = pos})c/g
827         and $foo eq 'abcde' and $bar eq 2 and pos eq 3;
828 print "ok $test\n";
829 $test++;
830
831 undef $foo; undef $bar;
832 pos = undef;
833 1 while /b(?{$foo = $_; $bar = pos})c/g;
834 print "#'$str','$foo','$bar'\nnot "
835     unless $foo eq 'abcde' and $bar eq 2 and not defined pos;
836 print "ok $test\n";
837 $test++;
838
839 undef $foo; undef $bar;
840 $_ = 'abcde|abcde';
841 print "#'$str','$foo','$bar','$_'\nnot "
842     unless s/b(?{$foo = $_; $bar = pos})c/x/g and $foo eq 'abcde|abcde'
843         and $bar eq 8 and $_ eq 'axde|axde';
844 print "ok $test\n";
845 $test++;
846
847 @res = ();
848 # List context:
849 $_ = 'abcde|abcde';
850 @dummy = /([ace]).(?{push @res, $1,$2})([ce])(?{push @res, $1,$2})/g;
851 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
852 $res = "@res";
853 print "#'@res' '$_'\nnot "
854     unless "@res" eq "'a' undef 'a' 'c' 'e' undef 'a' undef 'a' 'c'";
855 print "ok $test\n";
856 $test++;
857
858 @res = ();
859 @dummy = /([ace]).(?{push @res, $`,$&,$'})([ce])(?{push @res, $`,$&,$'})/g;
860 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
861 $res = "@res";
862 print "#'@res' '$_'\nnot "
863     unless "@res" eq
864   "'' 'ab' 'cde|abcde' " .
865   "'' 'abc' 'de|abcde' " .
866   "'abcd' 'e|' 'abcde' " .
867   "'abcde|' 'ab' 'cde' " .
868   "'abcde|' 'abc' 'de'" ;
869 print "ok $test\n";
870 $test++;
871
872 #Some more \G anchor checks
873 $foo='aabbccddeeffgg';
874
875 pos($foo)=1;
876
877 $foo=~/.\G(..)/g;
878 print "not " unless($1 eq 'ab');
879 print "ok $test\n";
880 $test++;
881
882 pos($foo) += 1;
883 $foo=~/.\G(..)/g;
884 print "not " unless($1 eq 'cc');
885 print "ok $test\n";
886 $test++;
887
888 pos($foo) += 1;
889 $foo=~/.\G(..)/g;
890 print "not " unless($1 eq 'de');
891 print "ok $test\n";
892 $test++;
893
894 print "not " unless $foo =~ /\Gef/g;
895 print "ok $test\n";
896 $test++;
897
898 undef pos $foo;
899
900 $foo=~/\G(..)/g;
901 print "not " unless($1  eq 'aa');
902 print "ok $test\n";
903 $test++;
904
905 $foo=~/\G(..)/g;
906 print "not " unless($1  eq 'bb');
907 print "ok $test\n";
908 $test++;
909
910 pos($foo)=5;
911 $foo=~/\G(..)/g;
912 print "not " unless($1  eq 'cd');
913 print "ok $test\n";
914 $test++;
915
916 $_='123x123';
917 @res = /(\d*|x)/g;
918 print "not " unless('123||x|123|' eq join '|', @res);
919 print "ok $test\n";
920 $test++;
921
922 # see if matching against temporaries (created via pp_helem()) is safe
923 { foo => "ok $test\n".$^X }->{foo} =~ /^(.*)\n/g;
924 print "$1\n";
925 $test++;
926
927 # See if $i work inside (?{}) in the presense of saved substrings and
928 # changing $_
929 @a = qw(foo bar);
930 @b = ();
931 s/(\w)(?{push @b, $1})/,$1,/g for @a;
932
933 print "# \@b='@b', expect 'f o o b a r'\nnot " unless("@b" eq "f o o b a r");
934 print "ok $test\n";
935 $test++;
936
937 print "not " unless("@a" eq ",f,,o,,o, ,b,,a,,r,");
938 print "ok $test\n";
939 $test++;
940
941 $brackets = qr{
942                  {  (?> [^{}]+ | (??{ $brackets }) )* }
943               }x;
944
945 "{{}" =~ $brackets;
946 print "ok $test\n";             # Did we survive?
947 $test++;
948
949 "something { long { and } hairy" =~ $brackets;
950 print "ok $test\n";             # Did we survive?
951 $test++;
952
953 "something { long { and } hairy" =~ m/((??{ $brackets }))/;
954 print "not " unless $1 eq "{ and }";
955 print "ok $test\n";
956 $test++;
957
958 $_ = "a-a\nxbb";
959 pos=1;
960 m/^-.*bb/mg and print "not ";
961 print "ok $test\n";
962 $test++;
963
964 $text = "aaXbXcc";
965 pos($text)=0;
966 $text =~ /\GXb*X/g and print 'not ';
967 print "ok $test\n";
968 $test++;
969
970 $text = "xA\n" x 500;
971 $text =~ /^\s*A/m and print 'not ';
972 print "ok $test\n";
973 $test++;
974
975 $text = "abc dbf";
976 @res = ($text =~ /.*?(b).*?\b/g);
977 "@res" eq 'b b' or print 'not ';
978 print "ok $test\n";
979 $test++;
980
981 @a = map chr,0..255;
982
983 @b = grep(/\S/,@a);
984 @c = grep(/[^\s]/,@a);
985 print "not " if "@b" ne "@c";
986 print "ok $test\n";
987 $test++;
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(/\D/,@a);
1008 @c = grep(/[^\d]/,@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(/\W/,@a);
1032 @c = grep(/[^\w]/,@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 # see if backtracking optimization works correctly
1056 "\n\n" =~ /\n  $ \n/x or print "not ";
1057 print "ok $test\n";
1058 $test++;
1059
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 [] =~ /^ARRAY/ or print "# [] \nnot ";
1069 print "ok $test\n";
1070 $test++;
1071
1072 eval << 'EOE';
1073 {
1074  package S;
1075  use overload '""' => sub { 'Object S' };
1076  sub new { bless [] }
1077 }
1078 $a = 'S'->new;
1079 EOE
1080
1081 $a and $a =~ /^Object\sS/ or print "# '$a' \nnot ";
1082 print "ok $test\n";
1083 $test++;
1084
1085 # test result of match used as match (!)
1086 'a1b' =~ ('xyz' =~ /y/) and $` eq 'a' or print "not ";
1087 print "ok $test\n";
1088 $test++;
1089
1090 'a1b' =~ ('xyz' =~ /t/) and $` eq 'a' or print "not ";
1091 print "ok $test\n";
1092 $test++;
1093
1094 $w = 0;
1095 {
1096     local $SIG{__WARN__} = sub { $w = 1 };
1097     local $^W = 1;
1098         $w = 1 if ("1\n" x 102) =~ /^\s*\n/m;
1099 }
1100 print $w ? "not " : "", "ok $test\n";
1101 $test++;
1102
1103 my %space = ( spc   => " ",
1104               tab   => "\t",
1105               cr    => "\r",
1106               lf    => "\n",
1107               ff    => "\f",
1108 # There's no \v but the vertical tabulator seems miraculously
1109 # be 11 both in ASCII and EBCDIC.
1110               vt    => chr(11),
1111               false => "space" );
1112
1113 my @space0 = sort grep { $space{$_} =~ /\s/ }          keys %space;
1114 my @space1 = sort grep { $space{$_} =~ /[[:space:]]/ } keys %space;
1115 my @space2 = sort grep { $space{$_} =~ /[[:blank:]]/ } keys %space;
1116
1117 print "not " unless "@space0" eq "cr ff lf spc tab";
1118 print "ok $test # @space0\n";
1119 $test++;
1120
1121 print "not " unless "@space1" eq "cr ff lf spc tab vt";
1122 print "ok $test # @space1\n";
1123 $test++;
1124
1125 print "not " unless "@space2" eq "spc tab";
1126 print "ok $test # @space2\n";
1127 $test++;
1128
1129 # bugid 20001021.005 - this caused a SEGV
1130 print "not " unless undef =~ /^([^\/]*)(.*)$/;
1131 print "ok $test\n";
1132 $test++;
1133
1134 # bugid 20000731.001
1135
1136 print "not " unless "A \x{263a} B z C" =~ /A . B (??{ "z" }) C/;
1137 print "ok $test\n";
1138 $test++;
1139
1140 my $ordA = ord('A');
1141
1142 $_ = "a\x{100}b";
1143 if (/(.)(\C)(\C)(.)/) {
1144   print "ok 232\n";
1145   if ($1 eq "a") {
1146     print "ok 233\n";
1147   } else {
1148     print "not ok 233\n";
1149   }
1150   if ($ordA == 65) { # ASCII (or equivalent), should be UTF-8
1151       if ($2 eq "\xC4") {
1152           print "ok 234\n";
1153       } else {
1154           print "not ok 234\n";
1155       }
1156       if ($3 eq "\x80") {
1157           print "ok 235\n";
1158       } else {
1159           print "not ok 235\n";
1160       }
1161   } elsif ($ordA == 193) { # EBCDIC (or equivalent), should be UTF-EBCDIC
1162       if ($2 eq "\x8C") {
1163           print "ok 234\n";
1164       } else {
1165           print "not ok 234\n";
1166       }
1167       if ($3 eq "\x41") {
1168           print "ok 235\n";
1169       } else {
1170           print "not ok 235\n";
1171       }
1172   } else {
1173       for (234..235) {
1174           print "not ok $_ # ord('A') == $ordA\n";
1175       }
1176   }
1177   if ($4 eq "b") {
1178     print "ok 236\n";
1179   } else {
1180     print "not ok 236\n";
1181   }
1182 } else {
1183   for (232..236) {
1184     print "not ok $_\n";
1185   }
1186 }
1187 $_ = "\x{100}";
1188 if (/(\C)/g) {
1189   print "ok 237\n";
1190   # currently \C are still tagged as UTF-8
1191   if ($ordA == 65) {
1192       if ($1 eq "\xC4") {
1193           print "ok 238\n";
1194       } else {
1195           print "not ok 238\n";
1196       }
1197   } elsif ($ordA == 193) {
1198       if ($1 eq "\x8C") {
1199           print "ok 238\n";
1200       } else {
1201           print "not ok 238\n";
1202       }
1203   } else {
1204       print "not ok 238 # ord('A') == $ordA\n";
1205   }
1206 } else {
1207   for (237..238) {
1208     print "not ok $_\n";
1209   }
1210 }
1211 if (/(\C)/g) {
1212   print "ok 239\n";
1213   # currently \C are still tagged as UTF-8
1214   if ($ordA == 65) {
1215       if ($1 eq "\x80") {
1216           print "ok 240\n";
1217       } else {
1218           print "not ok 240\n";
1219       }
1220   } elsif ($ordA == 193) {
1221       if ($1 eq "\x41") {
1222           print "ok 240\n";
1223       } else {
1224           print "not ok 240\n";
1225       }
1226   } else {
1227       print "not ok 240 # ord('A') == $ordA\n";
1228   }
1229 } else {
1230   for (239..240) {
1231     print "not ok $_\n";
1232   }
1233 }
1234
1235 {
1236   # japhy -- added 03/03/2001
1237   () = (my $str = "abc") =~ /(...)/;
1238   $str = "def";
1239   print "not " if $1 ne "abc";
1240   print "ok 241\n";
1241 }
1242
1243 # The 242 and 243 go with the 244 and 245.
1244 # The trick is that in EBCDIC the explicit numeric range should match
1245 # (as also in non-EBCDIC) but the explicit alphabetic range should not match.
1246
1247 if ("\x8e" =~ /[\x89-\x91]/) {
1248   print "ok 242\n";
1249 } else {
1250   print "not ok 242\n";
1251 }
1252
1253 if ("\xce" =~ /[\xc9-\xd1]/) {
1254   print "ok 243\n";
1255 } else {
1256   print "not ok 243\n";
1257 }
1258
1259 # In most places these tests would succeed since \x8e does not
1260 # in most character sets match 'i' or 'j' nor would \xce match
1261 # 'I' or 'J', but strictly speaking these tests are here for
1262 # the good of EBCDIC, so let's test these only there.
1263 if (ord('i') == 0x89 && ord('J') == 0xd1) { # EBCDIC
1264   if ("\x8e" !~ /[i-j]/) {
1265     print "ok 244\n";
1266   } else {
1267     print "not ok 244\n";
1268   }
1269   if ("\xce" !~ /[I-J]/) {
1270     print "ok 245\n";
1271   } else {
1272     print "not ok 245\n";
1273   }
1274 } else {
1275   for (244..245) {
1276     print "ok $_ # Skip: only in EBCDIC\n";
1277   }
1278 }
1279
1280 print "not " unless "\x{ab}" =~ /\x{ab}/;
1281 print "ok 246\n";
1282
1283 print "not " unless "\x{abcd}" =~ /\x{abcd}/;
1284 print "ok 247\n";
1285
1286 {
1287     # bug id 20001008.001
1288
1289     my $test = 248;
1290     my @x = ("stra\337e 138","stra\337e 138");
1291     for (@x) {
1292         s/(\d+)\s*([\w\-]+)/$1 . uc $2/e;
1293         my($latin) = /^(.+)(?:\s+\d)/;
1294         print $latin eq "stra\337e" ? "ok $test\n" :    # 248,249
1295             "#latin[$latin]\nnot ok $test\n";
1296         $test++;
1297         $latin =~ s/stra\337e/straße/; # \303\237 after the 2nd a
1298         use utf8; # needed for the raw UTF-8
1299         $latin =~ s!(s)tr(?:aß|s+e)!$1tr.!; # \303\237 after the a
1300     }
1301 }
1302
1303 {
1304     print "not " unless "ba\xd4c" =~ /([a\xd4]+)/ && $1 eq "a\xd4";
1305     print "ok 250\n";
1306
1307     print "not " unless "ba\xd4c" =~ /([a\xd4]+)/ && $1 eq "a\x{d4}";
1308     print "ok 251\n";
1309
1310     print "not " unless "ba\x{d4}c" =~ /([a\xd4]+)/ && $1 eq "a\x{d4}";
1311     print "ok 252\n";
1312
1313     print "not " unless "ba\x{d4}c" =~ /([a\xd4]+)/ && $1 eq "a\xd4";
1314     print "ok 253\n";
1315
1316     print "not " unless "ba\xd4c" =~ /([a\x{d4}]+)/ && $1 eq "a\xd4";
1317     print "ok 254\n";
1318
1319     print "not " unless "ba\xd4c" =~ /([a\x{d4}]+)/ && $1 eq "a\x{d4}";
1320     print "ok 255\n";
1321
1322     print "not " unless "ba\x{d4}c" =~ /([a\x{d4}]+)/ && $1 eq "a\x{d4}";
1323     print "ok 256\n";
1324
1325     print "not " unless "ba\x{d4}c" =~ /([a\x{d4}]+)/ && $1 eq "a\xd4";
1326     print "ok 257\n";
1327 }
1328
1329 {
1330     # the first half of 20001028.003
1331
1332     my $X = chr(1448);
1333     my ($Y) = $X =~ /(.*)/;
1334     print "not " unless $Y eq v1448 && length($Y) == 1;
1335     print "ok 258\n";
1336 }
1337
1338 {
1339     # 20001108.001
1340
1341     my $X = "Szab\x{f3},Bal\x{e1}zs";
1342     my $Y = $X;
1343     $Y =~ s/(B)/$1/ for 0..3;
1344     print "not " unless $Y eq $X && $X eq "Szab\x{f3},Bal\x{e1}zs";
1345     print "ok 259\n";
1346 }
1347
1348 {
1349     # the second half of 20001028.003
1350
1351     my $X = '';
1352     $X =~ s/^/chr(1488)/e;
1353     print "not " unless length $X == 1 && ord($X) == 1488;
1354     print "ok 260\n";
1355 }
1356
1357 {
1358     # 20000517.001
1359
1360     my $x = "\x{100}A";
1361
1362     $x =~ s/A/B/;
1363
1364     print "not " unless $x eq "\x{100}B" && length($x) == 2;
1365     print "ok 261\n";
1366 }
1367
1368 {
1369     # bug id 20001230.002
1370
1371     print "not " unless "École" =~ /^\C\C(.)/ && $1 eq 'c';
1372     print "ok 262\n";
1373
1374     print "not " unless "École" =~ /^\C\C(c)/;
1375     print "ok 263\n";
1376 }
1377
1378 SKIP: {
1379     my $test = 264; # till 575
1380
1381     use charnames ":full";
1382
1383     # This is far from complete testing, there are dozens of character
1384     # classes in Unicode.  The mixing of literals and \N{...} is
1385     # intentional so that in non-Latin-1 places we test the native
1386     # characters, not the Unicode code points.
1387
1388     my %s = (
1389              "a"                                => 'Ll',
1390              "\N{CYRILLIC SMALL LETTER A}"      => 'Ll',
1391              "A"                                => 'Lu',
1392              "\N{GREEK CAPITAL LETTER ALPHA}"   => 'Lu',
1393              "\N{HIRAGANA LETTER SMALL A}"      => 'Lo',
1394              "\N{COMBINING GRAVE ACCENT}"       => 'Mn',
1395              "0"                                => 'Nd',
1396              "\N{ARABIC-INDIC DIGIT ZERO}"      => 'Nd',
1397              "_"                                => 'N',
1398              "!"                                => 'P',
1399              " "                                => 'Zs',
1400              "\0"                               => 'Cc',
1401              );
1402         
1403     for my $char (map { s/^\S+ //; $_ }
1404                     sort map { sprintf("%06x", ord($_))." $_" } keys %s) {
1405         my $class = $s{$char};
1406         my $code  = sprintf("%06x", ord($char));
1407         printf "#\n# 0x$code\n#\n";
1408         print "# IsAlpha\n";
1409         if ($class =~ /^[LM]/) {
1410             print "not " unless $char =~ /\p{IsAlpha}/;
1411             print "ok $test\n"; $test++;
1412             print "not " if     $char =~ /\P{IsAlpha}/;
1413             print "ok $test\n"; $test++;
1414         } else {
1415             print "not " if     $char =~ /\p{IsAlpha}/;
1416             print "ok $test\n"; $test++;
1417             print "not " unless $char =~ /\P{IsAlpha}/;
1418             print "ok $test\n"; $test++;
1419         }
1420         print "# IsAlnum\n";
1421         if ($class =~ /^[LMN]/ && $char ne "_") {
1422             print "not " unless $char =~ /\p{IsAlnum}/;
1423             print "ok $test\n"; $test++;
1424             print "not " if     $char =~ /\P{IsAlnum}/;
1425             print "ok $test\n"; $test++;
1426         } else {
1427             print "not " if     $char =~ /\p{IsAlnum}/;
1428             print "ok $test\n"; $test++;
1429             print "not " unless $char =~ /\P{IsAlnum}/;
1430             print "ok $test\n"; $test++;
1431         }
1432         print "# IsASCII\n";
1433         if (ord("A") == 193) {
1434             print "ok $test # Skip: in EBCDIC\n"; $test++;
1435             print "ok $test # Skip: in EBCDIC\n"; $test++;
1436         } else {
1437             if ($code le '00007f') {
1438                 print "not " unless $char =~ /\p{IsASCII}/;
1439                 print "ok $test\n"; $test++;
1440                 print "not " if     $char =~ /\P{IsASCII}/;
1441                 print "ok $test\n"; $test++;
1442             } else {
1443                 print "not " if     $char =~ /\p{IsASCII}/;
1444                 print "ok $test\n"; $test++;
1445                 print "not " unless $char =~ /\P{IsASCII}/;
1446                 print "ok $test\n"; $test++;
1447             }
1448         }
1449         print "# IsCntrl\n";
1450         if ($class =~ /^C/) {
1451             print "not " unless $char =~ /\p{IsCntrl}/;
1452             print "ok $test\n"; $test++;
1453             print "not " if     $char =~ /\P{IsCntrl}/;
1454             print "ok $test\n"; $test++;
1455         } else {
1456             print "not " if     $char =~ /\p{IsCntrl}/;
1457             print "ok $test\n"; $test++;
1458             print "not " unless $char =~ /\P{IsCntrl}/;
1459             print "ok $test\n"; $test++;
1460         }
1461         print "# IsBlank\n";
1462         if ($class =~ /^Z[lp]/ || $char eq " ") {
1463             print "not " unless $char =~ /\p{IsBlank}/;
1464             print "ok $test\n"; $test++;
1465             print "not " if     $char =~ /\P{IsBlank}/;
1466             print "ok $test\n"; $test++;
1467         } else {
1468             print "not " if     $char =~ /\p{IsBlank}/;
1469             print "ok $test\n"; $test++;
1470             print "not " unless $char =~ /\P{IsBlank}/;
1471             print "ok $test\n"; $test++;
1472         }
1473         print "# IsDigit\n";
1474         if ($class =~ /^Nd$/) {
1475             print "not " unless $char =~ /\p{IsDigit}/;
1476             print "ok $test\n"; $test++;
1477             print "not " if     $char =~ /\P{IsDigit}/;
1478             print "ok $test\n"; $test++;
1479         } else {
1480             print "not " if     $char =~ /\p{IsDigit}/;
1481             print "ok $test\n"; $test++;
1482             print "not " unless $char =~ /\P{IsDigit}/;
1483             print "ok $test\n"; $test++;
1484         }
1485         print "# IsGraph\n";
1486         if ($class =~ /^([LMNPS])|Co/) {
1487             print "not " unless $char =~ /\p{IsGraph}/;
1488             print "ok $test\n"; $test++;
1489             print "not " if     $char =~ /\P{IsGraph}/;
1490             print "ok $test\n"; $test++;
1491         } else {
1492             print "not " if     $char =~ /\p{IsGraph}/;
1493             print "ok $test\n"; $test++;
1494             print "not " unless $char =~ /\P{IsGraph}/;
1495             print "ok $test\n"; $test++;
1496         }
1497         print "# IsLower\n";
1498         if ($class =~ /^Ll$/) {
1499             print "not " unless $char =~ /\p{IsLower}/;
1500             print "ok $test\n"; $test++;
1501             print "not " if     $char =~ /\P{IsLower}/;
1502             print "ok $test\n"; $test++;
1503         } else {
1504             print "not " if     $char =~ /\p{IsLower}/;
1505             print "ok $test\n"; $test++;
1506             print "not " unless $char =~ /\P{IsLower}/;
1507             print "ok $test\n"; $test++;
1508         }
1509         print "# IsPrint\n";
1510         if ($class =~ /^([LMNPS])|Co|Zs/) {
1511             print "not " unless $char =~ /\p{IsPrint}/;
1512             print "ok $test\n"; $test++;
1513             print "not " if     $char =~ /\P{IsPrint}/;
1514             print "ok $test\n"; $test++;
1515         } else {
1516             print "not " if     $char =~ /\p{IsPrint}/;
1517             print "ok $test\n"; $test++;
1518             print "not " unless $char =~ /\P{IsPrint}/;
1519             print "ok $test\n"; $test++;
1520         }
1521         print "# IsPunct\n";
1522         if ($class =~ /^P/ || $char eq "_") {
1523             print "not " unless $char =~ /\p{IsPunct}/;
1524             print "ok $test\n"; $test++;
1525             print "not " if     $char =~ /\P{IsPunct}/;
1526             print "ok $test\n"; $test++;
1527         } else {
1528             print "not " if     $char =~ /\p{IsPunct}/;
1529             print "ok $test\n"; $test++;
1530             print "not " unless $char =~ /\P{IsPunct}/;
1531             print "ok $test\n"; $test++;
1532         }
1533         print "# IsSpace\n";
1534         if ($class =~ /^Z/ || ($code =~ /^(0009|000A|000B|000C|000D)$/)) {
1535             print "not " unless $char =~ /\p{IsSpace}/;
1536             print "ok $test\n"; $test++;
1537             print "not " if     $char =~ /\P{IsSpace}/;
1538             print "ok $test\n"; $test++;
1539         } else {
1540             print "not " if     $char =~ /\p{IsSpace}/;
1541             print "ok $test\n"; $test++;
1542             print "not " unless $char =~ /\P{IsSpace}/;
1543             print "ok $test\n"; $test++;
1544         }
1545         print "# IsUpper\n";
1546         if ($class =~ /^L[ut]/) {
1547             print "not " unless $char =~ /\p{IsUpper}/;
1548             print "ok $test\n"; $test++;
1549             print "not " if     $char =~ /\P{IsUpper}/;
1550             print "ok $test\n"; $test++;
1551         } else {
1552             print "not " if     $char =~ /\p{IsUpper}/;
1553             print "ok $test\n"; $test++;
1554             print "not " unless $char =~ /\P{IsUpper}/;
1555             print "ok $test\n"; $test++;
1556         }
1557         print "# IsWord\n";
1558         if ($class =~ /^[LMN]/ || $char eq "_") {
1559             print "not " unless $char =~ /\p{IsWord}/;
1560             print "ok $test\n"; $test++;
1561             print "not " if     $char =~ /\P{IsWord}/;
1562             print "ok $test\n"; $test++;
1563         } else {
1564             print "not " if     $char =~ /\p{IsWord}/;
1565             print "ok $test\n"; $test++;
1566             print "not " unless $char =~ /\P{IsWord}/;
1567             print "ok $test\n"; $test++;
1568         }
1569     }
1570 }
1571
1572 {
1573     $_ = "abc\x{100}\x{200}\x{300}\x{380}\x{400}defg";
1574
1575     if (/(.\x{300})./) {
1576         print "ok 576\n";
1577
1578         print "not " unless $` eq "abc\x{100}" && length($`) == 4;
1579         print "ok 577\n";
1580
1581         print "not " unless $& eq "\x{200}\x{300}\x{380}" && length($&) == 3;
1582         print "ok 578\n";
1583
1584         print "not " unless $' eq "\x{400}defg" && length($') == 5;
1585         print "ok 579\n";
1586
1587         print "not " unless $1 eq "\x{200}\x{300}" && length($1) == 2;
1588         print "ok 580\n";
1589     } else {
1590         for (576..580) { print "not ok $_\n" }
1591     }
1592 }
1593
1594 {
1595     # bug id 20010306.008
1596
1597     $a = "a\x{1234}";
1598     # The original bug report had 'no utf8' here but that was irrelevant.
1599     $a =~ m/\w/; # used to core dump
1600
1601     print "ok 581\n";
1602 }
1603
1604 {
1605     $test = 582;
1606
1607     # bugid 20010410.006
1608     for my $rx (
1609                 '/(.*?)\{(.*?)\}/csg',
1610                 '/(.*?)\{(.*?)\}/cg',
1611                 '/(.*?)\{(.*?)\}/sg',
1612                 '/(.*?)\{(.*?)\}/g',
1613                 '/(.+?)\{(.+?)\}/csg',
1614                )
1615     {
1616         my($input, $i);
1617
1618         $i = 0;
1619         $input = "a{b}c{d}";
1620         eval <<EOT;
1621         while (eval \$input =~ $rx) {
1622             print "# \\\$1 = '\$1' \\\$2 = '\$2'\n";
1623             ++\$i;
1624         }
1625 EOT
1626         print "not " unless $i == 2;
1627         print "ok " . $test++ . "\n";
1628     }
1629 }
1630
1631 {
1632     # from Robin Houston
1633
1634     my $x = "\x{10FFFD}";
1635     $x =~ s/(.)/$1/g;
1636     print "not " unless ord($x) == 0x10FFFD && length($x) == 1;
1637     print "ok 587\n";
1638 }
1639
1640 {
1641     my $x = "\x7f";
1642
1643     print "not " if     $x =~ /[\x80-\xff]/;
1644     print "ok 588\n";
1645
1646     print "not " if     $x =~ /[\x80-\x{100}]/;
1647     print "ok 589\n";
1648
1649     print "not " if     $x =~ /[\x{100}]/;
1650     print "ok 590\n";
1651
1652     print "not " if     $x =~ /\p{InLatin1Supplement}/;
1653     print "ok 591\n";
1654
1655     print "not " unless $x =~ /\P{InLatin1Supplement}/;
1656     print "ok 592\n";
1657
1658     print "not " if     $x =~ /\p{InLatinExtendedA}/;
1659     print "ok 593\n";
1660
1661     print "not " unless $x =~ /\P{InLatinExtendedA}/;
1662     print "ok 594\n";
1663 }
1664
1665 {
1666     my $x = "\x80";
1667
1668     print "not " unless $x =~ /[\x80-\xff]/;
1669     print "ok 595\n";
1670
1671     print "not " unless $x =~ /[\x80-\x{100}]/;
1672     print "ok 596\n";
1673
1674     print "not " if     $x =~ /[\x{100}]/;
1675     print "ok 597\n";
1676
1677     print "not " unless $x =~ /\p{InLatin1Supplement}/;
1678     print "ok 598\n";
1679
1680     print "not " if    $x =~ /\P{InLatin1Supplement}/;
1681     print "ok 599\n";
1682
1683     print "not " if     $x =~ /\p{InLatinExtendedA}/;
1684     print "ok 600\n";
1685
1686     print "not " unless $x =~ /\P{InLatinExtendedA}/;
1687     print "ok 601\n";
1688 }
1689
1690 {
1691     my $x = "\xff";
1692
1693     print "not " unless $x =~ /[\x80-\xff]/;
1694     print "ok 602\n";
1695
1696     print "not " unless $x =~ /[\x80-\x{100}]/;
1697     print "ok 603\n";
1698
1699     print "not " if     $x =~ /[\x{100}]/;
1700     print "ok 604\n";
1701
1702     # the next two tests must be ignored on EBCDIC
1703     print "not " unless $x =~ /\p{InLatin1Supplement}/ or ord("A") == 193;
1704     print "ok 605\n";
1705
1706     print "not " if     $x =~ /\P{InLatin1Supplement}/ and ord("A") != 193;
1707     print "ok 606\n";
1708
1709     print "not " if     $x =~ /\p{InLatinExtendedA}/;
1710     print "ok 607\n";
1711
1712     print "not " unless $x =~ /\P{InLatinExtendedA}/;
1713     print "ok 608\n";
1714 }
1715
1716 {
1717     my $x = "\x{100}";
1718
1719     print "not " if     $x =~ /[\x80-\xff]/;
1720     print "ok 609\n";
1721
1722     print "not " unless $x =~ /[\x80-\x{100}]/;
1723     print "ok 610\n";
1724
1725     print "not " unless $x =~ /[\x{100}]/;
1726     print "ok 611\n";
1727
1728     print "not " if     $x =~ /\p{InLatin1Supplement}/;
1729     print "ok 612\n";
1730
1731     print "not " unless $x =~ /\P{InLatin1Supplement}/;
1732     print "ok 613\n";
1733
1734     print "not " unless $x =~ /\p{InLatinExtendedA}/;
1735     print "ok 614\n";
1736
1737     print "not " if     $x =~ /\P{InLatinExtendedA}/;
1738     print "ok 615\n";
1739 }
1740
1741 {
1742     # from japhy
1743     my $w;
1744     use warnings;    
1745     local $SIG{__WARN__} = sub { $w .= shift };
1746
1747     $w = "";
1748     eval 'qr/(?c)/';
1749     print "not " if $w !~ /^Useless \(\?c\)/;
1750     print "ok 616\n";
1751
1752     $w = "";
1753     eval 'qr/(?-c)/';
1754     print "not " if $w !~ /^Useless \(\?-c\)/;
1755     print "ok 617\n";
1756
1757     $w = "";
1758     eval 'qr/(?g)/';
1759     print "not " if $w !~ /^Useless \(\?g\)/;
1760     print "ok 618\n";
1761
1762     $w = "";
1763     eval 'qr/(?-g)/';
1764     print "not " if $w !~ /^Useless \(\?-g\)/;
1765     print "ok 619\n";
1766
1767     $w = "";
1768     eval 'qr/(?o)/';
1769     print "not " if $w !~ /^Useless \(\?o\)/;
1770     print "ok 620\n";
1771
1772     $w = "";
1773     eval 'qr/(?-o)/';
1774     print "not " if $w !~ /^Useless \(\?-o\)/;
1775     print "ok 621\n";
1776
1777     # now test multi-error regexes
1778
1779     $w = "";
1780     eval 'qr/(?g-o)/';
1781     print "not " if $w !~ /^Useless \(\?g\).*\nUseless \(\?-o\)/;
1782     print "ok 622\n";
1783
1784     $w = "";
1785     eval 'qr/(?g-c)/';
1786     print "not " if $w !~ /^Useless \(\?g\).*\nUseless \(\?-c\)/;
1787     print "ok 623\n";
1788
1789     $w = "";
1790     eval 'qr/(?o-cg)/';  # (?c) means (?g) error won't be thrown
1791     print "not " if $w !~ /^Useless \(\?o\).*\nUseless \(\?-c\)/;
1792     print "ok 624\n";
1793
1794     $w = "";
1795     eval 'qr/(?ogc)/';
1796     print "not " if $w !~ /^Useless \(\?o\).*\nUseless \(\?g\).*\nUseless \(\?c\)/;
1797     print "ok 625\n";
1798 }
1799
1800 # More Unicode "class" tests
1801
1802 {
1803     use charnames ':full';
1804
1805     print "not " unless "\N{LATIN CAPITAL LETTER A}" =~ /\p{InBasicLatin}/;
1806     print "ok 626\n";
1807
1808     print "not " unless "\N{LATIN CAPITAL LETTER A WITH GRAVE}" =~ /\p{InLatin1Supplement}/;
1809     print "ok 627\n";
1810
1811     print "not " unless "\N{LATIN CAPITAL LETTER A WITH MACRON}" =~ /\p{InLatinExtendedA}/;
1812     print "ok 628\n";
1813
1814     print "not " unless "\N{LATIN SMALL LETTER B WITH STROKE}" =~ /\p{InLatinExtendedB}/;
1815     print "ok 629\n";
1816
1817     print "not " unless "\N{KATAKANA LETTER SMALL A}" =~ /\p{InKatakana}/;
1818     print "ok 630\n";
1819 }
1820
1821 $_ = "foo";
1822
1823 eval <<"EOT"; die if $@;
1824   /f
1825    o\r
1826    o
1827    \$
1828   /x && print "ok 631\n";
1829 EOT
1830
1831 eval <<"EOT"; die if $@;
1832   /f
1833    o
1834    o
1835    \$\r
1836   /x && print "ok 632\n";
1837 EOT
1838
1839 #test /o feature
1840 sub test_o { $_[0] =~/$_[1]/o; return $1}
1841 if(test_o('abc','(.)..') eq 'a') {
1842     print "ok 633\n";
1843 } else {
1844     print "not ok 633\n";
1845 }
1846 if(test_o('abc','..(.)') eq 'a') {
1847     print "ok 634\n";
1848 } else {
1849     print "not ok 634\n";
1850 }
1851
1852 # 635..639: ID 20010619.003 (only the space character is
1853 # supposed to be [:print:], not the whole isprint()).
1854
1855 print "not " if "\n"     =~ /[[:print:]]/;
1856 print "ok 635\n";
1857
1858 print "not " if "\t"     =~ /[[:print:]]/;
1859 print "ok 636\n";
1860
1861 # Amazingly vertical tabulator is the same in ASCII and EBCDIC.
1862 print "not " if "\014"  =~ /[[:print:]]/;
1863 print "ok 637\n";
1864
1865 print "not " if "\r"    =~ /[[:print:]]/;
1866 print "ok 638\n";
1867
1868 print "not " unless " " =~ /[[:print:]]/;
1869 print "ok 639\n";
1870
1871 ##
1872 ## Test basic $^N usage outside of a regex
1873 ##
1874 $x = "abcdef";
1875 $T="ok 640\n";if ($x =~ /cde/ and not defined $^N)         {print $T} else {print "not $T"};
1876 $T="ok 641\n";if ($x =~ /(cde)/          and $^N eq "cde") {print $T} else {print "not $T"};
1877 $T="ok 642\n";if ($x =~ /(c)(d)(e)/      and $^N eq   "e") {print $T} else {print "not $T"};
1878 $T="ok 643\n";if ($x =~ /(c(d)e)/        and $^N eq "cde") {print $T} else {print "not $T"};
1879 $T="ok 644\n";if ($x =~ /(foo)|(c(d)e)/  and $^N eq "cde") {print $T} else {print "not $T"};
1880 $T="ok 645\n";if ($x =~ /(c(d)e)|(foo)/  and $^N eq "cde") {print $T} else {print "not $T"};
1881 $T="ok 646\n";if ($x =~ /(c(d)e)|(abc)/  and $^N eq "abc") {print $T} else {print "not $T"};
1882 $T="ok 647\n";if ($x =~ /(c(d)e)|(abc)x/ and $^N eq "cde") {print $T} else {print "not $T"};
1883 $T="ok 648\n";if ($x =~ /(c(d)e)(abc)?/  and $^N eq "cde") {print $T} else {print "not $T"};
1884 $T="ok 649\n";if ($x =~ /(?:c(d)e)/      and $^N eq  "d" ) {print $T} else {print "not $T"};
1885 $T="ok 650\n";if ($x =~ /(?:c(d)e)(?:f)/ and $^N eq  "d" ) {print $T} else {print "not $T"};
1886 $T="ok 651\n";if ($x =~ /(?:([abc])|([def]))*/ and $^N eq  "f" ){print $T} else {print "not $T"};
1887 $T="ok 652\n";if ($x =~ /(?:([ace])|([bdf]))*/ and $^N eq  "f" ){print $T} else {print "not $T"};
1888 $T="ok 653\n";if ($x =~ /(([ace])|([bd]))*/    and $^N eq  "e" ){print $T} else {print "not $T"};
1889 {
1890  $T="ok 654\n";if($x =~ /(([ace])|([bdf]))*/   and $^N eq  "f" ){print $T} else {print "not $T"};
1891 }
1892 ## test to see if $^N is automatically localized -- it should now
1893 ## have the value set in test 653
1894 $T="ok 655\n";if ($^N eq  "e" ){print $T} else {print "not $T"};
1895
1896 ##
1897 ## Now test inside (?{...})
1898 ##
1899 $T="ok 656\n";if ($x =~ /a([abc])(?{$y=$^N})c/      and $y eq "b" ){print $T} else {print "not $T"};
1900 $T="ok 657\n";if ($x =~ /a([abc]+)(?{$y=$^N})d/     and $y eq "bc"){print $T} else {print "not $T"};
1901 $T="ok 658\n";if ($x =~ /a([abcdefg]+)(?{$y=$^N})d/ and $y eq "bc"){print $T} else {print "not $T"};
1902 $T="ok 659\n";if ($x =~ /(a([abcdefg]+)(?{$y=$^N})d)(?{$z=$^N})e/ and $y eq "bc" and $z eq "abcd")
1903               {print $T} else {print "not $T"};
1904 $T="ok 660\n";if ($x =~ /(a([abcdefg]+)(?{$y=$^N})de)(?{$z=$^N})/ and $y eq "bc" and $z eq "abcde")
1905               {print $T} else {print "not $T"};
1906
1907 # Test the Unicode script classes
1908
1909 print "not " unless chr(0x100) =~ /\p{IsLatin}/; # outside Latin-1
1910 print "ok 661\n";
1911
1912 print "not " unless chr(0x212b) =~ /\p{IsLatin}/; # Angstrom sign, very outside
1913 print "ok 662\n";
1914
1915 print "not " unless chr(0x5d0) =~ /\p{IsHebrew}/; # inside InHebrew
1916 print "ok 663\n";
1917
1918 print "not " unless chr(0xfb4f) =~ /\p{IsHebrew}/; # outside InHebrew
1919 print "ok 664\n";
1920
1921 # # singleton (not in a range, this test must be ignored on EBCDIC)
1922 # print "not " unless chr(0xb5) =~ /\p{IsGreek}/ or ord("A") == 193;
1923 # print "ok 665\n";
1924 print "ok 665 # 0xb5 moved from Greek to Common with Unicode 4.0.1\n";
1925
1926 print "not " unless chr(0x37a) =~ /\p{IsGreek}/; # singleton
1927 print "ok 666\n";
1928
1929 print "not " unless chr(0x386) =~ /\p{IsGreek}/; # singleton
1930 print "ok 667\n";
1931
1932 print "not " unless chr(0x387) =~ /\P{IsGreek}/; # not there
1933 print "ok 668\n";
1934
1935 print "not " unless chr(0x388) =~ /\p{IsGreek}/; # range
1936 print "ok 669\n";
1937
1938 print "not " unless chr(0x38a) =~ /\p{IsGreek}/; # range
1939 print "ok 670\n";
1940
1941 print "not " unless chr(0x38b) =~ /\P{IsGreek}/; # not there
1942 print "ok 671\n";
1943
1944 print "not " unless chr(0x38c) =~ /\p{IsGreek}/; # singleton
1945 print "ok 672\n";
1946
1947 if (ord("A") == 65) {
1948 ##
1949 ## Test [:cntrl:]...
1950 ##
1951 ## Should probably put in tests for all the POSIX stuff, but not sure how to
1952 ## guarantee a specific locale......
1953 ##
1954     $AllBytes = join('', map { chr($_) } 0..255);
1955     ($x = $AllBytes) =~ s/[[:cntrl:]]//g;
1956     if ($x ne join('', map { chr($_) } 0x20..0x7E, 0x80..0xFF)) {
1957         print "not ";
1958     }
1959     print "ok 673\n";
1960
1961     ($x = $AllBytes) =~ s/[^[:cntrl:]]//g;
1962     if ($x ne join('', map { chr($_) } 0..0x1F, 0x7F)) { print "not " }
1963     print "ok 674\n";
1964 } else {
1965     print "ok $_ # Skip: EBCDIC\n" for 673..674;
1966 }
1967
1968 # With /s modifier UTF8 chars were interpreted as bytes
1969 {
1970     my $a = "Hello \x{263A} World";
1971     
1972     my @a = ($a =~ /./gs);
1973     
1974     print "not " unless $#a == 12;
1975     print "ok 675\n";
1976 }
1977
1978 @a = ("foo\nbar" =~ /./g);
1979 print "ok 676\n" if @a == 6 && "@a" eq "f o o b a r";
1980
1981 @a = ("foo\nbar" =~ /./gs);
1982 print "ok 677\n" if @a == 7 && "@a" eq "f o o \n b a r";
1983
1984 @a = ("foo\nbar" =~ /\C/g);
1985 print "ok 678\n" if @a == 7 && "@a" eq "f o o \n b a r";
1986
1987 @a = ("foo\nbar" =~ /\C/gs);
1988 print "ok 679\n" if @a == 7 && "@a" eq "f o o \n b a r";
1989
1990 @a = ("foo\n\x{100}bar" =~ /./g);
1991 print "ok 680\n" if @a == 7 && "@a" eq "f o o \x{100} b a r";
1992
1993 @a = ("foo\n\x{100}bar" =~ /./gs);
1994 print "ok 681\n" if @a == 8 && "@a" eq "f o o \n \x{100} b a r";
1995
1996 ($a, $b) = map { chr } ord('A') == 65 ? (0xc4, 0x80) : (0x8c, 0x41);
1997
1998 @a = ("foo\n\x{100}bar" =~ /\C/g);
1999 print "ok 682\n" if @a == 9 && "@a" eq "f o o \n $a $b b a r";
2000
2001 @a = ("foo\n\x{100}bar" =~ /\C/gs);
2002 print "ok 683\n" if @a == 9 && "@a" eq "f o o \n $a $b b a r";
2003
2004 {
2005     # [ID 20010814.004] pos() doesn't work when using =~m// in list context
2006     $_ = "ababacadaea";
2007     $a = join ":", /b./gc;
2008     $b = join ":", /a./gc;
2009     $c = pos;
2010     print "$a $b $c" eq 'ba:ba ad:ae 10' ? "ok 684\n" : "not ok 684\t# $a $b $c\n";
2011 }
2012
2013 {
2014     # [ID 20010407.006] matching utf8 return values from functions does not work
2015
2016     package ID_20010407_006;
2017
2018     sub x {
2019         "a\x{1234}";
2020     }
2021
2022     my $x = x;
2023     my $y;
2024
2025     $x =~ /(..)/; $y = $1;
2026     print "not " unless length($y) == 2 && $y eq $x;
2027     print "ok 685\n";
2028
2029     x  =~ /(..)/; $y = $1;
2030     print "not " unless length($y) == 2 && $y eq $x;
2031     print "ok 686\n";
2032 }
2033
2034
2035 my $test = 687;
2036
2037 # Force scalar context on the patern match
2038 sub ok ($$) {
2039     my($ok, $name) = @_;
2040
2041     printf "%sok %d - %s\n", ($ok ? "" : "not "), $test, $name;
2042
2043     printf "# Failed test at line %d\n", (caller)[2] unless $ok;
2044
2045     $test++;
2046     return $ok;
2047 }
2048
2049 {
2050     # Check that \x## works. 5.6.1 and 5.005_03 fail some of these.
2051     $x = "\x4e" . "E";
2052     ok ($x =~ /^\x4EE$/, "Check only 2 bytes of hex are matched.");
2053
2054     $x = "\x4e" . "i";
2055     ok ($x =~ /^\x4Ei$/, "Check that invalid hex digit stops it (2)");
2056
2057     $x = "\x4" . "j";
2058     ok ($x =~ /^\x4j$/,  "Check that invalid hex digit stops it (1)");
2059
2060     $x = "\x0" . "k";
2061     ok ($x =~ /^\xk$/,   "Check that invalid hex digit stops it (0)");
2062
2063     $x = "\x0" . "x";
2064     ok ($x =~ /^\xx$/, "\\xx isn't to be treated as \\0");
2065
2066     $x = "\x0" . "xa";
2067     ok ($x =~ /^\xxa$/, "\\xxa isn't to be treated as \\xa");
2068
2069     $x = "\x9" . "_b";
2070     ok ($x =~ /^\x9_b$/, "\\x9_b isn't to be treated as \\x9b");
2071
2072     print "# and now again in [] ranges\n";
2073
2074     $x = "\x4e" . "E";
2075     ok ($x =~ /^[\x4EE]{2}$/, "Check only 2 bytes of hex are matched.");
2076
2077     $x = "\x4e" . "i";
2078     ok ($x =~ /^[\x4Ei]{2}$/, "Check that invalid hex digit stops it (2)");
2079
2080     $x = "\x4" . "j";
2081     ok ($x =~ /^[\x4j]{2}$/,  "Check that invalid hex digit stops it (1)");
2082
2083     $x = "\x0" . "k";
2084     ok ($x =~ /^[\xk]{2}$/,   "Check that invalid hex digit stops it (0)");
2085
2086     $x = "\x0" . "x";
2087     ok ($x =~ /^[\xx]{2}$/, "\\xx isn't to be treated as \\0");
2088
2089     $x = "\x0" . "xa";
2090     ok ($x =~ /^[\xxa]{3}$/, "\\xxa isn't to be treated as \\xa");
2091
2092     $x = "\x9" . "_b";
2093     ok ($x =~ /^[\x9_b]{3}$/, "\\x9_b isn't to be treated as \\x9b");
2094
2095 }
2096
2097 {
2098     # Check that \x{##} works. 5.6.1 fails quite a few of these.
2099
2100     $x = "\x9b";
2101     ok ($x =~ /^\x{9_b}$/, "\\x{9_b} is to be treated as \\x9b");
2102
2103     $x = "\x9b" . "y";
2104     ok ($x =~ /^\x{9_b}y$/, "\\x{9_b} is to be treated as \\x9b (again)");
2105
2106     $x = "\x9b" . "y";
2107     ok ($x =~ /^\x{9b_}y$/, "\\x{9b_} is to be treated as \\x9b");
2108
2109     $x = "\x9b" . "y";
2110     ok ($x =~ /^\x{9_bq}y$/, "\\x{9_bc} is to be treated as \\x9b");
2111
2112     $x = "\x0" . "y";
2113     ok ($x =~ /^\x{x9b}y$/, "\\x{x9b} is to be treated as \\x0");
2114
2115     $x = "\x0" . "y";
2116     ok ($x =~ /^\x{0x9b}y$/, "\\x{0x9b} is to be treated as \\x0");
2117
2118     $x = "\x9b" . "y";
2119     ok ($x =~ /^\x{09b}y$/, "\\x{09b} is to be treated as \\x9b");
2120
2121     print "# and now again in [] ranges\n";
2122
2123     $x = "\x9b";
2124     ok ($x =~ /^[\x{9_b}]$/, "\\x{9_b} is to be treated as \\x9b");
2125
2126     $x = "\x9b" . "y";
2127     ok ($x =~ /^[\x{9_b}y]{2}$/, "\\x{9_b} is to be treated as \\x9b (again)");
2128
2129     $x = "\x9b" . "y";
2130     ok ($x =~ /^[\x{9b_}y]{2}$/, "\\x{9b_} is to be treated as \\x9b");
2131
2132     $x = "\x9b" . "y";
2133     ok ($x =~ /^[\x{9_bq}y]{2}$/, "\\x{9_bc} is to be treated as \\x9b");
2134
2135     $x = "\x0" . "y";
2136     ok ($x =~ /^[\x{x9b}y]{2}$/, "\\x{x9b} is to be treated as \\x0");
2137
2138     $x = "\x0" . "y";
2139     ok ($x =~ /^[\x{0x9b}y]{2}$/, "\\x{0x9b} is to be treated as \\x0");
2140
2141     $x = "\x9b" . "y";
2142     ok ($x =~ /^[\x{09b}y]{2}$/, "\\x{09b} is to be treated as \\x9b");
2143 }
2144
2145 {
2146     # high bit bug -- japhy
2147     my $x = "ab\200d";
2148     $x =~ /.*?\200/ or print "not ";
2149     print "ok 715\n";
2150 }
2151
2152 print "# some Unicode properties\n";
2153
2154 {
2155     # Dashes, underbars, case.
2156     print "not " unless "\x80" =~ /\p{in-latin1_SUPPLEMENT}/;
2157     print "ok 716\n";
2158
2159     # Complement, leading and trailing whitespace.
2160     print "not " unless "\x80" =~ /\P{  ^  In Latin 1 Supplement  }/;
2161     print "ok 717\n";
2162
2163     # No ^In, dashes, case, dash, any intervening (word-break) whitespace.
2164     # (well, newlines don't work...)
2165     print "not " unless "\x80" =~ /\p{latin-1   supplement}/;
2166     print "ok 718\n";
2167 }
2168
2169 {
2170     print "not " unless "a" =~ /\pL/;
2171     print "ok 719\n";
2172
2173     print "not " unless "a" =~ /\p{IsLl}/;
2174     print "ok 720\n";
2175
2176     print "not " if     "a" =~ /\p{IsLu}/;
2177     print "ok 721\n";
2178
2179     print "not " unless "a" =~ /\p{Ll}/;
2180     print "ok 722\n";
2181
2182     print "not " if     "a" =~ /\p{Lu}/;
2183     print "ok 723\n";
2184
2185     print "not " unless "A" =~ /\pL/;
2186     print "ok 724\n";
2187
2188     print "not " unless "A" =~ /\p{IsLu}/;
2189     print "ok 725\n";
2190
2191     print "not " if     "A" =~ /\p{IsLl}/;
2192     print "ok 726\n";
2193
2194     print "not " unless "A" =~ /\p{Lu}/;
2195     print "ok 727\n";
2196
2197     print "not " if     "A" =~ /\p{Ll}/;
2198     print "ok 728\n";
2199
2200     print "not " if     "a" =~ /\PL/;
2201     print "ok 729\n";
2202
2203     print "not " if     "a" =~ /\P{IsLl}/;
2204     print "ok 730\n";
2205
2206     print "not " unless "a" =~ /\P{IsLu}/;
2207     print "ok 731\n";
2208
2209     print "not " if     "a" =~ /\P{Ll}/;
2210     print "ok 732\n";
2211
2212     print "not " unless "a" =~ /\P{Lu}/;
2213     print "ok 733\n";
2214
2215     print "not " if     "A" =~ /\PL/;
2216     print "ok 734\n";
2217
2218     print "not " if     "A" =~ /\P{IsLu}/;
2219     print "ok 735\n";
2220
2221     print "not " unless "A" =~ /\P{IsLl}/;
2222     print "ok 736\n";
2223
2224     print "not " if     "A" =~ /\P{Lu}/;
2225     print "ok 737\n";
2226
2227     print "not " unless "A" =~ /\P{Ll}/;
2228     print "ok 738\n";
2229
2230 }
2231
2232 {
2233     print "not " if     "a" =~ /\p{Common}/;
2234     print "ok 739\n";
2235
2236     print "not " unless "1" =~ /\p{Common}/;
2237     print "ok 740\n";
2238 }
2239
2240 {
2241     print "not " if     "a"       =~ /\p{Inherited}/;
2242     print "ok 741\n";
2243
2244     print "not " unless "\x{300}" =~ /\p{Inherited}/;
2245     print "ok 742\n";
2246 }
2247
2248 {
2249     # L& and LC are the same
2250     print "not " unless "a" =~ /\p{LC}/ and "a" =~ /\p{L&}/;
2251     print "ok 743\n";
2252
2253     print "not " if     "1" =~ /\p{LC}/ or "1" =~ /\p{L&}/;
2254     print "ok 744\n";
2255 }
2256
2257 {
2258     print "not " unless "a" =~ /\p{Lowercase Letter}/;
2259     print "ok 745\n";
2260
2261     print "not " if     "A" =~ /\p{lowercaseletter}/;
2262     print "ok 746\n";
2263 }
2264
2265 {
2266     print "not " unless "\x{AC00}" =~ /\p{HangulSyllables}/;
2267     print "ok 747\n";
2268 }
2269
2270 {
2271     # Script=, Block=, Category=
2272
2273     print "not " unless "\x{0100}" =~ /\p{Script=Latin}/;
2274     print "ok 748\n";
2275
2276     print "not " unless "\x{0100}" =~ /\p{Block=LatinExtendedA}/;
2277     print "ok 749\n";
2278
2279     print "not " unless "\x{0100}" =~ /\p{Category=UppercaseLetter}/;
2280     print "ok 750\n";
2281 }
2282
2283 {
2284     print "# the basic character classes and Unicode \n";
2285
2286     # 0100;LATIN CAPITAL LETTER A WITH MACRON;Lu;0;L;0041 0304;;;;N;LATIN CAPITAL LETTER A MACRON;;;0101;
2287     print "not " unless "\x{0100}" =~ /\w/;
2288     print "ok 751\n";
2289
2290     # 0660;ARABIC-INDIC DIGIT ZERO;Nd;0;AN;;0;0;0;N;;;;;
2291     print "not " unless "\x{0660}" =~ /\d/;
2292     print "ok 752\n";
2293
2294     # 1680;OGHAM SPACE MARK;Zs;0;WS;;;;;N;;;;;
2295     print "not " unless "\x{1680}" =~ /\s/;
2296     print "ok 753\n";
2297 }
2298
2299 {
2300     print "# folding matches and Unicode\n";
2301
2302     print "not " unless "a\x{100}" =~ /A/i;
2303     print "ok 754\n";
2304
2305     print "not " unless "A\x{100}" =~ /a/i;
2306     print "ok 755\n";
2307
2308     print "not " unless "a\x{100}" =~ /a/i;
2309     print "ok 756\n";
2310
2311     print "not " unless "A\x{100}" =~ /A/i;
2312     print "ok 757\n";
2313
2314     print "not " unless "\x{101}a" =~ /\x{100}/i;
2315     print "ok 758\n";
2316
2317     print "not " unless "\x{100}a" =~ /\x{100}/i;
2318     print "ok 759\n";
2319
2320     print "not " unless "\x{101}a" =~ /\x{101}/i;
2321     print "ok 760\n";
2322
2323     print "not " unless "\x{100}a" =~ /\x{101}/i;
2324     print "ok 761\n";
2325
2326     print "not " unless "a\x{100}" =~ /A\x{100}/i;
2327     print "ok 762\n";
2328
2329     print "not " unless "A\x{100}" =~ /a\x{100}/i;
2330     print "ok 763\n";
2331
2332     print "not " unless "a\x{100}" =~ /a\x{100}/i;
2333     print "ok 764\n";
2334
2335     print "not " unless "A\x{100}" =~ /A\x{100}/i;
2336     print "ok 765\n";
2337
2338     print "not " unless "a\x{100}" =~ /[A]/i;
2339     print "ok 766\n";
2340
2341     print "not " unless "A\x{100}" =~ /[a]/i;
2342     print "ok 767\n";
2343
2344     print "not " unless "a\x{100}" =~ /[a]/i;
2345     print "ok 768\n";
2346
2347     print "not " unless "A\x{100}" =~ /[A]/i;
2348     print "ok 769\n";
2349
2350     print "not " unless "\x{101}a" =~ /[\x{100}]/i;
2351     print "ok 770\n";
2352
2353     print "not " unless "\x{100}a" =~ /[\x{100}]/i;
2354     print "ok 771\n";
2355
2356     print "not " unless "\x{101}a" =~ /[\x{101}]/i;
2357     print "ok 772\n";
2358
2359     print "not " unless "\x{100}a" =~ /[\x{101}]/i;
2360     print "ok 773\n";
2361
2362 }
2363
2364 {
2365     use charnames ':full';
2366
2367     print "# LATIN LETTER A WITH GRAVE\n";
2368     my $lower = "\N{LATIN SMALL LETTER A WITH GRAVE}";
2369     my $UPPER = "\N{LATIN CAPITAL LETTER A WITH GRAVE}";
2370
2371     print $lower =~ m/$UPPER/i   ? "ok 774\n" : "not ok 774\n";
2372     print $UPPER =~ m/$lower/i   ? "ok 775\n" : "not ok 775\n";
2373     print $lower =~ m/[$UPPER]/i ? "ok 776\n" : "not ok 776\n";
2374     print $UPPER =~ m/[$lower]/i ? "ok 777\n" : "not ok 777\n";
2375
2376     print "# GREEK LETTER ALPHA WITH VRACHY\n";
2377
2378     $lower = "\N{GREEK CAPITAL LETTER ALPHA WITH VRACHY}";
2379     $UPPER = "\N{GREEK SMALL LETTER ALPHA WITH VRACHY}";
2380
2381     print $lower =~ m/$UPPER/i   ? "ok 778\n" : "not ok 778\n";
2382     print $UPPER =~ m/$lower/i   ? "ok 779\n" : "not ok 779\n";
2383     print $lower =~ m/[$UPPER]/i ? "ok 780\n" : "not ok 780\n";
2384     print $UPPER =~ m/[$lower]/i ? "ok 781\n" : "not ok 781\n";
2385
2386     print "# LATIN LETTER Y WITH DIAERESIS\n";
2387
2388     $lower = "\N{LATIN CAPITAL LETTER Y WITH DIAERESIS}";
2389     $UPPER = "\N{LATIN SMALL LETTER Y WITH DIAERESIS}";
2390     print $lower =~ m/$UPPER/i   ? "ok 782\n" : "not ok 782\n";
2391     print $UPPER =~ m/$lower/i   ? "ok 783\n" : "not ok 783\n";
2392     print $lower =~ m/[$UPPER]/i ? "ok 784\n" : "not ok 784\n";
2393     print $UPPER =~ m/[$lower]/i ? "ok 785\n" : "not ok 785\n";
2394 }
2395
2396 {
2397     use warnings;
2398     use charnames ':full';
2399     
2400     print "# GREEK CAPITAL LETTER SIGMA vs COMBINING GREEK PERISPOMENI\n";
2401
2402     my $SIGMA = "\N{GREEK CAPITAL LETTER SIGMA}";
2403     my $char  = "\N{COMBINING GREEK PERISPOMENI}";
2404
2405     # Before #13843 this was failing by matching falsely.
2406     print "_:$char:_" =~ m/_:$SIGMA:_/i ? "not ok 786\n" : "ok 786\n";
2407 }
2408
2409 {
2410     print "# \\X\n";
2411
2412     use charnames ':full';
2413
2414     print "a!"              =~ /^(\X)!/ && $1 eq "a" ?
2415         "ok 787\n" : "not ok 787 # $1\n";
2416     print "\xDF!"           =~ /^(\X)!/ && $1 eq "\xDF" ?
2417         "ok 788\n" : "not ok 788 # $1\n";
2418     print "\x{100}!"        =~ /^(\X)!/ && $1 eq "\x{100}" ?
2419         "ok 789\n" : "not ok 789 # $1\n";
2420     print "\x{100}\x{300}!" =~ /^(\X)!/ && $1 eq "\x{100}\x{300}" ?
2421         "ok 790\n" : "not ok 790 # $1\n";
2422     print "\N{LATIN CAPITAL LETTER E}!" =~ /^(\X)!/ &&
2423         $1 eq "\N{LATIN CAPITAL LETTER E}" ?
2424         "ok 791\n" : "not ok 791 # $1\n";
2425     print "\N{LATIN CAPITAL LETTER E}\N{COMBINING GRAVE ACCENT}!" =~
2426         /^(\X)!/ &&
2427         $1 eq "\N{LATIN CAPITAL LETTER E}\N{COMBINING GRAVE ACCENT}" ?
2428         "ok 792\n" : "not ok 792 # $1\n";
2429 }
2430
2431 {
2432     print "#\\C and \\X\n";
2433
2434     print "!abc!" =~ /a\Cc/ ? "ok 793\n" : "not ok 793\n";
2435     print "!abc!" =~ /a\Xc/ ? "ok 794\n" : "not ok 794\n";
2436 }
2437
2438 {
2439     print "# FINAL SIGMA\n";
2440
2441     my $SIGMA = "\x{03A3}"; # CAPITAL
2442     my $Sigma = "\x{03C2}"; # SMALL FINAL
2443     my $sigma = "\x{03C3}"; # SMALL
2444
2445     print $SIGMA =~ /$SIGMA/i ? "ok 795\n" : "not ok 795\n";
2446     print $SIGMA =~ /$Sigma/i ? "ok 796\n" : "not ok 796\n";
2447     print $SIGMA =~ /$sigma/i ? "ok 797\n" : "not ok 797\n";
2448
2449     print $Sigma =~ /$SIGMA/i ? "ok 798\n" : "not ok 798\n";
2450     print $Sigma =~ /$Sigma/i ? "ok 799\n" : "not ok 799\n";
2451     print $Sigma =~ /$sigma/i ? "ok 800\n" : "not ok 800\n";
2452
2453     print $sigma =~ /$SIGMA/i ? "ok 801\n" : "not ok 801\n";
2454     print $sigma =~ /$Sigma/i ? "ok 802\n" : "not ok 802\n";
2455     print $sigma =~ /$sigma/i ? "ok 803\n" : "not ok 803\n";
2456     
2457     print $SIGMA =~ /[$SIGMA]/i ? "ok 804\n" : "not ok 804\n";
2458     print $SIGMA =~ /[$Sigma]/i ? "ok 805\n" : "not ok 805\n";
2459     print $SIGMA =~ /[$sigma]/i ? "ok 806\n" : "not ok 806\n";
2460
2461     print $Sigma =~ /[$SIGMA]/i ? "ok 807\n" : "not ok 807\n";
2462     print $Sigma =~ /[$Sigma]/i ? "ok 808\n" : "not ok 808\n";
2463     print $Sigma =~ /[$sigma]/i ? "ok 809\n" : "not ok 809\n";
2464
2465     print $sigma =~ /[$SIGMA]/i ? "ok 810\n" : "not ok 810\n";
2466     print $sigma =~ /[$Sigma]/i ? "ok 811\n" : "not ok 811\n";
2467     print $sigma =~ /[$sigma]/i ? "ok 812\n" : "not ok 812\n";
2468 }
2469
2470 {
2471     print "# parlez-vous?\n";
2472
2473     use charnames ':full';
2474
2475     print "fran\N{LATIN SMALL LETTER C}ais" =~
2476           /fran.ais/ &&
2477         $& eq "francais" ?
2478         "ok 813\n" : "not ok 813\n";
2479
2480     print "fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" =~
2481           /fran.ais/ &&
2482         $& eq "fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" ?
2483         "ok 814\n" : "not ok 814\n";
2484
2485     print "fran\N{LATIN SMALL LETTER C}ais" =~
2486            /fran\Cais/ &&
2487         $& eq "francais" ?
2488         "ok 815\n" : "not ok 815\n";
2489
2490     print "franc\N{COMBINING CEDILLA}ais" =~
2491           /franc\C\Cais/ ? # COMBINING CEDILLA is two bytes when encoded
2492         "ok 816\n" : "not ok 816\n";
2493
2494     print "fran\N{LATIN SMALL LETTER C}ais" =~
2495           /fran\Xais/ &&
2496         $& eq "francais" ?
2497         "ok 817\n" : "not ok 817\n";
2498
2499     print "fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" =~
2500           /fran\Xais/  &&
2501         $& eq "fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" ?
2502         "ok 818\n" : "not ok 818\n";
2503
2504     print "franc\N{COMBINING CEDILLA}ais" =~
2505           /fran\Xais/ &&
2506          $& eq "franc\N{COMBINING CEDILLA}ais" ?
2507          "ok 819\n" : "not ok 819\n";
2508
2509     print "fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" =~
2510           /fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais/  &&
2511         $& eq "fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" ?
2512         "ok 820\n" : "not ok 820\n";
2513
2514     print "franc\N{COMBINING CEDILLA}ais" =~
2515           /franc\N{COMBINING CEDILLA}ais/  &&
2516         $& eq "franc\N{COMBINING CEDILLA}ais" ?
2517         "ok 821\n" : "not ok 821\n";
2518
2519     print "fran\N{LATIN SMALL LETTER C}ais" =~
2520           /fran(?:c\N{COMBINING CEDILLA}?|\N{LATIN SMALL LETTER C WITH CEDILLA})ais/ &&
2521         $& eq "francais" ?
2522         "ok 822\n" : "not ok 822\n";
2523
2524     print "fran\N{LATIN SMALL LETTER C}ais" =~
2525           /fran(?:c\N{COMBINING CEDILLA}?|\N{LATIN SMALL LETTER C WITH CEDILLA})ais/ &&
2526         $& eq "francais" ?
2527         "ok 823\n" : "not ok 823\n";
2528
2529     print "fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" =~
2530           /fran(?:c\N{COMBINING CEDILLA}?|\N{LATIN SMALL LETTER C WITH CEDILLA})ais/ &&
2531         $& eq "fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" ?
2532         "ok 824\n" : "not ok 824\n";
2533
2534     print "franc\N{COMBINING CEDILLA}ais" =~
2535           /fran(?:c\N{COMBINING CEDILLA}?|\N{LATIN SMALL LETTER C WITH CEDILLA})ais/ &&
2536         $& eq "franc\N{COMBINING CEDILLA}ais" ?
2537         "ok 825\n" : "not ok 825\n";
2538 }
2539
2540 {
2541     print "# Does lingering (and useless) UTF8 flag mess up /i matching?\n";
2542
2543     {
2544         my $regex  = "ABcde";
2545         my $string = "abcDE\x{100}";
2546         chop($string);
2547         if ($string =~ m/$regex/i) {
2548             print "ok 826\n";
2549         } else {
2550             print "not ok 826\n";
2551         }
2552     }
2553
2554     {
2555         my $regex  = "ABcde\x{100}";
2556         my $string = "abcDE";
2557         chop($regex);
2558         if ($string =~ m/$regex/i) {
2559             print "ok 827\n";
2560         } else {
2561             print "not ok 827\n";
2562         }
2563     }
2564
2565     {
2566         my $regex  = "ABcde\x{100}";
2567         my $string = "abcDE\x{100}";
2568         chop($regex);
2569         chop($string);
2570         if ($string =~ m/$regex/i) {
2571             print "ok 828\n";
2572         } else {
2573             print "not ok 828\n";
2574         }
2575     }
2576 }
2577
2578 {
2579     print "# more SIGMAs\n";
2580
2581     my $SIGMA = "\x{03A3}"; # CAPITAL
2582     my $Sigma = "\x{03C2}"; # SMALL FINAL
2583     my $sigma = "\x{03C3}"; # SMALL
2584
2585     my $S3 = "$SIGMA$Sigma$sigma";
2586
2587     print ":$S3:" =~ /:(($SIGMA)+):/i   && $1 eq $S3 && $2 eq $sigma ?
2588         "ok 829\n" : "not ok 829\n";
2589     print ":$S3:" =~ /:(($Sigma)+):/i   && $1 eq $S3 && $2 eq $sigma ?
2590         "ok 830\n" : "not ok 830\n";
2591     print ":$S3:" =~ /:(($sigma)+):/i   && $1 eq $S3 && $2 eq $sigma ?
2592         "ok 831\n" : "not ok 831\n";
2593
2594     print ":$S3:" =~ /:(([$SIGMA])+):/i && $1 eq $S3 && $2 eq $sigma ?
2595         "ok 832\n" : "not ok 832\n";
2596     print ":$S3:" =~ /:(([$Sigma])+):/i && $1 eq $S3 && $2 eq $sigma ?
2597         "ok 833\n" : "not ok 833\n";
2598     print ":$S3:" =~ /:(([$sigma])+):/i && $1 eq $S3 && $2 eq $sigma ?
2599         "ok 834\n" : "not ok 834\n";
2600 }
2601
2602 {
2603     print "# LATIN SMALL LETTER SHARP S\n";
2604
2605     use charnames ':full';
2606
2607     print "\N{LATIN SMALL LETTER SHARP S}" =~
2608         /\N{LATIN SMALL LETTER SHARP S}/    ? "ok 835\n" : "not ok 835\n";
2609
2610     print "\N{LATIN SMALL LETTER SHARP S}" =~
2611         /\N{LATIN SMALL LETTER SHARP S}/i   ? "ok 836\n" : "not ok 836\n";
2612
2613     print "\N{LATIN SMALL LETTER SHARP S}" =~
2614         /[\N{LATIN SMALL LETTER SHARP S}]/  ? "ok 837\n" : "not ok 837\n";
2615
2616     print "\N{LATIN SMALL LETTER SHARP S}" =~
2617         /[\N{LATIN SMALL LETTER SHARP S}]/i ? "ok 838\n" : "not ok 838\n";
2618
2619     print "ss" =~
2620         /\N{LATIN SMALL LETTER SHARP S}/i   ? "ok 839\n" : "not ok 839\n";
2621
2622     print "SS" =~
2623         /\N{LATIN SMALL LETTER SHARP S}/i   ? "ok 840\n" : "not ok 840\n";
2624
2625     print "ss" =~
2626         /[\N{LATIN SMALL LETTER SHARP S}]/i ? "ok 841\n" : "not ok 841\n";
2627
2628     print "SS" =~
2629         /[\N{LATIN SMALL LETTER SHARP S}]/i ? "ok 842\n" : "not ok 842\n";
2630
2631     print "\N{LATIN SMALL LETTER SHARP S}" =~ /ss/i ?
2632         "ok 843\n" : "not ok 843\n";
2633
2634     print "\N{LATIN SMALL LETTER SHARP S}" =~ /SS/i ?
2635         "ok 844\n" : "not ok 844\n";
2636 }
2637
2638 {
2639     print "# more whitespace: U+0085, U+2028, U+2029\n";
2640
2641     # U+0085 needs to be forced to be Unicode, the \x{100} does that.
2642     print "<\x{100}\x{0085}>" =~ /<\x{100}\s>/ ? "ok 845\n" : "not ok 845\n";
2643     print "<\x{2028}>" =~ /<\s>/ ? "ok 846\n" : "not ok 846\n";
2644     print "<\x{2029}>" =~ /<\s>/ ? "ok 847\n" : "not ok 847\n";
2645 }
2646
2647 {
2648     print "# . with /s should work on characters, as opposed to bytes\n";
2649
2650     my $s = "\x{e4}\x{100}";
2651
2652     # This is not expected to match: the point is that
2653     # neither should we get "Malformed UTF-8" warnings.
2654     print $s =~ /\G(.+?)\n/gcs ?
2655         "not ok 848\n" : "ok 848\n";
2656
2657     my @c;
2658
2659     while ($s =~ /\G(.)/gs) {
2660         push @c, $1;
2661     }
2662
2663     print join("", @c) eq $s ? "ok 849\n" : "not ok 849\n";
2664
2665     my $t1 = "Q003\n\n\x{e4}\x{f6}\n\nQ004\n\n\x{e7}"; # test only chars < 256
2666     my $r1 = "";
2667     while ($t1 =~ / \G ( .+? ) \n\s+ ( .+? ) ( $ | \n\s+ ) /xgcs) {
2668         $r1 .= $1 . $2;
2669     }
2670
2671     my $t2 = $t1 . "\x{100}"; # repeat with a larger char
2672     my $r2 = "";
2673     while ($t2 =~ / \G ( .+? ) \n\s+ ( .+? ) ( $ | \n\s+ ) /xgcs) {
2674         $r2 .= $1 . $2;
2675     }
2676     $r2 =~ s/\x{100}//;
2677     print $r1 eq $r2 ? "ok 850\n" : "not ok 850\n";
2678 }
2679
2680 {
2681     print "# Unicode lookbehind\n";
2682
2683     print "A\x{100}B"        =~ /(?<=A.)B/  ? "ok 851\n" : "not ok 851\n";
2684     print "A\x{200}\x{300}B" =~ /(?<=A..)B/ ? "ok 852\n" : "not ok 852\n";
2685     print "\x{400}AB"        =~ /(?<=\x{400}.)B/ ? "ok 853\n" : "not ok 853\n";
2686     print "\x{500\x{600}}B"  =~ /(?<=\x{500}.)B/ ? "ok 854\n" : "not ok 854\n";
2687 }
2688
2689 {
2690     print "# UTF-8 hash keys and /\$/\n";
2691     # http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2002-01/msg01327.html
2692
2693     my $u = "a\x{100}";
2694     my $v = substr($u,0,1);
2695     my $w = substr($u,1,1);
2696     my %u = ( $u => $u, $v => $v, $w => $w );
2697     my $i = 855; 
2698     for (keys %u) {
2699         my $m1 = /^\w*$/ ? 1 : 0;
2700         my $m2 = $u{$_}=~/^\w*$/ ? 1 : 0;
2701         print $m1 == $m2 ? "ok $i\n" : "not ok $i # $m1 $m2\n";
2702         $i++;
2703     }
2704 }
2705
2706 {
2707     print "# [ID 20020124.005]\n";
2708     # Fixed by #14795.
2709     my $i = 858;
2710     for my $char ("a", "\x{df}", "\x{100}"){
2711         $x = "$char b $char";
2712         $x =~ s{($char)}{
2713             "c" =~ /c/;
2714             "x";
2715         }ge;
2716         print substr($x,0,1) eq substr($x,-1,1) ?
2717             "ok $i\n" : "not ok $i # debug: $x\n";
2718         $i++;
2719    }
2720 }
2721
2722 {
2723     print "# SEGV in s/// and UTF-8\n";
2724     $s = "s#\x{100}" x 4;
2725     $s =~ s/[^\w]/ /g;
2726     print $s eq "s \x{100}" x 4 ? "ok 861\n" : "not ok 861\n";
2727 }
2728
2729 {
2730     print "# UTF-8 bug (maybe alreayd known?)\n";
2731     my $u;
2732
2733     $u = "foo";
2734     $u =~ s/./\x{100}/g;
2735     print $u eq "\x{100}\x{100}\x{100}" ? "ok 862\n" : "not ok 862\n";
2736
2737     $u = "foobar";
2738     $u =~ s/[ao]/\x{100}/g;
2739     print $u eq "f\x{100}\x{100}b\x{100}r" ? "ok 863\n" : "not ok 863\n";
2740
2741     $u =~ s/\x{100}/e/g;
2742     print $u eq "feeber" ? "ok 864\n" : "not ok 864\n";
2743 }
2744
2745 {
2746     print "# UTF-8 bug with s///\n";
2747     # check utf8/non-utf8 mixtures
2748     # try to force all float/anchored check combinations
2749     my $c = "\x{100}";
2750     my $test = 865;
2751     my $subst;
2752     for my $re (
2753         "xx.*$c", "x.*$c$c", "$c.*xx", "$c$c.*x", "xx.*(?=$c)", "(?=$c).*xx",
2754     ) {
2755         print "xxx" =~ /$re/ ? "not ok $test\n" : "ok $test\n";
2756         ++$test;
2757         print +($subst = "xxx") =~ s/$re// ? "not ok $test\n" : "ok $test\n";
2758         ++$test;
2759     }
2760     for my $re ("xx.*$c*", "$c*.*xx") {
2761         print "xxx" =~ /$re/ ? "ok $test\n" : "not ok $test\n";
2762         ++$test;
2763         ($subst = "xxx") =~ s/$re//;
2764         print $subst eq '' ? "ok $test\n" : "not ok $test\t# $subst\n";
2765         ++$test;
2766     }
2767     for my $re ("xxy*", "y*xx") {
2768         print "xx$c" =~ /$re/ ? "ok $test\n" : "not ok $test\n";
2769         ++$test;
2770         ($subst = "xx$c") =~ s/$re//;
2771         print $subst eq $c ? "ok $test\n" : "not ok $test\n";
2772         ++$test;
2773         print "xy$c" =~ /$re/ ? "not ok $test\n" : "ok $test\n";
2774         ++$test;
2775         print +($subst = "xy$c") =~ /$re/ ? "not ok $test\n" : "ok $test\n";
2776         ++$test;
2777     }
2778     for my $re ("xy$c*z", "x$c*yz") {
2779         print "xyz" =~ /$re/ ? "ok $test\n" : "not ok $test\n";
2780         ++$test;
2781         ($subst = "xyz") =~ s/$re//;
2782         print $subst eq '' ? "ok $test\n" : "not ok $test\n";
2783         ++$test;
2784     }
2785 }
2786
2787 {
2788     print "# qr/.../x\n";
2789     my $test = 893;
2790
2791     my $R = qr/ A B C # D E/x;
2792
2793     print eval {"ABCDE" =~ $R} ? "ok $test\n" : "not ok $test\n";
2794     $test++;
2795
2796     print eval {"ABCDE" =~ m/$R/} ? "ok $test\n" : "not ok $test\n";
2797     $test++;
2798
2799     print eval {"ABCDE" =~ m/($R)/} ? "ok $test\n" : "not ok $test\n";
2800     $test++;
2801 }
2802
2803 {
2804     print "# illegal Unicode properties\n";
2805     my $test = 896;
2806
2807     print eval { "a" =~ /\pq / }      ? "not ok $test\n" : "ok $test\n";
2808     $test++;
2809
2810     print eval { "a" =~ /\p{qrst} / } ? "not ok $test\n" : "ok $test\n";
2811     $test++;
2812 }
2813
2814 {
2815     print "# [ID 20020412.005] wrong pmop flags checked when empty pattern\n";
2816     # requires reuse of last successful pattern
2817     my $test = 898;
2818     $test =~ /\d/;
2819     for (0 .. 1) {
2820         my $match = ?? + 0;
2821         if ($match != $_) {
2822             print "ok $test\n";
2823         } else {
2824             printf "not ok %s\t# 'match once' %s on %s iteration\n", $test,
2825                     $match ? 'succeeded' : 'failed', $_ ? 'second' : 'first';
2826         }
2827         ++$test;
2828     }
2829     $test =~ /(\d)/;
2830     my $result = join '', $test =~ //g;
2831     if ($result eq $test) {
2832         print "ok $test\n";
2833     } else {
2834         printf "not ok %s\t# expected '%s', got '%s'\n", $test, $test, $result;
2835     }
2836     ++$test;
2837 }
2838
2839 print "# user-defined character properties\n";
2840
2841 sub InKana1 {
2842     return <<'END';
2843 3040    309F
2844 30A0    30FF
2845 END
2846 }
2847
2848 sub InKana2 {
2849     return <<'END';
2850 +utf8::InHiragana
2851 +utf8::InKatakana
2852 END
2853 }
2854
2855 sub InKana3 {
2856     return <<'END';
2857 +utf8::InHiragana
2858 +utf8::InKatakana
2859 -utf8::IsCn
2860 END
2861 }
2862
2863 sub InNotKana {
2864     return <<'END';
2865 !utf8::InHiragana
2866 -utf8::InKatakana
2867 +utf8::IsCn
2868 END
2869 }
2870
2871 $test = 901;
2872
2873 print "\x{3040}" =~ /\p{InKana1}/ ? "ok $test\n" : "not ok $test\n"; $test++;
2874 print "\x{303F}" =~ /\P{InKana1}/ ? "ok $test\n" : "not ok $test\n"; $test++;
2875
2876 print "\x{3040}" =~ /\p{InKana2}/ ? "ok $test\n" : "not ok $test\n"; $test++;
2877 print "\x{303F}" =~ /\P{InKana2}/ ? "ok $test\n" : "not ok $test\n"; $test++;
2878
2879 print "\x{3041}" =~ /\p{InKana3}/ ? "ok $test\n" : "not ok $test\n"; $test++;
2880 print "\x{3040}" =~ /\P{InKana3}/ ? "ok $test\n" : "not ok $test\n"; $test++;
2881
2882 print "\x{3040}" =~ /\p{InNotKana}/ ? "ok $test\n" : "not ok $test\n"; $test++;
2883 print "\x{3041}" =~ /\P{InNotKana}/ ? "ok $test\n" : "not ok $test\n"; $test++;
2884
2885 sub InConsonant { # Not EBCDIC-aware.
2886     return <<EOF;
2887 0061    007f
2888 -0061
2889 -0065
2890 -0069
2891 -006f
2892 -0075
2893 EOF
2894 }
2895
2896 print "d" =~ /\p{InConsonant}/ ? "ok $test\n" : "not ok $test\n"; $test++;
2897 print "e" =~ /\P{InConsonant}/ ? "ok $test\n" : "not ok $test\n"; $test++;
2898
2899 {
2900     print "# [ID 20020630.002] utf8 regex only matches 32k\n";
2901     $test = 911;
2902     for ([ 'byte', "\x{ff}" ], [ 'utf8', "\x{1ff}" ]) {
2903         my($type, $char) = @$_;
2904         for my $len (32000, 32768, 33000) {
2905             my $s = $char . "f" x $len;
2906             my $r = $s =~ /$char([f]*)/gc;
2907             print $r ? "ok $test\n" : "not ok $test\t# <$type x $len> fail\n";
2908             ++$test;
2909             print +(!$r or pos($s) == $len + 1) ? "ok $test\n"
2910                 : "not ok $test\t# <$type x $len> pos @{[ pos($s) ]}\n";
2911             ++$test;
2912         }
2913     }
2914 }
2915
2916 $test = 923;
2917
2918 $a = bless qr/foo/, 'Foo';
2919 print(('goodfood' =~ $a ? '' : 'not '),
2920         "ok $test\t# reblessed qr// matches\n");
2921 ++$test;
2922
2923 print(($a eq '(?-xism:foo)' ? '' : 'not '),
2924         "ok $test\t# reblessed qr// stringizes\n");
2925 ++$test;
2926
2927 $x = "\x{3fe}";
2928 $z=$y = "\317\276"; # $y is byte representation of $x
2929
2930 $a = qr/$x/;
2931 print(($x =~ $a ? '' : 'not '), "ok $test - utf8 interpolation in qr//\n");
2932 ++$test;
2933
2934 print(("a$a" =~ $x ? '' : 'not '),
2935       "ok $test - stringifed qr// preserves utf8\n");
2936 ++$test;
2937
2938 print(("a$x" =~ /^a$a\z/ ? '' : 'not '),
2939       "ok $test - interpolated qr// preserves utf8\n");
2940 ++$test;
2941
2942 print(("a$x" =~ /^a(??{$a})\z/ ? '' : 'not '),
2943       "ok $test - postponed interpolation of qr// preserves utf8\n");
2944 ++$test;
2945
2946 print((length(qr/##/x) == 12 ? '' : 'not '),
2947       "ok $test - ## in qr// doesn't corrupt memory [perl #17776]\n");
2948 ++$test;
2949
2950 { use re 'eval';
2951
2952 print(("$x$x" =~ /^$x(??{$x})\z/ ? '' : 'not '),
2953       "ok $test - postponed utf8 string in utf8 re matches utf8\n");
2954 ++$test;
2955
2956 print(("$y$x" =~ /^$y(??{$x})\z/ ? '' : 'not '),
2957       "ok $test - postponed utf8 string in non-utf8 re matches utf8\n");
2958 ++$test;
2959
2960 print(("$y$x" !~ /^$y(??{$y})\z/ ? '' : 'not '),
2961       "ok $test - postponed non-utf8 string in non-utf8 re doesn't match utf8\n");
2962 ++$test;
2963
2964 print(("$x$x" !~ /^$x(??{$y})\z/ ? '' : 'not '),
2965       "ok $test - postponed non-utf8 string in utf8 re doesn't match utf8\n");
2966 ++$test;
2967
2968 print(("$y$y" =~ /^$y(??{$y})\z/ ? '' : 'not '),
2969       "ok $test - postponed non-utf8 string in non-utf8 re matches non-utf8\n");
2970 ++$test;
2971
2972 print(("$x$y" =~ /^$x(??{$y})\z/ ? '' : 'not '),
2973       "ok $test - postponed non-utf8 string in utf8 re matches non-utf8\n");
2974 ++$test;
2975 $y = $z; # reset $y after upgrade
2976
2977 print(("$x$y" !~ /^$x(??{$x})\z/ ? '' : 'not '),
2978       "ok $test - postponed utf8 string in utf8 re doesn't match non-utf8\n");
2979 ++$test;
2980 $y = $z; # reset $y after upgrade
2981
2982 print(("$y$y" !~ /^$y(??{$x})\z/ ? '' : 'not '),
2983       "ok $test - postponed utf8 string in non-utf8 re doesn't match non-utf8\n");
2984 ++$test;
2985
2986 } # no re 'eval'
2987
2988 print "# more user-defined character properties\n";
2989
2990 sub IsSyriac1 {
2991     return <<'END';
2992 0712    072C
2993 0730    074A
2994 END
2995 }
2996
2997 ok("\x{0712}" =~ /\p{IsSyriac1}/, '\x{0712}, \p{IsSyriac1}');
2998 ok("\x{072F}" =~ /\P{IsSyriac1}/, '\x{072F}, \P{IsSyriac1}');
2999
3000 sub Syriac1 {
3001     return <<'END';
3002 0712    072C
3003 0730    074A
3004 END
3005 }
3006
3007 ok("\x{0712}" =~ /\p{Syriac1}/, '\x{0712}, \p{Syriac1}');
3008 ok("\x{072F}" =~ /\P{Syriac1}/, '\x{072F}, \p{Syriac1}');
3009
3010 print "# user-defined character properties may lack \\n at the end\n";
3011 sub InGreekSmall   { return "03B1\t03C9" }
3012 sub InGreekCapital { return "0391\t03A9\n-03A2" }
3013
3014 ok("\x{03C0}" =~ /\p{InGreekSmall}/,   "Small pi");
3015 ok("\x{03C2}" =~ /\p{InGreekSmall}/,   "Final sigma");
3016 ok("\x{03A0}" =~ /\p{InGreekCapital}/, "Capital PI");
3017 ok("\x{03A2}" =~ /\P{InGreekCapital}/, "Reserved");
3018
3019 sub AsciiHexAndDash {
3020     return <<'END';
3021 +utf8::ASCII_Hex_Digit
3022 +utf8::Dash
3023 END
3024 }
3025
3026 ok("-" =~ /\p{Dash}/,            "'-' is Dash");
3027 ok("A" =~ /\p{ASCII_Hex_Digit}/, "'A' is ASCII_Hex_Digit");
3028 ok("-" =~ /\p{AsciiHexAndDash}/, "'-' is AsciiHexAndDash");
3029 ok("A" =~ /\p{AsciiHexAndDash}/, "'A' is AsciiHexAndDash");
3030
3031 {
3032     print "# Change #18179\n";
3033     # previously failed with "panic: end_shift
3034     my $s = "\x{100}" x 5;
3035     my $ok = $s =~ /(\x{100}{4})/;
3036     my($ord, $len) = (ord $1, length $1);
3037     print +($ok && $ord == 0x100 && $len == 4)
3038             ? "ok $test\n" : "not ok $test\t# $ok/$ord/$len\n";
3039     ++$test;
3040 }
3041
3042 {
3043     print "# [perl #15763]\n";
3044
3045     $a = "x\x{100}";
3046     chop $a; # but leaves the UTF-8 flag
3047     $a .= "y"; # 1 byte before "y"
3048
3049     ok($a =~ /^\C/,      'match one \C on 1-byte UTF-8');
3050     ok($a =~ /^\C{1}/,   'match \C{1}');
3051
3052     ok($a =~ /^\Cy/,      'match \Cy');
3053     ok($a =~ /^\C{1}y/,   'match \C{1}y');
3054
3055     $a = "\x{100}y"; # 2 bytes before "y"
3056
3057     ok($a =~ /^\C/,       'match one \C on 2-byte UTF-8');
3058     ok($a =~ /^\C{1}/,    'match \C{1}');
3059     ok($a =~ /^\C\C/,     'match two \C');
3060     ok($a =~ /^\C{2}/,    'match \C{2}');
3061
3062     ok($a =~ /^\C\C\C/,    'match three \C on 2-byte UTF-8 and a byte');
3063     ok($a =~ /^\C{3}/,     'match \C{3}');
3064
3065     ok($a =~ /^\C\Cy/,     'match two \C');
3066     ok($a =~ /^\C{2}y/,    'match \C{2}');
3067
3068     ok($a !~ /^\C\C\Cy/,    q{don't match three \Cy});
3069     ok($a !~ /^\C{2}\Cy/,   q{don't match \C{3}y});
3070
3071     $a = "\x{1000}y"; # 3 bytes before "y"
3072
3073     ok($a =~ /^\C/,         'match one \C on three-byte UTF-8');
3074     ok($a =~ /^\C{1}/,      'match \C{1}');
3075     ok($a =~ /^\C\C/,       'match two \C');
3076     ok($a =~ /^\C{2}/,      'match \C{2}');
3077     ok($a =~ /^\C\C\C/,     'match three \C');
3078     ok($a =~ /^\C{3}/,      'match \C{3}');
3079
3080     ok($a =~ /^\C\C\C\C/,   'match four \C on three-byte UTF-8 and a byte');
3081     ok($a =~ /^\C{4}/,      'match \C{4}');
3082
3083     ok($a =~ /^\C\C\Cy/,    'match three \Cy');
3084     ok($a =~ /^\C{3}y/,     'match \C{3}y');
3085
3086     ok($a !~ /^\C\C\C\C\y/, q{don't match four \Cy});
3087     ok($a !~ /^\C{4}y/,     q{don't match \C{4}y});
3088 }
3089
3090 $_ = 'aaaaaaaaaa';
3091 utf8::upgrade($_); chop $_; $\="\n";
3092 ok(/[^\s]+/, "m/[^\s]/ utf8");
3093 ok(/[^\d]+/, "m/[^\d]/ utf8");
3094 ok(($a = $_, $_ =~ s/[^\s]+/./g), "s/[^\s]/ utf8");
3095 ok(($a = $_, $a =~ s/[^\d]+/./g), "s/[^\s]/ utf8");
3096
3097 ok("\x{100}" =~ /\x{100}/, "[perl #15397]");
3098 ok("\x{100}" =~ /(\x{100})/, "[perl #15397]");
3099 ok("\x{100}" =~ /(\x{100}){1}/, "[perl #15397]");
3100 ok("\x{100}\x{100}" =~ /(\x{100}){2}/, "[perl #15397]");
3101 ok("\x{100}\x{100}" =~ /(\x{100})(\x{100})/, "[perl #15397]");
3102
3103 $x = "CD";
3104 $x =~ /(AB)*?CD/;
3105 ok(!defined $1, "[perl #7471]");
3106
3107 $x = "CD";
3108 $x =~ /(AB)*CD/;
3109 ok(!defined $1, "[perl #7471]");
3110
3111 $pattern = "^(b+?|a){1,2}c";
3112 ok("bac"    =~ /$pattern/ && $1 eq 'a', "[perl #3547]");
3113 ok("bbac"   =~ /$pattern/ && $1 eq 'a', "[perl #3547]");
3114 ok("bbbac"  =~ /$pattern/ && $1 eq 'a', "[perl #3547]");
3115 ok("bbbbac" =~ /$pattern/ && $1 eq 'a', "[perl #3547]");
3116
3117 {
3118     # [perl #18232]
3119     "\x{100}" =~ /(.)/;
3120     ok( $1 eq "\x{100}", '$1 is utf-8 [perl #18232]' );
3121     { 'a' =~ /./; }
3122     ok( $1 eq "\x{100}", '$1 is still utf-8' );
3123     ok( $1 ne "\xC4\x80", '$1 is not non-utf-8' );
3124 }
3125
3126 {
3127     use utf8;
3128     my $attr = 'Name-1' ;
3129
3130     my $NormalChar          = qr/[\p{IsDigit}\p{IsLower}\p{IsUpper}]/;
3131     my $NormalWord          = qr/${NormalChar}+?/;
3132     my $PredNameHyphen      = qr/^${NormalWord}(\-${NormalWord})*?$/;
3133
3134     $attr =~ /^$/;
3135     ok( $attr =~ $PredNameHyphen, "[perl #19767] original test" );
3136 }
3137
3138 {
3139     use utf8;
3140     "a" =~ m/[b]/;
3141     ok ( "0" =~ /\p{N}+\z/, "[perl #19767] variant test" );
3142 }
3143
3144 {
3145
3146     $p = 1;
3147     foreach (1,2,3,4) {
3148             $p++ if /(??{ $p })/
3149     }
3150     ok ($p == 5, "[perl #20683] (??{ }) returns stale values");
3151     { package P; $a=1; sub TIESCALAR { bless[] } sub FETCH { $a++ } }
3152     tie $p, P;
3153     foreach (1,2,3,4) {
3154             /(??{ $p })/
3155     }
3156     ok ( $p == 5, "(??{ }) returns stale values");
3157 }
3158
3159 {
3160   # Subject: Odd regexp behavior
3161   # From: Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>
3162   # Date: Wed, 26 Feb 2003 16:53:12 +0000
3163   # Message-Id: <E18o4nw-0008Ly-00@wisbech.cl.cam.ac.uk>
3164   # To: perl-unicode@perl.org
3165     
3166   $x = "\x{2019}\nk"; $x =~ s/(\S)\n(\S)/$1 $2/sg;
3167   ok($x eq "\x{2019} k", "Markus Kuhn 2003-02-26");
3168
3169   $x = "b\nk"; $x =~ s/(\S)\n(\S)/$1 $2/sg;
3170   ok($x eq "b k", "Markus Kuhn 2003-02-26");
3171
3172   ok("\x{2019}" =~ /\S/, "Markus Kuhn 2003-02-26");
3173 }
3174
3175 {
3176     my $i;
3177     ok('-1-3-5-' eq join('', split /((??{$i++}))/, '-1-3-5-'),
3178         "[perl #21411] (??{ .. }) corrupts split's stack");
3179     split /(?{'WOW'})/, 'abc';
3180     ok('a|b|c' eq join ('|', @_),
3181        "[perl #21411] (?{ .. }) version of the above");
3182 }
3183
3184 {
3185     # XXX DAPM 13-Apr-06. Recursive split is still broken. It's only luck it
3186     # hasn't been crashing. Disable this test until it is fixed properly.
3187     # XXX also check what it returns rather than just doing ok(1,...)
3188     # split /(?{ split "" })/, "abc";
3189     ok(1,'cache_re & "(?{": it dumps core in 5.6.1 & 5.8.0');
3190 }
3191
3192 {
3193     ok("\x{100}\n" =~ /\x{100}\n$/, "UTF8 length cache and fbm_compile");  
3194 }
3195
3196 {
3197     package Str;
3198     use overload q/""/ => sub { ${$_[0]}; };
3199     sub new { my ($c, $v) = @_; bless \$v, $c; }
3200
3201     package main;
3202     $_ = Str->new("a\x{100}/\x{100}b");
3203     ok(join(":", /\b(.)\x{100}/g) eq "a:/", "re_intuit_start and PL_bostr");
3204 }
3205
3206 {
3207     $_ = "code:   'x' { '...' }\n"; study;
3208     my @x; push @x, $& while m/'[^\']*'/gx;
3209     ok(join(":", @x) eq "'x':'...'",
3210        "[perl #17757] Parse::RecDescent triggers infinite loop");
3211 }
3212
3213 {
3214     my $re = qq/^([^X]*)X/;
3215     utf8::upgrade($re);
3216     ok("\x{100}X" =~ /$re/, "S_cl_and ANYOF_UNICODE & ANYOF_INVERTED");
3217 }
3218
3219 # bug #22354
3220 sub func ($) {
3221     ok( "a\nb" !~ /^b/, $_[0] );
3222     ok( "a\nb" =~ /^b/m, "$_[0] - with /m" );
3223 }
3224 func "standalone";
3225 $_ = "x"; s/x/func "in subst"/e;
3226 $_ = "x"; s/x/func "in multiline subst"/em;
3227 #$_ = "x"; /x(?{func "in regexp"})/;
3228 #$_ = "x"; /x(?{func "in multiline regexp"})/m;
3229
3230 # bug #19049
3231 $_="abcdef\n";
3232 @x = m/./g;
3233 ok("abcde" eq "$`", '# TODO #19049 - global match not setting $`');
3234
3235 ok("123\x{100}" =~ /^.*1.*23\x{100}$/, 'uft8 + multiple floating substr');
3236
3237 # LATIN SMALL/CAPITAL LETTER A WITH MACRON
3238 ok("  \x{101}" =~ qr/\x{100}/i,
3239    "<20030808193656.5109.1@llama.ni-s.u-net.com>");
3240
3241 # LATIN SMALL/CAPITAL LETTER A WITH RING BELOW
3242 ok("  \x{1E01}" =~ qr/\x{1E00}/i,
3243    "<20030808193656.5109.1@llama.ni-s.u-net.com>");
3244
3245 # DESERET SMALL/CAPITAL LETTER LONG I
3246 ok("  \x{10428}" =~ qr/\x{10400}/i,
3247    "<20030808193656.5109.1@llama.ni-s.u-net.com>");
3248
3249 # LATIN SMALL/CAPITAL LETTER A WITH RING BELOW + 'X'
3250 ok("  \x{1E01}x" =~ qr/\x{1E00}X/i,
3251    "<20030808193656.5109.1@llama.ni-s.u-net.com>");
3252
3253 {
3254     # [perl #23769] Unicode regex broken on simple example
3255     # regrepeat() didn't handle UTF-8 EXACT case right.
3256
3257     my $s = "\x{a0}\x{a0}\x{a0}\x{100}"; chop $s;
3258
3259     ok($s =~ /\x{a0}/,       "[perl #23769]");
3260     ok($s =~ /\x{a0}+/,      "[perl #23769]");
3261     ok($s =~ /\x{a0}\x{a0}/, "[perl #23769]");
3262
3263     ok("aaa\x{100}" =~ /(a+)/, "[perl #23769] easy invariant");
3264     ok($1 eq "aaa", "[perl #23769]");
3265
3266     ok("\xa0\xa0\xa0\x{100}" =~ /(\xa0+)/, "[perl #23769] regrepeat invariant");
3267     ok($1 eq "\xa0\xa0\xa0", "[perl #23769]");
3268
3269     ok("ababab\x{100}  " =~ /((?:ab)+)/, "[perl #23769] hard invariant");
3270     ok($1 eq "ababab", "[perl #23769]");
3271
3272     ok("\xa0\xa1\xa0\xa1\xa0\xa1\x{100}" =~ /((?:\xa0\xa1)+)/, "[perl #23769] hard variant");
3273     ok($1 eq "\xa0\xa1\xa0\xa1\xa0\xa1", "[perl #23769]");
3274
3275     ok("aaa\x{100}     " =~ /(a+?)/, "[perl #23769] easy invariant");
3276     ok($1 eq "a", "[perl #23769]");
3277
3278     ok("\xa0\xa0\xa0\x{100}    " =~ /(\xa0+?)/, "[perl #23769] regrepeat variant");
3279     ok($1 eq "\xa0", "[perl #23769]");
3280
3281     ok("ababab\x{100}  " =~ /((?:ab)+?)/, "[perl #23769] hard invariant");
3282     ok($1 eq "ab", "[perl #23769]");
3283
3284     ok("\xa0\xa1\xa0\xa1\xa0\xa1\x{100}" =~ /((?:\xa0\xa1)+?)/, "[perl #23769] hard variant");
3285     ok($1 eq "\xa0\xa1", "[perl #23769]");
3286
3287     ok("\xc4\xc4\xc4" !~ /(\x{100}+)/, "[perl #23769] don't match first byte of utf8 representation");
3288     ok("\xc4\xc4\xc4" !~ /(\x{100}+?)/, "[perl #23769] don't match first byte of utf8 representation");
3289 }
3290
3291 for (120 .. 130) {
3292     my $head = 'x' x $_;
3293     for my $tail ('\x{0061}', '\x{1234}') {
3294         ok(
3295             eval qq{ "$head$tail" =~ /$head$tail/ },
3296             '\x{...} misparsed in regexp near 127 char EXACT limit'
3297         );
3298     }
3299 }
3300
3301 # perl #25269: panic: pp_match start/end pointers
3302 ok("a-bc" eq eval {
3303         my($x, $y) = "bca" =~ /^(?=.*(a)).*(bc)/;
3304         "$x-$y";
3305 }, 'captures can move backwards in string');
3306
3307 # perl #27940: \cA not recognized in character classes
3308 ok("a\cAb" =~ /\cA/, '\cA in pattern');
3309 ok("a\cAb" =~ /[\cA]/, '\cA in character class');
3310 ok("a\cAb" =~ /[\cA-\cB]/, '\cA in character class range');
3311 ok("abc" =~ /[^\cA-\cB]/, '\cA in negated character class range');
3312 ok("a\cBb" =~ /[\cA-\cC]/, '\cB in character class range');
3313 ok("a\cCbc" =~ /[^\cA-\cB]/, '\cC in negated character class range');
3314 ok("a\cAb" =~ /(??{"\cA"})/, '\cA 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
3394
3395
3396 {
3397     my @normal=qw(these are some normal words);
3398     my $psycho=join "|",@normal,map chr $_,255..20000;
3399     ok(('these'=~/($psycho)/) && $1 eq 'these','Pyscho');
3400 }
3401
3402 # [perl #36207] mixed utf8 / latin-1 and case folding
3403
3404 {
3405     my $utf8 = "\xe9\x{100}"; chop $utf8;
3406     my $latin1 = "\xe9";
3407
3408     ok($utf8 =~ /\xe9/i, "utf8/latin");
3409     ok($utf8 =~ /$latin1/i, "utf8/latin runtime");
3410     ok($utf8 =~ /(abc|\xe9)/i, "utf8/latin trie");
3411     ok($utf8 =~ /(abc|$latin1)/i, "utf8/latin trie runtime");
3412
3413     ok("\xe9" =~ /$utf8/i, "# TODO latin/utf8");
3414     ok("\xe9" =~ /(abc|$utf8)/i, "# latin/utf8 trie");
3415     ok($latin1 =~ /$utf8/i, "# TODO latin/utf8 runtime");
3416     ok($latin1 =~ /(abc|$utf8)/i, "# latin/utf8 trie runtime");
3417 }
3418
3419 # [perl #37038] Global regular matches generate invalid pointers
3420
3421 {
3422     my $s = "abcd";
3423     $s =~ /(..)(..)/g;
3424     $s = $1;
3425     $s = $2;
3426     ok($s eq 'cd',
3427        "# assigning to original string should not corrupt match vars");
3428 }
3429
3430 {
3431     package wooosh;
3432     sub gloople {
3433       "!";
3434     }
3435     package main;
3436     
3437     my $aeek = bless {}, 'wooosh';
3438     eval {$aeek->gloople() =~ /(.)/g;};
3439     ok($@ eq "", "//g match against return value of sub") or print "# $@\n";
3440 }
3441
3442 {
3443     sub gloople {
3444       "!";
3445     }
3446     eval {gloople() =~ /(.)/g;};
3447     ok($@ eq "", "# 26410 didn't affect sub calls for some reason")
3448         or print "# $@\n";
3449 }
3450
3451 {
3452     package lv;
3453     $var = "abc";
3454     sub variable : lvalue { $var }
3455
3456     package main;
3457     my $o = bless [], "lv";
3458     my $f = "";
3459     eval { for (1..2) { $f .= $1 if $o->variable =~ /(.)/g } };
3460     ok($f eq "ab", "pos retained between calls # TODO") or print "# $@\n";
3461 }
3462
3463 {
3464     $var = "abc";
3465     sub variable : lvalue { $var }
3466
3467     my $f = "";
3468     eval { for (1..2) { $f .= $1 if variable() =~ /(.)/g } };
3469     ok($f eq "ab", "pos retained between calls # TODO") or print "# $@\n";
3470 }
3471
3472 # [perl #37836] Simple Regex causes SEGV when run on specific data
3473 if ($ordA == 193) {
3474     print "ok $test # Skip: in EBCDIC\n"; $test++;
3475 } else {
3476     no warnings 'utf8';
3477     $_ = pack('U0C2', 0xa2, 0xf8); # ill-formed UTF-8
3478     my $ret = 0;
3479     eval { $ret = s/[\0]+//g };
3480     ok($ret == 0, "ill-formed UTF-8 doesn't match NUL in class");
3481 }
3482
3483 { # [perl #38293] chr(65535) should be allowed in regexes
3484     no warnings 'utf8'; # to allow non-characters
3485     my($c, $r, $s);
3486
3487     $c = chr 0xffff;
3488     $c =~ s/$c//g;
3489     ok($c eq "", "U+FFFF, parsed as atom");
3490
3491     $c = chr 0xffff;
3492     $r = "\\$c";
3493     $c =~ s/$r//g;
3494     ok($c eq "", "U+FFFF backslashed, parsed as atom");
3495
3496     $c = chr 0xffff;
3497     $c =~ s/[$c]//g;
3498     ok($c eq "", "U+FFFF, parsed in class");
3499
3500     $c = chr 0xffff;
3501     $r = "[\\$c]";
3502     $c =~ s/$r//g;
3503     ok($c eq "", "U+FFFF backslashed, parsed in class");
3504
3505     $s = "A\x{ffff}B";
3506     $s =~ s/\x{ffff}//i;
3507     ok($s eq "AB", "U+FFFF, EXACTF");
3508
3509     $s = "\x{ffff}A";
3510     $s =~ s/\bA//;
3511     ok($s eq "\x{ffff}", "U+FFFF, BOUND");
3512
3513     $s = "\x{ffff}!";
3514     $s =~ s/\B!//;
3515     ok($s eq "\x{ffff}", "U+FFFF, NBOUND");
3516 } # non-characters end
3517
3518 {
3519     # https://rt.perl.org/rt3/Ticket/Display.html?id=39583
3520     
3521     # The printing characters
3522     my @chars = ("A".."Z");
3523     my $delim = ",";
3524     my $size = 32771 - 4;
3525     my $test = '';
3526
3527     # create some random junk. Inefficient, but it works.
3528     for ($i = 0 ; $i < $size ; $i++) {
3529         $test .= $chars[int(rand(@chars))];
3530     }
3531
3532     $test .= ($delim x 4);
3533     my $res;
3534     my $matched;
3535     if ($test =~ s/^(.*?)${delim}{4}//s) {
3536         $res = $1;
3537         $matched=1;
3538     } 
3539     ok($matched,'pattern matches');
3540     ok(length($test)==0,"Empty string");
3541     ok(defined($res) && length($res)==$size,"\$1 is correct size");
3542 }
3543
3544 # Keep the following test last -- it may crash perl
3545
3546 ok(("a" x (2**15 - 10)) =~ /^()(a|bb)*$/, "Recursive stack cracker: #24274")
3547     or print "# Unexpected outcome: should pass or crash perl\n";
3548
3549 # last test 1211