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