This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
revert to 5.8.x semantics for \s \w and \d
[perl5.git] / t / re / pat_advanced.t
CommitLineData
e425a60b
YO
1#!./perl
2#
3# This is a home for regular expression tests that don't fit into
4# the format supported by re/regexp.t. If you want to add a test
5# that does fit that format, add it to re/re_tests, not here.
6
7use strict;
8use warnings;
9use 5.010;
10
11
12sub run_tests;
13
14$| = 1;
15
e425a60b
YO
16
17BEGIN {
18 chdir 't' if -d 't';
9d45b377
YO
19 @INC = ('../lib','.');
20 do "re/ReTest.pl" or die $@;
e425a60b 21}
e425a60b 22
e425a60b 23
0f289c68 24plan tests => 1146; # Update this when adding/deleting tests.
e425a60b 25
9d45b377 26run_tests() unless caller;
e425a60b
YO
27
28#
29# Tests start here.
30#
31sub run_tests {
32
e425a60b
YO
33 SKIP:
34 {
35 local $Message = '\C matches octet';
36 $_ = "a\x{100}b";
37 ok /(.)(\C)(\C)(.)/ or skip q [\C doesn't match], 4;
38 iseq $1, "a";
39 if ($IS_ASCII) { # ASCII (or equivalent), should be UTF-8
40 iseq $2, "\xC4";
41 iseq $3, "\x80";
42 }
43 elsif ($IS_EBCDIC) { # EBCDIC (or equivalent), should be UTF-EBCDIC
44 iseq $2, "\x8C";
45 iseq $3, "\x41";
46 }
47 else {
48 SKIP: {
49 ok 0, "Unexpected platform", "ord ('A') = $ordA";
50 skip "Unexpected platform";
51 }
52 }
53 iseq $4, "b";
54 }
55
56
57 SKIP:
58 {
59 local $Message = '\C matches octet';
60 $_ = "\x{100}";
61 ok /(\C)/g or skip q [\C doesn't match], 2;
62 if ($IS_ASCII) {
63 iseq $1, "\xC4";
64 }
65 elsif ($IS_EBCDIC) {
66 iseq $1, "\x8C";
67 }
68 else {
69 ok 0, "Unexpected platform", "ord ('A') = $ordA";
70 }
71 ok /(\C)/g or skip q [\C doesn't match];
72 if ($IS_ASCII) {
73 iseq $1, "\x80";
74 }
75 elsif ($IS_EBCDIC) {
76 iseq $1, "\x41";
77 }
78 else {
79 ok 0, "Unexpected platform", "ord ('A') = $ordA";
80 }
81 }
82
83
84 {
85 # Japhy -- added 03/03/2001
86 () = (my $str = "abc") =~ /(...)/;
87 $str = "def";
88 iseq $1, "abc", 'Changing subject does not modify $1';
89 }
90
91
92 SKIP:
93 {
94 # The trick is that in EBCDIC the explicit numeric range should
95 # match (as also in non-EBCDIC) but the explicit alphabetic range
96 # should not match.
97 ok "\x8e" =~ /[\x89-\x91]/, '"\x8e" =~ /[\x89-\x91]/';
98 ok "\xce" =~ /[\xc9-\xd1]/, '"\xce" =~ /[\xc9-\xd1]/';
99
100 skip "Not an EBCDIC platform", 2 unless ord ('i') == 0x89 &&
101 ord ('J') == 0xd1;
102
103 # In most places these tests would succeed since \x8e does not
104 # in most character sets match 'i' or 'j' nor would \xce match
105 # 'I' or 'J', but strictly speaking these tests are here for
106 # the good of EBCDIC, so let's test these only there.
107 nok "\x8e" !~ /[i-j]/, '"\x8e" !~ /[i-j]/';
108 nok "\xce" !~ /[I-J]/, '"\xce" !~ /[I-J]/';
109 }
110
111
112 {
113 ok "\x{ab}" =~ /\x{ab}/, '"\x{ab}" =~ /\x{ab}/ ';
114 ok "\x{abcd}" =~ /\x{abcd}/, '"\x{abcd}" =~ /\x{abcd}/';
115 }
116
117
118 {
119 local $Message = 'bug id 20001008.001';
120
121 my @x = ("stra\337e 138", "stra\337e 138");
122 for (@x) {
123 ok s/(\d+)\s*([\w\-]+)/$1 . uc $2/e;
124 ok my ($latin) = /^(.+)(?:\s+\d)/;
125 iseq $latin, "stra\337e";
0f289c68 126 ok $latin =~ s/stra\337e/straße/;
e425a60b
YO
127 #
128 # Previous code follows, but outcommented - there were no tests.
129 #
130 # $latin =~ s/stra\337e/straße/; # \303\237 after the 2nd a
131 # use utf8; # needed for the raw UTF-8
132 # $latin =~ s!(s)tr(?:aß|s+e)!$1tr.!; # \303\237 after the a
133 }
134 }
135
136
137 {
138 local $Message = 'Test \x escapes';
139 ok "ba\xd4c" =~ /([a\xd4]+)/ && $1 eq "a\xd4";
140 ok "ba\xd4c" =~ /([a\xd4]+)/ && $1 eq "a\x{d4}";
141 ok "ba\x{d4}c" =~ /([a\xd4]+)/ && $1 eq "a\x{d4}";
142 ok "ba\x{d4}c" =~ /([a\xd4]+)/ && $1 eq "a\xd4";
143 ok "ba\xd4c" =~ /([a\x{d4}]+)/ && $1 eq "a\xd4";
144 ok "ba\xd4c" =~ /([a\x{d4}]+)/ && $1 eq "a\x{d4}";
145 ok "ba\x{d4}c" =~ /([a\x{d4}]+)/ && $1 eq "a\x{d4}";
146 ok "ba\x{d4}c" =~ /([a\x{d4}]+)/ && $1 eq "a\xd4";
147 }
148
149
e425a60b
YO
150 SKIP:
151 {
152 local $Message = 'Match code points > 255';
153 $_ = "abc\x{100}\x{200}\x{300}\x{380}\x{400}defg";
154 ok /(.\x{300})./ or skip "No match", 4;
155 ok $` eq "abc\x{100}" && length ($`) == 4;
156 ok $& eq "\x{200}\x{300}\x{380}" && length ($&) == 3;
157 ok $' eq "\x{400}defg" && length ($') == 5;
158 ok $1 eq "\x{200}\x{300}" && length ($1) == 2;
159 }
160
161
e425a60b
YO
162
163 {
164 my $x = "\x{10FFFD}";
165 $x =~ s/(.)/$1/g;
166 ok ord($x) == 0x10FFFD && length($x) == 1, "From Robin Houston";
167 }
168
169
170 {
171 my %d = (
172 "7f" => [0, 0, 0],
173 "80" => [1, 1, 0],
174 "ff" => [1, 1, 0],
175 "100" => [0, 1, 1],
176 );
177 SKIP:
178 while (my ($code, $match) = each %d) {
179 local $Message = "Properties of \\x$code";
180 my $char = eval qq ["\\x{$code}"];
181 my $i = 0;
182 ok (($char =~ /[\x80-\xff]/) xor !$$match [$i ++]);
183 ok (($char =~ /[\x80-\x{100}]/) xor !$$match [$i ++]);
184 ok (($char =~ /[\x{100}]/) xor !$$match [$i ++]);
185 }
186 }
187
188
189 {
190 # From Japhy
191 local $Message;
192 must_warn 'qr/(?c)/', '^Useless \(\?c\)';
193 must_warn 'qr/(?-c)/', '^Useless \(\?-c\)';
194 must_warn 'qr/(?g)/', '^Useless \(\?g\)';
195 must_warn 'qr/(?-g)/', '^Useless \(\?-g\)';
196 must_warn 'qr/(?o)/', '^Useless \(\?o\)';
197 must_warn 'qr/(?-o)/', '^Useless \(\?-o\)';
198
199 # Now test multi-error regexes
200 must_warn 'qr/(?g-o)/', '^Useless \(\?g\).*\nUseless \(\?-o\)';
201 must_warn 'qr/(?g-c)/', '^Useless \(\?g\).*\nUseless \(\?-c\)';
202 # (?c) means (?g) error won't be thrown
203 must_warn 'qr/(?o-cg)/', '^Useless \(\?o\).*\nUseless \(\?-c\)';
204 must_warn 'qr/(?ogc)/', '^Useless \(\?o\).*\nUseless \(\?g\).*\n' .
205 'Useless \(\?c\)';
206 }
207
208
209 {
210 local $Message = "/x tests";
211 $_ = "foo";
212 eval_ok <<" --";
213 /f
214 o\r
215 o
216 \$
217 /x
218 --
219 eval_ok <<" --";
220 /f
221 o
222 o
223 \$\r
224 /x
225 --
226 }
227
228
229 {
230 local $Message = "/o feature";
231 sub test_o {$_ [0] =~ /$_[1]/o; return $1}
232 iseq test_o ('abc', '(.)..'), 'a';
233 iseq test_o ('abc', '..(.)'), 'a';
234 }
235
e425a60b
YO
236 {
237 # Test basic $^N usage outside of a regex
238 local $Message = '$^N usage outside of a regex';
239 my $x = "abcdef";
240 ok ($x =~ /cde/ and !defined $^N);
241 ok ($x =~ /(cde)/ and $^N eq "cde");
242 ok ($x =~ /(c)(d)(e)/ and $^N eq "e");
243 ok ($x =~ /(c(d)e)/ and $^N eq "cde");
244 ok ($x =~ /(foo)|(c(d)e)/ and $^N eq "cde");
245 ok ($x =~ /(c(d)e)|(foo)/ and $^N eq "cde");
246 ok ($x =~ /(c(d)e)|(abc)/ and $^N eq "abc");
247 ok ($x =~ /(c(d)e)|(abc)x/ and $^N eq "cde");
248 ok ($x =~ /(c(d)e)(abc)?/ and $^N eq "cde");
249 ok ($x =~ /(?:c(d)e)/ and $^N eq "d");
250 ok ($x =~ /(?:c(d)e)(?:f)/ and $^N eq "d");
251 ok ($x =~ /(?:([abc])|([def]))*/ and $^N eq "f");
252 ok ($x =~ /(?:([ace])|([bdf]))*/ and $^N eq "f");
253 ok ($x =~ /(([ace])|([bd]))*/ and $^N eq "e");
254 {ok ($x =~ /(([ace])|([bdf]))*/ and $^N eq "f");}
255 ## Test to see if $^N is automatically localized -- it should now
256 ## have the value set in the previous test.
257 iseq $^N, "e", '$^N is automatically localized';
258
259 # Now test inside (?{ ... })
260 local $Message = '$^N usage inside (?{ ... })';
261 our ($y, $z);
262 ok ($x =~ /a([abc])(?{$y=$^N})c/ and $y eq "b");
263 ok ($x =~ /a([abc]+)(?{$y=$^N})d/ and $y eq "bc");
264 ok ($x =~ /a([abcdefg]+)(?{$y=$^N})d/ and $y eq "bc");
265 ok ($x =~ /(a([abcdefg]+)(?{$y=$^N})d)(?{$z=$^N})e/ and $y eq "bc"
266 and $z eq "abcd");
267 ok ($x =~ /(a([abcdefg]+)(?{$y=$^N})de)(?{$z=$^N})/ and $y eq "bc"
268 and $z eq "abcde");
269
270 }
271
272
273 SKIP:
274 {
275 ## Should probably put in tests for all the POSIX stuff,
276 ## but not sure how to guarantee a specific locale......
277
278 skip "Not an ASCII platform", 2 unless $IS_ASCII;
279 local $Message = 'Test [[:cntrl:]]';
280 my $AllBytes = join "" => map {chr} 0 .. 255;
281 (my $x = $AllBytes) =~ s/[[:cntrl:]]//g;
282 iseq $x, join "", map {chr} 0x20 .. 0x7E, 0x80 .. 0xFF;
283
284 ($x = $AllBytes) =~ s/[^[:cntrl:]]//g;
285 iseq $x, join "", map {chr} 0x00 .. 0x1F, 0x7F;
286 }
287
288
289 {
290 # With /s modifier UTF8 chars were interpreted as bytes
291 local $Message = "UTF-8 chars aren't bytes";
292 my $a = "Hello \x{263A} World";
293 my @a = ($a =~ /./gs);
294 iseq $#a, 12;
295 }
296
297
298 {
299 local $Message = '. matches \n with /s';
300 my $str1 = "foo\nbar";
301 my $str2 = "foo\n\x{100}bar";
302 my ($a, $b) = map {chr} $IS_ASCII ? (0xc4, 0x80) : (0x8c, 0x41);
303 my @a;
304 @a = $str1 =~ /./g; iseq @a, 6; iseq "@a", "f o o b a r";
305 @a = $str1 =~ /./gs; iseq @a, 7; iseq "@a", "f o o \n b a r";
306 @a = $str1 =~ /\C/g; iseq @a, 7; iseq "@a", "f o o \n b a r";
307 @a = $str1 =~ /\C/gs; iseq @a, 7; iseq "@a", "f o o \n b a r";
308 @a = $str2 =~ /./g; iseq @a, 7; iseq "@a", "f o o \x{100} b a r";
309 @a = $str2 =~ /./gs; iseq @a, 8; iseq "@a", "f o o \n \x{100} b a r";
310 @a = $str2 =~ /\C/g; iseq @a, 9; iseq "@a", "f o o \n $a $b b a r";
311 @a = $str2 =~ /\C/gs; iseq @a, 9; iseq "@a", "f o o \n $a $b b a r";
312 }
313
314
315 {
e425a60b
YO
316 no warnings 'digit';
317 # Check that \x## works. 5.6.1 and 5.005_03 fail some of these.
318 my $x;
319 $x = "\x4e" . "E";
320 ok ($x =~ /^\x4EE$/, "Check only 2 bytes of hex are matched.");
321
322 $x = "\x4e" . "i";
323 ok ($x =~ /^\x4Ei$/, "Check that invalid hex digit stops it (2)");
324
325 $x = "\x4" . "j";
326 ok ($x =~ /^\x4j$/, "Check that invalid hex digit stops it (1)");
327
328 $x = "\x0" . "k";
329 ok ($x =~ /^\xk$/, "Check that invalid hex digit stops it (0)");
330
331 $x = "\x0" . "x";
332 ok ($x =~ /^\xx$/, "\\xx isn't to be treated as \\0");
333
334 $x = "\x0" . "xa";
335 ok ($x =~ /^\xxa$/, "\\xxa isn't to be treated as \\xa");
336
337 $x = "\x9" . "_b";
338 ok ($x =~ /^\x9_b$/, "\\x9_b isn't to be treated as \\x9b");
339
340 # and now again in [] ranges
341
342 $x = "\x4e" . "E";
343 ok ($x =~ /^[\x4EE]{2}$/, "Check only 2 bytes of hex are matched.");
344
345 $x = "\x4e" . "i";
346 ok ($x =~ /^[\x4Ei]{2}$/, "Check that invalid hex digit stops it (2)");
347
348 $x = "\x4" . "j";
349 ok ($x =~ /^[\x4j]{2}$/, "Check that invalid hex digit stops it (1)");
350
351 $x = "\x0" . "k";
352 ok ($x =~ /^[\xk]{2}$/, "Check that invalid hex digit stops it (0)");
353
354 $x = "\x0" . "x";
355 ok ($x =~ /^[\xx]{2}$/, "\\xx isn't to be treated as \\0");
356
357 $x = "\x0" . "xa";
358 ok ($x =~ /^[\xxa]{3}$/, "\\xxa isn't to be treated as \\xa");
359
360 $x = "\x9" . "_b";
361 ok ($x =~ /^[\x9_b]{3}$/, "\\x9_b isn't to be treated as \\x9b");
362
363 # Check that \x{##} works. 5.6.1 fails quite a few of these.
364
365 $x = "\x9b";
366 ok ($x =~ /^\x{9_b}$/, "\\x{9_b} is to be treated as \\x9b");
367
368 $x = "\x9b" . "y";
369 ok ($x =~ /^\x{9_b}y$/, "\\x{9_b} is to be treated as \\x9b (again)");
370
371 $x = "\x9b" . "y";
372 ok ($x =~ /^\x{9b_}y$/, "\\x{9b_} is to be treated as \\x9b");
373
374 $x = "\x9b" . "y";
375 ok ($x =~ /^\x{9_bq}y$/, "\\x{9_bc} is to be treated as \\x9b");
376
377 $x = "\x0" . "y";
378 ok ($x =~ /^\x{x9b}y$/, "\\x{x9b} is to be treated as \\x0");
379
380 $x = "\x0" . "y";
381 ok ($x =~ /^\x{0x9b}y$/, "\\x{0x9b} is to be treated as \\x0");
382
383 $x = "\x9b" . "y";
384 ok ($x =~ /^\x{09b}y$/, "\\x{09b} is to be treated as \\x9b");
385
386 $x = "\x9b";
387 ok ($x =~ /^[\x{9_b}]$/, "\\x{9_b} is to be treated as \\x9b");
388
389 $x = "\x9b" . "y";
390 ok ($x =~ /^[\x{9_b}y]{2}$/,
391 "\\x{9_b} is to be treated as \\x9b (again)");
392
393 $x = "\x9b" . "y";
394 ok ($x =~ /^[\x{9b_}y]{2}$/, "\\x{9b_} is to be treated as \\x9b");
395
396 $x = "\x9b" . "y";
397 ok ($x =~ /^[\x{9_bq}y]{2}$/, "\\x{9_bc} is to be treated as \\x9b");
398
399 $x = "\x0" . "y";
400 ok ($x =~ /^[\x{x9b}y]{2}$/, "\\x{x9b} is to be treated as \\x0");
401
402 $x = "\x0" . "y";
403 ok ($x =~ /^[\x{0x9b}y]{2}$/, "\\x{0x9b} is to be treated as \\x0");
404
405 $x = "\x9b" . "y";
406 ok ($x =~ /^[\x{09b}y]{2}$/, "\\x{09b} is to be treated as \\x9b");
407
408 }
409
410
411 {
412 # High bit bug -- japhy
413 my $x = "ab\200d";
414 ok $x =~ /.*?\200/, "High bit fine";
415 }
416
417
418 {
419 # The basic character classes and Unicode
420 ok "\x{0100}" =~ /\w/, 'LATIN CAPITAL LETTER A WITH MACRON in /\w/';
421 ok "\x{0660}" =~ /\d/, 'ARABIC-INDIC DIGIT ZERO in /\d/';
422 ok "\x{1680}" =~ /\s/, 'OGHAM SPACE MARK in /\s/';
423 }
424
425
426 {
427 local $Message = "Folding matches and Unicode";
428 ok "a\x{100}" =~ /A/i;
429 ok "A\x{100}" =~ /a/i;
430 ok "a\x{100}" =~ /a/i;
431 ok "A\x{100}" =~ /A/i;
432 ok "\x{101}a" =~ /\x{100}/i;
433 ok "\x{100}a" =~ /\x{100}/i;
434 ok "\x{101}a" =~ /\x{101}/i;
435 ok "\x{100}a" =~ /\x{101}/i;
436 ok "a\x{100}" =~ /A\x{100}/i;
437 ok "A\x{100}" =~ /a\x{100}/i;
438 ok "a\x{100}" =~ /a\x{100}/i;
439 ok "A\x{100}" =~ /A\x{100}/i;
440 ok "a\x{100}" =~ /[A]/i;
441 ok "A\x{100}" =~ /[a]/i;
442 ok "a\x{100}" =~ /[a]/i;
443 ok "A\x{100}" =~ /[A]/i;
444 ok "\x{101}a" =~ /[\x{100}]/i;
445 ok "\x{100}a" =~ /[\x{100}]/i;
446 ok "\x{101}a" =~ /[\x{101}]/i;
447 ok "\x{100}a" =~ /[\x{101}]/i;
448 }
449
450
451 {
452 use charnames ':full';
453 local $Message = "Folding 'LATIN LETTER A WITH GRAVE'";
454
455 my $lower = "\N{LATIN SMALL LETTER A WITH GRAVE}";
456 my $UPPER = "\N{LATIN CAPITAL LETTER A WITH GRAVE}";
0f289c68 457
e425a60b
YO
458 ok $lower =~ m/$UPPER/i;
459 ok $UPPER =~ m/$lower/i;
460 ok $lower =~ m/[$UPPER]/i;
461 ok $UPPER =~ m/[$lower]/i;
462
463 local $Message = "Folding 'GREEK LETTER ALPHA WITH VRACHY'";
464
465 $lower = "\N{GREEK CAPITAL LETTER ALPHA WITH VRACHY}";
466 $UPPER = "\N{GREEK SMALL LETTER ALPHA WITH VRACHY}";
467
468 ok $lower =~ m/$UPPER/i;
469 ok $UPPER =~ m/$lower/i;
470 ok $lower =~ m/[$UPPER]/i;
471 ok $UPPER =~ m/[$lower]/i;
472
473 local $Message = "Folding 'LATIN LETTER Y WITH DIAERESIS'";
474
475 $lower = "\N{LATIN SMALL LETTER Y WITH DIAERESIS}";
476 $UPPER = "\N{LATIN CAPITAL LETTER Y WITH DIAERESIS}";
477
478 ok $lower =~ m/$UPPER/i;
479 ok $UPPER =~ m/$lower/i;
480 ok $lower =~ m/[$UPPER]/i;
481 ok $UPPER =~ m/[$lower]/i;
482 }
483
484
485 {
486 use charnames ':full';
487 local $PatchId = "13843";
488 local $Message = "GREEK CAPITAL LETTER SIGMA vs " .
489 "COMBINING GREEK PERISPOMENI";
490
491 my $SIGMA = "\N{GREEK CAPITAL LETTER SIGMA}";
492 my $char = "\N{COMBINING GREEK PERISPOMENI}";
493
494 may_not_warn sub {ok "_:$char:_" !~ m/_:$SIGMA:_/i};
495 }
496
497
498 {
499 local $Message = '\X';
500 use charnames ':full';
501
502 ok "a!" =~ /^(\X)!/ && $1 eq "a";
503 ok "\xDF!" =~ /^(\X)!/ && $1 eq "\xDF";
504 ok "\x{100}!" =~ /^(\X)!/ && $1 eq "\x{100}";
505 ok "\x{100}\x{300}!" =~ /^(\X)!/ && $1 eq "\x{100}\x{300}";
506 ok "\N{LATIN CAPITAL LETTER E}!" =~ /^(\X)!/ &&
507 $1 eq "\N{LATIN CAPITAL LETTER E}";
508 ok "\N{LATIN CAPITAL LETTER E}\N{COMBINING GRAVE ACCENT}!"
509 =~ /^(\X)!/ &&
510 $1 eq "\N{LATIN CAPITAL LETTER E}\N{COMBINING GRAVE ACCENT}";
511
512 local $Message = '\C and \X';
513 ok "!abc!" =~ /a\Cc/;
514 ok "!abc!" =~ /a\Xc/;
515 }
516
517
518 {
519 local $Message = "Final Sigma";
520
521 my $SIGMA = "\x{03A3}"; # CAPITAL
522 my $Sigma = "\x{03C2}"; # SMALL FINAL
523 my $sigma = "\x{03C3}"; # SMALL
524
525 ok $SIGMA =~ /$SIGMA/i;
526 ok $SIGMA =~ /$Sigma/i;
527 ok $SIGMA =~ /$sigma/i;
528
529 ok $Sigma =~ /$SIGMA/i;
530 ok $Sigma =~ /$Sigma/i;
531 ok $Sigma =~ /$sigma/i;
532
533 ok $sigma =~ /$SIGMA/i;
534 ok $sigma =~ /$Sigma/i;
535 ok $sigma =~ /$sigma/i;
0f289c68 536
e425a60b
YO
537 ok $SIGMA =~ /[$SIGMA]/i;
538 ok $SIGMA =~ /[$Sigma]/i;
539 ok $SIGMA =~ /[$sigma]/i;
540
541 ok $Sigma =~ /[$SIGMA]/i;
542 ok $Sigma =~ /[$Sigma]/i;
543 ok $Sigma =~ /[$sigma]/i;
544
545 ok $sigma =~ /[$SIGMA]/i;
546 ok $sigma =~ /[$Sigma]/i;
547 ok $sigma =~ /[$sigma]/i;
548
549 local $Message = "More final Sigma";
550
551 my $S3 = "$SIGMA$Sigma$sigma";
552
553 ok ":$S3:" =~ /:(($SIGMA)+):/i && $1 eq $S3 && $2 eq $sigma;
554 ok ":$S3:" =~ /:(($Sigma)+):/i && $1 eq $S3 && $2 eq $sigma;
555 ok ":$S3:" =~ /:(($sigma)+):/i && $1 eq $S3 && $2 eq $sigma;
556
557 ok ":$S3:" =~ /:(([$SIGMA])+):/i && $1 eq $S3 && $2 eq $sigma;
558 ok ":$S3:" =~ /:(([$Sigma])+):/i && $1 eq $S3 && $2 eq $sigma;
559 ok ":$S3:" =~ /:(([$sigma])+):/i && $1 eq $S3 && $2 eq $sigma;
560 }
561
562
563 {
564 use charnames ':full';
565 local $Message = "Parlez-Vous " .
566 "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais?";
567
568 ok "Fran\N{LATIN SMALL LETTER C}ais" =~ /Fran.ais/ &&
569 $& eq "Francais";
570 ok "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" =~ /Fran.ais/ &&
571 $& eq "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais";
572 ok "Fran\N{LATIN SMALL LETTER C}ais" =~ /Fran\Cais/ &&
573 $& eq "Francais";
574 # COMBINING CEDILLA is two bytes when encoded
575 ok "Franc\N{COMBINING CEDILLA}ais" =~ /Franc\C\Cais/;
576 ok "Fran\N{LATIN SMALL LETTER C}ais" =~ /Fran\Xais/ &&
577 $& eq "Francais";
578 ok "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" =~ /Fran\Xais/ &&
579 $& eq "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais";
580 ok "Franc\N{COMBINING CEDILLA}ais" =~ /Fran\Xais/ &&
581 $& eq "Franc\N{COMBINING CEDILLA}ais";
582 ok "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" =~
583 /Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais/ &&
584 $& eq "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais";
585 ok "Franc\N{COMBINING CEDILLA}ais" =~ /Franc\N{COMBINING CEDILLA}ais/ &&
586 $& eq "Franc\N{COMBINING CEDILLA}ais";
587
588 my @f = (
589 ["Fran\N{LATIN SMALL LETTER C}ais", "Francais"],
590 ["Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais",
591 "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais"],
592 ["Franc\N{COMBINING CEDILLA}ais", "Franc\N{COMBINING CEDILLA}ais"],
593 );
594 foreach my $entry (@f) {
595 my ($subject, $match) = @$entry;
596 ok $subject =~ /Fran(?:c\N{COMBINING CEDILLA}?|
597 \N{LATIN SMALL LETTER C WITH CEDILLA})ais/x &&
598 $& eq $match;
599 }
600 }
601
602
603 {
604 local $Message = "Lingering (and useless) UTF8 flag doesn't mess up /i";
605 my $pat = "ABcde";
606 my $str = "abcDE\x{100}";
607 chop $str;
608 ok $str =~ /$pat/i;
609
610 $pat = "ABcde\x{100}";
611 $str = "abcDE";
612 chop $pat;
613 ok $str =~ /$pat/i;
614
615 $pat = "ABcde\x{100}";
616 $str = "abcDE\x{100}";
617 chop $pat;
618 chop $str;
619 ok $str =~ /$pat/i;
620 }
621
622
623 {
624 use charnames ':full';
625 local $Message = "LATIN SMALL LETTER SHARP S " .
626 "(\N{LATIN SMALL LETTER SHARP S})";
627
628 ok "\N{LATIN SMALL LETTER SHARP S}" =~
629 /\N{LATIN SMALL LETTER SHARP S}/;
630 ok "\N{LATIN SMALL LETTER SHARP S}" =~
631 /\N{LATIN SMALL LETTER SHARP S}/i;
632 ok "\N{LATIN SMALL LETTER SHARP S}" =~
633 /[\N{LATIN SMALL LETTER SHARP S}]/;
634 ok "\N{LATIN SMALL LETTER SHARP S}" =~
635 /[\N{LATIN SMALL LETTER SHARP S}]/i;
636
637 ok "ss" =~ /\N{LATIN SMALL LETTER SHARP S}/i;
638 ok "SS" =~ /\N{LATIN SMALL LETTER SHARP S}/i;
639 ok "ss" =~ /[\N{LATIN SMALL LETTER SHARP S}]/i;
640 ok "SS" =~ /[\N{LATIN SMALL LETTER SHARP S}]/i;
641
642 ok "\N{LATIN SMALL LETTER SHARP S}" =~ /ss/i;
643 ok "\N{LATIN SMALL LETTER SHARP S}" =~ /SS/i;
0f289c68 644
e425a60b
YO
645 local $Message = "Unoptimized named sequence in class";
646 ok "ss" =~ /[\N{LATIN SMALL LETTER SHARP S}x]/i;
647 ok "SS" =~ /[\N{LATIN SMALL LETTER SHARP S}x]/i;
648 ok "\N{LATIN SMALL LETTER SHARP S}" =~
649 /[\N{LATIN SMALL LETTER SHARP S}x]/;
650 ok "\N{LATIN SMALL LETTER SHARP S}" =~
651 /[\N{LATIN SMALL LETTER SHARP S}x]/i;
652 }
653
654
655 {
656 # More whitespace: U+0085, U+2028, U+2029\n";
657
658 # U+0085, U+00A0 need to be forced to be Unicode, the \x{100} does that.
659 SKIP: {
660 skip "EBCDIC platform", 4 if $IS_EBCDIC;
661 # Do \x{0015} and \x{0041} match \s in EBCDIC?
662 ok "<\x{100}\x{0085}>" =~ /<\x{100}\s>/, '\x{0085} in \s';
663 ok "<\x{0085}>" =~ /<\v>/, '\x{0085} in \v';
664 ok "<\x{100}\x{00A0}>" =~ /<\x{100}\s>/, '\x{00A0} in \s';
665 ok "<\x{00A0}>" =~ /<\h>/, '\x{00A0} in \h';
666 }
667 my @h = map {sprintf "%05x" => $_} 0x01680, 0x0180E, 0x02000 .. 0x0200A,
668 0x0202F, 0x0205F, 0x03000;
669 my @v = map {sprintf "%05x" => $_} 0x02028, 0x02029;
670
671 my @H = map {sprintf "%05x" => $_} 0x01361, 0x0200B, 0x02408, 0x02420,
672 0x0303F, 0xE0020;
673 my @V = map {sprintf "%05x" => $_} 0x0008A .. 0x0008D, 0x00348, 0x10100,
674 0xE005F, 0xE007C;
675
676 for my $hex (@h) {
677 my $str = eval qq ["<\\x{$hex}>"];
678 ok $str =~ /<\s>/, "\\x{$hex} in \\s";
679 ok $str =~ /<\h>/, "\\x{$hex} in \\h";
680 ok $str !~ /<\v>/, "\\x{$hex} not in \\v";
681 }
682
683 for my $hex (@v) {
684 my $str = eval qq ["<\\x{$hex}>"];
685 ok $str =~ /<\s>/, "\\x{$hex} in \\s";
686 ok $str =~ /<\v>/, "\\x{$hex} in \\v";
687 ok $str !~ /<\h>/, "\\x{$hex} not in \\h";
688 }
689
690 for my $hex (@H) {
691 my $str = eval qq ["<\\x{$hex}>"];
692 ok $str =~ /<\S>/, "\\x{$hex} in \\S";
693 ok $str =~ /<\H>/, "\\x{$hex} in \\H";
694 }
695
696 for my $hex (@V) {
697 my $str = eval qq ["<\\x{$hex}>"];
698 ok $str =~ /<\S>/, "\\x{$hex} in \\S";
699 ok $str =~ /<\V>/, "\\x{$hex} in \\V";
700 }
701 }
702
703
704 {
705 # . with /s should work on characters, as opposed to bytes
706 local $Message = ". with /s works on characters, not bytes";
707
708 my $s = "\x{e4}\x{100}";
709 # This is not expected to match: the point is that
710 # neither should we get "Malformed UTF-8" warnings.
711 may_not_warn sub {$s =~ /\G(.+?)\n/gcs}, "No 'Malformed UTF-8' warning";
712
713 my @c;
714 push @c => $1 while $s =~ /\G(.)/gs;
715
716 local $" = "";
717 iseq "@c", $s;
718
719 # Test only chars < 256
720 my $t1 = "Q003\n\n\x{e4}\x{f6}\n\nQ004\n\n\x{e7}";
721 my $r1 = "";
722 while ($t1 =~ / \G ( .+? ) \n\s+ ( .+? ) ( $ | \n\s+ ) /xgcs) {
0f289c68 723 $r1 .= $1 . $2;
e425a60b
YO
724 }
725
726 my $t2 = $t1 . "\x{100}"; # Repeat with a larger char
727 my $r2 = "";
728 while ($t2 =~ / \G ( .+? ) \n\s+ ( .+? ) ( $ | \n\s+ ) /xgcs) {
0f289c68 729 $r2 .= $1 . $2;
e425a60b
YO
730 }
731 $r2 =~ s/\x{100}//;
732
733 iseq $r1, $r2;
734 }
735
736
737 {
738 local $Message = "Unicode lookbehind";
739 ok "A\x{100}B" =~ /(?<=A.)B/;
740 ok "A\x{200}\x{300}B" =~ /(?<=A..)B/;
741 ok "\x{400}AB" =~ /(?<=\x{400}.)B/;
742 ok "\x{500}\x{600}B" =~ /(?<=\x{500}.)B/;
743
744 # Original code also contained:
745 # ok "\x{500\x{600}}B" =~ /(?<=\x{500}.)B/;
746 # but that looks like a typo.
747 }
748
749
750 {
751 local $Message = 'UTF-8 hash keys and /$/';
752 # http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters
753 # /2002-01/msg01327.html
754
755 my $u = "a\x{100}";
756 my $v = substr ($u, 0, 1);
757 my $w = substr ($u, 1, 1);
758 my %u = ($u => $u, $v => $v, $w => $w);
759 for (keys %u) {
760 my $m1 = /^\w*$/ ? 1 : 0;
761 my $m2 = $u {$_} =~ /^\w*$/ ? 1 : 0;
762 iseq $m1, $m2;
763 }
764 }
765
766
767 {
e425a60b
YO
768 local $Message = "No SEGV in s/// and UTF-8";
769 my $s = "s#\x{100}" x 4;
770 ok $s =~ s/[^\w]/ /g;
2e84be61 771 if ( 1 or $ENV{PERL_TEST_LEGACY_POSIX_CC} ) {
e425a60b
YO
772 iseq $s, "s \x{100}" x 4;
773 }
774 else {
775 iseq $s, "s " x 4;
776 }
777 }
778
779
780 {
781 local $Message = "UTF-8 bug (maybe already known?)";
782 my $u = "foo";
783 $u =~ s/./\x{100}/g;
784 iseq $u, "\x{100}\x{100}\x{100}";
785
786 $u = "foobar";
787 $u =~ s/[ao]/\x{100}/g;
788 iseq $u, "f\x{100}\x{100}b\x{100}r";
789
790 $u =~ s/\x{100}/e/g;
791 iseq $u, "feeber";
792 }
793
794
795 {
796 local $Message = "UTF-8 bug with s///";
797 # check utf8/non-utf8 mixtures
798 # try to force all float/anchored check combinations
799
800 my $c = "\x{100}";
801 my $subst;
802 for my $re ("xx.*$c", "x.*$c$c", "$c.*xx", "$c$c.*x",
803 "xx.*(?=$c)", "(?=$c).*xx",) {
804 ok "xxx" !~ /$re/;
805 ok +($subst = "xxx") !~ s/$re//;
806 }
807 for my $re ("xx.*$c*", "$c*.*xx") {
808 ok "xxx" =~ /$re/;
809 ok +($subst = "xxx") =~ s/$re//;
810 iseq $subst, "";
811 }
812 for my $re ("xxy*", "y*xx") {
813 ok "xx$c" =~ /$re/;
814 ok +($subst = "xx$c") =~ s/$re//;
815 iseq $subst, $c;
816 ok "xy$c" !~ /$re/;
817 ok +($subst = "xy$c") !~ s/$re//;
818 }
819 for my $re ("xy$c*z", "x$c*yz") {
820 ok "xyz" =~ /$re/;
821 ok +($subst = "xyz") =~ s/$re//;
822 iseq $subst, "";
823 }
824 }
825
826
827 {
828 local $Message = "qr /.../x";
829 my $R = qr / A B C # D E/x;
830 ok "ABCDE" =~ $R && $& eq "ABC";
831 ok "ABCDE" =~ /$R/ && $& eq "ABC";
832 ok "ABCDE" =~ m/$R/ && $& eq "ABC";
833 ok "ABCDE" =~ /($R)/ && $1 eq "ABC";
834 ok "ABCDE" =~ m/($R)/ && $1 eq "ABC";
835 }
836
837
e425a60b 838
0f289c68 839
e425a60b
YO
840 {
841 local $\;
842 $_ = 'aaaaaaaaaa';
843 utf8::upgrade($_); chop $_; $\="\n";
844 ok /[^\s]+/, 'm/[^\s]/ utf8';
845 ok /[^\d]+/, 'm/[^\d]/ utf8';
846 ok +($a = $_, $_ =~ s/[^\s]+/./g), 's/[^\s]/ utf8';
847 ok +($a = $_, $a =~ s/[^\d]+/./g), 's/[^\s]/ utf8';
848 }
849
850
e425a60b
YO
851
852
853 {
854 # Subject: Odd regexp behavior
855 # From: Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>
856 # Date: Wed, 26 Feb 2003 16:53:12 +0000
857 # Message-Id: <E18o4nw-0008Ly-00@wisbech.cl.cam.ac.uk>
858 # To: perl-unicode@perl.org
859
860 local $Message = 'Markus Kuhn 2003-02-26';
0f289c68 861
e425a60b
YO
862 my $x = "\x{2019}\nk";
863 ok $x =~ s/(\S)\n(\S)/$1 $2/sg;
864 ok $x eq "\x{2019} k";
865
866 $x = "b\nk";
867 ok $x =~ s/(\S)\n(\S)/$1 $2/sg;
868 ok $x eq "b k";
869
870 ok "\x{2019}" =~ /\S/;
871 }
872
873
874 {
e425a60b
YO
875 # XXX DAPM 13-Apr-06. Recursive split is still broken. It's only luck it
876 # hasn't been crashing. Disable this test until it is fixed properly.
877 # XXX also check what it returns rather than just doing ok(1,...)
878 # split /(?{ split "" })/, "abc";
879 local $TODO = "Recursive split is still broken";
880 ok 0, 'cache_re & "(?{": it dumps core in 5.6.1 & 5.8.0';
881 }
882
883
884 {
885 ok "\x{100}\n" =~ /\x{100}\n$/, "UTF-8 length cache and fbm_compile";
886 }
887
888
889 {
890 package Str;
891 use overload q /""/ => sub {${$_ [0]};};
892 sub new {my ($c, $v) = @_; bless \$v, $c;}
893
894 package main;
895 $_ = Str -> new ("a\x{100}/\x{100}b");
896 ok join (":", /\b(.)\x{100}/g) eq "a:/", "re_intuit_start and PL_bostr";
897 }
898
e425a60b
YO
899 {
900 my $re = qq /^([^X]*)X/;
901 utf8::upgrade ($re);
902 ok "\x{100}X" =~ /$re/, "S_cl_and ANYOF_UNICODE & ANYOF_INVERTED";
903 }
904
e425a60b
YO
905 {
906 ok "123\x{100}" =~ /^.*1.*23\x{100}$/,
907 'UTF-8 + multiple floating substr';
908 }
909
e425a60b
YO
910 {
911 local $Message = '<20030808193656.5109.1@llama.ni-s.u-net.com>';
912
913 # LATIN SMALL/CAPITAL LETTER A WITH MACRON
914 ok " \x{101}" =~ qr/\x{100}/i;
915
916 # LATIN SMALL/CAPITAL LETTER A WITH RING BELOW
917 ok " \x{1E01}" =~ qr/\x{1E00}/i;
918
919 # DESERET SMALL/CAPITAL LETTER LONG I
920 ok " \x{10428}" =~ qr/\x{10400}/i;
921
922 # LATIN SMALL/CAPITAL LETTER A WITH RING BELOW + 'X'
923 ok " \x{1E01}x" =~ qr/\x{1E00}X/i;
924 }
925
e425a60b
YO
926 {
927 for (120 .. 130) {
928 my $head = 'x' x $_;
929 local $Message = q [Don't misparse \x{...} in regexp ] .
930 q [near 127 char EXACT limit];
931 for my $tail ('\x{0061}', '\x{1234}', '\x61') {
932 eval_ok qq ["$head$tail" =~ /$head$tail/];
933 }
934 local $Message = q [Don't misparse \N{...} in regexp ] .
935 q [near 127 char EXACT limit];
936 for my $tail ('\N{SNOWFLAKE}') {
937 eval_ok qq [use charnames ':full';
938 "$head$tail" =~ /$head$tail/];
939 }
940 }
941 }
942
e425a60b
YO
943 { # TRIE related
944 our @got = ();
945 "words" =~ /(word|word|word)(?{push @got, $1})s$/;
946 iseq @got, 1, "TRIE optimation";
947
948 @got = ();
949 "words" =~ /(word|word|word)(?{push @got,$1})s$/i;
950 iseq @got, 1,"TRIEF optimisation";
951
952 my @nums = map {int rand 1000} 1 .. 100;
953 my $re = "(" . (join "|", @nums) . ")";
954 $re = qr/\b$re\b/;
955
956 foreach (@nums) {
957 ok $_ =~ /$re/, "Trie nums";
958 }
959
960 $_ = join " ", @nums;
961 @got = ();
962 push @got, $1 while /$re/g;
963
964 my %count;
965 $count {$_} ++ for @got;
966 my $ok = 1;
967 for (@nums) {
968 $ok = 0 if --$count {$_} < 0;
969 }
970 ok $ok, "Trie min count matches";
971 }
972
973
974 {
975 # TRIE related
976 # LATIN SMALL/CAPITAL LETTER A WITH MACRON
977 ok "foba \x{101}foo" =~ qr/(foo|\x{100}foo|bar)/i &&
978 $1 eq "\x{101}foo",
979 "TRIEF + LATIN SMALL/CAPITAL LETTER A WITH MACRON";
980
981 # LATIN SMALL/CAPITAL LETTER A WITH RING BELOW
982 ok "foba \x{1E01}foo" =~ qr/(foo|\x{1E00}foo|bar)/i &&
983 $1 eq "\x{1E01}foo",
984 "TRIEF + LATIN SMALL/CAPITAL LETTER A WITH RING BELOW";
985
986 # DESERET SMALL/CAPITAL LETTER LONG I
987 ok "foba \x{10428}foo" =~ qr/(foo|\x{10400}foo|bar)/i &&
988 $1 eq "\x{10428}foo",
989 "TRIEF + DESERET SMALL/CAPITAL LETTER LONG I";
990
991 # LATIN SMALL/CAPITAL LETTER A WITH RING BELOW + 'X'
992 ok "foba \x{1E01}xfoo" =~ qr/(foo|\x{1E00}Xfoo|bar)/i &&
993 $1 eq "\x{1E01}xfoo",
994 "TRIEF + LATIN SMALL/CAPITAL LETTER A WITH RING BELOW + 'X'";
995
996 use charnames ':full';
997
998 my $s = "\N{LATIN SMALL LETTER SHARP S}";
999 ok "foba ba$s" =~ qr/(foo|Ba$s|bar)/i && $1 eq "ba$s",
1000 "TRIEF + LATIN SMALL LETTER SHARP S =~ ss";
1001 ok "foba ba$s" =~ qr/(Ba$s|foo|bar)/i && $1 eq "ba$s",
1002 "TRIEF + LATIN SMALL LETTER SHARP S =~ ss";
1003 ok "foba ba$s" =~ qr/(foo|bar|Ba$s)/i && $1 eq "ba$s",
1004 "TRIEF + LATIN SMALL LETTER SHARP S =~ ss";
1005
1006 ok "foba ba$s" =~ qr/(foo|Bass|bar)/i && $1 eq "ba$s",
1007 "TRIEF + LATIN SMALL LETTER SHARP S =~ ss";
1008
1009 ok "foba ba$s" =~ qr/(foo|BaSS|bar)/i && $1 eq "ba$s",
1010 "TRIEF + LATIN SMALL LETTER SHARP S =~ SS";
1011
1012 ok "foba ba${s}pxySS$s$s" =~ qr/(b(?:a${s}t|a${s}f|a${s}p)[xy]+$s*)/i
1013 && $1 eq "ba${s}pxySS$s$s",
1014 "COMMON PREFIX TRIEF + LATIN SMALL LETTER SHARP S";
1015 }
1016
1017
e425a60b
YO
1018
1019
1020 {
0f289c68
YO
1021 BEGIN {
1022 unshift @INC, 'lib';
1023 }
e425a60b 1024 use Cname;
0f289c68 1025
e425a60b
YO
1026 ok 'fooB' =~ /\N{foo}[\N{B}\N{b}]/, "Passthrough charname";
1027 my $test = 1233;
1028 #
1029 # Why doesn't must_warn work here?
1030 #
1031 my $w;
1032 local $SIG {__WARN__} = sub {$w .= "@_"};
1033 eval 'q(xxWxx) =~ /[\N{WARN}]/';
1034 ok $w && $w =~ /^Ignoring excess chars from/,
1035 "Ignoring excess chars warning";
1036
1037 undef $w;
1038 eval q [ok "\0" !~ /[\N{EMPTY-STR}XY]/,
1039 "Zerolength charname in charclass doesn't match \\0"];
1040 ok $w && $w =~ /^Ignoring zero length/,
1041 'Ignoring zero length \N{%} in character class warning';
1042
1043 ok 'AB' =~ /(\N{EVIL})/ && $1 eq 'A', 'Charname caching $1';
1044 ok 'ABC' =~ /(\N{EVIL})/, 'Charname caching $1';
1045 ok 'xy' =~ /x\N{EMPTY-STR}y/,
1046 'Empty string charname produces NOTHING node';
1047 ok '' =~ /\N{EMPTY-STR}/,
1048 'Empty string charname produces NOTHING node';
0f289c68 1049
e425a60b
YO
1050 }
1051
1052
1053 {
1054 use charnames ':full';
1055
1056 ok 'aabc' !~ /a\N{PLUS SIGN}b/, '/a\N{PLUS SIGN}b/ against aabc';
1057 ok 'a+bc' =~ /a\N{PLUS SIGN}b/, '/a\N{PLUS SIGN}b/ against a+bc';
1058
1059 ok ' A B' =~ /\N{SPACE}\N{U+0041}\N{SPACE}\N{U+0042}/,
1060 'Intermixed named and unicode escapes';
1061 ok "\N{SPACE}\N{U+0041}\N{SPACE}\N{U+0042}" =~
1062 /\N{SPACE}\N{U+0041}\N{SPACE}\N{U+0042}/,
1063 'Intermixed named and unicode escapes';
1064 ok "\N{SPACE}\N{U+0041}\N{SPACE}\N{U+0042}" =~
1065 /[\N{SPACE}\N{U+0041}][\N{SPACE}\N{U+0042}]/,
0f289c68 1066 'Intermixed named and unicode escapes';
e425a60b
YO
1067 }
1068
1069
1070 {
1071 our $brackets;
1072 $brackets = qr{
1073 { (?> [^{}]+ | (??{ $brackets }) )* }
1074 }x;
1075
1076 ok "{b{c}d" !~ m/^((??{ $brackets }))/, "Bracket mismatch";
1077
1078 SKIP: {
1079 our @stack = ();
1080 my @expect = qw(
1081 stuff1
1082 stuff2
1083 <stuff1>and<stuff2>
1084 right
1085 <right>
1086 <<right>>
1087 <<<right>>>
1088 <<stuff1>and<stuff2>><<<<right>>>>
1089 );
1090
1091 local $_ = '<<<stuff1>and<stuff2>><<<<right>>>>>';
1092 ok /^(<((?:(?>[^<>]+)|(?1))*)>(?{push @stack, $2 }))$/,
1093 "Recursion matches";
1094 iseq @stack, @expect, "Right amount of matches"
1095 or skip "Won't test individual results as count isn't equal",
1096 0 + @expect;
1097 my $idx = 0;
1098 foreach my $expect (@expect) {
1099 iseq $stack [$idx], $expect,
1100 "Expecting '$expect' at stack pos #$idx";
1101 $idx ++;
1102 }
1103 }
1104 }
1105
1106
1107 {
1108 my $s = '123453456';
1109 $s =~ s/(?<digits>\d+)\k<digits>/$+{digits}/;
1110 ok $s eq '123456', 'Named capture (angle brackets) s///';
1111 $s = '123453456';
1112 $s =~ s/(?'digits'\d+)\k'digits'/$+{digits}/;
0f289c68 1113 ok $s eq '123456', 'Named capture (single quotes) s///';
e425a60b
YO
1114 }
1115
1116
1117 {
1118 my @ary = (
1119 pack('U', 0x00F1), # n-tilde
1120 '_'.pack('U', 0x00F1), # _ + n-tilde
1121 'c'.pack('U', 0x0327), # c + cedilla
1122 pack('U*', 0x00F1, 0x0327), # n-tilde + cedilla
1123 'a'.pack('U', 0x00B2), # a + superscript two
1124 pack('U', 0x0391), # ALPHA
1125 pack('U', 0x0391).'2', # ALPHA + 2
1126 pack('U', 0x0391).'_', # ALPHA + _
1127 );
1128
1129 for my $uni (@ary) {
1130 my ($r1, $c1, $r2, $c2) = eval qq {
1131 use utf8;
1132 scalar ("..foo foo.." =~ /(?'${uni}'foo) \\k'${uni}'/),
1133 \$+{${uni}},
1134 scalar ("..bar bar.." =~ /(?<${uni}>bar) \\k<${uni}>/),
1135 \$+{${uni}};
1136 };
1137 ok $r1, "Named capture UTF (?'')";
1138 ok defined $c1 && $c1 eq 'foo', "Named capture UTF \%+";
1139 ok $r2, "Named capture UTF (?<>)";
1140 ok defined $c2 && $c2 eq 'bar', "Named capture UTF \%+";
1141 }
1142 }
1143
e425a60b
YO
1144 {
1145 my $s = 'foo bar baz';
1146 my @res;
1147 if ('1234' =~ /(?<A>1)(?<B>2)(?<A>3)(?<B>4)/) {
1148 foreach my $name (sort keys(%-)) {
1149 my $ary = $- {$name};
1150 foreach my $idx (0 .. $#$ary) {
1151 push @res, "$name:$idx:$ary->[$idx]";
1152 }
1153 }
1154 }
1155 my @expect = qw (A:0:1 A:1:3 B:0:2 B:1:4);
1156 iseq "@res", "@expect", "Check %-";
1157 eval'
1158 no warnings "uninitialized";
1159 print for $- {this_key_doesnt_exist};
1160 ';
1161 ok !$@,'lvalue $- {...} should not throw an exception';
1162 }
1163
e425a60b
YO
1164 {
1165 # \, breaks {3,4}
1166 ok "xaaay" !~ /xa{3\,4}y/, '\, in a pattern';
1167 ok "xa{3,4}y" =~ /xa{3\,4}y/, '\, in a pattern';
1168
1169 # \c\ followed by _
1170 ok "x\c_y" !~ /x\c\_y/, '\_ in a pattern';
1171 ok "x\c\_y" =~ /x\c\_y/, '\_ in a pattern';
1172
1173 # \c\ followed by other characters
1174 for my $c ("z", "\0", "!", chr(254), chr(256)) {
1175 my $targ = "a\034$c";
1176 my $reg = "a\\c\\$c";
1177 ok eval ("qq/$targ/ =~ /$reg/"), "\\c\\ in pattern";
1178 }
1179 }
1180
e425a60b
YO
1181 { # Test the (*PRUNE) pattern
1182 our $count = 0;
1183 'aaab' =~ /a+b?(?{$count++})(*FAIL)/;
1184 iseq $count, 9, "Expect 9 for no (*PRUNE)";
1185 $count = 0;
1186 'aaab' =~ /a+b?(*PRUNE)(?{$count++})(*FAIL)/;
1187 iseq $count, 3, "Expect 3 with (*PRUNE)";
1188 local $_ = 'aaab';
1189 $count = 0;
1190 1 while /.(*PRUNE)(?{$count++})(*FAIL)/g;
1191 iseq $count, 4, "/.(*PRUNE)/";
1192 $count = 0;
1193 'aaab' =~ /a+b?(??{'(*PRUNE)'})(?{$count++})(*FAIL)/;
1194 iseq $count, 3, "Expect 3 with (*PRUNE)";
1195 local $_ = 'aaab';
1196 $count = 0;
1197 1 while /.(??{'(*PRUNE)'})(?{$count++})(*FAIL)/g;
1198 iseq $count, 4, "/.(*PRUNE)/";
1199 }
1200
1201
1202 { # Test the (*SKIP) pattern
1203 our $count = 0;
1204 'aaab' =~ /a+b?(*SKIP)(?{$count++})(*FAIL)/;
1205 iseq $count, 1, "Expect 1 with (*SKIP)";
1206 local $_ = 'aaab';
1207 $count = 0;
1208 1 while /.(*SKIP)(?{$count++})(*FAIL)/g;
1209 iseq $count, 4, "/.(*SKIP)/";
1210 $_ = 'aaabaaab';
1211 $count = 0;
1212 our @res = ();
1213 1 while /(a+b?)(*SKIP)(?{$count++; push @res,$1})(*FAIL)/g;
1214 iseq $count, 2, "Expect 2 with (*SKIP)";
1215 iseq "@res", "aaab aaab", "Adjacent (*SKIP) works as expected";
1216 }
1217
1218
1219 { # Test the (*SKIP) pattern
1220 our $count = 0;
1221 'aaab' =~ /a+b?(*MARK:foo)(*SKIP)(?{$count++})(*FAIL)/;
1222 iseq $count, 1, "Expect 1 with (*SKIP)";
1223 local $_ = 'aaab';
1224 $count = 0;
1225 1 while /.(*MARK:foo)(*SKIP)(?{$count++})(*FAIL)/g;
1226 iseq $count, 4, "/.(*SKIP)/";
1227 $_ = 'aaabaaab';
1228 $count = 0;
1229 our @res = ();
1230 1 while /(a+b?)(*MARK:foo)(*SKIP)(?{$count++; push @res,$1})(*FAIL)/g;
1231 iseq $count, 2, "Expect 2 with (*SKIP)";
1232 iseq "@res", "aaab aaab", "Adjacent (*SKIP) works as expected";
1233 }
1234
1235
1236 { # Test the (*SKIP) pattern
1237 our $count = 0;
1238 'aaab' =~ /a*(*MARK:a)b?(*MARK:b)(*SKIP:a)(?{$count++})(*FAIL)/;
1239 iseq $count, 3, "Expect 3 with *MARK:a)b?(*MARK:b)(*SKIP:a)";
1240 local $_ = 'aaabaaab';
1241 $count = 0;
1242 our @res = ();
1243 1 while
1244 /(a*(*MARK:a)b?)(*MARK:x)(*SKIP:a)(?{$count++; push @res,$1})(*FAIL)/g;
1245 iseq $count, 5, "Expect 5 with (*MARK:a)b?)(*MARK:x)(*SKIP:a)";
1246 iseq "@res", "aaab b aaab b ",
1247 "Adjacent (*MARK:a)b?)(*MARK:x)(*SKIP:a) works as expected";
1248 }
1249
1250
1251 { # Test the (*COMMIT) pattern
1252 our $count = 0;
1253 'aaabaaab' =~ /a+b?(*COMMIT)(?{$count++})(*FAIL)/;
1254 iseq $count, 1, "Expect 1 with (*COMMIT)";
1255 local $_ = 'aaab';
1256 $count = 0;
1257 1 while /.(*COMMIT)(?{$count++})(*FAIL)/g;
1258 iseq $count, 1, "/.(*COMMIT)/";
1259 $_ = 'aaabaaab';
1260 $count = 0;
1261 our @res = ();
1262 1 while /(a+b?)(*COMMIT)(?{$count++; push @res,$1})(*FAIL)/g;
1263 iseq $count, 1, "Expect 1 with (*COMMIT)";
1264 iseq "@res", "aaab", "Adjacent (*COMMIT) works as expected";
1265 }
1266
1267
1268 {
1269 # Test named commits and the $REGERROR var
1270 our $REGERROR;
1271 for my $name ('', ':foo') {
1272 for my $pat ("(*PRUNE$name)",
1273 ($name ? "(*MARK$name)" : "") . "(*SKIP$name)",
0f289c68 1274 "(*COMMIT$name)") {
e425a60b
YO
1275 for my $suffix ('(*FAIL)', '') {
1276 'aaaab' =~ /a+b$pat$suffix/;
1277 iseq $REGERROR,
1278 ($suffix ? ($name ? 'foo' : "1") : ""),
1279 "Test $pat and \$REGERROR $suffix";
1280 }
1281 }
1282 }
1283 }
1284
1285
1286 {
1287 # Test named commits and the $REGERROR var
1288 package Fnorble;
1289 our $REGERROR;
1290 for my $name ('', ':foo') {
1291 for my $pat ("(*PRUNE$name)",
1292 ($name ? "(*MARK$name)" : "") . "(*SKIP$name)",
0f289c68 1293 "(*COMMIT$name)") {
e425a60b
YO
1294 for my $suffix ('(*FAIL)','') {
1295 'aaaab' =~ /a+b$pat$suffix/;
1296 ::iseq $REGERROR,
1297 ($suffix ? ($name ? 'foo' : "1") : ""),
1298 "Test $pat and \$REGERROR $suffix";
1299 }
1300 }
0f289c68
YO
1301 }
1302 }
e425a60b
YO
1303
1304
1305 {
1306 # Test named commits and the $REGERROR var
1307 local $Message = '$REGERROR';
1308 our $REGERROR;
1309 for my $word (qw (bar baz bop)) {
1310 $REGERROR = "";
1311 "aaaaa$word" =~
1312 /a+(?:bar(*COMMIT:bar)|baz(*COMMIT:baz)|bop(*COMMIT:bop))(*FAIL)/;
1313 iseq $REGERROR, $word;
0f289c68 1314 }
e425a60b
YO
1315 }
1316
e425a60b
YO
1317 {
1318 #Mindnumbingly simple test of (*THEN)
1319 for ("ABC","BAX") {
1320 ok /A (*THEN) X | B (*THEN) C/x, "Simple (*THEN) test";
1321 }
1322 }
1323
1324
1325 {
1326 local $Message = "Relative Recursion";
1327 my $parens = qr/(\((?:[^()]++|(?-1))*+\))/;
1328 local $_ = 'foo((2*3)+4-3) + bar(2*(3+4)-1*(2-3))';
1329 my ($all, $one, $two) = ('', '', '');
9d45b377 1330 ok m/foo $parens \s* \+ \s* bar $parens/x;
e425a60b
YO
1331 iseq $1, '((2*3)+4-3)';
1332 iseq $2, '(2*(3+4)-1*(2-3))';
1333 iseq $&, 'foo((2*3)+4-3) + bar(2*(3+4)-1*(2-3))';
1334 iseq $&, $_;
1335 }
1336
1337 {
1338 my $spaces=" ";
1339 local $_ = join 'bar', $spaces, $spaces;
1340 our $count = 0;
1341 s/(?>\s+bar)(?{$count++})//g;
1342 iseq $_, $spaces, "SUSPEND final string";
1343 iseq $count, 1, "Optimiser should have prevented more than one match";
1344 }
1345
e425a60b
YO
1346
1347 {
1348 # From Message-ID: <877ixs6oa6.fsf@k75.linux.bogus>
1349 my $dow_name = "nada";
1350 my $parser = "(\$dow_name) = \$time_string =~ /(D\x{e9}\\ " .
1351 "C\x{e9}adaoin|D\x{e9}\\ Sathairn|\\w+|\x{100})/";
1352 my $time_string = "D\x{e9} C\x{e9}adaoin";
1353 eval $parser;
1354 ok !$@, "Test Eval worked";
1355 iseq $dow_name, $time_string, "UTF-8 trie common prefix extraction";
1356 }
1357
1358
1359 {
1360 my $v;
1361 ($v = 'bar') =~ /(\w+)/g;
1362 $v = 'foo';
1363 iseq "$1", 'bar', '$1 is safe after /g - may fail due ' .
1364 'to specialized config in pp_hot.c'
1365 }
1366
1367
1368 {
1369 local $Message = "http://nntp.perl.org/group/perl.perl5.porters/118663";
1370 my $qr_barR1 = qr/(bar)\g-1/;
1371 ok "foobarbarxyz" =~ $qr_barR1;
1372 ok "foobarbarxyz" =~ qr/foo${qr_barR1}xyz/;
1373 ok "foobarbarxyz" =~ qr/(foo)${qr_barR1}xyz/;
1374 ok "foobarbarxyz" =~ qr/(foo)(bar)\g{-1}xyz/;
1375 ok "foobarbarxyz" =~ qr/(foo${qr_barR1})xyz/;
1376 ok "foobarbarxyz" =~ qr/(foo(bar)\g{-1})xyz/;
0f289c68 1377 }
e425a60b 1378
e425a60b
YO
1379 {
1380 local $Message = '$REGMARK';
1381 our @r = ();
1382 our ($REGMARK, $REGERROR);
1383 ok 'foofoo' =~ /foo (*MARK:foo) (?{push @r,$REGMARK}) /x;
0f289c68 1384 iseq "@r","foo";
e425a60b
YO
1385 iseq $REGMARK, "foo";
1386 ok 'foofoo' !~ /foo (*MARK:foo) (*FAIL) /x;
1387 ok !$REGMARK;
1388 iseq $REGERROR, 'foo';
1389 }
1390
1391
1392 {
1393 local $Message = '\K test';
1394 my $x;
1395 $x = "abc.def.ghi.jkl";
1396 $x =~ s/.*\K\..*//;
1397 iseq $x, "abc.def.ghi";
0f289c68 1398
e425a60b
YO
1399 $x = "one two three four";
1400 $x =~ s/o+ \Kthree//g;
1401 iseq $x, "one two four";
0f289c68 1402
e425a60b
YO
1403 $x = "abcde";
1404 $x =~ s/(.)\K/$1/g;
1405 iseq $x, "aabbccddee";
1406 }
1407
1408
1409 {
1410 sub kt {
1411 return '4' if $_[0] eq '09028623';
1412 }
1413 # Nested EVAL using PL_curpm (via $1 or friends)
1414 my $re;
1415 our $grabit = qr/ ([0-6][0-9]{7}) (??{ kt $1 }) [890] /x;
1416 $re = qr/^ ( (??{ $grabit }) ) $ /x;
1417 my @res = '0902862349' =~ $re;
1418 iseq join ("-", @res), "0902862349",
1419 'PL_curpm is set properly on nested eval';
1420
1421 our $qr = qr/ (o) (??{ $1 }) /x;
1422 ok 'boob'=~/( b (??{ $qr }) b )/x && 1, "PL_curpm, nested eval";
1423 }
1424
1425
1426 {
1427 use charnames ":full";
1428 ok "\N{ROMAN NUMERAL ONE}" =~ /\p{Alphabetic}/, "I =~ Alphabetic";
1429 ok "\N{ROMAN NUMERAL ONE}" =~ /\p{Uppercase}/, "I =~ Uppercase";
1430 ok "\N{ROMAN NUMERAL ONE}" !~ /\p{Lowercase}/, "I !~ Lowercase";
1431 ok "\N{ROMAN NUMERAL ONE}" =~ /\p{IDStart}/, "I =~ ID_Start";
1432 ok "\N{ROMAN NUMERAL ONE}" =~ /\p{IDContinue}/, "I =~ ID_Continue";
1433 ok "\N{SMALL ROMAN NUMERAL ONE}" =~ /\p{Alphabetic}/, "i =~ Alphabetic";
1434 ok "\N{SMALL ROMAN NUMERAL ONE}" !~ /\p{Uppercase}/, "i !~ Uppercase";
1435 ok "\N{SMALL ROMAN NUMERAL ONE}" =~ /\p{Lowercase}/, "i =~ Lowercase";
1436 ok "\N{SMALL ROMAN NUMERAL ONE}" =~ /\p{IDStart}/, "i =~ ID_Start";
1437 ok "\N{SMALL ROMAN NUMERAL ONE}" =~ /\p{IDContinue}/, "i =~ ID_Continue"
1438 }
1439
1440
1441 {
1442 # requirement of Unicode Technical Standard #18, 1.7 Code Points
1443 # cf. http://www.unicode.org/reports/tr18/#Supplementary_Characters
1444 for my $u (0x7FF, 0x800, 0xFFFF, 0x10000) {
1445 no warnings 'utf8'; # oops
1446 my $c = chr $u;
1447 my $x = sprintf '%04X', $u;
1448 ok "A${c}B" =~ /A[\0-\x{10000}]B/, "Unicode range - $x";
1449 }
1450 }
1451
1452
1453 {
1454 my $res="";
1455
1456 if ('1' =~ /(?|(?<digit>1)|(?<digit>2))/) {
1457 $res = "@{$- {digit}}";
1458 }
1459 iseq $res, "1",
1460 "Check that (?|...) doesnt cause dupe entries in the names array";
0f289c68 1461
e425a60b
YO
1462 $res = "";
1463 if ('11' =~ /(?|(?<digit>1)|(?<digit>2))(?&digit)/) {
1464 $res = "@{$- {digit}}";
1465 }
1466 iseq $res, "1", "Check that (?&..) to a buffer inside " .
1467 "a (?|...) goes to the leftmost";
1468 }
1469
1470
1471 {
1472 use warnings;
1473 local $Message = "ASCII pattern that really is UTF-8";
1474 my @w;
1475 local $SIG {__WARN__} = sub {push @w, "@_"};
0f289c68 1476 my $c = qq (\x{DF});
e425a60b
YO
1477 ok $c =~ /${c}|\x{100}/;
1478 ok @w == 0;
0f289c68 1479 }
e425a60b
YO
1480
1481
1482 {
1483 local $Message = "Corruption of match results of qr// across scopes";
1484 my $qr = qr/(fo+)(ba+r)/;
1485 'foobar' =~ /$qr/;
1486 iseq "$1$2", "foobar";
1487 {
1488 'foooooobaaaaar' =~ /$qr/;
0f289c68 1489 iseq "$1$2", 'foooooobaaaaar';
e425a60b
YO
1490 }
1491 iseq "$1$2", "foobar";
1492 }
1493
1494
1495 {
1496 local $Message = "HORIZWS";
1497 local $_ = "\t \r\n \n \t".chr(11)."\n";
1498 s/\H/H/g;
1499 s/\h/h/g;
1500 iseq $_, "hhHHhHhhHH";
1501 $_ = "\t \r\n \n \t" . chr (11) . "\n";
1502 utf8::upgrade ($_);
1503 s/\H/H/g;
1504 s/\h/h/g;
1505 iseq $_, "hhHHhHhhHH";
0f289c68 1506 }
e425a60b
YO
1507
1508
1509 {
1510 local $Message = "Various whitespace special patterns";
1511 my @h = map {chr $_} 0x09, 0x20, 0xa0, 0x1680, 0x180e, 0x2000,
1512 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006,
1513 0x2007, 0x2008, 0x2009, 0x200a, 0x202f, 0x205f,
1514 0x3000;
1515 my @v = map {chr $_} 0x0a, 0x0b, 0x0c, 0x0d, 0x85, 0x2028,
1516 0x2029;
1517 my @lb = ("\x0D\x0A", map {chr $_} 0x0A .. 0x0D, 0x85, 0x2028, 0x2029);
1518 foreach my $t ([\@h, qr/\h/, qr/\h+/],
1519 [\@v, qr/\v/, qr/\v+/],
1520 [\@lb, qr/\R/, qr/\R+/],) {
1521 my $ary = shift @$t;
1522 foreach my $pat (@$t) {
1523 foreach my $str (@$ary) {
1524 ok $str =~ /($pat)/, $pat;
1525 iseq $1, $str, $pat;
1526 utf8::upgrade ($str);
1527 ok $str =~ /($pat)/, "Upgraded string - $pat";
1528 iseq $1, $str, "Upgraded string - $pat";
1529 }
1530 }
1531 }
1532 }
1533
1534
1535 {
1536 local $Message = "Check that \\xDF match properly in its various forms";
1537 # Test that \xDF matches properly. this is pretty hacky stuff,
1538 # but its actually needed. The malarky with '-' is to prevent
1539 # compilation caching from playing any role in the test.
1540 my @df = (chr (0xDF), '-', chr (0xDF));
1541 utf8::upgrade ($df [2]);
1542 my @strs = ('ss', 'sS', 'Ss', 'SS', chr (0xDF));
1543 my @ss = map {("$_", "$_")} @strs;
1544 utf8::upgrade ($ss [$_ * 2 + 1]) for 0 .. $#strs;
1545
1546 for my $ssi (0 .. $#ss) {
1547 for my $dfi (0 .. $#df) {
1548 my $pat = $df [$dfi];
1549 my $str = $ss [$ssi];
1550 my $utf_df = ($dfi > 1) ? 'utf8' : '';
1551 my $utf_ss = ($ssi % 2) ? 'utf8' : '';
1552 (my $sstr = $str) =~ s/\xDF/\\xDF/;
1553
1554 if ($utf_df || $utf_ss || length ($ss [$ssi]) == 1) {
1555 my $ret = $str =~ /$pat/i;
1556 next if $pat eq '-';
1557 ok $ret, "\"$sstr\" =~ /\\xDF/i " .
1558 "(str is @{[$utf_ss||'latin']}, pat is " .
1559 "@{[$utf_df||'latin']})";
1560 }
1561 else {
1562 my $ret = $str !~ /$pat/i;
1563 next if $pat eq '-';
1564 ok $ret, "\"$sstr\" !~ /\\xDF/i " .
1565 "(str is @{[$utf_ss||'latin']}, pat is " .
1566 "@{[$utf_df||'latin']})";
1567 }
1568 }
1569 }
1570 }
1571
1572
1573 {
1574 local $Message = "BBC(Bleadperl Breaks CPAN) Today: String::Multibyte";
1575 my $re = qr/(?:[\x00-\xFF]{4})/;
1576 my $hyp = "\0\0\0-";
1577 my $esc = "\0\0\0\\";
1578
1579 my $str = "$esc$hyp$hyp$esc$esc";
1580 my @a = ($str =~ /\G(?:\Q$esc$esc\E|\Q$esc$hyp\E|$re)/g);
1581
1582 iseq @a,3;
1583 local $" = "=";
1584 iseq "@a","$esc$hyp=$hyp=$esc$esc";
1585 }
1586
1587
1588 {
1589 # Test for keys in %+ and %-
1590 local $Message = 'Test keys in %+ and %-';
1591 no warnings 'uninitialized';
1592 my $_ = "abcdef";
1593 /(?<foo>a)|(?<foo>b)/;
1594 iseq ((join ",", sort keys %+), "foo");
1595 iseq ((join ",", sort keys %-), "foo");
1596 iseq ((join ",", sort values %+), "a");
1597 iseq ((join ",", sort map "@$_", values %-), "a ");
1598 /(?<bar>a)(?<bar>b)(?<quux>.)/;
1599 iseq ((join ",", sort keys %+), "bar,quux");
1600 iseq ((join ",", sort keys %-), "bar,quux");
1601 iseq ((join ",", sort values %+), "a,c"); # leftmost
1602 iseq ((join ",", sort map "@$_", values %-), "a b,c");
1603 /(?<un>a)(?<deux>c)?/; # second buffer won't capture
1604 iseq ((join ",", sort keys %+), "un");
1605 iseq ((join ",", sort keys %-), "deux,un");
1606 iseq ((join ",", sort values %+), "a");
1607 iseq ((join ",", sort map "@$_", values %-), ",a");
1608 }
1609
1610
1611 {
1612 # length() on captures, the numbered ones end up in Perl_magic_len
1613 my $_ = "aoeu \xe6var ook";
1614 /^ \w+ \s (?<eek>\S+)/x;
1615
1616 iseq length ($`), 0, q[length $`];
1617 iseq length ($'), 4, q[length $'];
1618 iseq length ($&), 9, q[length $&];
1619 iseq length ($1), 4, q[length $1];
1620 iseq length ($+{eek}), 4, q[length $+{eek} == length $1];
1621 }
1622
1623
1624 {
1625 my $ok = -1;
1626
1627 $ok = exists ($-{x}) ? 1 : 0 if 'bar' =~ /(?<x>foo)|bar/;
1628 iseq $ok, 1, '$-{x} exists after "bar"=~/(?<x>foo)|bar/';
1629 iseq scalar (%+), 0, 'scalar %+ == 0 after "bar"=~/(?<x>foo)|bar/';
1630 iseq scalar (%-), 1, 'scalar %- == 1 after "bar"=~/(?<x>foo)|bar/';
1631
1632 $ok = -1;
1633 $ok = exists ($+{x}) ? 1 : 0 if 'bar' =~ /(?<x>foo)|bar/;
1634 iseq $ok, 0, '$+{x} not exists after "bar"=~/(?<x>foo)|bar/';
1635 iseq scalar (%+), 0, 'scalar %+ == 0 after "bar"=~/(?<x>foo)|bar/';
1636 iseq scalar (%-), 1, 'scalar %- == 1 after "bar"=~/(?<x>foo)|bar/';
1637
1638 $ok = -1;
1639 $ok = exists ($-{x}) ? 1 : 0 if 'foo' =~ /(?<x>foo)|bar/;
1640 iseq $ok, 1, '$-{x} exists after "foo"=~/(?<x>foo)|bar/';
1641 iseq scalar (%+), 1, 'scalar %+ == 1 after "foo"=~/(?<x>foo)|bar/';
1642 iseq scalar (%-), 1, 'scalar %- == 1 after "foo"=~/(?<x>foo)|bar/';
1643
1644 $ok = -1;
1645 $ok = exists ($+{x}) ? 1 : 0 if 'foo'=~/(?<x>foo)|bar/;
1646 iseq $ok, 1, '$+{x} exists after "foo"=~/(?<x>foo)|bar/';
1647 }
1648
1649
1650 {
1651 local $_;
1652 ($_ = 'abc') =~ /(abc)/g;
0f289c68 1653 $_ = '123';
e425a60b
YO
1654 iseq "$1", 'abc', "/g leads to unsafe match vars: $1";
1655 }
1656
1657
1658 {
1659 local $Message = 'Message-ID: <20070818091501.7eff4831@r2d2>';
1660 my $str = "";
1661 for (0 .. 5) {
1662 my @x;
1663 $str .= "@x"; # this should ALWAYS be the empty string
1664 'a' =~ /(a|)/;
1665 push @x, 1;
1666 }
1667 iseq length ($str), 0, "Trie scope error, string should be empty";
1668 $str = "";
1669 my @foo = ('a') x 5;
1670 for (@foo) {
1671 my @bar;
1672 $str .= "@bar";
1673 s/a|/push @bar, 1/e;
1674 }
1675 iseq length ($str), 0, "Trie scope error, string should be empty";
1676 }
1677
1678
1679 {
e425a60b 1680# more TRIE/AHOCORASICK problems with mixed utf8 / latin-1 and case folding
0f289c68
YO
1681 for my $chr (160 .. 255) {
1682 my $chr_byte = chr($chr);
1683 my $chr_utf8 = chr($chr); utf8::upgrade($chr_utf8);
1684 my $rx = qr{$chr_byte|X}i;
1685 ok($chr_utf8 =~ $rx, "utf8/latin, codepoint $chr");
1686 }
e425a60b
YO
1687 }
1688
1689 {
e425a60b
YO
1690 our $a = 3; "" =~ /(??{ $a })/;
1691 our $b = $a;
1692 iseq $b, $a, "Copy of scalar used for postponed subexpression";
1693 }
1694
1695
1696 {
e425a60b
YO
1697 our @ctl_n = ();
1698 our @plus = ();
1699 our $nested_tags;
1700 $nested_tags = qr{
1701 <
1702 (\w+)
1703 (?{
1704 push @ctl_n,$^N;
1705 push @plus,$+;
1706 })
1707 >
1708 (??{$nested_tags})*
1709 </\s* \w+ \s*>
1710 }x;
1711
1712 my $match = '<bla><blubb></blubb></bla>' =~ m/^$nested_tags$/;
1713 ok $match, 'nested construct matches';
1714 iseq "@ctl_n", "bla blubb", '$^N inside of (?{}) works as expected';
1715 iseq "@plus", "bla blubb", '$+ inside of (?{}) works as expected';
1716 }
1717
1718
e425a60b
YO
1719 SKIP: {
1720 # XXX: This set of tests is essentially broken, POSIX character classes
0f289c68 1721 # should not have differing definitions under Unicode.
e425a60b
YO
1722 # There are property names for that.
1723 skip "Tests assume ASCII", 4 unless $IS_ASCII;
1724
1725 my @notIsPunct = grep {/[[:punct:]]/ and not /\p{IsPunct}/}
1726 map {chr} 0x20 .. 0x7f;
1727 iseq join ('', @notIsPunct), '$+<=>^`|~',
1728 '[:punct:] disagress with IsPunct on Symbols';
1729
1730 my @isPrint = grep {not /[[:print:]]/ and /\p{IsPrint}/}
1731 map {chr} 0 .. 0x1f, 0x7f .. 0x9f;
1732 iseq join ('', @isPrint), "\x09\x0a\x0b\x0c\x0d\x85",
1733 'IsPrint disagrees with [:print:] on control characters';
1734
1735 my @isPunct = grep {/[[:punct:]]/ != /\p{IsPunct}/}
1736 map {chr} 0x80 .. 0xff;
0f289c68 1737 iseq join ('', @isPunct), "\xa1\xab\xb7\xbb\xbf", # ¡ « · » ¿
e425a60b
YO
1738 'IsPunct disagrees with [:punct:] outside ASCII';
1739
1740 my @isPunctLatin1 = eval q {
1741 use encoding 'latin1';
1742 grep {/[[:punct:]]/ != /\p{IsPunct}/} map {chr} 0x80 .. 0xff;
1743 };
1744 skip "Eval failed ($@)", 1 if $@;
1745 skip "PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS set to 0", 1
1746 if !$ENV{PERL_TEST_LEGACY_POSIX_CC};
0f289c68 1747 iseq join ('', @isPunctLatin1), '',
e425a60b 1748 'IsPunct agrees with [:punct:] with explicit Latin1';
0f289c68 1749 }
e425a60b 1750
e425a60b
YO
1751 #
1752 # Keep the following tests last -- they may crash perl
1753 #
1754 print "# Tests that follow may crash perl\n";
e425a60b
YO
1755 {
1756 eval '/\k/';
1757 ok $@ =~ /\QSequence \k... not terminated in regex;\E/,
1758 'Lone \k not allowed';
1759 }
1760
e425a60b
YO
1761 {
1762 local $Message = "Substitution with lookahead (possible segv)";
1763 $_ = "ns1ns1ns1";
1764 s/ns(?=\d)/ns_/g;
1765 iseq $_, "ns_1ns_1ns_1";
1766 $_ = "ns1";
1767 s/ns(?=\d)/ns_/;
1768 iseq $_, "ns_1";
1769 $_ = "123";
1770 s/(?=\d+)|(?<=\d)/!Bang!/g;
1771 iseq $_, "!Bang!1!Bang!2!Bang!3!Bang!";
1772 }
1773
e425a60b
YO
1774} # End of sub run_tests
1775
17761;