This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Skip the subnormals tests in tru64.
[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
da42332b 26plan tests => 802; # 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 {
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);
72e868c8
KW
976
977 # Test an inverted posix class with a char also in the class.
978 my $nbsp = chr utf8::unicode_to_native(0xA0);
979 my $non_s = chr utf8::unicode_to_native(0xA1);
980 my $pat_string = "[^\\S ]";
981 unlike(" ", qr/$pat_string/, "Verify ' ' !~ /$pat_string/");
982 like("\t", qr/$pat_string/, "Verify '\\t =~ /$pat_string/");
983 unlike($nbsp, qr/$pat_string/, "Verify non-utf8-NBSP !~ /$pat_string/");
984 utf8::upgrade($nbsp);
985 like($nbsp, qr/$pat_string/, "Verify utf8-NBSP =~ /$pat_string/");
986 unlike($non_s, qr/$pat_string/, "Verify non-utf8-inverted-bang !~ /$pat_string/");
987 utf8::upgrade($non_s);
988 unlike($non_s, qr/$pat_string/, "Verify utf8-inverted-bang !~ /$pat_string/");
84281c31
A
989 }
990 {
4f890a30 991 my $message = '\D, [\D], \d, [\d]';
84281c31
A
992 my @a = map chr, 0 .. 255;
993 my @b = grep /\D/, @a;
994 my @c = grep /[^\d]/, @a;
4f890a30 995 is("@b", "@c", $message);
84281c31
A
996
997 @b = grep /\D/, @a;
998 @c = grep /[\D]/, @a;
4f890a30 999 is("@b", "@c", $message);
84281c31
A
1000
1001 @b = grep /\d/, @a;
1002 @c = grep /[^\D]/, @a;
4f890a30 1003 is("@b", "@c", $message);
84281c31
A
1004
1005 @b = grep /\d/, @a;
1006 @c = grep /[\d]/, @a;
4f890a30 1007 is("@b", "@c", $message);
84281c31
A
1008 }
1009 {
4f890a30 1010 my $message = '\W, [\W], \w, [\w]';
84281c31
A
1011 my @a = map chr, 0 .. 255;
1012 my @b = grep /\W/, @a;
1013 my @c = grep /[^\w]/, @a;
4f890a30 1014 is("@b", "@c", $message);
84281c31
A
1015
1016 @b = grep /\W/, @a;
1017 @c = grep /[\W]/, @a;
4f890a30 1018 is("@b", "@c", $message);
84281c31
A
1019
1020 @b = grep /\w/, @a;
1021 @c = grep /[^\W]/, @a;
4f890a30 1022 is("@b", "@c", $message);
84281c31
A
1023
1024 @b = grep /\w/, @a;
1025 @c = grep /[\w]/, @a;
4f890a30 1026 is("@b", "@c", $message);
84281c31 1027 }
a4c04bdc 1028
84281c31
A
1029 {
1030 # see if backtracking optimization works correctly
4f890a30
NC
1031 my $message = 'Backtrack optimization';
1032 like("\n\n", qr/\n $ \n/x, $message);
1033 like("\n\n", qr/\n* $ \n/x, $message);
1034 like("\n\n", qr/\n+ $ \n/x, $message);
1035 like("\n\n", qr/\n? $ \n/x, $message);
1036 like("\n\n", qr/\n*? $ \n/x, $message);
1037 like("\n\n", qr/\n+? $ \n/x, $message);
1038 like("\n\n", qr/\n?? $ \n/x, $message);
1039 unlike("\n\n", qr/\n*+ $ \n/x, $message);
1040 unlike("\n\n", qr/\n++ $ \n/x, $message);
1041 like("\n\n", qr/\n?+ $ \n/x, $message);
84281c31 1042 }
a4c04bdc 1043
84281c31
A
1044 {
1045 package S;
1046 use overload '""' => sub {'Object S'};
1047 sub new {bless []}
0f289c68 1048
4f890a30 1049 my $message = "Ref stringification";
5895685f
NC
1050 ::ok(do { \my $v} =~ /^SCALAR/, "Scalar ref stringification") or diag($message);
1051 ::ok(do {\\my $v} =~ /^REF/, "Ref ref stringification") or diag($message);
1052 ::ok([] =~ /^ARRAY/, "Array ref stringification") or diag($message);
1053 ::ok({} =~ /^HASH/, "Hash ref stringification") or diag($message);
1054 ::ok('S' -> new =~ /^Object S/, "Object stringification") or diag($message);
84281c31 1055 }
a4c04bdc 1056
84281c31 1057 {
4f890a30
NC
1058 my $message = "Test result of match used as match";
1059 ok('a1b' =~ ('xyz' =~ /y/), $message);
1060 is($`, 'a', $message);
1061 ok('a1b' =~ ('xyz' =~ /t/), $message);
1062 is($`, 'a', $message);
84281c31 1063 }
a4c04bdc 1064
84281c31 1065 {
d728c370 1066 my $message = '"1" is not \s';
c11a8df3
NC
1067 warning_is(sub {unlike("1\n" x 102, qr/^\s*\n/m, $message)},
1068 undef, "$message (did not warn)");
84281c31 1069 }
a4c04bdc 1070
84281c31 1071 {
4f890a30 1072 my $message = '\s, [[:space:]] and [[:blank:]]';
84281c31
A
1073 my %space = (spc => " ",
1074 tab => "\t",
1075 cr => "\r",
1076 lf => "\n",
1077 ff => "\f",
1078 # There's no \v but the vertical tabulator seems miraculously
1079 # be 11 both in ASCII and EBCDIC.
1080 vt => chr(11),
1081 false => "space");
1082
1083 my @space0 = sort grep {$space {$_} =~ /\s/ } keys %space;
1084 my @space1 = sort grep {$space {$_} =~ /[[:space:]]/} keys %space;
1085 my @space2 = sort grep {$space {$_} =~ /[[:blank:]]/} keys %space;
1086
075b9d7d 1087 is("@space0", "cr ff lf spc tab vt", $message);
4f890a30
NC
1088 is("@space1", "cr ff lf spc tab vt", $message);
1089 is("@space2", "spc tab", $message);
84281c31 1090 }
a4c04bdc 1091
ff3f963a 1092 {
c9415951 1093 my $n= 50;
93f09d7b 1094 # this must be a high number and go from 0 to N, as the bug we are looking for doesn't
c9415951
YO
1095 # seem to be predictable. Slight changes to the test make it fail earlier or later.
1096 foreach my $i (0 .. $n)
1097 {
1098 my $str= "\n" x $i;
93f09d7b 1099 ok $str=~/.*\z/, "implicit MBOL check string disable does not break things length=$i";
c9415951
YO
1100 }
1101 }
92f3d482
YO
1102 {
1103 # we are actually testing that we dont die when executing these patterns
1104 use utf8;
1105 my $e = "Böck";
1106 ok(utf8::is_utf8($e),"got a unicode string - rt75680");
1107
1108 ok($e !~ m/.*?[x]$/, "unicode string against /.*?[x]\$/ - rt75680");
1109 ok($e !~ m/.*?\p{Space}$/i, "unicode string against /.*?\\p{space}\$/i - rt75680");
1110 ok($e !~ m/.*?[xyz]$/, "unicode string against /.*?[xyz]\$/ - rt75680");
1111 ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/, "unicode string against big pattern - rt75680");
1112 }
1113 {
1114 # we are actually testing that we dont die when executing these patterns
6d937e95 1115 my $e = "B" . uni_to_native("\x{f6}") . "ck";
92f3d482
YO
1116 ok(!utf8::is_utf8($e), "got a latin string - rt75680");
1117
1118 ok($e !~ m/.*?[x]$/, "latin string against /.*?[x]\$/ - rt75680");
1119 ok($e !~ m/.*?\p{Space}$/i, "latin string against /.*?\\p{space}\$/i - rt75680");
1120 ok($e !~ m/.*?[xyz]$/,"latin string against /.*?[xyz]\$/ - rt75680");
1121 ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/,"latin string against big pattern - rt75680");
1122 }
c920e018
A
1123
1124 {
1125 #
1126 # Tests for bug 77414.
1127 #
1128
4f890a30 1129 my $message = '\p property after empty * match';
c920e018 1130 {
4f890a30
NC
1131 like("1", qr/\s*\pN/, $message);
1132 like("-", qr/\s*\p{Dash}/, $message);
1133 like(" ", qr/\w*\p{Blank}/, $message);
c920e018
A
1134 }
1135
4f890a30
NC
1136 like("1", qr/\s*\pN+/, $message);
1137 like("-", qr/\s*\p{Dash}{1}/, $message);
1138 like(" ", qr/\w*\p{Blank}{1,4}/, $message);
c920e018
A
1139
1140 }
1141
6d937e95
KW
1142 { # Some constructs with Latin1 characters cause a utf8 string not
1143 # to match itself in non-utf8
1144 my $c = uni_to_native("\xc0");
1145 my $pattern = my $utf8_pattern = qr/(($c)+,?)/;
634c83a2
KW
1146 utf8::upgrade($utf8_pattern);
1147 ok $c =~ $pattern, "\\xc0 =~ $pattern; Neither pattern nor target utf8";
d4e0b827 1148 ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; Neither pattern nor target utf8";
634c83a2 1149 ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; pattern utf8, target not";
d4e0b827 1150 ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; pattern utf8, target not";
634c83a2
KW
1151 utf8::upgrade($c);
1152 ok $c =~ $pattern, "\\xc0 =~ $pattern; target utf8, pattern not";
d4e0b827 1153 ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; target utf8, pattern not";
634c83a2 1154 ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; Both target and pattern utf8";
d4e0b827 1155 ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; Both target and pattern utf8";
634c83a2
KW
1156 }
1157
6d937e95 1158 { # Make sure can override the formatting
8cc86590 1159 use feature 'unicode_strings';
6d937e95
KW
1160 ok uni_to_native("\xc0") =~ /\w/, 'Under unicode_strings: "\xc0" =~ /\w/';
1161 ok uni_to_native("\xc0") !~ /(?d:\w)/, 'Under unicode_strings: "\xc0" !~ /(?d:\w)/';
8cc86590
KW
1162 }
1163
704f71be 1164 {
5b6010b3
YO
1165 my $str= "\x{100}";
1166 chop $str;
1167 my $qr= qr/$str/;
de26e0cc 1168 is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag enabled - Bug #80212");
5b6010b3
YO
1169 $str= "";
1170 $qr= qr/$str/;
de26e0cc 1171 is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag disabled - Bug #80212");
5b6010b3
YO
1172
1173 }
1174
72aa120d 1175 {
04934b6d 1176 local $::TODO = "[perl #38133]";
72aa120d
KW
1177
1178 "A" =~ /(((?:A))?)+/;
1179 my $first = $2;
1180
1181 "A" =~ /(((A))?)+/;
1182 my $second = $2;
1183
de26e0cc 1184 is($first, $second);
72aa120d
KW
1185 }
1186
99ca48e1
DM
1187 {
1188 # RT #3516: \G in a m//g expression causes problems
1189 my $count = 0;
1190 while ("abc" =~ m/(\G[ac])?/g) {
1191 last if $count++ > 10;
1192 }
1193 ok($count < 10, 'RT #3516 A');
1194
1195 $count = 0;
1196 while ("abc" =~ m/(\G|.)[ac]/g) {
1197 last if $count++ > 10;
1198 }
1199 ok($count < 10, 'RT #3516 B');
1200
1201 $count = 0;
1202 while ("abc" =~ m/(\G?[ac])?/g) {
1203 last if $count++ > 10;
1204 }
1205 ok($count < 10, 'RT #3516 C');
1206 }
d774cd11
YO
1207 {
1208 # RT #84294: Is this a bug in the simple Perl regex?
1209 # : Nested buffers and (?{...}) dont play nicely on partial matches
1210 our @got= ();
1211 ok("ab" =~ /((\w+)(?{ push @got, $2 })){2}/,"RT #84294: Pattern should match");
d774cd11
YO
1212 my $want= "'ab', 'a', 'b'";
1213 my $got= join(", ", map { defined($_) ? "'$_'" : "undef" } @got);
1214 is($got,$want,'RT #84294: check that "ab" =~ /((\w+)(?{ push @got, $2 })){2}/ leaves @got in the correct state');
1215 }
1216
0bda3001
KW
1217 {
1218 # Suppress warnings, as the non-unicode one comes out even if turn off
1219 # warnings here (because the execution is done in another scope).
1220 local $SIG{__WARN__} = sub {};
1221 my $str = "\x{110000}";
1222
2d88a86a
KW
1223 unlike($str, qr/\p{ASCII_Hex_Digit=True}/, "Non-Unicode doesn't match \\p{AHEX=True}");
1224 like($str, qr/\p{ASCII_Hex_Digit=False}/, "Non-Unicode matches \\p{AHEX=False}");
1225 like($str, qr/\P{ASCII_Hex_Digit=True}/, "Non-Unicode matches \\P{AHEX=True}");
1226 unlike($str, qr/\P{ASCII_Hex_Digit=False}/, "Non-Unicode matches \\P{AHEX=FALSE}");
0bda3001
KW
1227 }
1228
b289b443 1229 {
c256547e 1230 # Test that IDstart works, but because the author (khw) knows
b289b443
KW
1231 # regexes much better than the rest of the core, it is being done here
1232 # in the context of a regex which relies on buffer names beginng with
1233 # IDStarts.
1234 use utf8;
1235 my $str = "abc";
1236 like($str, qr/(?<a>abc)/, "'a' is legal IDStart");
1237 like($str, qr/(?<_>abc)/, "'_' is legal IDStart");
1238 like($str, qr/(?<ß>abc)/, "U+00DF is legal IDStart");
1239 like($str, qr/(?<ℕ>abc)/, "U+2115' is legal IDStart");
1240
1241 # This test works on Unicode 6.0 in which U+2118 and U+212E are legal
1242 # IDStarts there, but are not Word characters, and therefore Perl
1243 # doesn't allow them to be IDStarts. But there is no guarantee that
1244 # Unicode won't change things around in the future so that at some
1245 # future Unicode revision these tests would need to be revised.
1246 foreach my $char ("%", "×", chr(0x2118), chr(0x212E)) {
1247 my $prog = <<"EOP";
1248use utf8;;
1249"abc" =~ qr/(?<$char>abc)/;
1250EOP
1251 utf8::encode($prog);
20e5bab4 1252 fresh_perl_like($prog, qr!Group name must start with a non-digit word character!, {},
b289b443
KW
1253 sprintf("'U+%04X not legal IDFirst'", ord($char)));
1254 }
1255 }
bbdd8bad
KW
1256
1257 { # [perl #101710]
1258 my $pat = "b";
1259 utf8::upgrade($pat);
1260 like("\xffb", qr/$pat/i, "/i: utf8 pattern, non-utf8 string, latin1-char preceding matching char in string");
1261 }
1262
ba510004
FC
1263 { # Crash with @a =~ // warning
1264 local $SIG{__WARN__} = sub {
1265 pass 'no crash for @a =~ // warning'
1266 };
1267 eval ' sub { my @a =~ // } ';
1268 }
1269
cc88c9aa
FC
1270 { # Concat overloading and qr// thingies
1271 my @refs;
1272 my $qr = qr//;
3bbc3d30
YO
1273 package Cat {
1274 require overload;
1275 overload->import(
cc88c9aa
FC
1276 '""' => sub { ${$_[0]} },
1277 '.' => sub {
1278 push @refs, ref $_[1] if ref $_[1];
1279 bless $_[2] ? \"$_[1]${$_[0]}" : \"${$_[0]}$_[1]"
1280 }
3bbc3d30 1281 );
cc88c9aa
FC
1282 }
1283 my $s = "foo";
1284 my $o = bless \$s, Cat::;
1285 /$o$qr/;
1286 is "@refs", "Regexp", '/$o$qr/ passes qr ref to cat overload meth';
1287 }
1288
21eede78
YO
1289 {
1290 my $count=0;
1291 my $str="\n";
1292 $count++ while $str=~/.*/g;
1293 is $count, 2, 'test that ANCH_MBOL works properly. We should get 2 from $count++ while "\n"=~/.*/g';
1294 my $class_count= 0;
1295 $class_count++ while $str=~/[^\n]*/g;
1296 is $class_count, $count, 'while "\n"=~/.*/g and while "\n"=~/[^\n]*/g should behave the same';
1297 my $anch_count= 0;
1298 $anch_count++ while $str=~/^.*/mg;
1299 is $anch_count, 1, 'while "\n"=~/^.*/mg should match only once';
1300 }
96f54887
KW
1301
1302 { # [perl #111174]
1303 use re '/u';
6d937e95
KW
1304 my $A_grave = uni_to_native("\xc0");
1305 like uni_to_native("\xe0"), qr/(?i:$A_grave)/, "(?i: shouldn't lose the passed in /u";
96f54887
KW
1306 use re '/a';
1307 unlike "\x{100}", qr/(?i:\w)/, "(?i: shouldn't lose the passed in /a";
1308 use re '/aa';
1309 unlike 'k', qr/(?i:\N{KELVIN SIGN})/, "(?i: shouldn't lose the passed in /aa";
1310 }
6ae44cd2
DM
1311
1312 {
1313 # the test for whether the pattern should be re-compiled should
1314 # consider the UTF8ness of the previous and current pattern
1315 # string, as well as the physical bytes of the pattern string
1316
6d937e95 1317 for my $s (byte_utf8a_to_utf8n("\xc4\x80"), "\x{100}") {
6ae44cd2
DM
1318 ok($s =~ /^$s$/, "re-compile check is UTF8-aware");
1319 }
1320 }
1321
e03b874a
DM
1322 # #113682 more overloading and qr//
1323 # when doing /foo$overloaded/, if $overloaded returns
1324 # a qr/(?{})/ via qr or "" overloading, then 'use re 'eval'
1325 # shouldn't be required. Via '.', it still is.
1326 {
1327 package Qr0;
1328 use overload 'qr' => sub { qr/(??{50})/ };
1329
1330 package Qr1;
1331 use overload '""' => sub { qr/(??{51})/ };
1332
1333 package Qr2;
1334 use overload '.' => sub { $_[1] . qr/(??{52})/ };
1335
1336 package Qr3;
1337 use overload '""' => sub { qr/(??{7})/ },
1338 '.' => sub { $_[1] . qr/(??{53})/ };
1339
1340 package Qr_indirect;
1341 use overload '""' => sub { $_[0][0] };
1342
1343 package main;
1344
1345 for my $i (0..3) {
1346 my $o = bless [], "Qr$i";
1347 if ((0,0,1,1)[$i]) {
1348 eval { "A5$i" =~ /^A$o$/ };
1349 like($@, qr/Eval-group not allowed/, "Qr$i");
1350 eval { "5$i" =~ /$o/ };
1351 like($@, ($i == 3 ? qr/^$/ : qr/no method found,/),
1352 "Qr$i bare");
1353 {
1354 use re 'eval';
1355 ok("A5$i" =~ /^A$o$/, "Qr$i - with use re eval");
1356 eval { "5$i" =~ /$o/ };
1357 like($@, ($i == 3 ? qr/^$/ : qr/no method found,/),
1358 "Qr$i bare - with use re eval");
1359 }
1360 }
1361 else {
1362 ok("A5$i" =~ /^A$o$/, "Qr$i");
1363 ok("5$i" =~ /$o/, "Qr$i bare");
1364 }
1365 }
1366
1367 my $o = bless [ bless [], "Qr1" ], 'Qr_indirect';
1368 ok("A51" =~ /^A$o/, "Qr_indirect");
1369 ok("51" =~ /$o/, "Qr_indirect bare");
1370 }
1371
34b39fc9
KW
1372 { # Various flags weren't being set when a [] is optimized into an
1373 # EXACTish node
1374 ;
1375 ;
6d937e95
KW
1376 my $sharp_s = uni_to_native("\xdf");
1377 ok("\x{017F}\x{017F}" =~ qr/^[$sharp_s]?$/i, "[] to EXACTish optimization");
34b39fc9
KW
1378 }
1379
5e4a1da1 1380 {
6d937e95 1381 for my $char (":", uni_to_native("\x{f7}"), "\x{2010}") {
5e4a1da1
KW
1382 my $utf8_char = $char;
1383 utf8::upgrade($utf8_char);
1384 my $display = $char;
1385 $display = display($display);
1386 my $utf8_display = "utf8::upgrade(\"$display\")";
1387
1388 like($char, qr/^$char?$/, "\"$display\" =~ /^$display?\$/");
1389 like($char, qr/^$utf8_char?$/, "my \$p = \"$display\"; utf8::upgrade(\$p); \"$display\" =~ /^\$p?\$/");
1390 like($utf8_char, qr/^$char?$/, "my \$c = \"$display\"; utf8::upgrade(\$c); \"\$c\" =~ /^$display?\$/");
1391 like($utf8_char, qr/^$utf8_char?$/, "my \$c = \"$display\"; utf8::upgrade(\$c); my \$p = \"$display\"; utf8::upgrade(\$p); \"\$c\" =~ /^\$p?\$/");
1392 }
1393 }
1394
4fab19ce
DM
1395 {
1396 # #116148: Pattern utf8ness sticks around globally
1397 # the utf8 in the first match was sticking around for the second
1398 # match
1399
1400 use feature 'unicode_strings';
1401
1402 my $x = "\x{263a}";
1403 $x =~ /$x/;
1404
1405 my $text = "Perl";
1406 ok("Perl" =~ /P.*$/i, '#116148');
1407 }
1408
b8372399
DIM
1409 { # 118297: Mixing up- and down-graded strings in regex
1410 utf8::upgrade(my $u = "\x{e5}");
1411 utf8::downgrade(my $d = "\x{e5}");
1412 my $warned;
1413 local $SIG{__WARN__} = sub { $warned++ if $_[0] =~ /\AMalformed UTF-8/ };
1414 my $re = qr/$u$d/;
1415 ok(!$warned, "no warnings when interpolating mixed up-/downgraded strings in pattern");
1416 my $c = "\x{e5}\x{e5}";
1417 utf8::downgrade($c);
1418 like($c, $re, "mixed up-/downgraded pattern matches downgraded string");
1419 utf8::upgrade($c);
1420 like($c, $re, "mixed up-/downgraded pattern matches upgraded string");
1421 }
4fab19ce 1422
f1e1b256
YO
1423 {
1424 # if we have 87 capture buffers defined then \87 should refer to the 87th.
1425 # test that this is true for 1..100
19438407
S
1426 # Note that this test causes the engine to recurse at runtime, and
1427 # hence use a lot of C stack.
f1e1b256 1428 for my $i (1..100) {
31108f3e
YO
1429 my $capture= "a";
1430 $capture= "($capture)" for 1 .. $i;
1431 for my $mid ("","b") {
1432 my $str= "a${mid}a";
1433 my $backref= "\\$i";
1434 eval {
1435 ok($str=~/$capture$mid$backref/,"\\$i works with $i buffers '$str'=~/...$mid$backref/");
1436 1;
1437 } or do {
1438 is("$@","","\\$i works with $i buffers works with $i buffers '$str'=~/...$mid$backref/");
1439 };
1440 }
f1e1b256
YO
1441 }
1442 }
1443
5411a0e5
DM
1444 # this mixture of readonly (not COWable) and COWable strings
1445 # messed up the capture buffers under COW. The actual test results
1446 # are incidental; the issue is was an AddressSanitizer failure
1447 {
1448 my $c ='AB';
1449 my $res = '';
1450 for ($c, 'C', $c, 'DE') {
1451 ok(/(.)/, "COWable match");
1452 $res .= $1;
1453 }
1454 is($res, "ACAD");
1455 }
1456
1457
61965f1d
FC
1458 {
1459 # RT #45667
1460 # /[#$x]/x didn't interpolate the var $x.
1461 my $b = 'cd';
1462 my $s = 'abcd$%#&';
1463 $s =~ s/[a#$b%]/X/g;
1464 is ($s, 'XbXX$XX&', 'RT #45667 without /x');
1465 $s = 'abcd$%#&';
1466 $s =~ s/[a#$b%]/X/gx;
1467 is ($s, 'XbXX$XX&', 'RT #45667 with /x');
1468 }
1469
0cb43d32
FC
1470 {
1471 no warnings "uninitialized";
1472 my @a;
1473 $a[1]++;
1474 /@a/;
1475 pass('no crash with /@a/ when array has nonexistent elems');
1476 }
1477
475b1e90
FC
1478 {
1479 is runperl(prog => 'delete $::{qq-\cR-}; //; print qq-ok\n-'),
1480 "ok\n",
4a2b275c 1481 'deleting *^R does not result in crashes';
d772ce1c 1482 no warnings 'once';
4a2b275c
FC
1483 *^R = *caretRglobwithnoscalar;
1484 "" =~ /(?{42})/;
1485 is $^R, 42, 'assigning to *^R does not result in a crash';
7e68f152
FC
1486 is runperl(
1487 stderr => 1,
1488 prog => 'eval q|'
1489 .' q-..- =~ /(??{undef *^R;q--})(?{42})/; '
1490 .' print qq-$^R\n-'
1491 .'|'
1492 ),
1493 "42\n",
1494 'undefining *^R within (??{}) does not result in a crash';
475b1e90
FC
1495 }
1496
8498497f
KW
1497 SKIP: { # Test literal range end point special handling
1498 unless ($::IS_EBCDIC) {
423df6e4 1499 skip "Valid only for EBCDIC", 24;
8498497f
KW
1500 }
1501
1502 like("\x89", qr/[i-j]/, '"\x89" should match [i-j]');
1503 unlike("\x8A", qr/[i-j]/, '"\x8A" shouldnt match [i-j]');
1504 unlike("\x90", qr/[i-j]/, '"\x90" shouldnt match [i-j]');
1505 like("\x91", qr/[i-j]/, '"\x91" should match [i-j]');
1506
423df6e4
KW
1507 like("\x89", qr/[i-\N{LATIN SMALL LETTER J}]/, '"\x89" should match [i-\N{LATIN SMALL LETTER J}]');
1508 unlike("\x8A", qr/[i-\N{LATIN SMALL LETTER J}]/, '"\x8A" shouldnt match [i-\N{LATIN SMALL LETTER J}]');
1509 unlike("\x90", qr/[i-\N{LATIN SMALL LETTER J}]/, '"\x90" shouldnt match [i-\N{LATIN SMALL LETTER J}]');
1510 like("\x91", qr/[i-\N{LATIN SMALL LETTER J}]/, '"\x91" should match [i-\N{LATIN SMALL LETTER J}]');
1511
1512 like("\x89", qr/[i-\N{U+6A}]/, '"\x89" should match [i-\N{U+6A}]');
1513 unlike("\x8A", qr/[i-\N{U+6A}]/, '"\x8A" shouldnt match [i-\N{U+6A}]');
1514 unlike("\x90", qr/[i-\N{U+6A}]/, '"\x90" shouldnt match [i-\N{U+6A}]');
1515 like("\x91", qr/[i-\N{U+6A}]/, '"\x91" should match [i-\N{U+6A}]');
1516
1517 like("\x89", qr/[\N{U+69}-\N{U+6A}]/, '"\x89" should match [\N{U+69}-\N{U+6A}]');
1518 unlike("\x8A", qr/[\N{U+69}-\N{U+6A}]/, '"\x8A" shouldnt match [\N{U+69}-\N{U+6A}]');
1519 unlike("\x90", qr/[\N{U+69}-\N{U+6A}]/, '"\x90" shouldnt match [\N{U+69}-\N{U+6A}]');
1520 like("\x91", qr/[\N{U+69}-\N{U+6A}]/, '"\x91" should match [\N{U+69}-\N{U+6A}]');
1521
8498497f
KW
1522 like("\x89", qr/[i-\x{91}]/, '"\x89" should match [i-\x{91}]');
1523 like("\x8A", qr/[i-\x{91}]/, '"\x8A" should match [i-\x{91}]');
1524 like("\x90", qr/[i-\x{91}]/, '"\x90" should match [i-\x{91}]');
1525 like("\x91", qr/[i-\x{91}]/, '"\x91" should match [i-\x{91}]');
1526
1527 # Need to use eval, because tries to compile on ASCII platforms even
1528 # though the tests are skipped, and fails because 0x89-j is an illegal
1529 # range there.
6d937e95
KW
1530 like("\x89", eval 'qr/[\x{89}-j]/', '"\x89" should match [\x{89}-j]');
1531 like("\x8A", eval 'qr/[\x{89}-j]/', '"\x8A" should match [\x{89}-j]');
1532 like("\x90", eval 'qr/[\x{89}-j]/', '"\x90" should match [\x{89}-j]');
1533 like("\x91", eval 'qr/[\x{89}-j]/', '"\x91" should match [\x{89}-j]');
8498497f 1534 }
d6ef1678 1535
749e076f
KW
1536 # These are based on looking at the code in regcomp.c
1537 # We don't look for specific code, just the existence of an SSC
1538 foreach my $re (qw( qr/a?c/
1539 qr/a?c/i
1540 qr/[ab]?c/
1541 qr/\R?c/
1542 qr/\d?c/d
1543 qr/\w?c/l
1544 qr/\s?c/a
b35552de 1545 qr/[[:lower:]]?c/u
749e076f 1546 )) {
d7919c41
FC
1547 SKIP: {
1548 skip "no re-debug under miniperl" if is_miniperl;
749e076f
KW
1549 my $prog = <<"EOP";
1550use re qw(Debug COMPILE);
1551$re;
1552EOP
20e5bab4 1553 fresh_perl_like($prog, qr/synthetic stclass/, { stderr=>1 }, "$re generates a synthetic start class");
d7919c41 1554 }
749e076f
KW
1555 }
1556
86897aa6
KW
1557 {
1558 like "\x{AA}", qr/a?[\W_]/d, "\\W with /d synthetic start class works";
1559 }
1560
6d937e95
KW
1561 SKIP: {
1562 skip("Tests are ASCII-centric, some would fail on EBCDIC", 12) if $::IS_EBCDIC;
1563
e8ea8356
JH
1564 # Verify that the very last Latin-1 U+00FF
1565 # (LATIN SMALL LETTER Y WITH DIAERESIS)
1566 # and its UPPER counterpart (U+0178 which is pure Unicode),
1567 # and likewise for the very first pure Unicode
1568 # (LATIN CAPITAL LETTER A WITH MACRON) fold-match properly,
1569 # and there are no off-by-one logic errors in the transition zone.
1570
1571 ok("\xFF" =~ /\xFF/i, "Y WITH DIAERESIS l =~ l");
1572 ok("\xFF" =~ /\x{178}/i, "Y WITH DIAERESIS l =~ u");
1573 ok("\x{178}" =~ /\xFF/i, "Y WITH DIAERESIS u =~ l");
1574 ok("\x{178}" =~ /\x{178}/i, "Y WITH DIAERESIS u =~ u");
86897aa6 1575
e8ea8356
JH
1576 # U+00FF with U+05D0 (non-casing Hebrew letter).
1577 ok("\xFF\x{5D0}" =~ /\xFF\x{5D0}/i, "Y WITH DIAERESIS l =~ l");
1578 ok("\xFF\x{5D0}" =~ /\x{178}\x{5D0}/i, "Y WITH DIAERESIS l =~ u");
1579 ok("\x{178}\x{5D0}" =~ /\xFF\x{5D0}/i, "Y WITH DIAERESIS u =~ l");
1580 ok("\x{178}\x{5D0}" =~ /\x{178}\x{5D0}/i, "Y WITH DIAERESIS u =~ u");
1581
1582 # U+0100.
1583 ok("\x{100}" =~ /\x{100}/i, "A WITH MACRON u =~ u");
1584 ok("\x{100}" =~ /\x{101}/i, "A WITH MACRON u =~ l");
1585 ok("\x{101}" =~ /\x{100}/i, "A WITH MACRON l =~ u");
1586 ok("\x{101}" =~ /\x{101}/i, "A WITH MACRON l =~ l");
1587 }
0b2c2a84 1588
8373491a
KW
1589 {
1590 use utf8;
1591 ok("abc" =~ /a\85b\85c/x, "NEL is white-space under /x");
1592 }
1593
412f55bb
KW
1594 {
1595 ok('a(b)c' =~ qr(a\(b\)c), "'\\(' is a literal in qr(...)");
1596 ok('a[b]c' =~ qr[a\[b\]c], "'\\[' is a literal in qr[...]");
1597 ok('a{3}c' =~ qr{a\{3\}c}, # Only failed when { could be a meta
1598 "'\\{' is a literal in qr{...}, where it could be a quantifier");
1599
1600 # This one is for completeness
1601 ok('a<b>c' =~ qr<a\<b\>c>, "'\\<' is a literal in qr<...>)");
1602 }
1603
b6e093f2
KW
1604 { # Was getting optimized into EXACT (non-folding node)
1605 my $x = qr/[x]/i;
1606 utf8::upgrade($x);
1607 like("X", qr/$x/, "UTF-8 of /[x]/i matches upper case");
1608 }
1609
fa542f55
FC
1610 { # make sure we get an error when \p{} cannot load Unicode tables
1611 fresh_perl_like(<<' prog that cannot load uni tables',
1612 BEGIN {
1613 @INC = '../lib';
1614 require utf8; require 'utf8_heavy.pl';
1615 @INC = ();
1616 }
1617 $name = 'A B';
1618 if ($name =~ /(\p{IsUpper}) (\p{IsUpper})/){
1619 print "It's good! >$1< >$2<\n";
1620 } else {
1621 print "It's not good...\n";
1622 }
1623 prog that cannot load uni tables
1624 qr/^Can't locate unicore\/Heavy\.pl(?x:
1625 )|^Can't find Unicode property definition/,
1626 undef,
1627 '\p{} should not fail silently when uni tables evanesce');
1628 }
59206727
KW
1629
1630 { # Special handling of literal-ended ranges in [...] was breaking this
1631 use utf8;
1632 like("ÿ", qr/[ÿ-ÿ]/, "\"ÿ\" should match [ÿ-ÿ]");
1633 }
405dffcb
KW
1634
1635 { # [perl #123539]
1636 like("TffffffffffffTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT5TTTTTTTTTTTTTTTTTTTTTTTTT3TTgTTTTTTTTTTTTTTTTTTTTT2TTTTTTTTTTTTTTTTTTTTTTTHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHiHHHHHHHfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff&ffff", qr/TffffffffffffTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT5TTTTTTTTTTTTTTTTTTTTTTTTT3TTgTTTTTTTTTTTTTTTTTTTTT2TTTTTTTTTTTTTTTTTTTTTTTHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHiHHHHHHHfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff&ffff/il, "");
1637 like("TffffffffffffT\x{100}TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT5TTTTTTTTTTTTTTTTTTTTTTTTT3TTgTTTTTTTTTTTTTTTTTTTTT2TTTTTTTTTTTTTTTTTTTTTTTHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHiHHHHHHHfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff&ffff", qr/TffffffffffffT\x{100}TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT5TTTTTTTTTTTTTTTTTTTTTTTTT3TTgTTTTTTTTTTTTTTTTTTTTT2TTTTTTTTTTTTTTTTTTTTTTTHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHiHHHHHHHfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff&ffff/il, "");
1638 }
1639
56f81afc
HS
1640 { # [perl #123604]
1641 my($s, $x, @x) = ('abc', 'a', 'd');
1642 my $long = 'b' x 2000;
1643 my $eval = q{$s =~ m{$x[bbb]c} ? 1 : 0};
1644 $eval =~ s{bbb}{$long};
1645 my $match = eval $eval;
1646 ok(1, "did not crash");
1647 ok($match, "[bbb...] resolved as character class, not subscript");
1648 }
8a6d8ec6
HS
1649
1650 { # [perl #123755]
1651 for my $pat ('(??', '(?P', '(?i-') {
1652 eval qq{ qr/$pat/ };
1653 ok(1, "qr/$pat/ did not crash");
1654 eval qq{ qr/${pat}\x{123}/ };
1655 my $e = $@;
1656 like($e, qr{\x{123}},
1657 "qr/${pat}x/ shows x in error even if it's a wide character");
1658 }
1659 }
b3725d49
HS
1660
1661 {
1662 # Expect one of these sizes to cause overflow and wrap to negative
1663 for my $bits (32, 64) {
1664 my $wrapneg = 2 ** ($bits - 2) * 3;
1665 for my $sign ('', '-') {
1666 my $pat = sprintf "qr/(?%s%u)/", $sign, $wrapneg;
1667 eval $pat;
1668 ok(1, "big backref $pat did not crash");
1669 }
1670 }
1671 }
65651c16 1672 {
65651c16
YO
1673 # Test that we handle qr/\8888888/ and variants without an infinite loop,
1674 # we use a test within a test so we can todo it, and make sure we don't
1675 # infinite loop our tests.
1676 # NOTE - Do not put quotes in the code!
1677 # NOTE - We have to triple escape the backref in the pattern below.
1678 my $code='
1679 BEGIN{require q(test.pl);}
1680 watchdog(3);
1681 for my $len (1 .. 20) {
1682 my $eights= q(8) x $len;
1683 eval qq{ qr/\\\\$eights/ };
1684 }
1685 print q(No infinite loop here!);
1686 ';
1687 fresh_perl_is($code, "No infinite loop here!", {},
1688 "test that we handle things like m/\\888888888/ without infinite loops" );
1689 }
4a0c7558 1690
22b433ef
KW
1691 { # Test that we handle some malformed UTF-8 without looping [perl
1692 # #123562]
1693
1694 my $code='
1695 BEGIN{require q(test.pl);}
1696 use Encode qw(_utf8_on);
95c6a212
KW
1697 # \x80 and \x41 are continuation bytes in their respective
1698 # character sets
1699 my $malformed = (ord("A") == 65) ? "a\x80\n" : "a\x41\n";
1700 utf8::downgrade($malformed);
22b433ef
KW
1701 _utf8_on($malformed);
1702 watchdog(3);
1703 $malformed =~ /(\n\r|\r)$/;
1704 print q(No infinite loop here!);
1705 ';
1706 fresh_perl_like($code, qr/Malformed UTF-8 character/, {},
1707 "test that we handle some UTF-8 malformations without looping" );
1708 }
1709
4a0c7558
HS
1710 {
1711 # [perl #123843] hits SEGV trying to compile this pattern
1712 my $match;
1713 eval q{ ($match) = ("xxyxxyxy" =~ m{(x+(y(?1))*)}) };
1714 ok(1, "compiled GOSUB in CURLYM ok");
1715 is($match, 'xxyxxyx', "matched GOSUB in CURLYM");
1716 }
e92b41c6
HS
1717
1718 {
1719 # [perl #123852] doesn't avoid all the capture-related work with
1720 # //n, leading to possible memory corruption
1721 eval q{ qr{()(?1)}n };
1722 my $error = $@;
1723 ok(1, "qr{()(?1)}n didn't crash");
1724 like($error, qr{Reference to nonexistent group},
1725 'gave appropriate error for qr{()(?1)}n');
1726 }
9457bb3f
KW
1727
1728 {
1729 # [perl #126406] panic with unmatchable quantifier
1730 my $code='
1731 no warnings "regexp";
1732 "" =~ m/(.0\N{6,0}0\N{6,0}000000000000000000000000000000000)/;
1733 ';
1734 fresh_perl_is($code, "", {},
1735 "perl [#126406] panic");
1736 }
ba6840fb 1737 {
401a8022
YO
1738 my $bug="[perl #126182]"; # test for infinite pattern recursion
1739 for my $tuple (
401a8022
YO
1740 [ 'q(a)=~/(.(?2))((?<=(?=(?1)).))/', "died", "look ahead left recursion fails fast" ],
1741 [ 'q(aa)=~/(?R)a/', "died", "left-recursion fails fast", ],
1742 [ 'q(bbaa)=~/(?&x)(?(DEFINE)(?<x>(?&y)*a)(?<y>(?&x)*b))/',
1743 "died", "inter-cyclic optional left recursion dies" ],
1744 [ 'q(abc) =~ /a((?1)?)c/', "died", "optional left recursion dies" ],
1745 [ 'q(abc) =~ /a((?1)??)c/', "died", "min mod left recursion dies" ],
1746 [ 'q(abc) =~ /a((?1)*)c/', "died", "* left recursion dies" ],
1747 [ 'q(abc) =~ /a((?1)+)c/', "died", "+ left recursion dies" ],
1748 [ 'q(abc) =~ /a((?1){0,3})c/', "died", "{0,3} left recursion fails fast" ],
1749
1750 [ 'q(aaabbb)=~/a(?R)?b/', "matched", "optional self recursion works" ],
ce12e254
YO
1751 [ '"((5maa-maa)(maa-3maa))" =~ /(\\\\((?:[^()]++|(?0))*+\\\\))/', "matched",
1752 "recursion and possessive captures", "((5maa-maa)(maa-3maa))"],
1753 [ '"((5maa-maa)(maa-3maa))" =~ /(\\\\((?:[^()]++|(?1))*+\\\\))/', "matched",
1754 "recursion and possessive captures", "((5maa-maa)(maa-3maa))"],
1755 [ '"((5maa-maa)(maa-3maa))" =~ /(\\\\((?:[^()]+|(?0))*\\\\))/', "matched",
1756 "recursion and possessive captures", "((5maa-maa)(maa-3maa))"],
1757 [ '"((5maa-maa)(maa-3maa))" =~ /(\\\\((?:[^()]+|(?1))*\\\\))/', "matched",
1758 "recursion and possessive captures", "((5maa-maa)(maa-3maa))"],
401a8022 1759 ) {
ce12e254 1760 my ($expr, $expect, $test_name, $cap1)= @$tuple;
401a8022
YO
1761 # avoid quotes in this code!
1762 my $code='
1763 BEGIN{require q(test.pl);}
1764 watchdog(3);
ce12e254
YO
1765 my $status= eval(q{ !(' . $expr . ') ? q(failed) : ' .
1766 ($cap1 ? '($1 ne q['.$cap1.']) ? qq(badmatch:$1) : ' : '') .
1767 ' q(matched) })
1768 || ( ( $@ =~ /Infinite recursion/ ) ? qq(died) : q(strange-death) );
401a8022
YO
1769 print $status;
1770 ';
1771 fresh_perl_is($code, $expect, {}, "$bug - $test_name" );
1772 }
ba6840fb 1773 }
8df928d2
YO
1774 {
1775 fresh_perl_is('
1776 BEGIN{require q(test.pl);}
1777 watchdog(3);
1778 $SIG{ALRM} = sub {print "Timeout\n"; exit(1)};
1779 alarm 1;
1780 $_ = "a" x 1000 . "b" x 1000 . "c" x 1000;
1781 /.*a.*b.*c.*[de]/;
1782 ',"Timeout",{},"Test Perl 73464")
1783 }
33bc5d3d
KW
1784
1785 { # [perl #128686], crashed the the interpreter
1786 my $AE = chr utf8::unicode_to_native(0xC6);
1787 my $ae = chr utf8::unicode_to_native(0xE6);
1788 my $re = qr/[$ae\s]/i;
1789 ok($AE !~ $re, '/[\xE6\s]/i doesn\'t match \xC6 when not in UTF-8');
1790 utf8::upgrade $AE;
1791 ok($AE =~ $re, '/[\xE6\s]/i matches \xC6 when in UTF-8');
1792 }
1793
aeb72ccb
KW
1794 { # [perl #126606 crashed the interpreter
1795 no warnings 'deprecated';
1796 like("sS", qr/\N{}Ss|/i, "\N{} with empty branch alternation works");
1797 }
1798
cf8fa733
DC
1799 TODO: {
1800 local $::TODO = "RT #21491: m'' interpolates escape sequences";
1801 is(0+("\n" =~ m'\n'), 0, q|RT #21491: m'\n' should not interpolate|);
da42332b
YO
1802 }
1803
1804 {
2c2284d8
KW
1805 my $str = "a\xB6";
1806 ok( $str =~ m{^(a|a\x{b6})$}, "fix [perl #129950] - latin1 case" );
da42332b 1807 utf8::upgrade($str);
2c2284d8 1808 ok( $str =~ m{^(a|a\x{b6})$}, "fix [perl #129950] - utf8 case" );
da42332b 1809 }
84281c31
A
1810} # End of sub run_tests
1811
18121;