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