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