This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
toke.c: Remove soon-to-be invalid t assumption
[perl5.git] / t / re / pat_advanced.t
CommitLineData
e425a60b
YO
1#!./perl
2#
c5de0829 3# This is a home for regular expression tests that do not fit into
e425a60b
YO
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
3fe9640a
JH
7BEGIN {
8 chdir 't' if -d 't';
9 @INC = qw(../lib .);
10 require './test.pl';
11 require './charset_tools.pl';
12 skip_all_if_miniperl("miniperl can't load Tie::Hash::NamedCapture, need for %+ and %-");
13}
14
e425a60b
YO
15use strict;
16use warnings;
17use 5.010;
fee50582 18our ($REGMARK, $REGERROR);
e425a60b 19
e425a60b
YO
20sub run_tests;
21
22$| = 1;
23
9d45b377 24run_tests() unless caller;
e425a60b
YO
25
26#
27# Tests start here.
28#
29sub run_tests {
30
e425a60b 31 {
e425a60b
YO
32 # Japhy -- added 03/03/2001
33 () = (my $str = "abc") =~ /(...)/;
34 $str = "def";
de26e0cc 35 is($1, "abc", 'Changing subject does not modify $1');
e425a60b
YO
36 }
37
e425a60b
YO
38 SKIP:
39 {
40 # The trick is that in EBCDIC the explicit numeric range should
41 # match (as also in non-EBCDIC) but the explicit alphabetic range
42 # should not match.
43 ok "\x8e" =~ /[\x89-\x91]/, '"\x8e" =~ /[\x89-\x91]/';
44 ok "\xce" =~ /[\xc9-\xd1]/, '"\xce" =~ /[\xc9-\xd1]/';
2ca8589c 45 ok "\xd0" =~ /[\xc9-\xd1]/, '"\xd0" =~ /[\xc9-\xd1]/';
e425a60b
YO
46
47 skip "Not an EBCDIC platform", 2 unless ord ('i') == 0x89 &&
48 ord ('J') == 0xd1;
49
50 # In most places these tests would succeed since \x8e does not
51 # in most character sets match 'i' or 'j' nor would \xce match
52 # 'I' or 'J', but strictly speaking these tests are here for
53 # the good of EBCDIC, so let's test these only there.
b33825c4
NC
54 unlike("\x8e", qr/[i-j]/, '"\x8e" !~ /[i-j]/');
55 unlike("\xce", qr/[I-J]/, '"\xce" !~ /[I-J]/');
2ca8589c 56 unlike("\xd0", qr/[I-J]/, '"\xd0" !~ /[I-J]/');
e425a60b
YO
57 }
58
e425a60b
YO
59 {
60 ok "\x{ab}" =~ /\x{ab}/, '"\x{ab}" =~ /\x{ab}/ ';
61 ok "\x{abcd}" =~ /\x{abcd}/, '"\x{abcd}" =~ /\x{abcd}/';
62 }
63
e425a60b 64 {
de946258 65 my $message = 'bug id 20001008.001';
e425a60b 66
f36cc39d
KW
67 my $strasse = "stra" . uni_to_native("\337") . "e";
68 my @x = ("$strasse 138", "$strasse 138");
e425a60b 69 for (@x) {
de946258
NC
70 ok(s/(\d+)\s*([\w\-]+)/$1 . uc $2/e, $message);
71 ok(my ($latin) = /^(.+)(?:\s+\d)/, $message);
f36cc39d
KW
72 is($latin, $strasse, $message);
73 ok($latin =~ s/$strasse/straße/, $message);
e425a60b
YO
74 #
75 # Previous code follows, but outcommented - there were no tests.
76 #
77 # $latin =~ s/stra\337e/straße/; # \303\237 after the 2nd a
78 # use utf8; # needed for the raw UTF-8
79 # $latin =~ s!(s)tr(?:aß|s+e)!$1tr.!; # \303\237 after the a
80 }
81 }
82
e425a60b 83 {
de946258
NC
84 my $message = 'Test \x escapes';
85 ok("ba\xd4c" =~ /([a\xd4]+)/ && $1 eq "a\xd4", $message);
86 ok("ba\xd4c" =~ /([a\xd4]+)/ && $1 eq "a\x{d4}", $message);
87 ok("ba\x{d4}c" =~ /([a\xd4]+)/ && $1 eq "a\x{d4}", $message);
88 ok("ba\x{d4}c" =~ /([a\xd4]+)/ && $1 eq "a\xd4", $message);
89 ok("ba\xd4c" =~ /([a\x{d4}]+)/ && $1 eq "a\xd4", $message);
90 ok("ba\xd4c" =~ /([a\x{d4}]+)/ && $1 eq "a\x{d4}", $message);
91 ok("ba\x{d4}c" =~ /([a\x{d4}]+)/ && $1 eq "a\x{d4}", $message);
92 ok("ba\x{d4}c" =~ /([a\x{d4}]+)/ && $1 eq "a\xd4", $message);
e425a60b
YO
93 }
94
e425a60b 95 {
de946258 96 my $message = 'Match code points > 255';
e425a60b 97 $_ = "abc\x{100}\x{200}\x{300}\x{380}\x{400}defg";
de946258
NC
98 ok(/(.\x{300})./, $message);
99 ok($` eq "abc\x{100}" && length ($`) == 4, $message);
100 ok($& eq "\x{200}\x{300}\x{380}" && length ($&) == 3, $message);
101 ok($' eq "\x{400}defg" && length ($') == 5, $message);
102 ok($1 eq "\x{200}\x{300}" && length ($1) == 2, $message);
e425a60b
YO
103 }
104
e425a60b
YO
105 {
106 my $x = "\x{10FFFD}";
107 $x =~ s/(.)/$1/g;
108 ok ord($x) == 0x10FFFD && length($x) == 1, "From Robin Houston";
109 }
110
e425a60b
YO
111 {
112 my %d = (
113 "7f" => [0, 0, 0],
114 "80" => [1, 1, 0],
115 "ff" => [1, 1, 0],
116 "100" => [0, 1, 1],
117 );
a2d248f9 118
e425a60b 119 while (my ($code, $match) = each %d) {
de946258 120 my $message = "Properties of \\x$code";
e425a60b 121 my $char = eval qq ["\\x{$code}"];
a2d248f9 122
de946258
NC
123 is(0 + ($char =~ /[\x80-\xff]/), $$match[0], $message);
124 is(0 + ($char =~ /[\x80-\x{100}]/), $$match[1], $message);
125 is(0 + ($char =~ /[\x{100}]/), $$match[2], $message);
e425a60b
YO
126 }
127 }
128
e425a60b
YO
129 {
130 # From Japhy
e4e5d8ba 131 foreach (qw(c g o)) {
4d18b353
NC
132 warning_like(sub {'' =~ "(?$_)"}, qr/^Useless \(\?$_\)/);
133 warning_like(sub {'' =~ "(?-$_)"}, qr/^Useless \(\?-$_\)/);
e4e5d8ba 134 }
e425a60b
YO
135
136 # Now test multi-error regexes
f4554ed5
NC
137 foreach (['(?g-o)', qr/^Useless \(\?g\)/, qr/^Useless \(\?-o\)/],
138 ['(?g-c)', qr/^Useless \(\?g\)/, qr/^Useless \(\?-c\)/],
139 # (?c) means (?g) error won't be thrown
140 ['(?o-cg)', qr/^Useless \(\?o\)/, qr/^Useless \(\?-c\)/],
141 ['(?ogc)', qr/^Useless \(\?o\)/, qr/^Useless \(\?g\)/,
142 qr/^Useless \(\?c\)/],
143 ) {
144 my ($re, @warnings) = @$_;
145 warnings_like(sub {eval "qr/$re/"}, \@warnings, "qr/$re/ warns");
146 }
e425a60b
YO
147 }
148
e425a60b 149 {
de946258 150 my $message = "/x tests";
e425a60b 151 $_ = "foo";
14358a41 152 foreach my $pat (<<" --", <<" --") {
e425a60b
YO
153 /f
154 o\r
155 o
156 \$
157 /x
158 --
e425a60b
YO
159 /f
160 o
161 o
162 \$\r
163 /x
164 --
14358a41
NC
165 is(eval $pat, 1, $message);
166 is($@, '', $message);
167 }
e425a60b
YO
168 }
169
e425a60b 170 {
de946258 171 my $message = "/o feature";
e425a60b 172 sub test_o {$_ [0] =~ /$_[1]/o; return $1}
de946258
NC
173 is(test_o ('abc', '(.)..'), 'a', $message);
174 is(test_o ('abc', '..(.)'), 'a', $message);
e425a60b
YO
175 }
176
e425a60b
YO
177 {
178 # Test basic $^N usage outside of a regex
de946258 179 my $message = '$^N usage outside of a regex';
e425a60b 180 my $x = "abcdef";
de946258
NC
181 ok(($x =~ /cde/ and !defined $^N), $message);
182 ok(($x =~ /(cde)/ and $^N eq "cde"), $message);
183 ok(($x =~ /(c)(d)(e)/ and $^N eq "e"), $message);
184 ok(($x =~ /(c(d)e)/ and $^N eq "cde"), $message);
185 ok(($x =~ /(foo)|(c(d)e)/ and $^N eq "cde"), $message);
186 ok(($x =~ /(c(d)e)|(foo)/ and $^N eq "cde"), $message);
187 ok(($x =~ /(c(d)e)|(abc)/ and $^N eq "abc"), $message);
188 ok(($x =~ /(c(d)e)|(abc)x/ and $^N eq "cde"), $message);
189 ok(($x =~ /(c(d)e)(abc)?/ and $^N eq "cde"), $message);
190 ok(($x =~ /(?:c(d)e)/ and $^N eq "d"), $message);
191 ok(($x =~ /(?:c(d)e)(?:f)/ and $^N eq "d"), $message);
192 ok(($x =~ /(?:([abc])|([def]))*/ and $^N eq "f"), $message);
193 ok(($x =~ /(?:([ace])|([bdf]))*/ and $^N eq "f"), $message);
194 ok(($x =~ /(([ace])|([bd]))*/ and $^N eq "e"), $message);
195 {ok(($x =~ /(([ace])|([bdf]))*/ and $^N eq "f"), $message);}
e425a60b
YO
196 ## Test to see if $^N is automatically localized -- it should now
197 ## have the value set in the previous test.
de26e0cc 198 is($^N, "e", '$^N is automatically localized');
e425a60b
YO
199
200 # Now test inside (?{ ... })
de946258 201 $message = '$^N usage inside (?{ ... })';
e425a60b 202 our ($y, $z);
de946258
NC
203 ok(($x =~ /a([abc])(?{$y=$^N})c/ and $y eq "b"), $message);
204 ok(($x =~ /a([abc]+)(?{$y=$^N})d/ and $y eq "bc"), $message);
205 ok(($x =~ /a([abcdefg]+)(?{$y=$^N})d/ and $y eq "bc"), $message);
206 ok(($x =~ /(a([abcdefg]+)(?{$y=$^N})d)(?{$z=$^N})e/ and $y eq "bc"
207 and $z eq "abcd"), $message);
208 ok(($x =~ /(a([abcdefg]+)(?{$y=$^N})de)(?{$z=$^N})/ and $y eq "bc"
209 and $z eq "abcde"), $message);
e425a60b
YO
210
211 }
212
e425a60b
YO
213 SKIP:
214 {
215 ## Should probably put in tests for all the POSIX stuff,
216 ## but not sure how to guarantee a specific locale......
217
de946258 218 my $message = 'Test [[:cntrl:]]';
e425a60b
YO
219 my $AllBytes = join "" => map {chr} 0 .. 255;
220 (my $x = $AllBytes) =~ s/[[:cntrl:]]//g;
628d95af
KW
221 $x = join "", sort { $a cmp $b }
222 map { chr utf8::native_to_unicode(ord $_) } split "", $x;
de946258 223 is($x, join("", map {chr} 0x20 .. 0x7E, 0x80 .. 0xFF), $message);
e425a60b
YO
224
225 ($x = $AllBytes) =~ s/[^[:cntrl:]]//g;
628d95af
KW
226 $x = join "", sort { $a cmp $b }
227 map { chr utf8::native_to_unicode(ord $_) } split "", $x;
de946258 228 is($x, (join "", map {chr} 0x00 .. 0x1F, 0x7F), $message);
e425a60b
YO
229 }
230
e425a60b
YO
231 {
232 # With /s modifier UTF8 chars were interpreted as bytes
de946258 233 my $message = "UTF-8 chars aren't bytes";
e425a60b
YO
234 my $a = "Hello \x{263A} World";
235 my @a = ($a =~ /./gs);
de946258 236 is($#a, 12, $message);
e425a60b
YO
237 }
238
e425a60b 239 {
e425a60b
YO
240 no warnings 'digit';
241 # Check that \x## works. 5.6.1 and 5.005_03 fail some of these.
242 my $x;
243 $x = "\x4e" . "E";
244 ok ($x =~ /^\x4EE$/, "Check only 2 bytes of hex are matched.");
245
246 $x = "\x4e" . "i";
247 ok ($x =~ /^\x4Ei$/, "Check that invalid hex digit stops it (2)");
248
249 $x = "\x4" . "j";
250 ok ($x =~ /^\x4j$/, "Check that invalid hex digit stops it (1)");
251
252 $x = "\x0" . "k";
253 ok ($x =~ /^\xk$/, "Check that invalid hex digit stops it (0)");
254
255 $x = "\x0" . "x";
256 ok ($x =~ /^\xx$/, "\\xx isn't to be treated as \\0");
257
258 $x = "\x0" . "xa";
259 ok ($x =~ /^\xxa$/, "\\xxa isn't to be treated as \\xa");
260
261 $x = "\x9" . "_b";
262 ok ($x =~ /^\x9_b$/, "\\x9_b isn't to be treated as \\x9b");
263
264 # and now again in [] ranges
265
266 $x = "\x4e" . "E";
267 ok ($x =~ /^[\x4EE]{2}$/, "Check only 2 bytes of hex are matched.");
268
269 $x = "\x4e" . "i";
270 ok ($x =~ /^[\x4Ei]{2}$/, "Check that invalid hex digit stops it (2)");
271
272 $x = "\x4" . "j";
273 ok ($x =~ /^[\x4j]{2}$/, "Check that invalid hex digit stops it (1)");
274
275 $x = "\x0" . "k";
276 ok ($x =~ /^[\xk]{2}$/, "Check that invalid hex digit stops it (0)");
277
278 $x = "\x0" . "x";
279 ok ($x =~ /^[\xx]{2}$/, "\\xx isn't to be treated as \\0");
280
281 $x = "\x0" . "xa";
282 ok ($x =~ /^[\xxa]{3}$/, "\\xxa isn't to be treated as \\xa");
283
284 $x = "\x9" . "_b";
285 ok ($x =~ /^[\x9_b]{3}$/, "\\x9_b isn't to be treated as \\x9b");
286
287 # Check that \x{##} works. 5.6.1 fails quite a few of these.
288
289 $x = "\x9b";
290 ok ($x =~ /^\x{9_b}$/, "\\x{9_b} is to be treated as \\x9b");
291
292 $x = "\x9b" . "y";
293 ok ($x =~ /^\x{9_b}y$/, "\\x{9_b} is to be treated as \\x9b (again)");
294
295 $x = "\x9b" . "y";
296 ok ($x =~ /^\x{9b_}y$/, "\\x{9b_} is to be treated as \\x9b");
297
298 $x = "\x9b" . "y";
299 ok ($x =~ /^\x{9_bq}y$/, "\\x{9_bc} is to be treated as \\x9b");
300
301 $x = "\x0" . "y";
302 ok ($x =~ /^\x{x9b}y$/, "\\x{x9b} is to be treated as \\x0");
303
304 $x = "\x0" . "y";
305 ok ($x =~ /^\x{0x9b}y$/, "\\x{0x9b} is to be treated as \\x0");
306
307 $x = "\x9b" . "y";
308 ok ($x =~ /^\x{09b}y$/, "\\x{09b} is to be treated as \\x9b");
309
310 $x = "\x9b";
311 ok ($x =~ /^[\x{9_b}]$/, "\\x{9_b} is to be treated as \\x9b");
312
313 $x = "\x9b" . "y";
314 ok ($x =~ /^[\x{9_b}y]{2}$/,
315 "\\x{9_b} is to be treated as \\x9b (again)");
316
317 $x = "\x9b" . "y";
318 ok ($x =~ /^[\x{9b_}y]{2}$/, "\\x{9b_} is to be treated as \\x9b");
319
320 $x = "\x9b" . "y";
321 ok ($x =~ /^[\x{9_bq}y]{2}$/, "\\x{9_bc} is to be treated as \\x9b");
322
323 $x = "\x0" . "y";
324 ok ($x =~ /^[\x{x9b}y]{2}$/, "\\x{x9b} is to be treated as \\x0");
325
326 $x = "\x0" . "y";
327 ok ($x =~ /^[\x{0x9b}y]{2}$/, "\\x{0x9b} is to be treated as \\x0");
328
329 $x = "\x9b" . "y";
330 ok ($x =~ /^[\x{09b}y]{2}$/, "\\x{09b} is to be treated as \\x9b");
331
332 }
333
e425a60b
YO
334 {
335 # High bit bug -- japhy
336 my $x = "ab\200d";
337 ok $x =~ /.*?\200/, "High bit fine";
338 }
339
e425a60b
YO
340 {
341 # The basic character classes and Unicode
342 ok "\x{0100}" =~ /\w/, 'LATIN CAPITAL LETTER A WITH MACRON in /\w/';
343 ok "\x{0660}" =~ /\d/, 'ARABIC-INDIC DIGIT ZERO in /\d/';
344 ok "\x{1680}" =~ /\s/, 'OGHAM SPACE MARK in /\s/';
345 }
346
e425a60b 347 {
de946258
NC
348 my $message = "Folding matches and Unicode";
349 like("a\x{100}", qr/A/i, $message);
350 like("A\x{100}", qr/a/i, $message);
351 like("a\x{100}", qr/a/i, $message);
352 like("A\x{100}", qr/A/i, $message);
353 like("\x{101}a", qr/\x{100}/i, $message);
354 like("\x{100}a", qr/\x{100}/i, $message);
355 like("\x{101}a", qr/\x{101}/i, $message);
356 like("\x{100}a", qr/\x{101}/i, $message);
357 like("a\x{100}", qr/A\x{100}/i, $message);
358 like("A\x{100}", qr/a\x{100}/i, $message);
359 like("a\x{100}", qr/a\x{100}/i, $message);
360 like("A\x{100}", qr/A\x{100}/i, $message);
361 like("a\x{100}", qr/[A]/i, $message);
362 like("A\x{100}", qr/[a]/i, $message);
363 like("a\x{100}", qr/[a]/i, $message);
364 like("A\x{100}", qr/[A]/i, $message);
365 like("\x{101}a", qr/[\x{100}]/i, $message);
366 like("\x{100}a", qr/[\x{100}]/i, $message);
367 like("\x{101}a", qr/[\x{101}]/i, $message);
368 like("\x{100}a", qr/[\x{101}]/i, $message);
e425a60b
YO
369 }
370
e425a60b
YO
371 {
372 use charnames ':full';
de946258 373 my $message = "Folding 'LATIN LETTER A WITH GRAVE'";
e425a60b
YO
374
375 my $lower = "\N{LATIN SMALL LETTER A WITH GRAVE}";
376 my $UPPER = "\N{LATIN CAPITAL LETTER A WITH GRAVE}";
0f289c68 377
de946258
NC
378 like($lower, qr/$UPPER/i, $message);
379 like($UPPER, qr/$lower/i, $message);
380 like($lower, qr/[$UPPER]/i, $message);
381 like($UPPER, qr/[$lower]/i, $message);
e425a60b 382
de946258 383 $message = "Folding 'GREEK LETTER ALPHA WITH VRACHY'";
e425a60b
YO
384
385 $lower = "\N{GREEK CAPITAL LETTER ALPHA WITH VRACHY}";
386 $UPPER = "\N{GREEK SMALL LETTER ALPHA WITH VRACHY}";
387
de946258
NC
388 like($lower, qr/$UPPER/i, $message);
389 like($UPPER, qr/$lower/i, $message);
390 like($lower, qr/[$UPPER]/i, $message);
391 like($UPPER, qr/[$lower]/i, $message);
e425a60b 392
de946258 393 $message = "Folding 'LATIN LETTER Y WITH DIAERESIS'";
e425a60b
YO
394
395 $lower = "\N{LATIN SMALL LETTER Y WITH DIAERESIS}";
396 $UPPER = "\N{LATIN CAPITAL LETTER Y WITH DIAERESIS}";
397
de946258
NC
398 like($lower, qr/$UPPER/i, $message);
399 like($UPPER, qr/$lower/i, $message);
400 like($lower, qr/[$UPPER]/i, $message);
401 like($UPPER, qr/[$lower]/i, $message);
e425a60b
YO
402 }
403
e425a60b
YO
404 {
405 use charnames ':full';
de946258 406 my $message = "GREEK CAPITAL LETTER SIGMA vs " .
e425a60b
YO
407 "COMBINING GREEK PERISPOMENI";
408
409 my $SIGMA = "\N{GREEK CAPITAL LETTER SIGMA}";
410 my $char = "\N{COMBINING GREEK PERISPOMENI}";
411
c11a8df3
NC
412 warning_is(sub {unlike("_:$char:_", qr/_:$SIGMA:_/i, $message)}, undef,
413 'Did not warn [change a5961de5f4215b5c]');
e425a60b
YO
414 }
415
e425a60b 416 {
de946258 417 my $message = '\X';
e425a60b
YO
418 use charnames ':full';
419
de946258
NC
420 ok("a!" =~ /^(\X)!/ && $1 eq "a", $message);
421 ok("\xDF!" =~ /^(\X)!/ && $1 eq "\xDF", $message);
422 ok("\x{100}!" =~ /^(\X)!/ && $1 eq "\x{100}", $message);
423 ok("\x{100}\x{300}!" =~ /^(\X)!/ && $1 eq "\x{100}\x{300}", $message);
424 ok("\N{LATIN CAPITAL LETTER E}!" =~ /^(\X)!/ &&
425 $1 eq "\N{LATIN CAPITAL LETTER E}", $message);
426 ok("\N{LATIN CAPITAL LETTER E}\N{COMBINING GRAVE ACCENT}!"
e425a60b 427 =~ /^(\X)!/ &&
de946258 428 $1 eq "\N{LATIN CAPITAL LETTER E}\N{COMBINING GRAVE ACCENT}", $message);
e425a60b 429
e425a60b
YO
430 }
431
e425a60b 432 {
de946258 433 my $message = "Final Sigma";
e425a60b
YO
434
435 my $SIGMA = "\x{03A3}"; # CAPITAL
436 my $Sigma = "\x{03C2}"; # SMALL FINAL
437 my $sigma = "\x{03C3}"; # SMALL
438
de946258
NC
439 like($SIGMA, qr/$SIGMA/i, $message);
440 like($SIGMA, qr/$Sigma/i, $message);
441 like($SIGMA, qr/$sigma/i, $message);
e425a60b 442
de946258
NC
443 like($Sigma, qr/$SIGMA/i, $message);
444 like($Sigma, qr/$Sigma/i, $message);
445 like($Sigma, qr/$sigma/i, $message);
e425a60b 446
de946258
NC
447 like($sigma, qr/$SIGMA/i, $message);
448 like($sigma, qr/$Sigma/i, $message);
449 like($sigma, qr/$sigma/i, $message);
0f289c68 450
de946258
NC
451 like($SIGMA, qr/[$SIGMA]/i, $message);
452 like($SIGMA, qr/[$Sigma]/i, $message);
453 like($SIGMA, qr/[$sigma]/i, $message);
e425a60b 454
de946258
NC
455 like($Sigma, qr/[$SIGMA]/i, $message);
456 like($Sigma, qr/[$Sigma]/i, $message);
457 like($Sigma, qr/[$sigma]/i, $message);
e425a60b 458
de946258
NC
459 like($sigma, qr/[$SIGMA]/i, $message);
460 like($sigma, qr/[$Sigma]/i, $message);
461 like($sigma, qr/[$sigma]/i, $message);
e425a60b 462
de946258 463 $message = "More final Sigma";
e425a60b
YO
464
465 my $S3 = "$SIGMA$Sigma$sigma";
466
de946258
NC
467 ok(":$S3:" =~ /:(($SIGMA)+):/i && $1 eq $S3 && $2 eq $sigma, $message);
468 ok(":$S3:" =~ /:(($Sigma)+):/i && $1 eq $S3 && $2 eq $sigma, $message);
469 ok(":$S3:" =~ /:(($sigma)+):/i && $1 eq $S3 && $2 eq $sigma, $message);
e425a60b 470
de946258
NC
471 ok(":$S3:" =~ /:(([$SIGMA])+):/i && $1 eq $S3 && $2 eq $sigma, $message);
472 ok(":$S3:" =~ /:(([$Sigma])+):/i && $1 eq $S3 && $2 eq $sigma, $message);
473 ok(":$S3:" =~ /:(([$sigma])+):/i && $1 eq $S3 && $2 eq $sigma, $message);
e425a60b
YO
474 }
475
e425a60b
YO
476 {
477 use charnames ':full';
de946258 478 my $message = "Parlez-Vous " .
e425a60b
YO
479 "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais?";
480
de946258
NC
481 ok("Fran\N{LATIN SMALL LETTER C}ais" =~ /Fran.ais/ &&
482 $& eq "Francais", $message);
483 ok("Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" =~ /Fran.ais/ &&
484 $& eq "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais", $message);
de946258
NC
485 ok("Fran\N{LATIN SMALL LETTER C}ais" =~ /Fran\Xais/ &&
486 $& eq "Francais", $message);
487 ok("Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" =~ /Fran\Xais/ &&
488 $& eq "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais", $message);
489 ok("Franc\N{COMBINING CEDILLA}ais" =~ /Fran\Xais/ &&
490 $& eq "Franc\N{COMBINING CEDILLA}ais", $message);
491 ok("Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" =~
e425a60b 492 /Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais/ &&
de946258
NC
493 $& eq "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais", $message);
494 ok("Franc\N{COMBINING CEDILLA}ais" =~ /Franc\N{COMBINING CEDILLA}ais/ &&
495 $& eq "Franc\N{COMBINING CEDILLA}ais", $message);
e425a60b
YO
496
497 my @f = (
498 ["Fran\N{LATIN SMALL LETTER C}ais", "Francais"],
499 ["Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais",
500 "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais"],
501 ["Franc\N{COMBINING CEDILLA}ais", "Franc\N{COMBINING CEDILLA}ais"],
502 );
503 foreach my $entry (@f) {
504 my ($subject, $match) = @$entry;
de946258 505 ok($subject =~ /Fran(?:c\N{COMBINING CEDILLA}?|
e425a60b 506 \N{LATIN SMALL LETTER C WITH CEDILLA})ais/x &&
de946258 507 $& eq $match, $message);
e425a60b
YO
508 }
509 }
510
e425a60b 511 {
de946258 512 my $message = "Lingering (and useless) UTF8 flag doesn't mess up /i";
e425a60b
YO
513 my $pat = "ABcde";
514 my $str = "abcDE\x{100}";
515 chop $str;
de946258 516 like($str, qr/$pat/i, $message);
e425a60b
YO
517
518 $pat = "ABcde\x{100}";
519 $str = "abcDE";
520 chop $pat;
de946258 521 like($str, qr/$pat/i, $message);
e425a60b
YO
522
523 $pat = "ABcde\x{100}";
524 $str = "abcDE\x{100}";
525 chop $pat;
526 chop $str;
de946258 527 like($str, qr/$pat/i, $message);
e425a60b
YO
528 }
529
e425a60b
YO
530 {
531 use charnames ':full';
de946258 532 my $message = "LATIN SMALL LETTER SHARP S " .
e425a60b
YO
533 "(\N{LATIN SMALL LETTER SHARP S})";
534
de946258
NC
535 like("\N{LATIN SMALL LETTER SHARP S}",
536 qr/\N{LATIN SMALL LETTER SHARP S}/, $message);
537 like("\N{LATIN SMALL LETTER SHARP S}",
538 qr/\N{LATIN SMALL LETTER SHARP S}/i, $message);
539 like("\N{LATIN SMALL LETTER SHARP S}",
540 qr/[\N{LATIN SMALL LETTER SHARP S}]/, $message);
541 like("\N{LATIN SMALL LETTER SHARP S}",
542 qr/[\N{LATIN SMALL LETTER SHARP S}]/i, $message);
e425a60b 543
de946258
NC
544 like("ss", qr /\N{LATIN SMALL LETTER SHARP S}/i, $message);
545 like("SS", qr /\N{LATIN SMALL LETTER SHARP S}/i, $message);
546 like("ss", qr/[\N{LATIN SMALL LETTER SHARP S}]/i, $message);
547 like("SS", qr/[\N{LATIN SMALL LETTER SHARP S}]/i, $message);
e425a60b 548
de946258
NC
549 like("\N{LATIN SMALL LETTER SHARP S}", qr/ss/i, $message);
550 like("\N{LATIN SMALL LETTER SHARP S}", qr/SS/i, $message);
0f289c68 551
de946258
NC
552 $message = "Unoptimized named sequence in class";
553 like("ss", qr/[\N{LATIN SMALL LETTER SHARP S}x]/i, $message);
554 like("SS", qr/[\N{LATIN SMALL LETTER SHARP S}x]/i, $message);
555 like("\N{LATIN SMALL LETTER SHARP S}",
556 qr/[\N{LATIN SMALL LETTER SHARP S}x]/, $message);
557 like("\N{LATIN SMALL LETTER SHARP S}",
558 qr/[\N{LATIN SMALL LETTER SHARP S}x]/i, $message);
e425a60b
YO
559 }
560
e425a60b
YO
561 {
562 # More whitespace: U+0085, U+2028, U+2029\n";
563
564 # U+0085, U+00A0 need to be forced to be Unicode, the \x{100} does that.
f36cc39d
KW
565 ok "<\x{100}" . uni_to_native("\x{0085}") . ">" =~ /<\x{100}\s>/, '\x{0085} in \s';
566 ok "<" . uni_to_native("\x{0085}") . ">" =~ /<\v>/, '\x{0085} in \v';
567 ok "<\x{100}" . uni_to_native("\x{00A0}") . ">" =~ /<\x{100}\s>/, '\x{00A0} in \s';
568 ok "<" . uni_to_native("\x{00A0}") . ">" =~ /<\h>/, '\x{00A0} in \h';
a9c9e371 569 my @h = map {sprintf "%05x" => $_} 0x01680, 0x02000 .. 0x0200A,
e425a60b
YO
570 0x0202F, 0x0205F, 0x03000;
571 my @v = map {sprintf "%05x" => $_} 0x02028, 0x02029;
572
573 my @H = map {sprintf "%05x" => $_} 0x01361, 0x0200B, 0x02408, 0x02420,
a9c9e371 574 0x0303F, 0xE0020, 0x180E;
e425a60b 575 my @V = map {sprintf "%05x" => $_} 0x0008A .. 0x0008D, 0x00348, 0x10100,
a9c9e371 576 0xE005F, 0xE007C, 0x180E;
e425a60b
YO
577
578 for my $hex (@h) {
579 my $str = eval qq ["<\\x{$hex}>"];
580 ok $str =~ /<\s>/, "\\x{$hex} in \\s";
581 ok $str =~ /<\h>/, "\\x{$hex} in \\h";
582 ok $str !~ /<\v>/, "\\x{$hex} not in \\v";
583 }
584
585 for my $hex (@v) {
586 my $str = eval qq ["<\\x{$hex}>"];
587 ok $str =~ /<\s>/, "\\x{$hex} in \\s";
588 ok $str =~ /<\v>/, "\\x{$hex} in \\v";
589 ok $str !~ /<\h>/, "\\x{$hex} not in \\h";
590 }
591
592 for my $hex (@H) {
593 my $str = eval qq ["<\\x{$hex}>"];
594 ok $str =~ /<\S>/, "\\x{$hex} in \\S";
595 ok $str =~ /<\H>/, "\\x{$hex} in \\H";
596 }
597
598 for my $hex (@V) {
599 my $str = eval qq ["<\\x{$hex}>"];
600 ok $str =~ /<\S>/, "\\x{$hex} in \\S";
601 ok $str =~ /<\V>/, "\\x{$hex} in \\V";
602 }
603 }
604
e425a60b
YO
605 {
606 # . with /s should work on characters, as opposed to bytes
de946258 607 my $message = ". with /s works on characters, not bytes";
e425a60b
YO
608
609 my $s = "\x{e4}\x{100}";
610 # This is not expected to match: the point is that
611 # neither should we get "Malformed UTF-8" warnings.
c11a8df3
NC
612 warning_is(sub {$s =~ /\G(.+?)\n/gcs}, undef,
613 "No 'Malformed UTF-8' warning");
e425a60b
YO
614
615 my @c;
616 push @c => $1 while $s =~ /\G(.)/gs;
617
618 local $" = "";
de946258 619 is("@c", $s, $message);
e425a60b
YO
620
621 # Test only chars < 256
622 my $t1 = "Q003\n\n\x{e4}\x{f6}\n\nQ004\n\n\x{e7}";
623 my $r1 = "";
624 while ($t1 =~ / \G ( .+? ) \n\s+ ( .+? ) ( $ | \n\s+ ) /xgcs) {
0f289c68 625 $r1 .= $1 . $2;
e425a60b
YO
626 }
627
628 my $t2 = $t1 . "\x{100}"; # Repeat with a larger char
629 my $r2 = "";
630 while ($t2 =~ / \G ( .+? ) \n\s+ ( .+? ) ( $ | \n\s+ ) /xgcs) {
0f289c68 631 $r2 .= $1 . $2;
e425a60b
YO
632 }
633 $r2 =~ s/\x{100}//;
634
de946258 635 is($r1, $r2, $message);
e425a60b
YO
636 }
637
e425a60b 638 {
de946258
NC
639 my $message = "Unicode lookbehind";
640 like("A\x{100}B" , qr/(?<=A.)B/, $message);
641 like("A\x{200}\x{300}B", qr/(?<=A..)B/, $message);
642 like("\x{400}AB" , qr/(?<=\x{400}.)B/, $message);
643 like("\x{500}\x{600}B" , qr/(?<=\x{500}.)B/, $message);
e425a60b
YO
644
645 # Original code also contained:
646 # ok "\x{500\x{600}}B" =~ /(?<=\x{500}.)B/;
647 # but that looks like a typo.
648 }
649
e425a60b 650 {
de946258 651 my $message = 'UTF-8 hash keys and /$/';
e425a60b
YO
652 # http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters
653 # /2002-01/msg01327.html
654
655 my $u = "a\x{100}";
656 my $v = substr ($u, 0, 1);
657 my $w = substr ($u, 1, 1);
658 my %u = ($u => $u, $v => $v, $w => $w);
659 for (keys %u) {
660 my $m1 = /^\w*$/ ? 1 : 0;
661 my $m2 = $u {$_} =~ /^\w*$/ ? 1 : 0;
de946258 662 is($m1, $m2, $message);
e425a60b
YO
663 }
664 }
665
e425a60b 666 {
de946258 667 my $message = "No SEGV in s/// and UTF-8";
e425a60b 668 my $s = "s#\x{100}" x 4;
de946258 669 ok($s =~ s/[^\w]/ /g, $message);
2e84be61 670 if ( 1 or $ENV{PERL_TEST_LEGACY_POSIX_CC} ) {
de946258 671 is($s, "s \x{100}" x 4, $message);
e425a60b
YO
672 }
673 else {
de946258 674 is($s, "s " x 4, $message);
e425a60b
YO
675 }
676 }
677
e425a60b 678 {
de946258 679 my $message = "UTF-8 bug (maybe already known?)";
e425a60b
YO
680 my $u = "foo";
681 $u =~ s/./\x{100}/g;
de946258 682 is($u, "\x{100}\x{100}\x{100}", $message);
e425a60b
YO
683
684 $u = "foobar";
685 $u =~ s/[ao]/\x{100}/g;
de946258 686 is($u, "f\x{100}\x{100}b\x{100}r", $message);
e425a60b
YO
687
688 $u =~ s/\x{100}/e/g;
de946258 689 is($u, "feeber", $message);
e425a60b
YO
690 }
691
e425a60b 692 {
de946258 693 my $message = "UTF-8 bug with s///";
e425a60b
YO
694 # check utf8/non-utf8 mixtures
695 # try to force all float/anchored check combinations
696
697 my $c = "\x{100}";
698 my $subst;
699 for my $re ("xx.*$c", "x.*$c$c", "$c.*xx", "$c$c.*x",
700 "xx.*(?=$c)", "(?=$c).*xx",) {
de946258
NC
701 unlike("xxx", qr/$re/, $message);
702 ok(+($subst = "xxx") !~ s/$re//, $message);
e425a60b
YO
703 }
704 for my $re ("xx.*$c*", "$c*.*xx") {
de946258
NC
705 like("xxx", qr/$re/, $message);
706 ok(+($subst = "xxx") =~ s/$re//, $message);
707 is($subst, "", $message);
e425a60b
YO
708 }
709 for my $re ("xxy*", "y*xx") {
de946258
NC
710 like("xx$c", qr/$re/, $message);
711 ok(+($subst = "xx$c") =~ s/$re//, $message);
712 is($subst, $c, $message);
713 unlike("xy$c", qr/$re/, $message);
714 ok(+($subst = "xy$c") !~ s/$re//, $message);
e425a60b
YO
715 }
716 for my $re ("xy$c*z", "x$c*yz") {
de946258
NC
717 like("xyz", qr/$re/, $message);
718 ok(+($subst = "xyz") =~ s/$re//, $message);
719 is($subst, "", $message);
e425a60b
YO
720 }
721 }
722
e425a60b 723 {
c72077c4
AC
724 # The second half of RT #114808
725 warning_is(sub {'aa' =~ /.+\x{100}/}, undef,
726 'utf8-only floating substr, non-utf8 target, no warning');
727 }
728
729 {
de946258 730 my $message = "qr /.../x";
e425a60b 731 my $R = qr / A B C # D E/x;
de946258
NC
732 ok("ABCDE" =~ $R && $& eq "ABC", $message);
733 ok("ABCDE" =~ /$R/ && $& eq "ABC", $message);
734 ok("ABCDE" =~ m/$R/ && $& eq "ABC", $message);
735 ok("ABCDE" =~ /($R)/ && $1 eq "ABC", $message);
736 ok("ABCDE" =~ m/($R)/ && $1 eq "ABC", $message);
e425a60b
YO
737 }
738
e425a60b
YO
739 {
740 local $\;
741 $_ = 'aaaaaaaaaa';
742 utf8::upgrade($_); chop $_; $\="\n";
743 ok /[^\s]+/, 'm/[^\s]/ utf8';
744 ok /[^\d]+/, 'm/[^\d]/ utf8';
745 ok +($a = $_, $_ =~ s/[^\s]+/./g), 's/[^\s]/ utf8';
746 ok +($a = $_, $a =~ s/[^\d]+/./g), 's/[^\s]/ utf8';
747 }
748
e425a60b
YO
749 {
750 # Subject: Odd regexp behavior
751 # From: Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>
752 # Date: Wed, 26 Feb 2003 16:53:12 +0000
753 # Message-Id: <E18o4nw-0008Ly-00@wisbech.cl.cam.ac.uk>
754 # To: perl-unicode@perl.org
755
de946258 756 my $message = 'Markus Kuhn 2003-02-26';
0f289c68 757
e425a60b 758 my $x = "\x{2019}\nk";
de946258
NC
759 ok($x =~ s/(\S)\n(\S)/$1 $2/sg, $message);
760 is($x, "\x{2019} k", $message);
e425a60b
YO
761
762 $x = "b\nk";
de946258
NC
763 ok($x =~ s/(\S)\n(\S)/$1 $2/sg, $message);
764 is($x, "b k", $message);
e425a60b 765
de946258 766 like("\x{2019}", qr/\S/, $message);
e425a60b
YO
767 }
768
e425a60b 769 {
e425a60b
YO
770 ok "\x{100}\n" =~ /\x{100}\n$/, "UTF-8 length cache and fbm_compile";
771 }
772
e425a60b
YO
773 {
774 package Str;
775 use overload q /""/ => sub {${$_ [0]};};
776 sub new {my ($c, $v) = @_; bless \$v, $c;}
777
778 package main;
779 $_ = Str -> new ("a\x{100}/\x{100}b");
780 ok join (":", /\b(.)\x{100}/g) eq "a:/", "re_intuit_start and PL_bostr";
781 }
782
e425a60b
YO
783 {
784 my $re = qq /^([^X]*)X/;
785 utf8::upgrade ($re);
786 ok "\x{100}X" =~ /$re/, "S_cl_and ANYOF_UNICODE & ANYOF_INVERTED";
0ab1ff8e
KW
787 my $loc_re = qq /(?l:^([^X]*)X)/;
788 utf8::upgrade ($loc_re);
613abc6d 789 no warnings 'locale';
0ab1ff8e 790 ok "\x{100}X" =~ /$loc_re/, "locale, S_cl_and ANYOF_UNICODE & ANYOF_INVERTED";
e425a60b
YO
791 }
792
e425a60b
YO
793 {
794 ok "123\x{100}" =~ /^.*1.*23\x{100}$/,
795 'UTF-8 + multiple floating substr';
796 }
797
e425a60b 798 {
de946258 799 my $message = '<20030808193656.5109.1@llama.ni-s.u-net.com>';
e425a60b
YO
800
801 # LATIN SMALL/CAPITAL LETTER A WITH MACRON
de946258 802 like(" \x{101}", qr/\x{100}/i, $message);
e425a60b
YO
803
804 # LATIN SMALL/CAPITAL LETTER A WITH RING BELOW
de946258 805 like(" \x{1E01}", qr/\x{1E00}/i, $message);
e425a60b
YO
806
807 # DESERET SMALL/CAPITAL LETTER LONG I
de946258 808 like(" \x{10428}", qr/\x{10400}/i, $message);
e425a60b
YO
809
810 # LATIN SMALL/CAPITAL LETTER A WITH RING BELOW + 'X'
de946258 811 like(" \x{1E01}x", qr/\x{1E00}X/i, $message);
e425a60b
YO
812 }
813
e425a60b 814 {
26faadbd 815 for (120 .. 130, 240 .. 260) {
e425a60b 816 my $head = 'x' x $_;
de946258 817 my $message = q [Don't misparse \x{...} in regexp ] .
26faadbd 818 q [near EXACT char count limit];
e425a60b 819 for my $tail ('\x{0061}', '\x{1234}', '\x61') {
14358a41
NC
820 eval qq{like("$head$tail", qr/$head$tail/, \$message)};
821 is($@, '', $message);
e425a60b 822 }
de946258 823 $message = q [Don't misparse \N{...} in regexp ] .
26faadbd 824 q [near EXACT char count limit];
e425a60b 825 for my $tail ('\N{SNOWFLAKE}') {
14358a41
NC
826 eval qq {use charnames ':full';
827 like("$head$tail", qr/$head$tail/, \$message)};
828 is($@, '', $message);
e425a60b
YO
829 }
830 }
831 }
832
e425a60b
YO
833 { # TRIE related
834 our @got = ();
835 "words" =~ /(word|word|word)(?{push @got, $1})s$/;
de26e0cc 836 is(@got, 1, "TRIE optimisation");
e425a60b
YO
837
838 @got = ();
839 "words" =~ /(word|word|word)(?{push @got,$1})s$/i;
de26e0cc 840 is(@got, 1,"TRIEF optimisation");
e425a60b
YO
841
842 my @nums = map {int rand 1000} 1 .. 100;
843 my $re = "(" . (join "|", @nums) . ")";
844 $re = qr/\b$re\b/;
845
846 foreach (@nums) {
847 ok $_ =~ /$re/, "Trie nums";
848 }
849
850 $_ = join " ", @nums;
851 @got = ();
852 push @got, $1 while /$re/g;
853
854 my %count;
855 $count {$_} ++ for @got;
856 my $ok = 1;
857 for (@nums) {
858 $ok = 0 if --$count {$_} < 0;
859 }
860 ok $ok, "Trie min count matches";
861 }
862
e425a60b
YO
863 {
864 # TRIE related
865 # LATIN SMALL/CAPITAL LETTER A WITH MACRON
866 ok "foba \x{101}foo" =~ qr/(foo|\x{100}foo|bar)/i &&
867 $1 eq "\x{101}foo",
868 "TRIEF + LATIN SMALL/CAPITAL LETTER A WITH MACRON";
869
870 # LATIN SMALL/CAPITAL LETTER A WITH RING BELOW
871 ok "foba \x{1E01}foo" =~ qr/(foo|\x{1E00}foo|bar)/i &&
872 $1 eq "\x{1E01}foo",
873 "TRIEF + LATIN SMALL/CAPITAL LETTER A WITH RING BELOW";
874
875 # DESERET SMALL/CAPITAL LETTER LONG I
876 ok "foba \x{10428}foo" =~ qr/(foo|\x{10400}foo|bar)/i &&
877 $1 eq "\x{10428}foo",
878 "TRIEF + DESERET SMALL/CAPITAL LETTER LONG I";
879
880 # LATIN SMALL/CAPITAL LETTER A WITH RING BELOW + 'X'
881 ok "foba \x{1E01}xfoo" =~ qr/(foo|\x{1E00}Xfoo|bar)/i &&
882 $1 eq "\x{1E01}xfoo",
883 "TRIEF + LATIN SMALL/CAPITAL LETTER A WITH RING BELOW + 'X'";
884
885 use charnames ':full';
886
887 my $s = "\N{LATIN SMALL LETTER SHARP S}";
888 ok "foba ba$s" =~ qr/(foo|Ba$s|bar)/i && $1 eq "ba$s",
889 "TRIEF + LATIN SMALL LETTER SHARP S =~ ss";
890 ok "foba ba$s" =~ qr/(Ba$s|foo|bar)/i && $1 eq "ba$s",
891 "TRIEF + LATIN SMALL LETTER SHARP S =~ ss";
892 ok "foba ba$s" =~ qr/(foo|bar|Ba$s)/i && $1 eq "ba$s",
893 "TRIEF + LATIN SMALL LETTER SHARP S =~ ss";
894
895 ok "foba ba$s" =~ qr/(foo|Bass|bar)/i && $1 eq "ba$s",
896 "TRIEF + LATIN SMALL LETTER SHARP S =~ ss";
897
898 ok "foba ba$s" =~ qr/(foo|BaSS|bar)/i && $1 eq "ba$s",
899 "TRIEF + LATIN SMALL LETTER SHARP S =~ SS";
900
901 ok "foba ba${s}pxySS$s$s" =~ qr/(b(?:a${s}t|a${s}f|a${s}p)[xy]+$s*)/i
902 && $1 eq "ba${s}pxySS$s$s",
903 "COMMON PREFIX TRIEF + LATIN SMALL LETTER SHARP S";
904 }
905
e425a60b 906 {
3fe9640a
JH
907 BEGIN {
908 unshift @INC, 'lib';
909 }
e425a60b 910 use Cname;
0f289c68 911
e425a60b 912 ok 'fooB' =~ /\N{foo}[\N{B}\N{b}]/, "Passthrough charname";
94ca1619
KW
913 my $name = "foo\xDF";
914 my $result = eval "'A${name}B' =~ /^A\\N{$name}B\$/";
915 ok !$@ && $result, "Passthrough charname of non-ASCII, Latin1";
e425a60b
YO
916 #
917 # Why doesn't must_warn work here?
918 #
919 my $w;
920 local $SIG {__WARN__} = sub {$w .= "@_"};
a78e2a97 921 $result = eval 'q(WARN) =~ /^[\N{WARN}]$/';
8f0cd35a 922 ok !$@ && $result && ! $w, '\N{} returning multi-char works';
e425a60b
YO
923
924 undef $w;
925 eval q [ok "\0" !~ /[\N{EMPTY-STR}XY]/,
5895685f 926 "Zerolength charname in charclass doesn't match \\\\0"];
ff3f963a
KW
927 ok $w && $w =~ /Ignoring zero length/,
928 'Ignoring zero length \N{} in character class warning';
67048c1b
KW
929 undef $w;
930 eval q [ok 'xy' =~ /x[\N{EMPTY-STR} y]/x,
931 'Empty string charname in [] is ignored; finds a following character'];
932 ok $w && $w =~ /Ignoring zero length/,
933 'Ignoring zero length \N{} in character class warning';
934 undef $w;
935 eval q [ok 'x ' =~ /x[\N{EMPTY-STR} y]/,
936 'Empty string charname in [] is ignored; finds a following blank under /x'];
937 ok $w && $w =~ /Ignoring zero length/,
938 'Ignoring zero length \N{} in character class warning';
e425a60b
YO
939
940 ok 'AB' =~ /(\N{EVIL})/ && $1 eq 'A', 'Charname caching $1';
941 ok 'ABC' =~ /(\N{EVIL})/, 'Charname caching $1';
942 ok 'xy' =~ /x\N{EMPTY-STR}y/,
943 'Empty string charname produces NOTHING node';
944 ok '' =~ /\N{EMPTY-STR}/,
945 'Empty string charname produces NOTHING node';
ff3f963a
KW
946 ok "\N{LONG-STR}" =~ /^\N{LONG-STR}$/, 'Verify that long string works';
947 ok "\N{LONG-STR}" =~ /^\N{LONG-STR}$/i, 'Verify under folding that long string works';
948
902994e4
KW
949 eval '/(?[[\N{EMPTY-STR}]])/';
950 ok $@ && $@ =~ /Zero length \\N\{}/;
951
bd299e29 952 undef $w;
bd299e29 953 {
3036c853 954 () = eval q ["\N{TOO MANY SPACES}"];
2d8eb851 955 like ($@, qr/charnames alias definitions may not contain a sequence of multiple spaces/, "Multiple spaces in a row in a charnames alias is fatal");
3036c853 956 eval q [use utf8; () = "\N{TOO MANY SPACES}"];
2d8eb851 957 like ($@, qr/charnames alias definitions may not contain a sequence of multiple spaces/, "... same under utf8");
94ec3a20 958 }
bd299e29
KW
959
960 undef $w;
bd299e29 961 {
94ec3a20 962 () = eval q ["\N{TRAILING SPACE }"];
2d8eb851 963 like ($@, qr/charnames alias definitions may not contain trailing white-space/, "Trailing white-space in a charnames alias is fatal");
94ec3a20 964 eval q [use utf8; () = "\N{TRAILING SPACE }"];
2d8eb851 965 like ($@, qr/charnames alias definitions may not contain trailing white-space/, "... same under utf8");
94ec3a20 966 }
bd299e29 967
2d8eb851 968 undef $w;
7fc82458 969 my $Cedilla_Latin1 = "GAR"
d1cef54a 970 . uni_to_native("\xC7")
7fc82458
KW
971 . "ON";
972 my $Cedilla_utf8 = $Cedilla_Latin1;
973 utf8::upgrade($Cedilla_utf8);
974 eval qq[is("\\N{$Cedilla_Latin1}", "$Cedilla_Latin1", "A cedilla in character name works")];
975 undef $w;
976 {
977 use feature 'unicode_eval';
978 eval qq[use utf8; is("\\N{$Cedilla_utf8}", "$Cedilla_utf8", "... same under 'use utf8': they work")];
979 }
980
981 undef $w;
df758df2 982 my $NBSP_Latin1 = "NBSP"
d1cef54a 983 . uni_to_native("\xA0")
df758df2 984 . "SEPARATED"
d1cef54a 985 . uni_to_native("\xA0")
df758df2
KW
986 . "SPACE";
987 my $NBSP_utf8 = $NBSP_Latin1;
988 utf8::upgrade($NBSP_utf8);
989 eval qq[is("\\N{$NBSP_Latin1}", "$NBSP_Latin1", "An NBSP in character name works")];
990 like ($w, qr/NO-BREAK SPACE in a charnames alias definition is deprecated/, "... but returns a deprecation warning");
991 undef $w;
992 {
993 use feature 'unicode_eval';
994 eval qq[use utf8; is("\\N{$NBSP_utf8}", "$NBSP_utf8", "Same under 'use utf8': they work")];
995 like ($w, qr/NO-BREAK SPACE in a charnames alias definition is deprecated/, "... but return a deprecation warning");
996 }
997 {
998 # disable lexical warnings
999 BEGIN { ${^WARNING_BITS} = undef; $^W = 0 }
1000 undef $w;
1001 () = eval qq["\\N{$NBSP_Latin1}"];
1002 like ($w, qr/NO-BREAK SPACE in a charnames alias definition is deprecated/, "And returns a deprecation warning outside of lexical warnings");
1003 undef $w;
1004 use feature 'unicode_eval';
1005 eval qq[use utf8; () = "\\N{$NBSP_utf8}"];
1006 like ($w, qr/NO-BREAK SPACE in a charnames alias definition is deprecated/, "... same under utf8");
1007 }
1008 {
1009 no warnings 'deprecated';
1010 undef $w;
1011 eval qq["\\N{$NBSP_Latin1}"];
1012 ok (! defined $w, "... and no warning if warnings are off");
1013 use feature 'unicode_eval';
1014 eval qq[use utf8; "\\N{$NBSP_utf8}"];
1015 ok (! defined $w, "... same under 'use utf8'");
1016 }
1017 {
1018 use warnings FATAL=>'deprecated';
1019 () = eval qq["\\N{$NBSP_Latin1}"];
1020 like ($@, qr/NO-BREAK SPACE in a charnames alias definition is deprecated/, "... the warning can be fatal");
1021 use feature 'unicode_eval';
1022 eval qq[use utf8; () = "\\N{$NBSP_utf8}"];
1023 like ($@, qr/NO-BREAK SPACE in a charnames alias definition is deprecated/, "... same under utf8");
1024 }
1025
6a642c21
FC
1026 {
1027 BEGIN { no strict; *CnameTest:: = *{"_charnames\0A::" } }
1028 package CnameTest { sub translator { pop } }
1029 BEGIN { $^H{charnames} = \&CnameTest::translator }
1030 undef $w;
1031 () = eval q ["\N{TOO MANY SPACES}"];
2d8eb851 1032 like ($@, qr/charnames alias definitions may not contain a sequence of multiple spaces/,
6a642c21
FC
1033 'translators in _charnames\0* packages get validated');
1034 }
1035
ff3f963a
KW
1036 # If remove the limitation in regcomp code these should work
1037 # differently
1038 undef $w;
e2a7e165 1039 eval q [ok "\N{TOO-LONG-STR}" =~ /^\N{TOO-LONG-STR}$/, 'Verify that what once was too long a string works'];
cb233ae3 1040 eval 'q() =~ /\N{4F}/';
4d7cd482 1041 ok $@ && $@ =~ /Invalid character/, 'Verify that leading digit in name gives error';
cb233ae3 1042 eval 'q() =~ /\N{COM,MA}/';
4d7cd482 1043 ok $@ && $@ =~ /Invalid character/, 'Verify that comma in name gives error';
f36cc39d 1044 $name = "A" . uni_to_native("\x{D7}") . "O";
cb233ae3 1045 eval "q(W) =~ /\\N{$name}/";
4d7cd482 1046 ok $@ && $@ =~ /Invalid character/, 'Verify that latin1 symbol in name gives error';
bde9e88d
KW
1047 my $utf8_name = "7 CITIES OF GOLD";
1048 utf8::upgrade($utf8_name);
1049 eval "use utf8; q(W) =~ /\\N{$utf8_name}/";
1050 ok $@ && $@ =~ /Invalid character/, 'Verify that leading digit in utf8 name gives error';
1051 $utf8_name = "SHARP #";
1052 utf8::upgrade($utf8_name);
1053 eval "use utf8; q(W) =~ /\\N{$utf8_name}/";
1054 ok $@ && $@ =~ /Invalid character/, 'Verify that ASCII symbol in utf8 name gives error';
f36cc39d 1055 $utf8_name = "A HOUSE " . uni_to_native("\xF7") . " AGAINST ITSELF";
bde9e88d
KW
1056 utf8::upgrade($utf8_name);
1057 eval "use utf8; q(W) =~ /\\N{$utf8_name}/";
1058 ok $@ && $@ =~ /Invalid character/, 'Verify that latin1 symbol in utf8 name gives error';
1059 $utf8_name = "\x{664} HORSEMEN}";
1060 eval "use utf8; q(W) =~ /\\N{$utf8_name}/";
1061 ok $@ && $@ =~ /Invalid character/, 'Verify that leading above Latin1 digit in utf8 name gives error';
1062 $utf8_name = "A \x{1F4A9} WOULD SMELL AS SWEET}";
1063 eval "use utf8; q(W) =~ /\\N{$utf8_name}/";
1064 ok $@ && $@ =~ /Invalid character/, 'Verify that above Latin1 symbol in utf8 name gives error';
1065
cb233ae3 1066 undef $w;
f36cc39d 1067 $name = "A" . uni_to_native("\x{D1}") . "O";
cb233ae3
KW
1068 eval "q(W) =~ /\\N{$name}/";
1069 ok ! $w, 'Verify that latin1 letter in name doesnt give warning';
0f289c68 1070
ba7b73c5
NC
1071 # This tests the code path that restarts the parse when the recursive
1072 # call to S_reg() from within S_grok_bslash_N() discovers that the
1073 # pattern needs to be recalculated as UTF-8. use eval to avoid
1074 # needing literal Unicode in this source file:
1075 my $r = eval "qr/\\N{\x{100}\x{100}}/";
1076 isnt $r, undef, "Generated regex for multi-char UTF-8 charname"
1077 or diag($@);
1078 ok "\x{100}\x{100}" =~ $r, "which matches";
e425a60b
YO
1079 }
1080
e425a60b
YO
1081 {
1082 use charnames ':full';
1083
1084 ok 'aabc' !~ /a\N{PLUS SIGN}b/, '/a\N{PLUS SIGN}b/ against aabc';
1085 ok 'a+bc' =~ /a\N{PLUS SIGN}b/, '/a\N{PLUS SIGN}b/ against a+bc';
1086
1087 ok ' A B' =~ /\N{SPACE}\N{U+0041}\N{SPACE}\N{U+0042}/,
1088 'Intermixed named and unicode escapes';
1089 ok "\N{SPACE}\N{U+0041}\N{SPACE}\N{U+0042}" =~
1090 /\N{SPACE}\N{U+0041}\N{SPACE}\N{U+0042}/,
1091 'Intermixed named and unicode escapes';
1092 ok "\N{SPACE}\N{U+0041}\N{SPACE}\N{U+0042}" =~
1093 /[\N{SPACE}\N{U+0041}][\N{SPACE}\N{U+0042}]/,
0f289c68 1094 'Intermixed named and unicode escapes';
ff3f963a 1095 ok "\0" =~ /^\N{NULL}$/, 'Verify that \N{NULL} works; is not confused with an error';
e425a60b
YO
1096 }
1097
e425a60b
YO
1098 {
1099 our $brackets;
1100 $brackets = qr{
1101 { (?> [^{}]+ | (??{ $brackets }) )* }
1102 }x;
1103
1104 ok "{b{c}d" !~ m/^((??{ $brackets }))/, "Bracket mismatch";
1105
1106 SKIP: {
1107 our @stack = ();
1108 my @expect = qw(
1109 stuff1
1110 stuff2
1111 <stuff1>and<stuff2>
1112 right
1113 <right>
1114 <<right>>
1115 <<<right>>>
1116 <<stuff1>and<stuff2>><<<<right>>>>
1117 );
1118
1119 local $_ = '<<<stuff1>and<stuff2>><<<<right>>>>>';
1120 ok /^(<((?:(?>[^<>]+)|(?1))*)>(?{push @stack, $2 }))$/,
1121 "Recursion matches";
de26e0cc 1122 is(@stack, @expect, "Right amount of matches")
e425a60b
YO
1123 or skip "Won't test individual results as count isn't equal",
1124 0 + @expect;
1125 my $idx = 0;
1126 foreach my $expect (@expect) {
de26e0cc
NC
1127 is($stack [$idx], $expect,
1128 "Expecting '$expect' at stack pos #$idx");
e425a60b
YO
1129 $idx ++;
1130 }
1131 }
1132 }
1133
e425a60b
YO
1134 {
1135 my $s = '123453456';
1136 $s =~ s/(?<digits>\d+)\k<digits>/$+{digits}/;
1137 ok $s eq '123456', 'Named capture (angle brackets) s///';
1138 $s = '123453456';
1139 $s =~ s/(?'digits'\d+)\k'digits'/$+{digits}/;
0f289c68 1140 ok $s eq '123456', 'Named capture (single quotes) s///';
e425a60b
YO
1141 }
1142
e425a60b
YO
1143 {
1144 my @ary = (
f36cc39d
KW
1145 pack('U', 0x00F1), # n-tilde
1146 '_'.pack('U', 0x00F1), # _ + n-tilde
e425a60b 1147 'c'.pack('U', 0x0327), # c + cedilla
f36cc39d 1148 pack('U*', 0x00F1, 0x0327),# n-tilde + cedilla
e425a60b
YO
1149 pack('U', 0x0391), # ALPHA
1150 pack('U', 0x0391).'2', # ALPHA + 2
1151 pack('U', 0x0391).'_', # ALPHA + _
1152 );
1153
1154 for my $uni (@ary) {
1155 my ($r1, $c1, $r2, $c2) = eval qq {
1156 use utf8;
1157 scalar ("..foo foo.." =~ /(?'${uni}'foo) \\k'${uni}'/),
1158 \$+{${uni}},
1159 scalar ("..bar bar.." =~ /(?<${uni}>bar) \\k<${uni}>/),
1160 \$+{${uni}};
1161 };
1162 ok $r1, "Named capture UTF (?'')";
1163 ok defined $c1 && $c1 eq 'foo', "Named capture UTF \%+";
1164 ok $r2, "Named capture UTF (?<>)";
1165 ok defined $c2 && $c2 eq 'bar', "Named capture UTF \%+";
1166 }
1167 }
1168
e425a60b
YO
1169 {
1170 my $s = 'foo bar baz';
1171 my @res;
1172 if ('1234' =~ /(?<A>1)(?<B>2)(?<A>3)(?<B>4)/) {
1173 foreach my $name (sort keys(%-)) {
1174 my $ary = $- {$name};
1175 foreach my $idx (0 .. $#$ary) {
1176 push @res, "$name:$idx:$ary->[$idx]";
1177 }
1178 }
1179 }
1180 my @expect = qw (A:0:1 A:1:3 B:0:2 B:1:4);
de26e0cc 1181 is("@res", "@expect", "Check %-");
e425a60b
YO
1182 eval'
1183 no warnings "uninitialized";
1184 print for $- {this_key_doesnt_exist};
1185 ';
1186 ok !$@,'lvalue $- {...} should not throw an exception';
1187 }
1188
e425a60b
YO
1189 {
1190 # \, breaks {3,4}
412f55bb 1191 no warnings qw{deprecated regexp};
e425a60b
YO
1192 ok "xaaay" !~ /xa{3\,4}y/, '\, in a pattern';
1193 ok "xa{3,4}y" =~ /xa{3\,4}y/, '\, in a pattern';
1194
1195 # \c\ followed by _
1196 ok "x\c_y" !~ /x\c\_y/, '\_ in a pattern';
1197 ok "x\c\_y" =~ /x\c\_y/, '\_ in a pattern';
1198
1199 # \c\ followed by other characters
1200 for my $c ("z", "\0", "!", chr(254), chr(256)) {
f36cc39d 1201 my $targ = "a" . uni_to_native("\034") . "$c";
e425a60b
YO
1202 my $reg = "a\\c\\$c";
1203 ok eval ("qq/$targ/ =~ /$reg/"), "\\c\\ in pattern";
1204 }
1205 }
1206
e425a60b
YO
1207 { # Test the (*PRUNE) pattern
1208 our $count = 0;
1209 'aaab' =~ /a+b?(?{$count++})(*FAIL)/;
de26e0cc 1210 is($count, 9, "Expect 9 for no (*PRUNE)");
e425a60b
YO
1211 $count = 0;
1212 'aaab' =~ /a+b?(*PRUNE)(?{$count++})(*FAIL)/;
de26e0cc 1213 is($count, 3, "Expect 3 with (*PRUNE)");
e425a60b
YO
1214 local $_ = 'aaab';
1215 $count = 0;
1216 1 while /.(*PRUNE)(?{$count++})(*FAIL)/g;
de26e0cc 1217 is($count, 4, "/.(*PRUNE)/");
e425a60b
YO
1218 $count = 0;
1219 'aaab' =~ /a+b?(??{'(*PRUNE)'})(?{$count++})(*FAIL)/;
de26e0cc 1220 is($count, 3, "Expect 3 with (*PRUNE)");
e425a60b
YO
1221 local $_ = 'aaab';
1222 $count = 0;
1223 1 while /.(??{'(*PRUNE)'})(?{$count++})(*FAIL)/g;
de26e0cc 1224 is($count, 4, "/.(*PRUNE)/");
e425a60b
YO
1225 }
1226
e425a60b
YO
1227 { # Test the (*SKIP) pattern
1228 our $count = 0;
1229 'aaab' =~ /a+b?(*SKIP)(?{$count++})(*FAIL)/;
de26e0cc 1230 is($count, 1, "Expect 1 with (*SKIP)");
e425a60b
YO
1231 local $_ = 'aaab';
1232 $count = 0;
1233 1 while /.(*SKIP)(?{$count++})(*FAIL)/g;
de26e0cc 1234 is($count, 4, "/.(*SKIP)/");
e425a60b
YO
1235 $_ = 'aaabaaab';
1236 $count = 0;
1237 our @res = ();
1238 1 while /(a+b?)(*SKIP)(?{$count++; push @res,$1})(*FAIL)/g;
de26e0cc
NC
1239 is($count, 2, "Expect 2 with (*SKIP)");
1240 is("@res", "aaab aaab", "Adjacent (*SKIP) works as expected");
e425a60b
YO
1241 }
1242
e425a60b
YO
1243 { # Test the (*SKIP) pattern
1244 our $count = 0;
1245 'aaab' =~ /a+b?(*MARK:foo)(*SKIP)(?{$count++})(*FAIL)/;
de26e0cc 1246 is($count, 1, "Expect 1 with (*SKIP)");
e425a60b
YO
1247 local $_ = 'aaab';
1248 $count = 0;
1249 1 while /.(*MARK:foo)(*SKIP)(?{$count++})(*FAIL)/g;
de26e0cc 1250 is($count, 4, "/.(*SKIP)/");
e425a60b
YO
1251 $_ = 'aaabaaab';
1252 $count = 0;
1253 our @res = ();
1254 1 while /(a+b?)(*MARK:foo)(*SKIP)(?{$count++; push @res,$1})(*FAIL)/g;
de26e0cc
NC
1255 is($count, 2, "Expect 2 with (*SKIP)");
1256 is("@res", "aaab aaab", "Adjacent (*SKIP) works as expected");
e425a60b
YO
1257 }
1258
e425a60b
YO
1259 { # Test the (*SKIP) pattern
1260 our $count = 0;
1261 'aaab' =~ /a*(*MARK:a)b?(*MARK:b)(*SKIP:a)(?{$count++})(*FAIL)/;
de26e0cc 1262 is($count, 3, "Expect 3 with *MARK:a)b?(*MARK:b)(*SKIP:a)");
e425a60b
YO
1263 local $_ = 'aaabaaab';
1264 $count = 0;
1265 our @res = ();
1266 1 while
1267 /(a*(*MARK:a)b?)(*MARK:x)(*SKIP:a)(?{$count++; push @res,$1})(*FAIL)/g;
de26e0cc
NC
1268 is($count, 5, "Expect 5 with (*MARK:a)b?)(*MARK:x)(*SKIP:a)");
1269 is("@res", "aaab b aaab b ",
1270 "Adjacent (*MARK:a)b?)(*MARK:x)(*SKIP:a) works as expected");
e425a60b
YO
1271 }
1272
e425a60b
YO
1273 { # Test the (*COMMIT) pattern
1274 our $count = 0;
1275 'aaabaaab' =~ /a+b?(*COMMIT)(?{$count++})(*FAIL)/;
de26e0cc 1276 is($count, 1, "Expect 1 with (*COMMIT)");
e425a60b
YO
1277 local $_ = 'aaab';
1278 $count = 0;
1279 1 while /.(*COMMIT)(?{$count++})(*FAIL)/g;
de26e0cc 1280 is($count, 1, "/.(*COMMIT)/");
e425a60b
YO
1281 $_ = 'aaabaaab';
1282 $count = 0;
1283 our @res = ();
1284 1 while /(a+b?)(*COMMIT)(?{$count++; push @res,$1})(*FAIL)/g;
de26e0cc
NC
1285 is($count, 1, "Expect 1 with (*COMMIT)");
1286 is("@res", "aaab", "Adjacent (*COMMIT) works as expected");
3542935d
DM
1287
1288 ok("1\n2a\n" !~ /^\d+(*COMMIT)\w+/m, "COMMIT and anchors");
e425a60b
YO
1289 }
1290
e425a60b
YO
1291 {
1292 # Test named commits and the $REGERROR var
fee50582 1293 local $REGERROR;
e425a60b
YO
1294 for my $name ('', ':foo') {
1295 for my $pat ("(*PRUNE$name)",
1296 ($name ? "(*MARK$name)" : "") . "(*SKIP$name)",
0f289c68 1297 "(*COMMIT$name)") {
e425a60b
YO
1298 for my $suffix ('(*FAIL)', '') {
1299 'aaaab' =~ /a+b$pat$suffix/;
de26e0cc 1300 is($REGERROR,
e425a60b 1301 ($suffix ? ($name ? 'foo' : "1") : ""),
de26e0cc 1302 "Test $pat and \$REGERROR $suffix");
e425a60b
YO
1303 }
1304 }
1305 }
1306 }
1307
e425a60b
YO
1308 {
1309 # Test named commits and the $REGERROR var
1310 package Fnorble;
1311 our $REGERROR;
fee50582 1312 local $REGERROR;
e425a60b
YO
1313 for my $name ('', ':foo') {
1314 for my $pat ("(*PRUNE$name)",
1315 ($name ? "(*MARK$name)" : "") . "(*SKIP$name)",
0f289c68 1316 "(*COMMIT$name)") {
e425a60b
YO
1317 for my $suffix ('(*FAIL)','') {
1318 'aaaab' =~ /a+b$pat$suffix/;
de26e0cc 1319 ::is($REGERROR,
e425a60b 1320 ($suffix ? ($name ? 'foo' : "1") : ""),
de26e0cc 1321 "Test $pat and \$REGERROR $suffix");
e425a60b
YO
1322 }
1323 }
0f289c68
YO
1324 }
1325 }
e425a60b 1326
e425a60b
YO
1327 {
1328 # Test named commits and the $REGERROR var
de946258 1329 my $message = '$REGERROR';
fee50582 1330 local $REGERROR;
e425a60b
YO
1331 for my $word (qw (bar baz bop)) {
1332 $REGERROR = "";
1333 "aaaaa$word" =~
1334 /a+(?:bar(*COMMIT:bar)|baz(*COMMIT:baz)|bop(*COMMIT:bop))(*FAIL)/;
de946258 1335 is($REGERROR, $word, $message);
0f289c68 1336 }
e425a60b
YO
1337 }
1338
e425a60b
YO
1339 {
1340 #Mindnumbingly simple test of (*THEN)
1341 for ("ABC","BAX") {
1342 ok /A (*THEN) X | B (*THEN) C/x, "Simple (*THEN) test";
1343 }
1344 }
1345
e425a60b 1346 {
de946258 1347 my $message = "Relative Recursion";
e425a60b
YO
1348 my $parens = qr/(\((?:[^()]++|(?-1))*+\))/;
1349 local $_ = 'foo((2*3)+4-3) + bar(2*(3+4)-1*(2-3))';
1350 my ($all, $one, $two) = ('', '', '');
de946258
NC
1351 ok(m/foo $parens \s* \+ \s* bar $parens/x, $message);
1352 is($1, '((2*3)+4-3)', $message);
1353 is($2, '(2*(3+4)-1*(2-3))', $message);
1354 is($&, 'foo((2*3)+4-3) + bar(2*(3+4)-1*(2-3))', $message);
1355 is($&, $_, $message);
e425a60b
YO
1356 }
1357
1358 {
1359 my $spaces=" ";
1360 local $_ = join 'bar', $spaces, $spaces;
1361 our $count = 0;
1362 s/(?>\s+bar)(?{$count++})//g;
de26e0cc
NC
1363 is($_, $spaces, "SUSPEND final string");
1364 is($count, 1, "Optimiser should have prevented more than one match");
e425a60b
YO
1365 }
1366
e425a60b
YO
1367 {
1368 # From Message-ID: <877ixs6oa6.fsf@k75.linux.bogus>
1369 my $dow_name = "nada";
1370 my $parser = "(\$dow_name) = \$time_string =~ /(D\x{e9}\\ " .
1371 "C\x{e9}adaoin|D\x{e9}\\ Sathairn|\\w+|\x{100})/";
1372 my $time_string = "D\x{e9} C\x{e9}adaoin";
1373 eval $parser;
1374 ok !$@, "Test Eval worked";
de26e0cc 1375 is($dow_name, $time_string, "UTF-8 trie common prefix extraction");
e425a60b
YO
1376 }
1377
e425a60b
YO
1378 {
1379 my $v;
1380 ($v = 'bar') =~ /(\w+)/g;
1381 $v = 'foo';
de26e0cc
NC
1382 is("$1", 'bar',
1383 '$1 is safe after /g - may fail due to specialized config in pp_hot.c');
e425a60b
YO
1384 }
1385
e425a60b 1386 {
de946258 1387 my $message = "http://nntp.perl.org/group/perl.perl5.porters/118663";
e425a60b 1388 my $qr_barR1 = qr/(bar)\g-1/;
de946258
NC
1389 like("foobarbarxyz", $qr_barR1, $message);
1390 like("foobarbarxyz", qr/foo${qr_barR1}xyz/, $message);
1391 like("foobarbarxyz", qr/(foo)${qr_barR1}xyz/, $message);
1392 like("foobarbarxyz", qr/(foo)(bar)\g{-1}xyz/, $message);
1393 like("foobarbarxyz", qr/(foo${qr_barR1})xyz/, $message);
1394 like("foobarbarxyz", qr/(foo(bar)\g{-1})xyz/, $message);
0f289c68 1395 }
e425a60b 1396
e425a60b 1397 {
de946258 1398 my $message = '$REGMARK';
e425a60b 1399 our @r = ();
fee50582
YO
1400 local $REGMARK;
1401 local $REGERROR;
de946258
NC
1402 like('foofoo', qr/foo (*MARK:foo) (?{push @r,$REGMARK}) /x, $message);
1403 is("@r","foo", $message);
1404 is($REGMARK, "foo", $message);
1405 unlike('foofoo', qr/foo (*MARK:foo) (*FAIL) /x, $message);
1406 is($REGMARK, '', $message);
1407 is($REGERROR, 'foo', $message);
e425a60b
YO
1408 }
1409
e425a60b 1410 {
de946258 1411 my $message = '\K test';
e425a60b
YO
1412 my $x;
1413 $x = "abc.def.ghi.jkl";
1414 $x =~ s/.*\K\..*//;
de946258 1415 is($x, "abc.def.ghi", $message);
0f289c68 1416
e425a60b
YO
1417 $x = "one two three four";
1418 $x =~ s/o+ \Kthree//g;
de946258 1419 is($x, "one two four", $message);
0f289c68 1420
e425a60b
YO
1421 $x = "abcde";
1422 $x =~ s/(.)\K/$1/g;
de946258 1423 is($x, "aabbccddee", $message);
e425a60b
YO
1424 }
1425
e425a60b
YO
1426 {
1427 sub kt {
1428 return '4' if $_[0] eq '09028623';
1429 }
1430 # Nested EVAL using PL_curpm (via $1 or friends)
1431 my $re;
1432 our $grabit = qr/ ([0-6][0-9]{7}) (??{ kt $1 }) [890] /x;
1433 $re = qr/^ ( (??{ $grabit }) ) $ /x;
1434 my @res = '0902862349' =~ $re;
de26e0cc
NC
1435 is(join ("-", @res), "0902862349",
1436 'PL_curpm is set properly on nested eval');
e425a60b
YO
1437
1438 our $qr = qr/ (o) (??{ $1 }) /x;
1439 ok 'boob'=~/( b (??{ $qr }) b )/x && 1, "PL_curpm, nested eval";
1440 }
1441
e425a60b
YO
1442 {
1443 use charnames ":full";
1444 ok "\N{ROMAN NUMERAL ONE}" =~ /\p{Alphabetic}/, "I =~ Alphabetic";
1445 ok "\N{ROMAN NUMERAL ONE}" =~ /\p{Uppercase}/, "I =~ Uppercase";
1446 ok "\N{ROMAN NUMERAL ONE}" !~ /\p{Lowercase}/, "I !~ Lowercase";
1447 ok "\N{ROMAN NUMERAL ONE}" =~ /\p{IDStart}/, "I =~ ID_Start";
1448 ok "\N{ROMAN NUMERAL ONE}" =~ /\p{IDContinue}/, "I =~ ID_Continue";
1449 ok "\N{SMALL ROMAN NUMERAL ONE}" =~ /\p{Alphabetic}/, "i =~ Alphabetic";
1450 ok "\N{SMALL ROMAN NUMERAL ONE}" !~ /\p{Uppercase}/, "i !~ Uppercase";
0ab1ff8e
KW
1451 ok "\N{SMALL ROMAN NUMERAL ONE}" =~ /\p{Uppercase}/i, "i =~ Uppercase under /i";
1452 ok "\N{SMALL ROMAN NUMERAL ONE}" !~ /\p{Titlecase}/, "i !~ Titlecase";
1453 ok "\N{SMALL ROMAN NUMERAL ONE}" =~ /\p{Titlecase}/i, "i =~ Titlecase under /i";
5895685f
NC
1454 ok "\N{ROMAN NUMERAL ONE}" =~ /\p{Lowercase}/i, "I =~ Lowercase under /i";
1455
e425a60b
YO
1456 ok "\N{SMALL ROMAN NUMERAL ONE}" =~ /\p{Lowercase}/, "i =~ Lowercase";
1457 ok "\N{SMALL ROMAN NUMERAL ONE}" =~ /\p{IDStart}/, "i =~ ID_Start";
1458 ok "\N{SMALL ROMAN NUMERAL ONE}" =~ /\p{IDContinue}/, "i =~ ID_Continue"
1459 }
1460
0ab1ff8e
KW
1461 { # More checking that /i works on the few properties that it makes a
1462 # difference. Uppercase, Lowercase, and Titlecase were done in the
1463 # block above
1464 ok "A" =~ /\p{PosixUpper}/, "A =~ PosixUpper";
1465 ok "A" =~ /\p{PosixUpper}/i, "A =~ PosixUpper under /i";
1466 ok "A" !~ /\p{PosixLower}/, "A !~ PosixLower";
1467 ok "A" =~ /\p{PosixLower}/i, "A =~ PosixLower under /i";
1468 ok "a" !~ /\p{PosixUpper}/, "a !~ PosixUpper";
1469 ok "a" =~ /\p{PosixUpper}/i, "a =~ PosixUpper under /i";
1470 ok "a" =~ /\p{PosixLower}/, "a =~ PosixLower";
1471 ok "a" =~ /\p{PosixLower}/i, "a =~ PosixLower under /i";
1472
f36cc39d
KW
1473 ok uni_to_native("\xC0") =~ /\p{XPosixUpper}/, "\\xC0 =~ XPosixUpper";
1474 ok uni_to_native("\xC0") =~ /\p{XPosixUpper}/i, "\\xC0 =~ XPosixUpper under /i";
1475 ok uni_to_native("\xC0") !~ /\p{XPosixLower}/, "\\xC0 !~ XPosixLower";
1476 ok uni_to_native("\xC0") =~ /\p{XPosixLower}/i, "\\xC0 =~ XPosixLower under /i";
1477 ok uni_to_native("\xE0") !~ /\p{XPosixUpper}/, "\\xE0 !~ XPosixUpper";
1478 ok uni_to_native("\xE0") =~ /\p{XPosixUpper}/i, "\\xE0 =~ XPosixUpper under /i";
1479 ok uni_to_native("\xE0") =~ /\p{XPosixLower}/, "\\xE0 =~ XPosixLower";
1480 ok uni_to_native("\xE0") =~ /\p{XPosixLower}/i, "\\xE0 =~ XPosixLower under /i";
1481
1482 ok uni_to_native("\xC0") =~ /\p{UppercaseLetter}/, "\\xC0 =~ UppercaseLetter";
1483 ok uni_to_native("\xC0") =~ /\p{UppercaseLetter}/i, "\\xC0 =~ UppercaseLetter under /i";
1484 ok uni_to_native("\xC0") !~ /\p{LowercaseLetter}/, "\\xC0 !~ LowercaseLetter";
1485 ok uni_to_native("\xC0") =~ /\p{LowercaseLetter}/i, "\\xC0 =~ LowercaseLetter under /i";
1486 ok uni_to_native("\xC0") !~ /\p{TitlecaseLetter}/, "\\xC0 !~ TitlecaseLetter";
1487 ok uni_to_native("\xC0") =~ /\p{TitlecaseLetter}/i, "\\xC0 =~ TitlecaseLetter under /i";
1488 ok uni_to_native("\xE0") !~ /\p{UppercaseLetter}/, "\\xE0 !~ UppercaseLetter";
1489 ok uni_to_native("\xE0") =~ /\p{UppercaseLetter}/i, "\\xE0 =~ UppercaseLetter under /i";
1490 ok uni_to_native("\xE0") =~ /\p{LowercaseLetter}/, "\\xE0 =~ LowercaseLetter";
1491 ok uni_to_native("\xE0") =~ /\p{LowercaseLetter}/i, "\\xE0 =~ LowercaseLetter under /i";
1492 ok uni_to_native("\xE0") !~ /\p{TitlecaseLetter}/, "\\xE0 !~ TitlecaseLetter";
1493 ok uni_to_native("\xE0") =~ /\p{TitlecaseLetter}/i, "\\xE0 =~ TitlecaseLetter under /i";
0ab1ff8e
KW
1494 ok "\x{1C5}" !~ /\p{UppercaseLetter}/, "\\x{1C5} !~ UppercaseLetter";
1495 ok "\x{1C5}" =~ /\p{UppercaseLetter}/i, "\\x{1C5} =~ UppercaseLetter under /i";
1496 ok "\x{1C5}" !~ /\p{LowercaseLetter}/, "\\x{1C5} !~ LowercaseLetter";
1497 ok "\x{1C5}" =~ /\p{LowercaseLetter}/i, "\\x{1C5} =~ LowercaseLetter under /i";
1498 ok "\x{1C5}" =~ /\p{TitlecaseLetter}/, "\\x{1C5} =~ TitlecaseLetter";
1499 ok "\x{1C5}" =~ /\p{TitlecaseLetter}/i, "\\x{1C5} =~ TitlecaseLetter under /i";
1500 }
1501
e425a60b
YO
1502 {
1503 # requirement of Unicode Technical Standard #18, 1.7 Code Points
1504 # cf. http://www.unicode.org/reports/tr18/#Supplementary_Characters
1505 for my $u (0x7FF, 0x800, 0xFFFF, 0x10000) {
1506 no warnings 'utf8'; # oops
1507 my $c = chr $u;
1508 my $x = sprintf '%04X', $u;
1509 ok "A${c}B" =~ /A[\0-\x{10000}]B/, "Unicode range - $x";
1510 }
1511 }
1512
e425a60b
YO
1513 {
1514 my $res="";
1515
1516 if ('1' =~ /(?|(?<digit>1)|(?<digit>2))/) {
1517 $res = "@{$- {digit}}";
1518 }
de26e0cc
NC
1519 is($res, "1",
1520 "Check that (?|...) doesnt cause dupe entries in the names array");
0f289c68 1521
e425a60b
YO
1522 $res = "";
1523 if ('11' =~ /(?|(?<digit>1)|(?<digit>2))(?&digit)/) {
1524 $res = "@{$- {digit}}";
1525 }
de26e0cc
NC
1526 is($res, "1",
1527 "Check that (?&..) to a buffer inside a (?|...) goes to the leftmost");
e425a60b
YO
1528 }
1529
e425a60b
YO
1530 {
1531 use warnings;
de946258 1532 my $message = "ASCII pattern that really is UTF-8";
e425a60b
YO
1533 my @w;
1534 local $SIG {__WARN__} = sub {push @w, "@_"};
0f289c68 1535 my $c = qq (\x{DF});
de946258
NC
1536 like($c, qr/${c}|\x{100}/, $message);
1537 is("@w", '', $message);
0f289c68 1538 }
e425a60b 1539
e425a60b 1540 {
de946258 1541 my $message = "Corruption of match results of qr// across scopes";
e425a60b
YO
1542 my $qr = qr/(fo+)(ba+r)/;
1543 'foobar' =~ /$qr/;
de946258 1544 is("$1$2", "foobar", $message);
e425a60b
YO
1545 {
1546 'foooooobaaaaar' =~ /$qr/;
de946258 1547 is("$1$2", 'foooooobaaaaar', $message);
e425a60b 1548 }
de946258 1549 is("$1$2", "foobar", $message);
e425a60b
YO
1550 }
1551
e425a60b 1552 {
de946258 1553 my $message = "HORIZWS";
e425a60b
YO
1554 local $_ = "\t \r\n \n \t".chr(11)."\n";
1555 s/\H/H/g;
1556 s/\h/h/g;
de946258 1557 is($_, "hhHHhHhhHH", $message);
e425a60b
YO
1558 $_ = "\t \r\n \n \t" . chr (11) . "\n";
1559 utf8::upgrade ($_);
1560 s/\H/H/g;
1561 s/\h/h/g;
de946258 1562 is($_, "hhHHhHhhHH", $message);
0f289c68 1563 }
e425a60b 1564
e425a60b 1565 {
de946258 1566 # Various whitespace special patterns
f36cc39d
KW
1567 my @h = map {chr utf8::unicode_to_native($_) }
1568 0x09, 0x20, 0xa0, 0x1680, 0x2000,
e425a60b
YO
1569 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006,
1570 0x2007, 0x2008, 0x2009, 0x200a, 0x202f, 0x205f,
1571 0x3000;
f36cc39d
KW
1572 my @v = map {chr utf8::unicode_to_native($_) }
1573 0x0a, 0x0b, 0x0c, 0x0d, 0x85, 0x2028,
e425a60b 1574 0x2029;
f36cc39d
KW
1575 my @lb = (uni_to_native("\x0D\x0A"),
1576 map {chr utf8::unicode_to_native($_) }
1577 0x0A .. 0x0D, 0x85, 0x2028, 0x2029);
e425a60b
YO
1578 foreach my $t ([\@h, qr/\h/, qr/\h+/],
1579 [\@v, qr/\v/, qr/\v+/],
1580 [\@lb, qr/\R/, qr/\R+/],) {
1581 my $ary = shift @$t;
1582 foreach my $pat (@$t) {
1583 foreach my $str (@$ary) {
df6841b6
KW
1584 my $temp_str = $str;
1585 $temp_str = display($temp_str);
1586 ok $str =~ /($pat)/, $temp_str . " =~ /($pat)";
1587 my $temp_1 = $1;
1588 is($1, $str, "\$1='" . display($temp_1) . "' eq '" . $temp_str . "' after ($pat)");
e425a60b 1589 utf8::upgrade ($str);
df6841b6
KW
1590 ok $str =~ /($pat)/, "Upgraded " . $temp_str . " =~ /($pat)/";
1591 is($1, $str, "\$1='" . display($temp_1) . "' eq '" . $temp_str . "'(upgraded) after ($pat)");
e425a60b
YO
1592 }
1593 }
1594 }
1595 }
1596
e425a60b 1597 {
de946258 1598 # Check that \\xDF match properly in its various forms
e425a60b
YO
1599 # Test that \xDF matches properly. this is pretty hacky stuff,
1600 # but its actually needed. The malarky with '-' is to prevent
1601 # compilation caching from playing any role in the test.
f36cc39d 1602 my @df = (chr utf8::unicode_to_native(0xDF), '-', chr utf8::unicode_to_native(0xDF));
e425a60b 1603 utf8::upgrade ($df [2]);
f36cc39d 1604 my @strs = ('ss', 'sS', 'Ss', 'SS', chr utf8::unicode_to_native(0xDF));
e425a60b
YO
1605 my @ss = map {("$_", "$_")} @strs;
1606 utf8::upgrade ($ss [$_ * 2 + 1]) for 0 .. $#strs;
1607
1608 for my $ssi (0 .. $#ss) {
1609 for my $dfi (0 .. $#df) {
1610 my $pat = $df [$dfi];
1611 my $str = $ss [$ssi];
1612 my $utf_df = ($dfi > 1) ? 'utf8' : '';
1613 my $utf_ss = ($ssi % 2) ? 'utf8' : '';
f36cc39d
KW
1614 my $sstr; # We hard-code the ebcdic value below to avoid
1615 # perturbing the test
1616 ($sstr = $str) =~ s/\xDF/\\xDF/ if $::IS_ASCII;
1617 ($sstr = $str) =~ s/\x59/\\x59/ if $::IS_EBCDIC;
e425a60b
YO
1618
1619 if ($utf_df || $utf_ss || length ($ss [$ssi]) == 1) {
1620 my $ret = $str =~ /$pat/i;
1621 next if $pat eq '-';
f36cc39d
KW
1622 if ($::IS_ASCII) {
1623 ok $ret, "\"$sstr\" =~ /\\xDF/i " .
1624 "(str is @{[$utf_ss||'latin']}, pat is " .
1625 "@{[$utf_df||'latin']})";
1626 }
1627 else {
1628 ok $ret, "\"$sstr\" =~ /\\x59/i " .
e425a60b
YO
1629 "(str is @{[$utf_ss||'latin']}, pat is " .
1630 "@{[$utf_df||'latin']})";
f36cc39d 1631 }
e425a60b
YO
1632 }
1633 else {
1634 my $ret = $str !~ /$pat/i;
1635 next if $pat eq '-';
f36cc39d
KW
1636 if ($::IS_EBCDIC) {
1637 ok $ret, "\"$sstr\" !~ /\\x59/i " .
e425a60b
YO
1638 "(str is @{[$utf_ss||'latin']}, pat is " .
1639 "@{[$utf_df||'latin']})";
f36cc39d
KW
1640 }
1641 else {
1642 ok $ret, "\"$sstr\" !~ /\\xDF/i " .
1643 "(str is @{[$utf_ss||'latin']}, pat is " .
1644 "@{[$utf_df||'latin']})";
1645 }
e425a60b
YO
1646 }
1647 }
1648 }
1649 }
1650
e425a60b 1651 {
de946258 1652 my $message = "BBC(Bleadperl Breaks CPAN) Today: String::Multibyte";
e425a60b
YO
1653 my $re = qr/(?:[\x00-\xFF]{4})/;
1654 my $hyp = "\0\0\0-";
1655 my $esc = "\0\0\0\\";
1656
1657 my $str = "$esc$hyp$hyp$esc$esc";
1658 my @a = ($str =~ /\G(?:\Q$esc$esc\E|\Q$esc$hyp\E|$re)/g);
1659
de946258 1660 is(@a,3, $message);
e425a60b 1661 local $" = "=";
de946258 1662 is("@a","$esc$hyp=$hyp=$esc$esc", $message);
e425a60b
YO
1663 }
1664
e425a60b
YO
1665 {
1666 # Test for keys in %+ and %-
de946258 1667 my $message = 'Test keys in %+ and %-';
7fba2966
FC
1668 no warnings 'uninitialized';
1669 local $_ = "abcdef";
e425a60b 1670 /(?<foo>a)|(?<foo>b)/;
de946258
NC
1671 is((join ",", sort keys %+), "foo", $message);
1672 is((join ",", sort keys %-), "foo", $message);
1673 is((join ",", sort values %+), "a", $message);
1674 is((join ",", sort map "@$_", values %-), "a ", $message);
e425a60b 1675 /(?<bar>a)(?<bar>b)(?<quux>.)/;
de946258
NC
1676 is((join ",", sort keys %+), "bar,quux", $message);
1677 is((join ",", sort keys %-), "bar,quux", $message);
1678 is((join ",", sort values %+), "a,c", $message); # leftmost
1679 is((join ",", sort map "@$_", values %-), "a b,c", $message);
e425a60b 1680 /(?<un>a)(?<deux>c)?/; # second buffer won't capture
de946258
NC
1681 is((join ",", sort keys %+), "un", $message);
1682 is((join ",", sort keys %-), "deux,un", $message);
1683 is((join ",", sort values %+), "a", $message);
1684 is((join ",", sort map "@$_", values %-), ",a", $message);
e425a60b
YO
1685 }
1686
e425a60b
YO
1687 {
1688 # length() on captures, the numbered ones end up in Perl_magic_len
7fba2966 1689 local $_ = "aoeu " . uni_to_native("\xe6") . "var ook";
e425a60b
YO
1690 /^ \w+ \s (?<eek>\S+)/x;
1691
de26e0cc
NC
1692 is(length $`, 0, q[length $`]);
1693 is(length $', 4, q[length $']);
1694 is(length $&, 9, q[length $&]);
1695 is(length $1, 4, q[length $1]);
1696 is(length $+{eek}, 4, q[length $+{eek} == length $1]);
e425a60b
YO
1697 }
1698
e425a60b
YO
1699 {
1700 my $ok = -1;
1701
1702 $ok = exists ($-{x}) ? 1 : 0 if 'bar' =~ /(?<x>foo)|bar/;
de26e0cc
NC
1703 is($ok, 1, '$-{x} exists after "bar"=~/(?<x>foo)|bar/');
1704 is(scalar (%+), 0, 'scalar %+ == 0 after "bar"=~/(?<x>foo)|bar/');
1705 is(scalar (%-), 1, 'scalar %- == 1 after "bar"=~/(?<x>foo)|bar/');
e425a60b
YO
1706
1707 $ok = -1;
1708 $ok = exists ($+{x}) ? 1 : 0 if 'bar' =~ /(?<x>foo)|bar/;
de26e0cc
NC
1709 is($ok, 0, '$+{x} not exists after "bar"=~/(?<x>foo)|bar/');
1710 is(scalar (%+), 0, 'scalar %+ == 0 after "bar"=~/(?<x>foo)|bar/');
1711 is(scalar (%-), 1, 'scalar %- == 1 after "bar"=~/(?<x>foo)|bar/');
e425a60b
YO
1712
1713 $ok = -1;
1714 $ok = exists ($-{x}) ? 1 : 0 if 'foo' =~ /(?<x>foo)|bar/;
de26e0cc
NC
1715 is($ok, 1, '$-{x} exists after "foo"=~/(?<x>foo)|bar/');
1716 is(scalar (%+), 1, 'scalar %+ == 1 after "foo"=~/(?<x>foo)|bar/');
1717 is(scalar (%-), 1, 'scalar %- == 1 after "foo"=~/(?<x>foo)|bar/');
e425a60b
YO
1718
1719 $ok = -1;
1720 $ok = exists ($+{x}) ? 1 : 0 if 'foo'=~/(?<x>foo)|bar/;
de26e0cc 1721 is($ok, 1, '$+{x} exists after "foo"=~/(?<x>foo)|bar/');
e425a60b
YO
1722 }
1723
e425a60b
YO
1724 {
1725 local $_;
1726 ($_ = 'abc') =~ /(abc)/g;
0f289c68 1727 $_ = '123';
de26e0cc 1728 is("$1", 'abc', "/g leads to unsafe match vars: $1");
1b474ee3
NC
1729
1730 fresh_perl_is(<<'EOP', ">abc<\n", {}, 'mention $&');
1731$&;
1732my $x;
1733($x='abc')=~/(abc)/g;
1734$x='123';
1735print ">$1<\n";
1736EOP
1737
1b474ee3
NC
1738 fresh_perl_is(<<'EOP', ">abc<\n", {}, 'no mention of $&');
1739my $x;
1740($x='abc')=~/(abc)/g;
1741$x='123';
1742print ">$1<\n";
1743EOP
e425a60b
YO
1744 }
1745
e425a60b 1746 {
de946258 1747 # Message-ID: <20070818091501.7eff4831@r2d2>
e425a60b
YO
1748 my $str = "";
1749 for (0 .. 5) {
1750 my @x;
1751 $str .= "@x"; # this should ALWAYS be the empty string
1752 'a' =~ /(a|)/;
1753 push @x, 1;
1754 }
de26e0cc 1755 is(length $str, 0, "Trie scope error, string should be empty");
e425a60b
YO
1756 $str = "";
1757 my @foo = ('a') x 5;
1758 for (@foo) {
1759 my @bar;
1760 $str .= "@bar";
1761 s/a|/push @bar, 1/e;
1762 }
de26e0cc 1763 is(length $str, 0, "Trie scope error, string should be empty");
e425a60b
YO
1764 }
1765
e425a60b 1766 {
e425a60b 1767# more TRIE/AHOCORASICK problems with mixed utf8 / latin-1 and case folding
f36cc39d
KW
1768 for my $ord (160 .. 255) {
1769 my $chr = utf8::unicode_to_native($ord);
0f289c68
YO
1770 my $chr_byte = chr($chr);
1771 my $chr_utf8 = chr($chr); utf8::upgrade($chr_utf8);
1772 my $rx = qr{$chr_byte|X}i;
1773 ok($chr_utf8 =~ $rx, "utf8/latin, codepoint $chr");
1774 }
e425a60b
YO
1775 }
1776
1777 {
e425a60b
YO
1778 our $a = 3; "" =~ /(??{ $a })/;
1779 our $b = $a;
de26e0cc 1780 is($b, $a, "Copy of scalar used for postponed subexpression");
e425a60b
YO
1781 }
1782
e425a60b 1783 {
e425a60b
YO
1784 our @ctl_n = ();
1785 our @plus = ();
1786 our $nested_tags;
1787 $nested_tags = qr{
1788 <
1789 (\w+)
1790 (?{
1791 push @ctl_n,$^N;
1792 push @plus,$+;
1793 })
1794 >
1795 (??{$nested_tags})*
1796 </\s* \w+ \s*>
1797 }x;
1798
1799 my $match = '<bla><blubb></blubb></bla>' =~ m/^$nested_tags$/;
1800 ok $match, 'nested construct matches';
de26e0cc
NC
1801 is("@ctl_n", "bla blubb", '$^N inside of (?{}) works as expected');
1802 is("@plus", "bla blubb", '$+ inside of (?{}) works as expected');
e425a60b
YO
1803 }
1804
e425a60b
YO
1805 SKIP: {
1806 # XXX: This set of tests is essentially broken, POSIX character classes
0f289c68 1807 # should not have differing definitions under Unicode.
e425a60b 1808 # There are property names for that.
ef237063 1809 skip "Tests assume ASCII", 4 unless $::IS_ASCII;
e425a60b
YO
1810
1811 my @notIsPunct = grep {/[[:punct:]]/ and not /\p{IsPunct}/}
1812 map {chr} 0x20 .. 0x7f;
1a3f15c1 1813 is(join ('', @notIsPunct), '$+<=>^`|~',
de26e0cc 1814 '[:punct:] disagrees with IsPunct on Symbols');
e425a60b
YO
1815
1816 my @isPrint = grep {not /[[:print:]]/ and /\p{IsPrint}/}
1817 map {chr} 0 .. 0x1f, 0x7f .. 0x9f;
de26e0cc
NC
1818 is(join ('', @isPrint), "",
1819 'IsPrint agrees with [:print:] on control characters');
e425a60b
YO
1820
1821 my @isPunct = grep {/[[:punct:]]/ != /\p{IsPunct}/}
1822 map {chr} 0x80 .. 0xff;
7620cb10 1823 is(join ('', @isPunct), "\xa1\xa7\xab\xb6\xb7\xbb\xbf", # ¡ « · » ¿
de26e0cc 1824 'IsPunct disagrees with [:punct:] outside ASCII');
e425a60b
YO
1825
1826 my @isPunctLatin1 = eval q {
3fc5553b 1827 grep {/[[:punct:]]/u != /\p{IsPunct}/} map {chr} 0x80 .. 0xff;
e425a60b
YO
1828 };
1829 skip "Eval failed ($@)", 1 if $@;
1830 skip "PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS set to 0", 1
1831 if !$ENV{PERL_TEST_LEGACY_POSIX_CC};
de26e0cc
NC
1832 is(join ('', @isPunctLatin1), '',
1833 'IsPunct agrees with [:punct:] with explicit Latin1');
0f289c68 1834 }
d1c771f5 1835
d1c771f5
B
1836 {
1837 # Tests for [#perl 71942]
1838 our $count_a;
1839 our $count_b;
1840
1841 my $c = 0;
1842 for my $re (
1843# [
1844# should match?,
1845# input string,
1846# re 1,
1847# re 2,
1848# expected values of count_a and count_b,
1849# ]
1850 [
1851 0,
1852 "xababz",
1853 qr/a+(?{$count_a++})b?(*COMMIT)(*FAIL)/,
1854 qr/a+(?{$count_b++})b?(*COMMIT)z/,
1855 1,
1856 ],
1857 [
1858 0,
1859 "xababz",
1860 qr/a+(?{$count_a++})b?(*COMMIT)\s*(*FAIL)/,
1861 qr/a+(?{$count_b++})b?(*COMMIT)\s*z/,
1862 1,
1863 ],
1864 [
1865 0,
1866 "xababz",
1867 qr/a+(?{$count_a++})(?:b|)?(*COMMIT)(*FAIL)/,
1868 qr/a+(?{$count_b++})(?:b|)?(*COMMIT)z/,
1869 1,
1870 ],
1871 [
1872 0,
1873 "xababz",
1874 qr/a+(?{$count_a++})b{0,6}(*COMMIT)(*FAIL)/,
1875 qr/a+(?{$count_b++})b{0,6}(*COMMIT)z/,
1876 1,
1877 ],
1878 [
1879 0,
1880 "xabcabcz",
1881 qr/a+(?{$count_a++})(bc){0,6}(*COMMIT)(*FAIL)/,
1882 qr/a+(?{$count_b++})(bc){0,6}(*COMMIT)z/,
1883 1,
1884 ],
1885 [
1886 0,
1887 "xabcabcz",
1888 qr/a+(?{$count_a++})(bc*){0,6}(*COMMIT)(*FAIL)/,
1889 qr/a+(?{$count_b++})(bc*){0,6}(*COMMIT)z/,
1890 1,
1891 ],
1892
1893
1894 [
1895 0,
1896 "aaaabtz",
1897 qr/a+(?{$count_a++})b?(*PRUNE)(*FAIL)/,
1898 qr/a+(?{$count_b++})b?(*PRUNE)z/,
1899 4,
1900 ],
1901 [
1902 0,
1903 "aaaabtz",
1904 qr/a+(?{$count_a++})b?(*PRUNE)\s*(*FAIL)/,
1905 qr/a+(?{$count_b++})b?(*PRUNE)\s*z/,
1906 4,
1907 ],
1908 [
1909 0,
1910 "aaaabtz",
1911 qr/a+(?{$count_a++})(?:b|)(*PRUNE)(*FAIL)/,
1912 qr/a+(?{$count_b++})(?:b|)(*PRUNE)z/,
1913 4,
1914 ],
1915 [
1916 0,
1917 "aaaabtz",
1918 qr/a+(?{$count_a++})b{0,6}(*PRUNE)(*FAIL)/,
1919 qr/a+(?{$count_b++})b{0,6}(*PRUNE)z/,
1920 4,
1921 ],
1922 [
1923 0,
1924 "aaaabctz",
1925 qr/a+(?{$count_a++})(bc){0,6}(*PRUNE)(*FAIL)/,
1926 qr/a+(?{$count_b++})(bc){0,6}(*PRUNE)z/,
1927 4,
1928 ],
1929 [
1930 0,
1931 "aaaabctz",
1932 qr/a+(?{$count_a++})(bc*){0,6}(*PRUNE)(*FAIL)/,
1933 qr/a+(?{$count_b++})(bc*){0,6}(*PRUNE)z/,
1934 4,
1935 ],
1936
1937 [
1938 0,
1939 "aaabaaab",
1940 qr/a+(?{$count_a++;})b?(*SKIP)(*FAIL)/,
1941 qr/a+(?{$count_b++;})b?(*SKIP)z/,
1942 2,
1943 ],
1944 [
1945 0,
1946 "aaabaaab",
1947 qr/a+(?{$count_a++;})b?(*SKIP)\s*(*FAIL)/,
1948 qr/a+(?{$count_b++;})b?(*SKIP)\s*z/,
1949 2,
1950 ],
1951 [
1952 0,
1953 "aaabaaab",
1954 qr/a+(?{$count_a++;})(?:b|)(*SKIP)(*FAIL)/,
1955 qr/a+(?{$count_b++;})(?:b|)(*SKIP)z/,
1956 2,
1957 ],
1958 [
1959 0,
1960 "aaabaaab",
1961 qr/a+(?{$count_a++;})b{0,6}(*SKIP)(*FAIL)/,
1962 qr/a+(?{$count_b++;})b{0,6}(*SKIP)z/,
1963 2,
1964 ],
1965 [
1966 0,
1967 "aaabcaaabc",
1968 qr/a+(?{$count_a++;})(bc){0,6}(*SKIP)(*FAIL)/,
1969 qr/a+(?{$count_b++;})(bc){0,6}(*SKIP)z/,
1970 2,
1971 ],
1972 [
1973 0,
1974 "aaabcaaabc",
1975 qr/a+(?{$count_a++;})(bc*){0,6}(*SKIP)(*FAIL)/,
1976 qr/a+(?{$count_b++;})(bc*){0,6}(*SKIP)z/,
1977 2,
1978 ],
1979
1980
1981 [
1982 0,
1983 "aaddbdaabyzc",
1984 qr/a (?{$count_a++;}) (*MARK:T1) (a*) .*? b? (*SKIP:T1) (*FAIL) \s* c \1 /x,
1985 qr/a (?{$count_b++;}) (*MARK:T1) (a*) .*? b? (*SKIP:T1) z \s* c \1 /x,
1986 4,
1987 ],
1988 [
1989 0,
1990 "aaddbdaabyzc",
1991 qr/a (?{$count_a++;}) (*MARK:T1) (a*) .*? b? (*SKIP:T1) \s* (*FAIL) \s* c \1 /x,
1992 qr/a (?{$count_b++;}) (*MARK:T1) (a*) .*? b? (*SKIP:T1) \s* z \s* c \1 /x,
1993 4,
1994 ],
1995 [
1996 0,
1997 "aaddbdaabyzc",
1998 qr/a (?{$count_a++;}) (*MARK:T1) (a*) .*? (?:b|) (*SKIP:T1) (*FAIL) \s* c \1 /x,
1999 qr/a (?{$count_b++;}) (*MARK:T1) (a*) .*? (?:b|) (*SKIP:T1) z \s* c \1 /x,
2000 4,
2001 ],
2002 [
2003 0,
2004 "aaddbdaabyzc",
2005 qr/a (?{$count_a++;}) (*MARK:T1) (a*) .*? b{0,6} (*SKIP:T1) (*FAIL) \s* c \1 /x,
2006 qr/a (?{$count_b++;}) (*MARK:T1) (a*) .*? b{0,6} (*SKIP:T1) z \s* c \1 /x,
2007 4,
2008 ],
2009 [
2010 0,
2011 "aaddbcdaabcyzc",
2012 qr/a (?{$count_a++;}) (*MARK:T1) (a*) .*? (bc){0,6} (*SKIP:T1) (*FAIL) \s* c \1 /x,
2013 qr/a (?{$count_b++;}) (*MARK:T1) (a*) .*? (bc){0,6} (*SKIP:T1) z \s* c \1 /x,
2014 4,
2015 ],
2016 [
2017 0,
2018 "aaddbcdaabcyzc",
2019 qr/a (?{$count_a++;}) (*MARK:T1) (a*) .*? (bc*){0,6} (*SKIP:T1) (*FAIL) \s* c \1 /x,
2020 qr/a (?{$count_b++;}) (*MARK:T1) (a*) .*? (bc*){0,6} (*SKIP:T1) z \s* c \1 /x,
2021 4,
2022 ],
2023
2024
2025 [
2026 0,
2027 "aaaaddbdaabyzc",
2028 qr/a (?{$count_a++;}) (a?) (*MARK:T1) (a*) .*? b? (*MARK:T1) (*SKIP:T1) (*FAIL) \s* c \1 /x,
2029 qr/a (?{$count_b++;}) (a?) (*MARK:T1) (a*) .*? b? (*MARK:T1) (*SKIP:T1) z \s* c \1 /x,
2030 2,
2031 ],
2032 [
2033 0,
2034 "aaaaddbdaabyzc",
2035 qr/a (?{$count_a++;}) (a?) (*MARK:T1) (a*) .*? b? (*MARK:T1) (*SKIP:T1) \s* (*FAIL) \s* c \1 /x,
2036 qr/a (?{$count_b++;}) (a?) (*MARK:T1) (a*) .*? b? (*MARK:T1) (*SKIP:T1) \s* z \s* c \1 /x,
2037 2,
2038 ],
2039 [
2040 0,
2041 "aaaaddbdaabyzc",
2042 qr/a (?{$count_a++;}) (a?) (*MARK:T1) (a*) .*? (?:b|) (*MARK:T1) (*SKIP:T1) (*FAIL) \s* c \1 /x,
2043 qr/a (?{$count_b++;}) (a?) (*MARK:T1) (a*) .*? (?:b|) (*MARK:T1) (*SKIP:T1) z \s* c \1 /x,
2044 2,
2045 ],
2046 [
2047 0,
2048 "aaaaddbdaabyzc",
2049 qr/a (?{$count_a++;}) (a?) (*MARK:T1) (a*) .*? b{0,6} (*MARK:T1) (*SKIP:T1) (*FAIL) \s* c \1 /x,
2050 qr/a (?{$count_b++;}) (a?) (*MARK:T1) (a*) .*? b{0,6} (*MARK:T1) (*SKIP:T1) z \s* c \1 /x,
2051 2,
2052 ],
2053 [
2054 0,
2055 "aaaaddbcdaabcyzc",
2056 qr/a (?{$count_a++;}) (a?) (*MARK:T1) (a*) .*? (bc){0,6} (*MARK:T1) (*SKIP:T1) (*FAIL) \s* c \1 /x,
2057 qr/a (?{$count_b++;}) (a?) (*MARK:T1) (a*) .*? (bc){0,6} (*MARK:T1) (*SKIP:T1) z \s* c \1 /x,
2058 2,
2059 ],
2060 [
2061 0,
2062 "aaaaddbcdaabcyzc",
2063 qr/a (?{$count_a++;}) (a?) (*MARK:T1) (a*) .*? (bc*){0,6} (*MARK:T1) (*SKIP:T1) (*FAIL) \s* c \1 /x,
2064 qr/a (?{$count_b++;}) (a?) (*MARK:T1) (a*) .*? (bc*){0,6} (*MARK:T1) (*SKIP:T1) z \s* c \1 /x,
2065 2,
2066 ],
2067
2068
2069 [
2070 0,
2071 "AbcdCBefgBhiBqz",
2072 qr/(A (.*) (?{ $count_a++ }) C? (*THEN) | A D) (*FAIL)/x,
2073 qr/(A (.*) (?{ $count_b++ }) C? (*THEN) | A D) z/x,
2074 1,
2075 ],
2076 [
2077 0,
2078 "AbcdCBefgBhiBqz",
2079 qr/(A (.*) (?{ $count_a++ }) C? (*THEN) | A D) \s* (*FAIL)/x,
2080 qr/(A (.*) (?{ $count_b++ }) C? (*THEN) | A D) \s* z/x,
2081 1,
2082 ],
2083 [
2084 0,
2085 "AbcdCBefgBhiBqz",
2086 qr/(A (.*) (?{ $count_a++ }) (?:C|) (*THEN) | A D) (*FAIL)/x,
2087 qr/(A (.*) (?{ $count_b++ }) (?:C|) (*THEN) | A D) z/x,
2088 1,
2089 ],
2090 [
2091 0,
2092 "AbcdCBefgBhiBqz",
2093 qr/(A (.*) (?{ $count_a++ }) C{0,6} (*THEN) | A D) (*FAIL)/x,
2094 qr/(A (.*) (?{ $count_b++ }) C{0,6} (*THEN) | A D) z/x,
2095 1,
2096 ],
2097 [
2098 0,
2099 "AbcdCEBefgBhiBqz",
2100 qr/(A (.*) (?{ $count_a++ }) (CE){0,6} (*THEN) | A D) (*FAIL)/x,
2101 qr/(A (.*) (?{ $count_b++ }) (CE){0,6} (*THEN) | A D) z/x,
2102 1,
2103 ],
2104 [
2105 0,
2106 "AbcdCBefgBhiBqz",
2107 qr/(A (.*) (?{ $count_a++ }) (CE*){0,6} (*THEN) | A D) (*FAIL)/x,
2108 qr/(A (.*) (?{ $count_b++ }) (CE*){0,6} (*THEN) | A D) z/x,
2109 1,
2110 ],
2111 ) {
2112 $c++;
2113 $count_a = 0;
2114 $count_b = 0;
2115
2116 my $match_a = ($re->[1] =~ $re->[2]) || 0;
2117 my $match_b = ($re->[1] =~ $re->[3]) || 0;
2118
de26e0cc
NC
2119 is($match_a, $re->[0], "match a " . ($re->[0] ? "succeeded" : "failed") . " ($c)");
2120 is($match_b, $re->[0], "match b " . ($re->[0] ? "succeeded" : "failed") . " ($c)");
2121 is($count_a, $re->[4], "count a ($c)");
2122 is($count_b, $re->[4], "count b ($c)");
d1c771f5
B
2123 }
2124 }
e425a60b 2125
b57e4118
KW
2126 { # Bleadperl v5.13.8-292-gf56b639 breaks NEZUMI/Unicode-LineBreak-1.011
2127 # \xdf in lookbehind failed to compile as is multi-char fold
14358a41
NC
2128 my $message = "Lookbehind with \\xdf matchable compiles";
2129 my $r = eval 'qr{
b57e4118
KW
2130 (?u: (?<=^url:) |
2131 (?<=[/]) (?=[^/]) |
2132 (?<=[^-.]) (?=[-~.,_?\#%=&]) |
2133 (?<=[=&]) (?=.)
14358a41
NC
2134 )}iox';
2135 is($@, '', $message);
bbce3ca6 2136 object_ok($r, 'Regexp', $message);
b57e4118
KW
2137 }
2138
84193928
KW
2139 # RT #82610
2140 ok 'foo/file.fob' =~ m,^(?=[^\.])[^/]*/(?=[^\.])[^/]*\.fo[^/]$,;
2141
295c2f7d 2142 { # This was failing unless an explicit /d was added
f36cc39d
KW
2143 my $E0 = uni_to_native("\xE0");
2144 my $p = qr/[_$E0]/i;
295c2f7d 2145 utf8::upgrade($p);
f36cc39d 2146 like(uni_to_native("\xC0"), qr/$p/, "Verify \"\\xC0\" =~ /[\\xE0_]/i; pattern in utf8");
295c2f7d
KW
2147 }
2148
a40630bf
YO
2149 ok "x" =~ /\A(?>(?:(?:)A|B|C?x))\z/,
2150 "Check TRIE does not overwrite EXACT following NOTHING at start - RT #111842";
c221a219 2151
26faadbd
KW
2152 {
2153 my $single = ":";
2154 my $upper = "\x{390}"; # Fold is 3 chars.
2155 my $multi = CORE::fc($upper);
2156
2157 my $failed = 0;
2158
2159 # Try forcing a node to be split, with a multi-char fold at the
2160 # boundary
2161 for my $repeat (1 .. 300) {
2162 my $string = $single x $repeat;
2163 my $lhs = $string . $upper;
2164 if ($lhs !~ m/$string$multi/i) {
2165 $failed = $repeat;
2166 last;
2167 }
2168 }
2169 ok(! $failed, "Matched multi-char fold across EXACTFish node boundaries; if failed, was at count $failed");
2170
b9c48b5b
KW
2171 $failed = 0;
2172 for my $repeat (1 .. 300) {
2173 my $string = $single x $repeat;
2174 my $lhs = $string . "\N{LATIN SMALL LIGATURE FFI}";
2175 if ($lhs !~ m/${string}ff\N{LATIN SMALL LETTER I}/i) {
2176 $failed = $repeat;
2177 last;
2178 }
2179 }
2180 ok(! $failed, "Matched multi-char fold across EXACTFish node boundaries; if failed, was at count $failed");
2181
2182 $failed = 0;
2183 for my $repeat (1 .. 300) {
2184 my $string = $single x $repeat;
2185 my $lhs = $string . "\N{LATIN SMALL LIGATURE FFL}";
2186 if ($lhs !~ m/${string}ff\N{U+6c}/i) {
2187 $failed = $repeat;
2188 last;
2189 }
2190 }
2191 ok(! $failed, "Matched multi-char fold across EXACTFish node boundaries; if failed, was at count $failed");
88b3a463
KW
2192
2193 # This tests that under /d matching that an 'ss' split across two
2194 # parts of a node doesn't end up turning into something that matches
2195 # \xDF unless it is in utf8.
2196 $failed = 0;
2197 $single = 'a'; # Is non-terminal multi-char fold char
2198 for my $repeat (1 .. 300) {
2199 my $string = $single x $repeat;
2200 my $lhs = "$string\N{LATIN SMALL LETTER SHARP S}";
2201 utf8::downgrade($lhs);
2202 $string .= "s";
2203 if ($lhs =~ m/${string}s/di) {
2204 $failed = $repeat;
2205 last;
2206 }
2207 }
2208 ok(! $failed, "Matched multi-char fold 'ss' across EXACTF node boundaries; if failed, was at count $failed");
26faadbd
KW
2209 }
2210
2b233a8f 2211 {
2b233a8f
KW
2212 fresh_perl_is('print eval "\"\x{101}\" =~ /[[:lower:]]/", "\n"; print eval "\"\x{100}\" =~ /[[:lower:]]/i", "\n";',
2213 "1\n1", # Both re's should match
20e5bab4 2214 {},
2b233a8f
KW
2215 "get [:lower:] swash in first eval; test under /i in second");
2216 }
2217
c5de0829 2218 {
3553f4fa
DM
2219 fresh_perl_is(<<'EOF',
2220 my $s = "\x{41c}";
2221 $s =~ /(.*)/ or die;
2222 $ls = lc $1;
2223 print $ls eq lc $s ? "good\n" : "bad: [$ls]\n";
2224EOF
2225 "good\n",
2226 {},
2227 "swash triggered by lc() doesn't corrupt \$1"
2228 );
2229 }
2230
2231 {
c5de0829 2232 #' RT #119075
fd2268bc 2233 no warnings 'regexp'; # Silence "has useless greediness modifier"
c5de0829
JK
2234 local $@;
2235 eval { /a{0}?/; };
2236 ok(! $@,
2237 "PCRE regression test: No 'Quantifier follows nothing in regex' warning");
2238
2239 }
2240
709d08c2
KW
2241 {
2242 unlike("\xB5", qr/^_?\p{IsMyRuntimeProperty}\z/, "yadayada");
2243 like("\xB6", qr/^_?\p{IsMyRuntimeProperty}\z/, "yadayada");
2244 unlike("\xB7", qr/^_?\p{IsMyRuntimeProperty}\z/, "yadayada");
2245 like("\xB5", qr/^_?\P{IsMyRuntimeProperty}\z/, "yadayada");
2246 unlike("\xB6", qr/^_?\P{IsMyRuntimeProperty}\z/, "yadayada");
2247 like("\xB7", qr/^_?\P{IsMyRuntimeProperty}\z/, "yadayada");
2248
2249 unlike("_\xB5", qr/^_?\p{IsMyRuntimeProperty}\z/, "yadayada");
2250 like("_\xB6", qr/^_?\p{IsMyRuntimeProperty}\z/, "yadayada");
2251 unlike("_\xB7", qr/^_?\p{IsMyRuntimeProperty}\z/, "yadayada");
2252 like("_\xB5", qr/^_?\P{IsMyRuntimeProperty}\z/, "yadayada");
2253 unlike("_\xB6", qr/^_?\P{IsMyRuntimeProperty}\z/, "yadayada");
2254 like("_\xB7", qr/^_?\P{IsMyRuntimeProperty}\z/, "yadayada");
2255 }
2256
2257 # These are defined later, so won't be known at regex compile time above
2258 sub IsMyRuntimeProperty {
2259 return "B6\n";
2260 }
2261
2262 sub IsntMyRuntimeProperty {
2263 return "!B6\n";
2264 }
2265
80836a6e
KW
2266 { # [perl 121777]
2267 my $regex;
2268 { package Some;
2269 # define a Unicode propertyIs_q
2270 sub Is_q
2271 {
2272 sprintf '%x', ord 'q'
2273 }
2274 $regex = qr/\p{Is_q}/;
2275
2276 # If we uncomment the following line, prior to the patch that
de68ac66
KW
2277 # fixed this, everything would work because we would have expanded
2278 # the property by the time the regex in the 'like' below got
2279 # compiled.
80836a6e
KW
2280 #'q' =~ $regex;
2281 }
2282
2283 like('q', $regex, 'User-defined property matches outside package');
c0611711
KW
2284
2285 package Some {
2286 main::like('abcq', qr/abc$regex/, 'Run-time compiled in-package user-defined property matches');
2287 }
80836a6e
KW
2288 }
2289
bc031a7d
KW
2290 { # From Lingua::Stem::UniNE; no ticket filed but related to #121778
2291 use utf8;
2292 my $word = 'рабта';
2293 $word =~ s{ (?:
2294 ия # definite articles for nouns:
2295 | ът # ∙ masculine
2296 | та # ∙ feminine
2297 | то # ∙ neutral
2298 | те # ∙ plural
2299 ) $ }{}x;
2300 is($word, 'раб', "Handles UTF8 trie correctly");
2301 }
2302
842a9d21
KW
2303 { # [perl #122460]
2304 my $a = "rdvark";
2305 $a =~ /(?{})(?=[A-Za-z0-9_])a*?/g;
2306 is (pos $a, 0, "optimizer correctly thinks (?=...) is 0-length");
2307 }
2308
4cbd7e22
FC
2309 { # [perl #123417] multi-char \N{...} tripping roundly
2310 use Cname;
2311 my $qr = qr$(\N{foo})$;
2312 "afoot" =~ eval "qr/$qr/";
2313 is "$1" || $@, "foo", 'multichar \N{...} stringified and retoked';
2314 }
b7e6151c
FC
2315 { # empty \N{...} tripping roundly
2316 BEGIN { $^H{charnames} = sub { "" } }
2317 my $qr = qr$(a\N{foo}t)$;
2318 "at" =~ eval "qr/$qr/";
2319 is "$1" || $@, "at", 'empty \N{...} stringified and retoked';
2320 }
4cbd7e22 2321
7195e5da
KW
2322 is (scalar split(/\b{sb}/, "Don't think twice. It's all right."),
2323 2, '\b{wb} splits sentences correctly');
2324
4cbd7e22 2325
de085464
KW
2326 # !!! NOTE! Keep the following tests last -- they may crash perl
2327
e425a60b 2328 print "# Tests that follow may crash perl\n";
e425a60b
YO
2329 {
2330 eval '/\k/';
2331 ok $@ =~ /\QSequence \k... not terminated in regex;\E/,
2332 'Lone \k not allowed';
2333 }
2334
e425a60b 2335 {
de946258 2336 my $message = "Substitution with lookahead (possible segv)";
e425a60b
YO
2337 $_ = "ns1ns1ns1";
2338 s/ns(?=\d)/ns_/g;
de946258 2339 is($_, "ns_1ns_1ns_1", $message);
e425a60b
YO
2340 $_ = "ns1";
2341 s/ns(?=\d)/ns_/;
de946258 2342 is($_, "ns_1", $message);
e425a60b
YO
2343 $_ = "123";
2344 s/(?=\d+)|(?<=\d)/!Bang!/g;
de946258 2345 is($_, "!Bang!1!Bang!2!Bang!3!Bang!", $message);
e425a60b
YO
2346 }
2347
6182169b
KW
2348 {
2349 # Earlier versions of Perl said this was fatal.
de946258 2350 my $message = "U+0FFFF shouldn't crash the regex engine";
6182169b
KW
2351 no warnings 'utf8';
2352 my $a = eval "chr(65535)";
2353 use warnings;
2354 my $warning_message;
2355 local $SIG{__WARN__} = sub { $warning_message = $_[0] };
2356 eval $a =~ /[a-z]/;
de946258 2357 ok(1, $message); # If it didn't crash, it worked.
6182169b 2358 }
b57e4118 2359
06345901
NC
2360 TODO: { # Was looping
2361 todo_skip('Triggers thread clone SEGV. See #86550')
2362 if $::running_as_thread && $::running_as_thread;
478c6b74 2363 watchdog(10); # Use a bigger value for busy systems
6438af90
KW
2364 like("\x{00DF}", qr/[\x{1E9E}_]*/i, "\"\\x{00DF}\" =~ /[\\x{1E9E}_]*/i was looping");
2365 }
2366
f9126265
KW
2367 { # Bug #90536, caused failed assertion
2368 unlike("s\N{U+DF}", qr/^\x{00DF}/i, "\"s\\N{U+DF}\", qr/^\\x{00DF}/i");
2369 }
2370
361ee0fe
KW
2371 # User-defined Unicode properties to match above-Unicode code points
2372 sub Is_32_Bit_Super { return "110000\tFFFFFFFF\n" }
2373 sub Is_Portable_Super { return '!utf8::Any' } # Matches beyond 32 bits
45d91b83 2374
f36cc39d 2375 SKIP:
45d91b83 2376 { # Assertion was failing on on 64-bit platforms; just didn't work on 32.
f36cc39d 2377 skip("EBCDIC only goes to 31 bits", 4) if $::IS_EBCDIC;
45d91b83
KW
2378 no warnings qw(non_unicode portable);
2379 use Config;
2380
2381 # We use 'ok' instead of 'like' because the warnings are lexically
2382 # scoped, and want to turn them off, so have to do the match in this
f36cc39d 2383 # scope. (EBCDIC platforms can't handle above 2**32 - 1
84ea5ef6 2384 if ($Config{uvsize} < 8) {
361ee0fe 2385 ok(chr(0xFFFF_FFFE) =~ /\p{Is_32_Bit_Super}/,
84ea5ef6 2386 "chr(0xFFFF_FFFE) can match a Unicode property");
6cb05c12
KW
2387 ok(chr(0xFFFF_FFFF) =~ /\p{Is_32_Bit_Super}/,
2388 "chr(0xFFFF_FFFF) can match a Unicode property");
01c5845a
KW
2389 my $p = qr/^[\x{FFFF_FFFF}]$/;
2390 ok(chr(0xFFFF_FFFF) =~ $p,
2391 "chr(0xFFFF_FFFF) can match itself in a [class]");
3ef5b9b5
KW
2392 ok(chr(0xFFFF_FFFF) =~ $p, # Tests any caching
2393 "chr(0xFFFF_FFFF) can match itself in a [class] subsequently");
84ea5ef6
KW
2394 }
2395 else {
2396 no warnings 'overflow';
361ee0fe 2397 ok(chr(0xFFFF_FFFF_FFFF_FFFE) =~ qr/\p{Is_Portable_Super}/,
84ea5ef6 2398 "chr(0xFFFF_FFFF_FFFF_FFFE) can match a Unicode property");
6cb05c12
KW
2399 ok(chr(0xFFFF_FFFF_FFFF_FFFF) =~ qr/^\p{Is_Portable_Super}$/,
2400 "chr(0xFFFF_FFFF_FFFF_FFFF) can match a Unicode property");
361ee0fe 2401
01c5845a
KW
2402 my $p = qr/^[\x{FFFF_FFFF_FFFF_FFFF}]$/;
2403 ok(chr(0xFFFF_FFFF_FFFF_FFFF) =~ $p,
2404 "chr(0xFFFF_FFFF_FFFF_FFFF) can match itself in a [class]");
3ef5b9b5
KW
2405 ok(chr(0xFFFF_FFFF_FFFF_FFFF) =~ $p, # Tests any caching
2406 "chr(0xFFFF_FFFF_FFFF_FFFF) can match itself in a [class] subsequently");
01c5845a 2407
361ee0fe
KW
2408 # This test is because something was declared as 32 bits, but
2409 # should have been cast to 64; only a problem where
2410 # sizeof(STRLEN) != sizeof(UV)
2411 ok(chr(0xFFFF_FFFF_FFFF_FFFE) !~ qr/\p{Is_32_Bit_Super}/, "chr(0xFFFF_FFFF_FFFF_FFFE) shouldn't match a range ending in 0xFFFF_FFFF");
45d91b83
KW
2412 }
2413 }
2414
9d501133
KW
2415 { # [perl #112530], the code below caused a panic
2416 sub InFoo { "a\tb\n9\ta\n" }
f36cc39d 2417 like(chr(0xA), qr/\p{InFoo}/,
9d501133
KW
2418 "Overlapping ranges in user-defined properties");
2419 }
2420
512c0f5a
KW
2421 { # [perl #125990], the final 2 tests below each caused a panic.
2422 # The \0's are not necessary; it could be a printable character
2423 # instead, but were in the ticket, so using them.
2424 my $sharp_s = chr utf8::unicode_to_native(0xdf);
2425 my $string = ("\0" x 8)
2426 . ($sharp_s x 3)
2427 . ("\0" x 42)
2428 . "ý";
2429 my $folded_string = ("\0" x 8)
2430 . ("ss" x 3)
2431 . ("\0" x 42)
2432 . "ý";
2433 utf8::downgrade($string);
2434 utf8::downgrade($folded_string);
2435
2436 like($string, qr/$string/i, "LATIN SMALL SHARP S matches itself under /id");
2437 unlike($folded_string, qr/$string/i, "LATIN SMALL SHARP S doesn't match 'ss' under /di");
2438 like($folded_string, qr/\N{}$string/i, "\\N{} earlier than LATIN SMALL SHARP S transforms /di into /ui, matches 'ss'");
2439 like($folded_string, qr/$string\N{}/i, "\\N{} after LATIN SMALL SHARP S transforms /di into /ui, matches 'ss'");
2440 }
2441
2e3a23da
KW
2442 { # Regexp:Grammars was broken:
2443 # http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2013-06/msg01290.html
2444 fresh_perl_like('use warnings; "abc" =~ qr{(?&foo){0}abc(?<foo>)}',
aaa63dae 2445 qr/Quantifier unexpected on zero-length expression/,
20e5bab4 2446 {},
2e3a23da
KW
2447 'No segfault on qr{(?&foo){0}abc(?<foo>)}');
2448 }
2449
668fcfea
TC
2450 SKIP:
2451 { # [perl #125826] buffer overflow in TRIE_STORE_REVCHAR
2452 # (during compilation, so use a fresh perl)
2453 $Config{uvsize} == 8
2454 or skip("need large code-points for this test", 1);
2455 fresh_perl_is('/\x{E000000000}|/ and print qq(ok\n)', "ok\n", {},
2456 "buffer overflow in TRIE_STORE_REVCHAR");
2457 }
2458
b57e4118 2459 # !!! NOTE that tests that aren't at all likely to crash perl should go
de085464
KW
2460 # a ways above, above these last ones. There's a comment there that, like
2461 # this comment, contains the word 'NOTE'
f4554ed5
NC
2462
2463 done_testing();
e425a60b
YO
2464} # End of sub run_tests
2465
24661;