This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add test for \p{} failing silently
[perl5.git] / t / re / pat.t
CommitLineData
8d063cd8 1#!./perl
8d37f932
DD
2#
3# This is a home for regular expression tests that don't fit into
67a2b8c6 4# the format supported by re/regexp.t. If you want to add a test
9fa196e1 5# that does fit that format, add it to re/re_tests, not here.
8d063cd8 6
84281c31
A
7use strict;
8use warnings;
9use 5.010;
10
84281c31
A
11sub run_tests;
12
9133bbab 13$| = 1;
3568d838 14
8d37f932 15
e4d48cc9
GS
16BEGIN {
17 chdir 't' if -d 't';
cc7e6304 18 @INC = ('../lib','.','../ext/re');
569f7fc5 19 require Config; import Config;
6f4e0180 20 require './test.pl';
e4d48cc9 21}
84281c31 22
fa542f55 23plan tests => 739; # Update this when adding/deleting tests.
b7a35066 24
9d45b377 25run_tests() unless caller;
b7a35066 26
84281c31
A
27#
28# Tests start here.
29#
30sub run_tests {
0ef3e39e 31
84281c31 32 {
84281c31 33 my $x = "abc\ndef\n";
5895685f 34 (my $x_pretty = $x) =~ s/\n/\\n/g;
fd291da9 35
5895685f
NC
36 ok $x =~ /^abc/, qq ["$x_pretty" =~ /^abc/];
37 ok $x !~ /^def/, qq ["$x_pretty" !~ /^def/];
fd291da9 38
84281c31 39 # used to be a test for $*
5895685f 40 ok $x =~ /^def/m, qq ["$x_pretty" =~ /^def/m];
fd291da9 41
b33825c4
NC
42 ok(!($x =~ /^xxx/), qq ["$x_pretty" =~ /^xxx/]);
43 ok(!($x !~ /^abc/), qq ["$x_pretty" !~ /^abc/]);
fd291da9 44
5895685f 45 ok $x =~ /def/, qq ["$x_pretty" =~ /def/];
b33825c4 46 ok(!($x !~ /def/), qq ["$x_pretty" !~ /def/]);
4765795a 47
5895685f 48 ok $x !~ /.def/, qq ["$x_pretty" !~ /.def/];
b33825c4 49 ok(!($x =~ /.def/), qq ["$x_pretty" =~ /.def/]);
4765795a 50
5895685f 51 ok $x =~ /\ndef/, qq ["$x_pretty" =~ /\\ndef/];
b33825c4 52 ok(!($x !~ /\ndef/), qq ["$x_pretty" !~ /\\ndef/]);
84281c31 53 }
4765795a 54
84281c31
A
55 {
56 $_ = '123';
57 ok /^([0-9][0-9]*)/, qq [\$_ = '$_'; /^([0-9][0-9]*)/];
58 }
f9969324 59
84281c31
A
60 {
61 $_ = 'aaabbbccc';
62 ok /(a*b*)(c*)/ && $1 eq 'aaabbb' && $2 eq 'ccc',
63 qq [\$_ = '$_'; /(a*b*)(c*)/];
64 ok /(a+b+c+)/ && $1 eq 'aaabbbccc', qq [\$_ = '$_'; /(a+b+c+)/];
b33825c4 65 unlike($_, qr/a+b?c+/, qq [\$_ = '$_'; /a+b?c+/]);
84281c31
A
66
67 $_ = 'aaabccc';
68 ok /a+b?c+/, qq [\$_ = '$_'; /a+b?c+/];
69 ok /a*b?c*/, qq [\$_ = '$_'; /a*b?c*/];
70
71 $_ = 'aaaccc';
72 ok /a*b?c*/, qq [\$_ = '$_'; /a*b?c*/];
b33825c4 73 unlike($_, qr/a*b+c*/, qq [\$_ = '$_'; /a*b+c*/]);
84281c31
A
74
75 $_ = 'abcdef';
76 ok /bcd|xyz/, qq [\$_ = '$_'; /bcd|xyz/];
77 ok /xyz|bcd/, qq [\$_ = '$_'; /xyz|bcd/];
78 ok m|bc/*d|, qq [\$_ = '$_'; m|bc/*d|];
79 ok /^$_$/, qq [\$_ = '$_'; /^\$_\$/];
4765795a 80 }
4765795a 81
84281c31
A
82 {
83 # used to be a test for $*
5895685f 84 ok "ab\ncd\n" =~ /^cd/m, q ["ab\ncd\n" =~ /^cd/m];
84281c31 85 }
4765795a 86
84281c31
A
87 {
88 our %XXX = map {($_ => $_)} 123, 234, 345;
89
90 our @XXX = ('ok 1','not ok 1', 'ok 2','not ok 2','not ok 3');
91 while ($_ = shift(@XXX)) {
b33825c4 92 my $e = index ($_, 'not') >= 0 ? '' : 1;
725a61d7 93 my $r = m?(.*)?;
b33825c4 94 is($r, $e, "?(.*)?");
84281c31
A
95 /not/ && reset;
96 if (/not ok 2/) {
97 if ($^O eq 'VMS') {
98 $_ = shift(@XXX);
99 }
100 else {
101 reset 'X';
102 }
103 }
104 }
4765795a 105
84281c31
A
106 SKIP: {
107 if ($^O eq 'VMS') {
108 skip "Reset 'X'", 1;
109 }
110 ok !keys %XXX, "%XXX is empty";
111 }
4765795a 112
84281c31 113 }
4765795a 114
84281c31 115 {
4f890a30 116 my $message = "Test empty pattern";
84281c31
A
117 my $xyz = 'xyz';
118 my $cde = 'cde';
119
120 $cde =~ /[^ab]*/;
121 $xyz =~ //;
4f890a30 122 is($&, $xyz, $message);
84281c31
A
123
124 my $foo = '[^ab]*';
125 $cde =~ /$foo/;
126 $xyz =~ //;
4f890a30 127 is($&, $xyz, $message);
84281c31
A
128
129 $cde =~ /$foo/;
130 my $null;
131 no warnings 'uninitialized';
132 $xyz =~ /$null/;
4f890a30 133 is($&, $xyz, $message);
84281c31
A
134
135 $null = "";
136 $xyz =~ /$null/;
4f890a30 137 is($&, $xyz, $message);
84281c31 138 }
4765795a 139
84281c31 140 {
4f890a30 141 my $message = q !Check $`, $&, $'!;
84281c31 142 $_ = 'abcdefghi';
0f289c68 143 /def/; # optimized up to cmd
4f890a30 144 is("$`:$&:$'", 'abc:def:ghi', $message);
4765795a 145
84281c31 146 no warnings 'void';
0f289c68 147 /cde/ + 0; # optimized only to spat
4f890a30 148 is("$`:$&:$'", 'ab:cde:fghi', $message);
4765795a 149
0f289c68 150 /[d][e][f]/; # not optimized
4f890a30 151 is("$`:$&:$'", 'abc:def:ghi', $message);
84281c31 152 }
4765795a 153
84281c31
A
154 {
155 $_ = 'now is the {time for all} good men to come to.';
2a53d331 156 / \{([^}]*)}/;
de26e0cc 157 is($1, 'time for all', "Match braces");
84281c31 158 }
4765795a 159
84281c31 160 {
4f890a30 161 my $message = "{N,M} quantifier";
84281c31 162 $_ = 'xxx {3,4} yyy zzz';
4f890a30
NC
163 ok(/( {3,4})/, $message);
164 is($1, ' ', $message);
165 unlike($_, qr/( {4,})/, $message);
166 ok(/( {2,3}.)/, $message);
167 is($1, ' y', $message);
168 ok(/(y{2,3}.)/, $message);
169 is($1, 'yyy ', $message);
170 unlike($_, qr/x {3,4}/, $message);
171 unlike($_, qr/^xxx {3,4}/, $message);
84281c31 172 }
4765795a 173
84281c31 174 {
4f890a30 175 my $message = "Test /g";
84281c31
A
176 local $" = ":";
177 $_ = "now is the time for all good men to come to.";
178 my @words = /(\w+)/g;
179 my $exp = "now:is:the:time:for:all:good:men:to:come:to";
4765795a 180
4f890a30 181 is("@words", $exp, $message);
4765795a 182
84281c31
A
183 @words = ();
184 while (/\w+/g) {
185 push (@words, $&);
186 }
4f890a30 187 is("@words", $exp, $message);
4765795a 188
84281c31
A
189 @words = ();
190 pos = 0;
191 while (/to/g) {
192 push(@words, $&);
193 }
4f890a30 194 is("@words", "to:to", $message);
4765795a 195
84281c31
A
196 pos $_ = 0;
197 @words = /to/g;
4f890a30 198 is("@words", "to:to", $message);
84281c31 199 }
4765795a 200
84281c31
A
201 {
202 $_ = "abcdefghi";
203
204 my $pat1 = 'def';
205 my $pat2 = '^def';
206 my $pat3 = '.def.';
207 my $pat4 = 'abc';
208 my $pat5 = '^abc';
209 my $pat6 = 'abc$';
210 my $pat7 = 'ghi';
211 my $pat8 = '\w*ghi';
212 my $pat9 = 'ghi$';
213
214 my $t1 = my $t2 = my $t3 = my $t4 = my $t5 =
215 my $t6 = my $t7 = my $t8 = my $t9 = 0;
216
217 for my $iter (1 .. 5) {
218 $t1++ if /$pat1/o;
219 $t2++ if /$pat2/o;
220 $t3++ if /$pat3/o;
221 $t4++ if /$pat4/o;
222 $t5++ if /$pat5/o;
223 $t6++ if /$pat6/o;
224 $t7++ if /$pat7/o;
225 $t8++ if /$pat8/o;
226 $t9++ if /$pat9/o;
227 }
228 my $x = "$t1$t2$t3$t4$t5$t6$t7$t8$t9";
de26e0cc 229 is($x, '505550555', "Test /o");
84281c31 230 }
4765795a 231
4f890a30 232 {
84281c31
A
233 my $xyz = 'xyz';
234 ok "abc" =~ /^abc$|$xyz/, "| after \$";
4765795a 235
84281c31 236 # perl 4.009 says "unmatched ()"
4f890a30 237 my $message = '$ inside ()';
4765795a 238
84281c31
A
239 my $result;
240 eval '"abc" =~ /a(bc$)|$xyz/; $result = "$&:$1"';
4f890a30
NC
241 is($@, "", $message);
242 is($result, "abc:bc", $message);
84281c31 243 }
4765795a 244
84281c31 245 {
4f890a30 246 my $message = "Scalar /g";
84281c31
A
247 $_ = "abcfooabcbar";
248
4f890a30
NC
249 ok( /abc/g && $` eq "", $message);
250 ok( /abc/g && $` eq "abcfoo", $message);
251 ok(!/abc/g, $message);
84281c31 252
4f890a30 253 $message = "Scalar /gi";
84281c31 254 pos = 0;
4f890a30
NC
255 ok( /ABC/gi && $` eq "", $message);
256 ok( /ABC/gi && $` eq "abcfoo", $message);
257 ok(!/ABC/gi, $message);
84281c31 258
4f890a30 259 $message = "Scalar /g";
84281c31 260 pos = 0;
4f890a30
NC
261 ok( /abc/g && $' eq "fooabcbar", $message);
262 ok( /abc/g && $' eq "bar", $message);
84281c31
A
263
264 $_ .= '';
265 my @x = /abc/g;
de26e0cc 266 is(@x, 2, "/g reset after assignment");
4765795a 267 }
4765795a 268
84281c31 269 {
4f890a30 270 my $message = '/g, \G and pos';
84281c31
A
271 $_ = "abdc";
272 pos $_ = 2;
273 /\Gc/gc;
4f890a30 274 is(pos $_, 2, $message);
84281c31 275 /\Gc/g;
4f890a30 276 is(pos $_, undef, $message);
84281c31 277 }
4765795a 278
84281c31 279 {
4f890a30 280 my $message = '(?{ })';
84281c31
A
281 our $out = 1;
282 'abc' =~ m'a(?{ $out = 2 })b';
4f890a30 283 is($out, 2, $message);
84281c31
A
284
285 $out = 1;
286 'abc' =~ m'a(?{ $out = 3 })c';
4f890a30 287 is($out, 1, $message);
84281c31 288 }
4765795a 289
84281c31
A
290 {
291 $_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6';
292 my @out = /(?<!foo)bar./g;
de26e0cc 293 is("@out", 'bar2 barf', "Negative lookbehind");
84281c31 294 }
4765795a 295
84281c31 296 {
4f890a30 297 my $message = "REG_INFTY tests";
84281c31 298 # Tests which depend on REG_INFTY
19d6612d
NC
299
300 # Defaults assumed if this fails
301 eval { require Config; };
302 $::reg_infty = $Config::Config{reg_infty} // 32767;
84281c31
A
303 $::reg_infty_m = $::reg_infty - 1;
304 $::reg_infty_p = $::reg_infty + 1;
93f09d7b 305 $::reg_infty_m = $::reg_infty_m; # Suppress warning.
84281c31
A
306
307 # As well as failing if the pattern matches do unexpected things, the
308 # next three tests will fail if you should have picked up a lower-than-
309 # default value for $reg_infty from Config.pm, but have not.
310
14358a41
NC
311 is(eval q{('aaa' =~ /(a{1,$::reg_infty_m})/)[0]}, 'aaa', $message);
312 is($@, '', $message);
313 is(eval q{('a' x $::reg_infty_m) =~ /a{$::reg_infty_m}/}, 1, $message);
314 is($@, '', $message);
315 isnt(q{('a' x ($::reg_infty_m - 1)) !~ /a{$::reg_infty_m}/}, 1, $message);
316 is($@, '', $message);
317
84281c31 318 eval "'aaa' =~ /a{1,$::reg_infty}/";
224b2e7e 319 like($@, qr/^\QQuantifier in {,} bigger than/, $message);
84281c31 320 eval "'aaa' =~ /a{1,$::reg_infty_p}/";
4f890a30 321 like($@, qr/^\QQuantifier in {,} bigger than/, $message);
4765795a 322 }
8269fa76 323
84281c31
A
324 {
325 # Poke a couple more parse failures
326 my $context = 'x' x 256;
327 eval qq("${context}y" =~ /(?<=$context)y/);
328 ok $@ =~ /^\QLookbehind longer than 255 not/, "Lookbehind limit";
329 }
8269fa76 330
84281c31
A
331 {
332 # Long Monsters
84281c31
A
333 for my $l (125, 140, 250, 270, 300000, 30) { # Ordered to free memory
334 my $a = 'a' x $l;
4f890a30
NC
335 my $message = "Long monster, length = $l";
336 like("ba$a=", qr/a$a=/, $message);
337 unlike("b$a=", qr/a$a=/, $message);
338 like("b$a=", qr/ba+=/, $message);
84281c31 339
224b2e7e 340 like("ba$a=", qr/b(?:a|b)+=/, $message);
84281c31
A
341 }
342 }
8269fa76 343
84281c31
A
344 {
345 # 20000 nodes, each taking 3 words per string, and 1 per branch
346 my $long_constant_len = join '|', 12120 .. 32645;
347 my $long_var_len = join '|', 8120 .. 28645;
348 my %ans = ( 'ax13876y25677lbc' => 1,
349 'ax13876y25677mcb' => 0, # not b.
350 'ax13876y35677nbc' => 0, # Num too big
351 'ax13876y25677y21378obc' => 1,
0f289c68 352 'ax13876y25677y21378zbc' => 0, # Not followed by [k-o]
84281c31
A
353 'ax13876y25677y21378y21378kbc' => 1,
354 'ax13876y25677y21378y21378kcb' => 0, # Not b.
355 'ax13876y25677y21378y21378y21378kbc' => 0, # 5 runs
356 );
357
84281c31 358 for (keys %ans) {
4f890a30
NC
359 my $message = "20000 nodes, const-len '$_'";
360 ok !($ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o), $message;
84281c31 361
4f890a30
NC
362 $message = "20000 nodes, var-len '$_'";
363 ok !($ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o,), $message;
84281c31 364 }
b8ef571c 365 }
209a9bc1 366
84281c31 367 {
4f890a30 368 my $message = "Complicated backtracking";
84281c31
A
369 $_ = " a (bla()) and x(y b((l)u((e))) and b(l(e)e)e";
370 my $expect = "(bla()) ((l)u((e))) (l(e)e)";
371
372 use vars '$c';
373 sub matchit {
374 m/
375 (
376 \(
0f289c68 377 (?{ $c = 1 }) # Initialize
84281c31
A
378 (?:
379 (?(?{ $c == 0 }) # PREVIOUS iteration was OK, stop the loop
380 (?!
0f289c68
YO
381 ) # Fail: will unwind one iteration back
382 )
84281c31 383 (?:
0f289c68 384 [^()]+ # Match a big chunk
84281c31
A
385 (?=
386 [()]
0f289c68 387 ) # Do not try to match subchunks
84281c31
A
388 |
389 \(
390 (?{ ++$c })
391 |
392 \)
393 (?{ --$c })
394 )
0f289c68 395 )+ # This may not match with different subblocks
84281c31
A
396 )
397 (?(?{ $c != 0 })
398 (?!
0f289c68
YO
399 ) # Fail
400 ) # Otherwise the chunk 1 may succeed with $c>0
84281c31
A
401 /xg;
402 }
3568d838 403
84281c31
A
404 my @ans = ();
405 my $res;
406 push @ans, $res while $res = matchit;
4f890a30 407 is("@ans", "1 1 1", $message);
3568d838 408
84281c31 409 @ans = matchit;
4f890a30 410 is("@ans", $expect, $message);
3568d838 411
4f890a30 412 $message = "Recursion with (??{ })";
84281c31
A
413 our $matched;
414 $matched = qr/\((?:(?>[^()]+)|(??{$matched}))*\)/;
3568d838 415
84281c31
A
416 @ans = my @ans1 = ();
417 push (@ans, $res), push (@ans1, $&) while $res = m/$matched/g;
3568d838 418
4f890a30
NC
419 is("@ans", "1 1 1", $message);
420 is("@ans1", $expect, $message);
3568d838 421
84281c31 422 @ans = m/$matched/g;
4f890a30 423 is("@ans", $expect, $message);
3568d838 424
84281c31 425 }
3568d838 426
84281c31
A
427 {
428 ok "abc" =~ /^(??{"a"})b/, '"abc" =~ /^(??{"a"})b/';
429 }
3568d838 430
84281c31 431 {
0f289c68 432 my @ans = ('a/b' =~ m%(.*/)?(.*)%); # Stack may be bad
de26e0cc 433 is("@ans", 'a/ b', "Stack may be bad");
84281c31 434 }
3568d838 435
84281c31 436 {
4f890a30 437 my $message = "Eval-group not allowed at runtime";
84281c31
A
438 my $code = '{$blah = 45}';
439 our $blah = 12;
440 eval { /(?$code)/ };
4f890a30 441 ok($@ && $@ =~ /not allowed at runtime/ && $blah == 12, $message);
84281c31 442
3044771b
NC
443 $blah = 12;
444 my $res = eval { "xx" =~ /(?$code)/o };
445 {
446 no warnings 'uninitialized';
5895685f 447 chomp $@; my $message = "$message '$@', '$res', '$blah'";
4f890a30 448 ok($@ && $@ =~ /not allowed at runtime/ && $blah == 12, $message);
3044771b
NC
449 }
450
451 $code = '=xx';
452 $blah = 12;
453 $res = eval { "xx" =~ /(?$code)/o };
454 {
455 no warnings 'uninitialized';
4f890a30
NC
456 my $message = "$message '$@', '$res', '$blah'";
457 ok(!$@ && $res, $message);
3044771b 458 }
3568d838 459
84281c31
A
460 $code = '{$blah = 45}';
461 $blah = 12;
462 eval "/(?$code)/";
4f890a30 463 is($blah, 45, $message);
3568d838 464
84281c31
A
465 $blah = 12;
466 /(?{$blah = 45})/;
4f890a30 467 is($blah, 45, $message);
84281c31 468 }
3568d838 469
84281c31 470 {
4f890a30 471 my $message = "Pos checks";
84281c31
A
472 my $x = 'banana';
473 $x =~ /.a/g;
4f890a30 474 is(pos $x, 2, $message);
3568d838 475
84281c31 476 $x =~ /.z/gc;
4f890a30 477 is(pos $x, 2, $message);
3568d838 478
84281c31
A
479 sub f {
480 my $p = $_[0];
481 return $p;
482 }
3568d838 483
84281c31 484 $x =~ /.a/g;
4f890a30 485 is(f (pos $x), 4, $message);
84281c31 486 }
3568d838 487
84281c31 488 {
4f890a30 489 my $message = 'Checking $^R';
84281c31
A
490 our $x = $^R = 67;
491 'foot' =~ /foo(?{$x = 12; 75})[t]/;
4f890a30 492 is($^R, 75, $message);
84281c31
A
493
494 $x = $^R = 67;
495 'foot' =~ /foo(?{$x = 12; 75})[xy]/;
4f890a30 496 ok($^R eq '67' && $x eq '12', $message);
84281c31
A
497
498 $x = $^R = 67;
499 'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/;
4f890a30 500 ok($^R eq '79' && $x eq '12', $message);
84281c31 501 }
3568d838 502
84281c31 503 {
de26e0cc
NC
504 is(qr/\b\v$/i, '(?^i:\b\v$)', 'qr/\b\v$/i');
505 is(qr/\b\v$/s, '(?^s:\b\v$)', 'qr/\b\v$/s');
506 is(qr/\b\v$/m, '(?^m:\b\v$)', 'qr/\b\v$/m');
507 is(qr/\b\v$/x, '(?^x:\b\v$)', 'qr/\b\v$/x');
508 is(qr/\b\v$/xism, '(?^msix:\b\v$)', 'qr/\b\v$/xism');
509 is(qr/\b\v$/, '(?^:\b\v$)', 'qr/\b\v$/');
84281c31 510 }
3568d838 511
9de15fec 512 { # Test that charset modifier work, and are interpolated
de26e0cc
NC
513 is(qr/\b\v$/, '(?^:\b\v$)', 'Verify no locale, no unicode_strings gives default modifier');
514 is(qr/(?l:\b\v$)/, '(?^:(?l:\b\v$))', 'Verify infix l modifier compiles');
515 is(qr/(?u:\b\v$)/, '(?^:(?u:\b\v$))', 'Verify infix u modifier compiles');
516 is(qr/(?l)\b\v$/, '(?^:(?l)\b\v$)', 'Verify (?l) compiles');
517 is(qr/(?u)\b\v$/, '(?^:(?u)\b\v$)', 'Verify (?u) compiles');
9de15fec
KW
518
519 my $dual = qr/\b\v$/;
569f7fc5
JR
520 my $locale;
521
522 SKIP: {
523 skip 'No locale testing without d_setlocale', 1 if(!$Config{d_setlocale});
524
5f1269ab 525 use locale;
569f7fc5
JR
526 $locale = qr/\b\v$/;
527 is($locale, '(?^l:\b\v$)', 'Verify has l modifier when compiled under use locale');
528 no locale;
529 }
9de15fec
KW
530
531 use feature 'unicode_strings';
532 my $unicode = qr/\b\v$/;
de26e0cc
NC
533 is($unicode, '(?^u:\b\v$)', 'Verify has u modifier when compiled under unicode_strings');
534 is(qr/abc$dual/, '(?^u:abc(?^:\b\v$))', 'Verify retains d meaning when interpolated under locale');
569f7fc5
JR
535
536 SKIP: {
537 skip 'No locale testing without d_setlocale', 1 if(!$Config{d_setlocale});
538
539 is(qr/abc$locale/, '(?^u:abc(?^l:\b\v$))', 'Verify retains l when interpolated under unicode_strings');
540 }
9de15fec
KW
541
542 no feature 'unicode_strings';
569f7fc5
JR
543 SKIP: {
544 skip 'No locale testing without d_setlocale', 1 if(!$Config{d_setlocale});
545
546 is(qr/abc$locale/, '(?^:abc(?^l:\b\v$))', 'Verify retains l when interpolated outside locale and unicode strings');
547 }
548
de26e0cc 549 is(qr/def$unicode/, '(?^:def(?^u:\b\v$))', 'Verify retains u when interpolated outside locale and unicode strings');
9de15fec 550
569f7fc5
JR
551 SKIP: {
552 skip 'No locale testing without d_setlocale', 2 if(!$Config{d_setlocale});
553
5f1269ab 554 use locale;
569f7fc5
JR
555 is(qr/abc$dual/, '(?^l:abc(?^:\b\v$))', 'Verify retains d meaning when interpolated under locale');
556 is(qr/abc$unicode/, '(?^l:abc(?^u:\b\v$))', 'Verify retains u when interpolated under locale');
557 }
9de15fec
KW
558 }
559
84281c31 560 {
4f890a30 561 my $message = "Look around";
84281c31 562 $_ = 'xabcx';
84281c31 563 foreach my $ans ('', 'c') {
4f890a30
NC
564 ok(/(?<=(?=a)..)((?=c)|.)/g, $message);
565 is($1, $ans, $message);
84281c31
A
566 }
567 }
3568d838 568
84281c31 569 {
4f890a30 570 my $message = "Empty clause";
84281c31
A
571 $_ = 'a';
572 foreach my $ans ('', 'a', '') {
4f890a30
NC
573 ok(/^|a|$/g, $message);
574 is($&, $ans, $message);
84281c31
A
575 }
576 }
3568d838 577
84281c31 578 {
84281c31 579 sub prefixify {
4f890a30
NC
580 my $message = "Prefixify";
581 {
84281c31 582 my ($v, $a, $b, $res) = @_;
4f890a30
NC
583 ok($v =~ s/\Q$a\E/$b/, $message);
584 is($v, $res, $message);
84281c31
A
585 }
586 }
3568d838 587
84281c31
A
588 prefixify ('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch');
589 prefixify ('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch');
590 }
3568d838 591
84281c31
A
592 {
593 $_ = 'var="foo"';
594 /(\")/;
595 ok $1 && /$1/, "Capture a quote";
596 }
3568d838 597
84281c31 598 {
84281c31 599 no warnings 'closure';
4f890a30 600 my $message = '(?{ $var } refers to package vars';
84281c31
A
601 package aa;
602 our $c = 2;
603 $::c = 3;
604 '' =~ /(?{ $c = 4 })/;
4f890a30
NC
605 main::is($c, 4, $message);
606 main::is($::c, 3, $message);
84281c31 607 }
3568d838 608
84281c31 609 {
cb124425
NC
610 is(eval 'q(a:[b]:) =~ /[x[:foo:]]/', undef);
611 like ($@, qr/POSIX class \[:[^:]+:\] unknown in regex/,
612 'POSIX class [: :] must have valid name');
84281c31
A
613
614 for my $d (qw [= .]) {
cb124425
NC
615 is(eval "/[[${d}foo${d}]]/", undef);
616 like ($@, qr/\QPOSIX syntax [$d $d] is reserved for future extensions/,
617 "POSIX syntax [[$d $d]] is an error");
84281c31
A
618 }
619 }
3568d838 620
84281c31
A
621 {
622 # test if failure of patterns returns empty list
4f890a30 623 my $message = "Failed pattern returns empty list";
84281c31
A
624 $_ = 'aaa';
625 @_ = /bbb/;
4f890a30 626 is("@_", "", $message);
3568d838 627
84281c31 628 @_ = /bbb/g;
4f890a30 629 is("@_", "", $message);
a72deede 630
84281c31 631 @_ = /(bbb)/;
4f890a30 632 is("@_", "", $message);
a72deede 633
84281c31 634 @_ = /(bbb)/g;
4f890a30 635 is("@_", "", $message);
84281c31 636 }
a72deede 637
84281c31 638 {
4f890a30 639 my $message = '@- and @+ tests';
84281c31
A
640
641 /a(?=.$)/;
4f890a30
NC
642 is($#+, 0, $message);
643 is($#-, 0, $message);
644 is($+ [0], 2, $message);
645 is($- [0], 1, $message);
646 ok(!defined $+ [1] && !defined $- [1] &&
647 !defined $+ [2] && !defined $- [2], $message);
84281c31
A
648
649 /a(a)(a)/;
4f890a30
NC
650 is($#+, 2, $message);
651 is($#-, 2, $message);
652 is($+ [0], 3, $message);
653 is($- [0], 0, $message);
654 is($+ [1], 2, $message);
655 is($- [1], 1, $message);
656 is($+ [2], 3, $message);
657 is($- [2], 2, $message);
658 ok(!defined $+ [3] && !defined $- [3] &&
659 !defined $+ [4] && !defined $- [4], $message);
84281c31 660
54a4274e 661 # Exists has a special check for @-/@+ - bug 45147
4f890a30
NC
662 ok(exists $-[0], $message);
663 ok(exists $+[0], $message);
664 ok(exists $-[2], $message);
665 ok(exists $+[2], $message);
666 ok(!exists $-[3], $message);
667 ok(!exists $+[3], $message);
668 ok(exists $-[-1], $message);
669 ok(exists $+[-1], $message);
670 ok(exists $-[-3], $message);
671 ok(exists $+[-3], $message);
672 ok(!exists $-[-4], $message);
673 ok(!exists $+[-4], $message);
84281c31
A
674
675 /.(a)(b)?(a)/;
4f890a30
NC
676 is($#+, 3, $message);
677 is($#-, 3, $message);
678 is($+ [1], 2, $message);
679 is($- [1], 1, $message);
680 is($+ [3], 3, $message);
681 is($- [3], 2, $message);
682 ok(!defined $+ [2] && !defined $- [2] &&
683 !defined $+ [4] && !defined $- [4], $message);
84281c31 684
84281c31 685 /.(a)/;
4f890a30
NC
686 is($#+, 1, $message);
687 is($#-, 1, $message);
688 is($+ [0], 2, $message);
689 is($- [0], 0, $message);
690 is($+ [1], 2, $message);
691 is($- [1], 1, $message);
692 ok(!defined $+ [2] && !defined $- [2] &&
693 !defined $+ [3] && !defined $- [3], $message);
84281c31
A
694
695 /.(a)(ba*)?/;
4f890a30
NC
696 is($#+, 2, $message);
697 is($#-, 1, $message);
232af1f8
FC
698
699 # Check that values don’t stick
700 " "=~/()()()(.)(..)/;
701 my($m,$p) = (\$-[5], \$+[5]);
702 () = "$$_" for $m, $p; # FETCH (or eqv.)
703 " " =~ /()/;
704 is $$m, undef, 'values do not stick to @- elements';
705 is $$p, undef, 'values do not stick to @+ elements';
84281c31 706 }
a72deede 707
d9bad346
FC
708 foreach ('$+[0] = 13', '$-[0] = 13', '@+ = (7, 6, 5)',
709 '@- = qw (foo bar)', '$^N = 42') {
cb124425
NC
710 is(eval $_, undef);
711 like($@, qr/^Modification of a read-only value attempted/,
d9bad346 712 '$^N, @- and @+ are read-only');
84281c31 713 }
a72deede 714
84281c31 715 {
4f890a30 716 my $message = '\G testing';
84281c31
A
717 $_ = 'aaa';
718 pos = 1;
719 my @a = /\Ga/g;
4f890a30 720 is("@a", "a a", $message);
84281c31
A
721
722 my $str = 'abcde';
723 pos $str = 2;
4f890a30
NC
724 unlike($str, qr/^\G/, $message);
725 unlike($str, qr/^.\G/, $message);
726 like($str, qr/^..\G/, $message);
727 unlike($str, qr/^...\G/, $message);
728 ok($str =~ /\G../ && $& eq 'cd', $message);
4f890a30 729 ok($str =~ /.\G./ && $& eq 'bc', $message);
cf44e600
DM
730
731 }
732
733 {
734 my $message = '\G and intuit and anchoring';
735 $_ = "abcdef";
736 pos = 0;
737 ok($_ =~ /\Gabc/, $message);
738 ok($_ =~ /^\Gabc/, $message);
739
740 pos = 3;
741 ok($_ =~ /\Gdef/, $message);
742 pos = 3;
743 ok($_ =~ /\Gdef$/, $message);
744 pos = 3;
745 ok($_ =~ /abc\Gdef$/, $message);
746 pos = 3;
747 ok($_ =~ /^abc\Gdef$/, $message);
748 pos = 3;
749 ok($_ =~ /c\Gd/, $message);
e0362b86
DM
750 pos = 3;
751 ok($_ =~ /..\GX?def/, $message);
84281c31 752 }
a72deede 753
84281c31 754 {
fe3974be
DM
755 my $s = '123';
756 pos($s) = 1;
757 my @a = $s =~ /(\d)\G/g; # this infinitely looped up till 5.19.1
758 is("@a", "1", '\G looping');
759 }
760
761
762 {
4f890a30 763 my $message = 'pos inside (?{ })';
84281c31
A
764 my $str = 'abcde';
765 our ($foo, $bar);
4f890a30
NC
766 like($str, qr/b(?{$foo = $_; $bar = pos})c/, $message);
767 is($foo, $str, $message);
768 is($bar, 2, $message);
769 is(pos $str, undef, $message);
84281c31
A
770
771 undef $foo;
772 undef $bar;
773 pos $str = undef;
4f890a30
NC
774 ok($str =~ /b(?{$foo = $_; $bar = pos})c/g, $message);
775 is($foo, $str, $message);
776 is($bar, 2, $message);
777 is(pos $str, 3, $message);
84281c31
A
778
779 $_ = $str;
780 undef $foo;
781 undef $bar;
4f890a30
NC
782 like($_, qr/b(?{$foo = $_; $bar = pos})c/, $message);
783 is($foo, $str, $message);
784 is($bar, 2, $message);
84281c31
A
785
786 undef $foo;
787 undef $bar;
4f890a30
NC
788 ok(/b(?{$foo = $_; $bar = pos})c/g, $message);
789 is($foo, $str, $message);
790 is($bar, 2, $message);
791 is(pos, 3, $message);
84281c31
A
792
793 undef $foo;
794 undef $bar;
795 pos = undef;
796 1 while /b(?{$foo = $_; $bar = pos})c/g;
4f890a30
NC
797 is($foo, $str, $message);
798 is($bar, 2, $message);
799 is(pos, undef, $message);
84281c31
A
800
801 undef $foo;
802 undef $bar;
803 $_ = 'abcde|abcde';
4f890a30
NC
804 ok(s/b(?{$foo = $_; $bar = pos})c/x/g, $message);
805 is($foo, 'abcde|abcde', $message);
806 is($bar, 8, $message);
807 is($_, 'axde|axde', $message);
84281c31
A
808
809 # List context:
810 $_ = 'abcde|abcde';
811 our @res;
812 () = /([ace]).(?{push @res, $1,$2})([ce])(?{push @res, $1,$2})/g;
813 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
4f890a30 814 is("@res", "'a' undef 'a' 'c' 'e' undef 'a' undef 'a' 'c'", $message);
84281c31
A
815
816 @res = ();
817 () = /([ace]).(?{push @res, $`,$&,$'})([ce])(?{push @res, $`,$&,$'})/g;
818 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
4f890a30 819 is("@res", "'' 'ab' 'cde|abcde' " .
84281c31
A
820 "'' 'abc' 'de|abcde' " .
821 "'abcd' 'e|' 'abcde' " .
822 "'abcde|' 'ab' 'cde' " .
4f890a30 823 "'abcde|' 'abc' 'de'", $message);
84281c31 824 }
f33976b4 825
84281c31 826 {
4f890a30 827 my $message = '\G anchor checks';
84281c31
A
828 my $foo = 'aabbccddeeffgg';
829 pos ($foo) = 1;
cce850e4 830
98bee633
DM
831 ok($foo =~ /.\G(..)/g, $message);
832 is($1, 'ab', $message);
cce850e4 833
98bee633
DM
834 pos ($foo) += 1;
835 ok($foo =~ /.\G(..)/g, $message);
836 is($1, 'cc', $message);
cce850e4 837
98bee633
DM
838 pos ($foo) += 1;
839 ok($foo =~ /.\G(..)/g, $message);
840 is($1, 'de', $message);
841
842 ok($foo =~ /\Gef/g, $message);
cce850e4 843
84281c31 844 undef pos $foo;
4f890a30
NC
845 ok($foo =~ /\G(..)/g, $message);
846 is($1, 'aa', $message);
cce850e4 847
4f890a30
NC
848 ok($foo =~ /\G(..)/g, $message);
849 is($1, 'bb', $message);
cce850e4 850
84281c31 851 pos ($foo) = 5;
4f890a30
NC
852 ok($foo =~ /\G(..)/g, $message);
853 is($1, 'cd', $message);
84281c31 854 }
cce850e4 855
84281c31 856 {
9c13586e
DM
857 my $message = 'basic \G floating checks';
858 my $foo = 'aabbccddeeffgg';
859 pos ($foo) = 1;
860
861 ok($foo =~ /a+\G(..)/g, "$message: a+\\G");
862 is($1, 'ab', "$message: ab");
863
864 pos ($foo) += 1;
865 ok($foo =~ /b+\G(..)/g, "$message: b+\\G");
866 is($1, 'cc', "$message: cc");
867
868 pos ($foo) += 1;
869 ok($foo =~ /d+\G(..)/g, "$message: d+\\G");
870 is($1, 'de', "$message: de");
871
872 ok($foo =~ /\Gef/g, "$message: \\Gef");
873
874 pos ($foo) = 1;
875
876 ok($foo =~ /(?=a+\G)(..)/g, "$message: (?a+\\G)");
877 is($1, 'aa', "$message: aa");
878
879 pos ($foo) = 2;
880
881 ok($foo =~ /a(?=a+\G)(..)/g, "$message: a(?=a+\\G)");
882 is($1, 'ab', "$message: ab");
883
884 }
885
886 {
84281c31
A
887 $_ = '123x123';
888 my @res = /(\d*|x)/g;
889 local $" = '|';
de26e0cc 890 is("@res", "123||x|123|", "0 match in alternation");
84281c31 891 }
cce850e4 892
84281c31 893 {
4f890a30 894 my $message = "Match against temporaries (created via pp_helem())" .
84281c31 895 " is safe";
4f890a30
NC
896 ok({foo => "bar\n" . $^X} -> {foo} =~ /^(.*)\n/g, $message);
897 is($1, "bar", $message);
84281c31 898 }
75685a94 899
84281c31 900 {
4f890a30 901 my $message = 'package $i inside (?{ }), ' .
84281c31
A
902 'saved substrings and changing $_';
903 our @a = qw [foo bar];
904 our @b = ();
905 s/(\w)(?{push @b, $1})/,$1,/g for @a;
4f890a30
NC
906 is("@b", "f o o b a r", $message);
907 is("@a", ",f,,o,,o, ,b,,a,,r,", $message);
84281c31 908
4f890a30 909 $message = 'lexical $i inside (?{ }), ' .
84281c31
A
910 'saved substrings and changing $_';
911 no warnings 'closure';
912 my @c = qw [foo bar];
913 my @d = ();
914 s/(\w)(?{push @d, $1})/,$1,/g for @c;
4f890a30
NC
915 is("@d", "f o o b a r", $message);
916 is("@c", ",f,,o,,o, ,b,,a,,r,", $message);
d9f424b2
JH
917 }
918
84281c31 919 {
4f890a30 920 my $message = 'Brackets';
84281c31
A
921 our $brackets;
922 $brackets = qr {
923 { (?> [^{}]+ | (??{ $brackets }) )* }
924 }x;
925
4f890a30
NC
926 ok("{{}" =~ $brackets, $message);
927 is($&, "{}", $message);
928 ok("something { long { and } hairy" =~ $brackets, $message);
929 is($&, "{ and }", $message);
930 ok("something { long { and } hairy" =~ m/((??{ $brackets }))/, $message);
931 is($&, "{ and }", $message);
84281c31 932 }
a4c04bdc 933
84281c31
A
934 {
935 $_ = "a-a\nxbb";
936 pos = 1;
b33825c4 937 ok(!m/^-.*bb/mg, '$_ = "a-a\nxbb"; m/^-.*bb/mg');
84281c31 938 }
a4c04bdc 939
84281c31 940 {
4f890a30 941 my $message = '\G anchor checks';
84281c31
A
942 my $text = "aaXbXcc";
943 pos ($text) = 0;
4f890a30 944 ok($text !~ /\GXb*X/g, $message);
84281c31 945 }
a4c04bdc 946
84281c31
A
947 {
948 $_ = "xA\n" x 500;
b33825c4 949 unlike($_, qr/^\s*A/m, '$_ = "xA\n" x 500; /^\s*A/m"');
a4c04bdc 950
84281c31
A
951 my $text = "abc dbf";
952 my @res = ($text =~ /.*?(b).*?\b/g);
de26e0cc 953 is("@res", "b b", '\b is not special');
987aaf07 954 }
a4c04bdc 955
84281c31 956 {
4f890a30 957 my $message = '\S, [\S], \s, [\s]';
84281c31 958 my @a = map chr, 0 .. 255;
9d45b377
YO
959 my @b = grep m/\S/, @a;
960 my @c = grep m/[^\s]/, @a;
4f890a30 961 is("@b", "@c", $message);
84281c31
A
962
963 @b = grep /\S/, @a;
964 @c = grep /[\S]/, @a;
4f890a30 965 is("@b", "@c", $message);
84281c31
A
966
967 @b = grep /\s/, @a;
968 @c = grep /[^\S]/, @a;
4f890a30 969 is("@b", "@c", $message);
84281c31
A
970
971 @b = grep /\s/, @a;
972 @c = grep /[\s]/, @a;
4f890a30 973 is("@b", "@c", $message);
84281c31
A
974 }
975 {
4f890a30 976 my $message = '\D, [\D], \d, [\d]';
84281c31
A
977 my @a = map chr, 0 .. 255;
978 my @b = grep /\D/, @a;
979 my @c = grep /[^\d]/, @a;
4f890a30 980 is("@b", "@c", $message);
84281c31
A
981
982 @b = grep /\D/, @a;
983 @c = grep /[\D]/, @a;
4f890a30 984 is("@b", "@c", $message);
84281c31
A
985
986 @b = grep /\d/, @a;
987 @c = grep /[^\D]/, @a;
4f890a30 988 is("@b", "@c", $message);
84281c31
A
989
990 @b = grep /\d/, @a;
991 @c = grep /[\d]/, @a;
4f890a30 992 is("@b", "@c", $message);
84281c31
A
993 }
994 {
4f890a30 995 my $message = '\W, [\W], \w, [\w]';
84281c31
A
996 my @a = map chr, 0 .. 255;
997 my @b = grep /\W/, @a;
998 my @c = grep /[^\w]/, @a;
4f890a30 999 is("@b", "@c", $message);
84281c31
A
1000
1001 @b = grep /\W/, @a;
1002 @c = grep /[\W]/, @a;
4f890a30 1003 is("@b", "@c", $message);
84281c31
A
1004
1005 @b = grep /\w/, @a;
1006 @c = grep /[^\W]/, @a;
4f890a30 1007 is("@b", "@c", $message);
84281c31
A
1008
1009 @b = grep /\w/, @a;
1010 @c = grep /[\w]/, @a;
4f890a30 1011 is("@b", "@c", $message);
84281c31 1012 }
a4c04bdc 1013
84281c31
A
1014 {
1015 # see if backtracking optimization works correctly
4f890a30
NC
1016 my $message = 'Backtrack optimization';
1017 like("\n\n", qr/\n $ \n/x, $message);
1018 like("\n\n", qr/\n* $ \n/x, $message);
1019 like("\n\n", qr/\n+ $ \n/x, $message);
1020 like("\n\n", qr/\n? $ \n/x, $message);
1021 like("\n\n", qr/\n*? $ \n/x, $message);
1022 like("\n\n", qr/\n+? $ \n/x, $message);
1023 like("\n\n", qr/\n?? $ \n/x, $message);
1024 unlike("\n\n", qr/\n*+ $ \n/x, $message);
1025 unlike("\n\n", qr/\n++ $ \n/x, $message);
1026 like("\n\n", qr/\n?+ $ \n/x, $message);
84281c31 1027 }
a4c04bdc 1028
84281c31
A
1029 {
1030 package S;
1031 use overload '""' => sub {'Object S'};
1032 sub new {bless []}
0f289c68 1033
4f890a30 1034 my $message = "Ref stringification";
5895685f
NC
1035 ::ok(do { \my $v} =~ /^SCALAR/, "Scalar ref stringification") or diag($message);
1036 ::ok(do {\\my $v} =~ /^REF/, "Ref ref stringification") or diag($message);
1037 ::ok([] =~ /^ARRAY/, "Array ref stringification") or diag($message);
1038 ::ok({} =~ /^HASH/, "Hash ref stringification") or diag($message);
1039 ::ok('S' -> new =~ /^Object S/, "Object stringification") or diag($message);
84281c31 1040 }
a4c04bdc 1041
84281c31 1042 {
4f890a30
NC
1043 my $message = "Test result of match used as match";
1044 ok('a1b' =~ ('xyz' =~ /y/), $message);
1045 is($`, 'a', $message);
1046 ok('a1b' =~ ('xyz' =~ /t/), $message);
1047 is($`, 'a', $message);
84281c31 1048 }
a4c04bdc 1049
84281c31 1050 {
d728c370 1051 my $message = '"1" is not \s';
c11a8df3
NC
1052 warning_is(sub {unlike("1\n" x 102, qr/^\s*\n/m, $message)},
1053 undef, "$message (did not warn)");
84281c31 1054 }
a4c04bdc 1055
84281c31 1056 {
4f890a30 1057 my $message = '\s, [[:space:]] and [[:blank:]]';
84281c31
A
1058 my %space = (spc => " ",
1059 tab => "\t",
1060 cr => "\r",
1061 lf => "\n",
1062 ff => "\f",
1063 # There's no \v but the vertical tabulator seems miraculously
1064 # be 11 both in ASCII and EBCDIC.
1065 vt => chr(11),
1066 false => "space");
1067
1068 my @space0 = sort grep {$space {$_} =~ /\s/ } keys %space;
1069 my @space1 = sort grep {$space {$_} =~ /[[:space:]]/} keys %space;
1070 my @space2 = sort grep {$space {$_} =~ /[[:blank:]]/} keys %space;
1071
075b9d7d 1072 is("@space0", "cr ff lf spc tab vt", $message);
4f890a30
NC
1073 is("@space1", "cr ff lf spc tab vt", $message);
1074 is("@space2", "spc tab", $message);
84281c31 1075 }
a4c04bdc 1076
ff3f963a 1077 {
c9415951 1078 my $n= 50;
93f09d7b 1079 # this must be a high number and go from 0 to N, as the bug we are looking for doesn't
c9415951
YO
1080 # seem to be predictable. Slight changes to the test make it fail earlier or later.
1081 foreach my $i (0 .. $n)
1082 {
1083 my $str= "\n" x $i;
93f09d7b 1084 ok $str=~/.*\z/, "implicit MBOL check string disable does not break things length=$i";
c9415951
YO
1085 }
1086 }
92f3d482
YO
1087 {
1088 # we are actually testing that we dont die when executing these patterns
1089 use utf8;
1090 my $e = "Böck";
1091 ok(utf8::is_utf8($e),"got a unicode string - rt75680");
1092
1093 ok($e !~ m/.*?[x]$/, "unicode string against /.*?[x]\$/ - rt75680");
1094 ok($e !~ m/.*?\p{Space}$/i, "unicode string against /.*?\\p{space}\$/i - rt75680");
1095 ok($e !~ m/.*?[xyz]$/, "unicode string against /.*?[xyz]\$/ - rt75680");
1096 ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/, "unicode string against big pattern - rt75680");
1097 }
1098 {
1099 # we are actually testing that we dont die when executing these patterns
1100 my $e = "B\x{f6}ck";
1101 ok(!utf8::is_utf8($e), "got a latin string - rt75680");
1102
1103 ok($e !~ m/.*?[x]$/, "latin string against /.*?[x]\$/ - rt75680");
1104 ok($e !~ m/.*?\p{Space}$/i, "latin string against /.*?\\p{space}\$/i - rt75680");
1105 ok($e !~ m/.*?[xyz]$/,"latin string against /.*?[xyz]\$/ - rt75680");
1106 ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/,"latin string against big pattern - rt75680");
1107 }
c920e018
A
1108
1109 {
1110 #
1111 # Tests for bug 77414.
1112 #
1113
4f890a30 1114 my $message = '\p property after empty * match';
c920e018 1115 {
4f890a30
NC
1116 like("1", qr/\s*\pN/, $message);
1117 like("-", qr/\s*\p{Dash}/, $message);
1118 like(" ", qr/\w*\p{Blank}/, $message);
c920e018
A
1119 }
1120
4f890a30
NC
1121 like("1", qr/\s*\pN+/, $message);
1122 like("-", qr/\s*\p{Dash}{1}/, $message);
1123 like(" ", qr/\w*\p{Blank}{1,4}/, $message);
c920e018
A
1124
1125 }
1126
7c17ea2f
KW
1127 SKIP: { # Some constructs with Latin1 characters cause a utf8 string not
1128 # to match itself in non-utf8
ef237063 1129 if ($::IS_EBCDIC) {
7c17ea2f
KW
1130 skip "Needs to be customized to run on EBCDIC", 6;
1131 }
634c83a2
KW
1132 my $c = "\xc0";
1133 my $pattern = my $utf8_pattern = qr/((\xc0)+,?)/;
1134 utf8::upgrade($utf8_pattern);
1135 ok $c =~ $pattern, "\\xc0 =~ $pattern; Neither pattern nor target utf8";
d4e0b827 1136 ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; Neither pattern nor target utf8";
634c83a2 1137 ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; pattern utf8, target not";
d4e0b827 1138 ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; pattern utf8, target not";
634c83a2
KW
1139 utf8::upgrade($c);
1140 ok $c =~ $pattern, "\\xc0 =~ $pattern; target utf8, pattern not";
d4e0b827 1141 ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; target utf8, pattern not";
634c83a2 1142 ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; Both target and pattern utf8";
d4e0b827 1143 ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; Both target and pattern utf8";
634c83a2
KW
1144 }
1145
8cc86590 1146 SKIP: { # Make sure can override the formatting
ef237063 1147 if ($::IS_EBCDIC) {
8cc86590
KW
1148 skip "Needs to be customized to run on EBCDIC", 2;
1149 }
1150 use feature 'unicode_strings';
1151 ok "\xc0" =~ /\w/, 'Under unicode_strings: "\xc0" =~ /\w/';
1152 ok "\xc0" !~ /(?d:\w)/, 'Under unicode_strings: "\xc0" !~ /(?d:\w)/';
1153 }
1154
704f71be 1155 {
5b6010b3
YO
1156 my $str= "\x{100}";
1157 chop $str;
1158 my $qr= qr/$str/;
de26e0cc 1159 is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag enabled - Bug #80212");
5b6010b3
YO
1160 $str= "";
1161 $qr= qr/$str/;
de26e0cc 1162 is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag disabled - Bug #80212");
5b6010b3
YO
1163
1164 }
1165
72aa120d 1166 {
04934b6d 1167 local $::TODO = "[perl #38133]";
72aa120d
KW
1168
1169 "A" =~ /(((?:A))?)+/;
1170 my $first = $2;
1171
1172 "A" =~ /(((A))?)+/;
1173 my $second = $2;
1174
de26e0cc 1175 is($first, $second);
72aa120d
KW
1176 }
1177
99ca48e1
DM
1178 {
1179 # RT #3516: \G in a m//g expression causes problems
1180 my $count = 0;
1181 while ("abc" =~ m/(\G[ac])?/g) {
1182 last if $count++ > 10;
1183 }
1184 ok($count < 10, 'RT #3516 A');
1185
1186 $count = 0;
1187 while ("abc" =~ m/(\G|.)[ac]/g) {
1188 last if $count++ > 10;
1189 }
1190 ok($count < 10, 'RT #3516 B');
1191
1192 $count = 0;
1193 while ("abc" =~ m/(\G?[ac])?/g) {
1194 last if $count++ > 10;
1195 }
1196 ok($count < 10, 'RT #3516 C');
1197 }
d774cd11
YO
1198 {
1199 # RT #84294: Is this a bug in the simple Perl regex?
1200 # : Nested buffers and (?{...}) dont play nicely on partial matches
1201 our @got= ();
1202 ok("ab" =~ /((\w+)(?{ push @got, $2 })){2}/,"RT #84294: Pattern should match");
d774cd11
YO
1203 my $want= "'ab', 'a', 'b'";
1204 my $got= join(", ", map { defined($_) ? "'$_'" : "undef" } @got);
1205 is($got,$want,'RT #84294: check that "ab" =~ /((\w+)(?{ push @got, $2 })){2}/ leaves @got in the correct state');
1206 }
1207
0bda3001
KW
1208 {
1209 # Suppress warnings, as the non-unicode one comes out even if turn off
1210 # warnings here (because the execution is done in another scope).
1211 local $SIG{__WARN__} = sub {};
1212 my $str = "\x{110000}";
1213
2d88a86a
KW
1214 unlike($str, qr/\p{ASCII_Hex_Digit=True}/, "Non-Unicode doesn't match \\p{AHEX=True}");
1215 like($str, qr/\p{ASCII_Hex_Digit=False}/, "Non-Unicode matches \\p{AHEX=False}");
1216 like($str, qr/\P{ASCII_Hex_Digit=True}/, "Non-Unicode matches \\P{AHEX=True}");
1217 unlike($str, qr/\P{ASCII_Hex_Digit=False}/, "Non-Unicode matches \\P{AHEX=FALSE}");
0bda3001
KW
1218 }
1219
b289b443 1220 {
c256547e 1221 # Test that IDstart works, but because the author (khw) knows
b289b443
KW
1222 # regexes much better than the rest of the core, it is being done here
1223 # in the context of a regex which relies on buffer names beginng with
1224 # IDStarts.
1225 use utf8;
1226 my $str = "abc";
1227 like($str, qr/(?<a>abc)/, "'a' is legal IDStart");
1228 like($str, qr/(?<_>abc)/, "'_' is legal IDStart");
1229 like($str, qr/(?<ß>abc)/, "U+00DF is legal IDStart");
1230 like($str, qr/(?<ℕ>abc)/, "U+2115' is legal IDStart");
1231
1232 # This test works on Unicode 6.0 in which U+2118 and U+212E are legal
1233 # IDStarts there, but are not Word characters, and therefore Perl
1234 # doesn't allow them to be IDStarts. But there is no guarantee that
1235 # Unicode won't change things around in the future so that at some
1236 # future Unicode revision these tests would need to be revised.
1237 foreach my $char ("%", "×", chr(0x2118), chr(0x212E)) {
1238 my $prog = <<"EOP";
1239use utf8;;
1240"abc" =~ qr/(?<$char>abc)/;
1241EOP
1242 utf8::encode($prog);
20e5bab4 1243 fresh_perl_like($prog, qr!Group name must start with a non-digit word character!, {},
b289b443
KW
1244 sprintf("'U+%04X not legal IDFirst'", ord($char)));
1245 }
1246 }
bbdd8bad
KW
1247
1248 { # [perl #101710]
1249 my $pat = "b";
1250 utf8::upgrade($pat);
1251 like("\xffb", qr/$pat/i, "/i: utf8 pattern, non-utf8 string, latin1-char preceding matching char in string");
1252 }
1253
ba510004
FC
1254 { # Crash with @a =~ // warning
1255 local $SIG{__WARN__} = sub {
1256 pass 'no crash for @a =~ // warning'
1257 };
1258 eval ' sub { my @a =~ // } ';
1259 }
1260
cc88c9aa
FC
1261 { # Concat overloading and qr// thingies
1262 my @refs;
1263 my $qr = qr//;
3bbc3d30
YO
1264 package Cat {
1265 require overload;
1266 overload->import(
cc88c9aa
FC
1267 '""' => sub { ${$_[0]} },
1268 '.' => sub {
1269 push @refs, ref $_[1] if ref $_[1];
1270 bless $_[2] ? \"$_[1]${$_[0]}" : \"${$_[0]}$_[1]"
1271 }
3bbc3d30 1272 );
cc88c9aa
FC
1273 }
1274 my $s = "foo";
1275 my $o = bless \$s, Cat::;
1276 /$o$qr/;
1277 is "@refs", "Regexp", '/$o$qr/ passes qr ref to cat overload meth';
1278 }
1279
21eede78
YO
1280 {
1281 my $count=0;
1282 my $str="\n";
1283 $count++ while $str=~/.*/g;
1284 is $count, 2, 'test that ANCH_MBOL works properly. We should get 2 from $count++ while "\n"=~/.*/g';
1285 my $class_count= 0;
1286 $class_count++ while $str=~/[^\n]*/g;
1287 is $class_count, $count, 'while "\n"=~/.*/g and while "\n"=~/[^\n]*/g should behave the same';
1288 my $anch_count= 0;
1289 $anch_count++ while $str=~/^.*/mg;
1290 is $anch_count, 1, 'while "\n"=~/^.*/mg should match only once';
1291 }
96f54887
KW
1292
1293 { # [perl #111174]
1294 use re '/u';
1295 like "\xe0", qr/(?i:\xc0)/, "(?i: shouldn't lose the passed in /u";
1296 use re '/a';
1297 unlike "\x{100}", qr/(?i:\w)/, "(?i: shouldn't lose the passed in /a";
1298 use re '/aa';
1299 unlike 'k', qr/(?i:\N{KELVIN SIGN})/, "(?i: shouldn't lose the passed in /aa";
1300 }
6ae44cd2
DM
1301
1302 {
1303 # the test for whether the pattern should be re-compiled should
1304 # consider the UTF8ness of the previous and current pattern
1305 # string, as well as the physical bytes of the pattern string
1306
1307 for my $s ("\xc4\x80", "\x{100}") {
1308 ok($s =~ /^$s$/, "re-compile check is UTF8-aware");
1309 }
1310 }
1311
e03b874a
DM
1312 # #113682 more overloading and qr//
1313 # when doing /foo$overloaded/, if $overloaded returns
1314 # a qr/(?{})/ via qr or "" overloading, then 'use re 'eval'
1315 # shouldn't be required. Via '.', it still is.
1316 {
1317 package Qr0;
1318 use overload 'qr' => sub { qr/(??{50})/ };
1319
1320 package Qr1;
1321 use overload '""' => sub { qr/(??{51})/ };
1322
1323 package Qr2;
1324 use overload '.' => sub { $_[1] . qr/(??{52})/ };
1325
1326 package Qr3;
1327 use overload '""' => sub { qr/(??{7})/ },
1328 '.' => sub { $_[1] . qr/(??{53})/ };
1329
1330 package Qr_indirect;
1331 use overload '""' => sub { $_[0][0] };
1332
1333 package main;
1334
1335 for my $i (0..3) {
1336 my $o = bless [], "Qr$i";
1337 if ((0,0,1,1)[$i]) {
1338 eval { "A5$i" =~ /^A$o$/ };
1339 like($@, qr/Eval-group not allowed/, "Qr$i");
1340 eval { "5$i" =~ /$o/ };
1341 like($@, ($i == 3 ? qr/^$/ : qr/no method found,/),
1342 "Qr$i bare");
1343 {
1344 use re 'eval';
1345 ok("A5$i" =~ /^A$o$/, "Qr$i - with use re eval");
1346 eval { "5$i" =~ /$o/ };
1347 like($@, ($i == 3 ? qr/^$/ : qr/no method found,/),
1348 "Qr$i bare - with use re eval");
1349 }
1350 }
1351 else {
1352 ok("A5$i" =~ /^A$o$/, "Qr$i");
1353 ok("5$i" =~ /$o/, "Qr$i bare");
1354 }
1355 }
1356
1357 my $o = bless [ bless [], "Qr1" ], 'Qr_indirect';
1358 ok("A51" =~ /^A$o/, "Qr_indirect");
1359 ok("51" =~ /$o/, "Qr_indirect bare");
1360 }
1361
34b39fc9
KW
1362 { # Various flags weren't being set when a [] is optimized into an
1363 # EXACTish node
1364 ;
1365 ;
1366 ok("\x{017F}\x{017F}" =~ qr/^[\x{00DF}]?$/i, "[] to EXACTish optimization");
1367 }
1368
5e4a1da1
KW
1369 {
1370 for my $char (":", "\x{f7}", "\x{2010}") {
1371 my $utf8_char = $char;
1372 utf8::upgrade($utf8_char);
1373 my $display = $char;
1374 $display = display($display);
1375 my $utf8_display = "utf8::upgrade(\"$display\")";
1376
1377 like($char, qr/^$char?$/, "\"$display\" =~ /^$display?\$/");
1378 like($char, qr/^$utf8_char?$/, "my \$p = \"$display\"; utf8::upgrade(\$p); \"$display\" =~ /^\$p?\$/");
1379 like($utf8_char, qr/^$char?$/, "my \$c = \"$display\"; utf8::upgrade(\$c); \"\$c\" =~ /^$display?\$/");
1380 like($utf8_char, qr/^$utf8_char?$/, "my \$c = \"$display\"; utf8::upgrade(\$c); my \$p = \"$display\"; utf8::upgrade(\$p); \"\$c\" =~ /^\$p?\$/");
1381 }
1382 }
1383
4fab19ce
DM
1384 {
1385 # #116148: Pattern utf8ness sticks around globally
1386 # the utf8 in the first match was sticking around for the second
1387 # match
1388
1389 use feature 'unicode_strings';
1390
1391 my $x = "\x{263a}";
1392 $x =~ /$x/;
1393
1394 my $text = "Perl";
1395 ok("Perl" =~ /P.*$/i, '#116148');
1396 }
1397
b8372399
DIM
1398 { # 118297: Mixing up- and down-graded strings in regex
1399 utf8::upgrade(my $u = "\x{e5}");
1400 utf8::downgrade(my $d = "\x{e5}");
1401 my $warned;
1402 local $SIG{__WARN__} = sub { $warned++ if $_[0] =~ /\AMalformed UTF-8/ };
1403 my $re = qr/$u$d/;
1404 ok(!$warned, "no warnings when interpolating mixed up-/downgraded strings in pattern");
1405 my $c = "\x{e5}\x{e5}";
1406 utf8::downgrade($c);
1407 like($c, $re, "mixed up-/downgraded pattern matches downgraded string");
1408 utf8::upgrade($c);
1409 like($c, $re, "mixed up-/downgraded pattern matches upgraded string");
1410 }
4fab19ce 1411
f1e1b256
YO
1412 {
1413 # if we have 87 capture buffers defined then \87 should refer to the 87th.
1414 # test that this is true for 1..100
19438407
S
1415 # Note that this test causes the engine to recurse at runtime, and
1416 # hence use a lot of C stack.
f1e1b256 1417 for my $i (1..100) {
31108f3e
YO
1418 my $capture= "a";
1419 $capture= "($capture)" for 1 .. $i;
1420 for my $mid ("","b") {
1421 my $str= "a${mid}a";
1422 my $backref= "\\$i";
1423 eval {
1424 ok($str=~/$capture$mid$backref/,"\\$i works with $i buffers '$str'=~/...$mid$backref/");
1425 1;
1426 } or do {
1427 is("$@","","\\$i works with $i buffers works with $i buffers '$str'=~/...$mid$backref/");
1428 };
1429 }
f1e1b256
YO
1430 }
1431 }
1432
5411a0e5
DM
1433 # this mixture of readonly (not COWable) and COWable strings
1434 # messed up the capture buffers under COW. The actual test results
1435 # are incidental; the issue is was an AddressSanitizer failure
1436 {
1437 my $c ='AB';
1438 my $res = '';
1439 for ($c, 'C', $c, 'DE') {
1440 ok(/(.)/, "COWable match");
1441 $res .= $1;
1442 }
1443 is($res, "ACAD");
1444 }
1445
1446
61965f1d
FC
1447 {
1448 # RT #45667
1449 # /[#$x]/x didn't interpolate the var $x.
1450 my $b = 'cd';
1451 my $s = 'abcd$%#&';
1452 $s =~ s/[a#$b%]/X/g;
1453 is ($s, 'XbXX$XX&', 'RT #45667 without /x');
1454 $s = 'abcd$%#&';
1455 $s =~ s/[a#$b%]/X/gx;
1456 is ($s, 'XbXX$XX&', 'RT #45667 with /x');
1457 }
1458
0cb43d32
FC
1459 {
1460 no warnings "uninitialized";
1461 my @a;
1462 $a[1]++;
1463 /@a/;
1464 pass('no crash with /@a/ when array has nonexistent elems');
1465 }
1466
475b1e90
FC
1467 {
1468 is runperl(prog => 'delete $::{qq-\cR-}; //; print qq-ok\n-'),
1469 "ok\n",
4a2b275c 1470 'deleting *^R does not result in crashes';
d772ce1c 1471 no warnings 'once';
4a2b275c
FC
1472 *^R = *caretRglobwithnoscalar;
1473 "" =~ /(?{42})/;
1474 is $^R, 42, 'assigning to *^R does not result in a crash';
7e68f152
FC
1475 is runperl(
1476 stderr => 1,
1477 prog => 'eval q|'
1478 .' q-..- =~ /(??{undef *^R;q--})(?{42})/; '
1479 .' print qq-$^R\n-'
1480 .'|'
1481 ),
1482 "42\n",
1483 'undefining *^R within (??{}) does not result in a crash';
475b1e90
FC
1484 }
1485
0b2c2a84
DM
1486 {
1487 # [perl #120446]
1488 # this code should be virtually instantaneous. If it takes 10s of
1489 # seconds, there a bug in intuit_start.
1490 # (this test doesn't actually test for slowness - that involves
1491 # too much danger of false positives on loaded machines - but by
1492 # putting it here, hopefully someone might notice if it suddenly
1493 # runs slowly)
1494 my $s = ('a' x 1_000_000) . 'b';
1495 my $i = 0;
1496 for (1..10_000) {
1497 pos($s) = $_;
1498 $i++ if $s =~/\Gb/g;
1499 }
1500 is($i, 0, "RT 120446: mustn't run slowly");
1501 }
1502
d6ef1678
DM
1503 {
1504 # [perl #120692]
1505 # these tests should be virtually instantaneous. If they take 10s of
1506 # seconds, there's a bug in intuit_start.
1507
1508 my $s = 'ab' x 1_000_000;
1509 utf8::upgrade($s);
1510 1 while $s =~ m/\Ga+ba+b/g;
1511 pass("RT#120692 \\G mustn't run slowly");
1512
1513 $s=~ /^a{1,2}x/ for 1..10_000;
1514 pass("RT#120692 a{1,2} mustn't run slowly");
8e9f2289
DM
1515
1516 $s=~ /ab.{1,2}x/;
1517 pass("RT#120692 ab.{1,2} mustn't run slowly");
b96863b8
DM
1518
1519 $s = "-a-bc" x 250_000;
1520 $s .= "1a1bc";
1521 utf8::upgrade($s);
1522 ok($s =~ /\da\d{0,30000}bc/, "\\d{30000}");
1523
ec19cf6b
DM
1524 $s = "-ab\n" x 250_000;
1525 $s .= "abx";
1526 ok($s =~ /^ab.*x/m, "distant float with /m");
1527
8f10278a
DM
1528 my $r = qr/^abcd/;
1529 $s = "abcd-xyz\n" x 500_000;
1530 $s =~ /$r\d{1,2}xyz/m for 1..200;
1531 pass("BOL within //m mustn't run slowly");
1532
d0d44648
DM
1533 $s = "abcdefg" x 1_000_000;
1534 $s =~ /(?-m:^)abcX?fg/m for 1..100;
1535 pass("BOL within //m mustn't skip absolute anchored check");
1536
e0362b86
DM
1537 $s = "abcdefg" x 1_000_000;
1538 $s =~ /^XX\d{1,10}cde/ for 1..100;
1539 pass("abs anchored float string should fail quickly");
1540
d6ef1678
DM
1541 }
1542
749e076f
KW
1543 # These are based on looking at the code in regcomp.c
1544 # We don't look for specific code, just the existence of an SSC
1545 foreach my $re (qw( qr/a?c/
1546 qr/a?c/i
1547 qr/[ab]?c/
1548 qr/\R?c/
1549 qr/\d?c/d
1550 qr/\w?c/l
1551 qr/\s?c/a
1552 qr/[[:alpha:]]?c/u
1553 )) {
d7919c41
FC
1554 SKIP: {
1555 skip "no re-debug under miniperl" if is_miniperl;
749e076f
KW
1556 my $prog = <<"EOP";
1557use re qw(Debug COMPILE);
1558$re;
1559EOP
20e5bab4 1560 fresh_perl_like($prog, qr/synthetic stclass/, { stderr=>1 }, "$re generates a synthetic start class");
d7919c41 1561 }
749e076f
KW
1562 }
1563
86897aa6
KW
1564 {
1565 like "\x{AA}", qr/a?[\W_]/d, "\\W with /d synthetic start class works";
1566 }
1567
e8ea8356
JH
1568 {
1569 # Verify that the very last Latin-1 U+00FF
1570 # (LATIN SMALL LETTER Y WITH DIAERESIS)
1571 # and its UPPER counterpart (U+0178 which is pure Unicode),
1572 # and likewise for the very first pure Unicode
1573 # (LATIN CAPITAL LETTER A WITH MACRON) fold-match properly,
1574 # and there are no off-by-one logic errors in the transition zone.
1575
1576 ok("\xFF" =~ /\xFF/i, "Y WITH DIAERESIS l =~ l");
1577 ok("\xFF" =~ /\x{178}/i, "Y WITH DIAERESIS l =~ u");
1578 ok("\x{178}" =~ /\xFF/i, "Y WITH DIAERESIS u =~ l");
1579 ok("\x{178}" =~ /\x{178}/i, "Y WITH DIAERESIS u =~ u");
86897aa6 1580
e8ea8356
JH
1581 # U+00FF with U+05D0 (non-casing Hebrew letter).
1582 ok("\xFF\x{5D0}" =~ /\xFF\x{5D0}/i, "Y WITH DIAERESIS l =~ l");
1583 ok("\xFF\x{5D0}" =~ /\x{178}\x{5D0}/i, "Y WITH DIAERESIS l =~ u");
1584 ok("\x{178}\x{5D0}" =~ /\xFF\x{5D0}/i, "Y WITH DIAERESIS u =~ l");
1585 ok("\x{178}\x{5D0}" =~ /\x{178}\x{5D0}/i, "Y WITH DIAERESIS u =~ u");
1586
1587 # U+0100.
1588 ok("\x{100}" =~ /\x{100}/i, "A WITH MACRON u =~ u");
1589 ok("\x{100}" =~ /\x{101}/i, "A WITH MACRON u =~ l");
1590 ok("\x{101}" =~ /\x{100}/i, "A WITH MACRON l =~ u");
1591 ok("\x{101}" =~ /\x{101}/i, "A WITH MACRON l =~ l");
1592 }
0b2c2a84 1593
8373491a
KW
1594 {
1595 use utf8;
1596 ok("abc" =~ /a\85b\85c/x, "NEL is white-space under /x");
1597 }
1598
412f55bb
KW
1599 {
1600 ok('a(b)c' =~ qr(a\(b\)c), "'\\(' is a literal in qr(...)");
1601 ok('a[b]c' =~ qr[a\[b\]c], "'\\[' is a literal in qr[...]");
1602 ok('a{3}c' =~ qr{a\{3\}c}, # Only failed when { could be a meta
1603 "'\\{' is a literal in qr{...}, where it could be a quantifier");
1604
1605 # This one is for completeness
1606 ok('a<b>c' =~ qr<a\<b\>c>, "'\\<' is a literal in qr<...>)");
1607 }
1608
b6e093f2
KW
1609 { # Was getting optimized into EXACT (non-folding node)
1610 my $x = qr/[x]/i;
1611 utf8::upgrade($x);
1612 like("X", qr/$x/, "UTF-8 of /[x]/i matches upper case");
1613 }
1614
fa542f55
FC
1615 { # make sure we get an error when \p{} cannot load Unicode tables
1616 fresh_perl_like(<<' prog that cannot load uni tables',
1617 BEGIN {
1618 @INC = '../lib';
1619 require utf8; require 'utf8_heavy.pl';
1620 @INC = ();
1621 }
1622 $name = 'A B';
1623 if ($name =~ /(\p{IsUpper}) (\p{IsUpper})/){
1624 print "It's good! >$1< >$2<\n";
1625 } else {
1626 print "It's not good...\n";
1627 }
1628 prog that cannot load uni tables
1629 qr/^Can't locate unicore\/Heavy\.pl(?x:
1630 )|^Can't find Unicode property definition/,
1631 undef,
1632 '\p{} should not fail silently when uni tables evanesce');
1633 }
84281c31
A
1634} # End of sub run_tests
1635
16361;