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