/
pat_advanced.t
2712 lines (2385 loc) · 102 KB
/
pat_advanced.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!./perl
#
# This is a home for regular expression tests that do not fit into
# the format supported by re/regexp.t. If you want to add a test
# that does fit that format, add it to re/re_tests, not here.
BEGIN {
chdir 't' if -d 't';
require './test.pl';
set_up_inc(qw '../lib .');
require './charset_tools.pl';
skip_all_if_miniperl("miniperl can't load Tie::Hash::NamedCapture, need for %+ and %-");
}
use strict;
use warnings;
use 5.010;
our ($REGMARK, $REGERROR);
sub run_tests;
$| = 1;
run_tests() unless caller;
#
# Tests start here.
#
sub run_tests {
{
# Japhy -- added 03/03/2001
() = (my $str = "abc") =~ /(...)/;
$str = "def";
is($1, "abc", 'Changing subject does not modify $1');
}
SKIP:
{
# The trick is that in EBCDIC the explicit numeric range should
# match (as also in non-EBCDIC) but the explicit alphabetic range
# should not match.
like "\x8e", qr/[\x89-\x91]/, '"\x8e" =~ /[\x89-\x91]/';
like "\xce", qr/[\xc9-\xd1]/, '"\xce" =~ /[\xc9-\xd1]/';
like "\xd0", qr/[\xc9-\xd1]/, '"\xd0" =~ /[\xc9-\xd1]/';
skip "Not an EBCDIC platform", 2 unless ord ('i') == 0x89 &&
ord ('J') == 0xd1;
# In most places these tests would succeed since \x8e does not
# in most character sets match 'i' or 'j' nor would \xce match
# 'I' or 'J', but strictly speaking these tests are here for
# the good of EBCDIC, so let's test these only there.
unlike("\x8e", qr/[i-j]/, '"\x8e" !~ /[i-j]/');
unlike("\xce", qr/[I-J]/, '"\xce" !~ /[I-J]/');
unlike("\xd0", qr/[I-J]/, '"\xd0" !~ /[I-J]/');
}
{
like "\x{ab}", qr/\x{ab}/, '"\x{ab}" =~ /\x{ab}/ ';
like "\x{abcd}", qr/\x{abcd}/, '"\x{abcd}" =~ /\x{abcd}/';
}
{
my $message = 'bug id 20001008.001 (#4407)';
my $strasse = "stra" . uni_to_native("\337") . "e";
my @x = ("$strasse 138", "$strasse 138");
for (@x) {
ok(s/(\d+)\s*([\w\-]+)/$1 . uc $2/e, $message);
ok(my ($latin) = /^(.+)(?:\s+\d)/, $message);
is($latin, $strasse, $message);
ok($latin =~ s/$strasse/straße/, $message);
#
# Previous code follows, but outcommented - there were no tests.
#
# $latin =~ s/stra\337e/straße/; # \303\237 after the 2nd a
# use utf8; # needed for the raw UTF-8
# $latin =~ s!(s)tr(?:aß|s+e)!$1tr.!; # \303\237 after the a
}
}
{
my $message = 'Test \x escapes';
ok("ba\xd4c" =~ /([a\xd4]+)/ && $1 eq "a\xd4", $message);
ok("ba\xd4c" =~ /([a\xd4]+)/ && $1 eq "a\x{d4}", $message);
ok("ba\x{d4}c" =~ /([a\xd4]+)/ && $1 eq "a\x{d4}", $message);
ok("ba\x{d4}c" =~ /([a\xd4]+)/ && $1 eq "a\xd4", $message);
ok("ba\xd4c" =~ /([a\x{d4}]+)/ && $1 eq "a\xd4", $message);
ok("ba\xd4c" =~ /([a\x{d4}]+)/ && $1 eq "a\x{d4}", $message);
ok("ba\x{d4}c" =~ /([a\x{d4}]+)/ && $1 eq "a\x{d4}", $message);
ok("ba\x{d4}c" =~ /([a\x{d4}]+)/ && $1 eq "a\xd4", $message);
}
{
my $message = 'Match code points > 255';
$_ = "abc\x{100}\x{200}\x{300}\x{380}\x{400}defg";
ok(/(.\x{300})./, $message);
ok($` eq "abc\x{100}" && length ($`) == 4, $message);
ok($& eq "\x{200}\x{300}\x{380}" && length ($&) == 3, $message);
ok($' eq "\x{400}defg" && length ($') == 5, $message);
ok($1 eq "\x{200}\x{300}" && length ($1) == 2, $message);
}
{
my $x = "\x{10FFFD}";
$x =~ s/(.)/$1/g;
ok ord($x) == 0x10FFFD && length($x) == 1, "From Robin Houston";
}
{
my %d = (
"7f" => [0, 0, 0],
"80" => [1, 1, 0],
"ff" => [1, 1, 0],
"100" => [0, 1, 1],
);
while (my ($code, $match) = each %d) {
my $message = "Properties of \\x$code";
my $char = eval qq ["\\x{$code}"];
is(0 + ($char =~ /[\x80-\xff]/), $$match[0], $message);
is(0 + ($char =~ /[\x80-\x{100}]/), $$match[1], $message);
is(0 + ($char =~ /[\x{100}]/), $$match[2], $message);
}
}
{
# From Japhy
foreach (qw(c g o)) {
warning_like(sub {'' =~ "(?$_)"}, qr/^Useless \(\?$_\)/);
warning_like(sub {'' =~ "(?-$_)"}, qr/^Useless \(\?-$_\)/);
}
# Now test multi-error regexes
foreach (['(?g-o)', qr/^Useless \(\?g\)/, qr/^Useless \(\?-o\)/],
['(?g-c)', qr/^Useless \(\?g\)/, qr/^Useless \(\?-c\)/],
# (?c) means (?g) error won't be thrown
['(?o-cg)', qr/^Useless \(\?o\)/, qr/^Useless \(\?-c\)/],
['(?ogc)', qr/^Useless \(\?o\)/, qr/^Useless \(\?g\)/,
qr/^Useless \(\?c\)/],
) {
my ($re, @warnings) = @$_;
warnings_like(sub {eval "qr/$re/"}, \@warnings, "qr/$re/ warns");
}
}
{
my $message = "/x tests";
$_ = "foo";
foreach my $pat (<<" --", <<" --") {
/f
o\r
o
\$
/x
--
/f
o
o
\$\r
/x
--
is(eval $pat, 1, $message);
is($@, '', $message);
}
}
{
my $message = "/o feature";
sub test_o {$_ [0] =~ /$_[1]/o; return $1}
is(test_o ('abc', '(.)..'), 'a', $message);
is(test_o ('abc', '..(.)'), 'a', $message);
}
{
# Test basic $^N usage outside of a regex
my $message = '$^N usage outside of a regex';
my $x = "abcdef";
ok(($x =~ /cde/ and !defined $^N), $message);
ok(($x =~ /(cde)/ and $^N eq "cde"), $message);
ok(($x =~ /(c)(d)(e)/ and $^N eq "e"), $message);
ok(($x =~ /(c(d)e)/ and $^N eq "cde"), $message);
ok(($x =~ /(foo)|(c(d)e)/ and $^N eq "cde"), $message);
ok(($x =~ /(c(d)e)|(foo)/ and $^N eq "cde"), $message);
ok(($x =~ /(c(d)e)|(abc)/ and $^N eq "abc"), $message);
ok(($x =~ /(c(d)e)|(abc)x/ and $^N eq "cde"), $message);
ok(($x =~ /(c(d)e)(abc)?/ and $^N eq "cde"), $message);
ok(($x =~ /(?:c(d)e)/ and $^N eq "d"), $message);
ok(($x =~ /(?:c(d)e)(?:f)/ and $^N eq "d"), $message);
ok(($x =~ /(?:([abc])|([def]))*/ and $^N eq "f"), $message);
ok(($x =~ /(?:([ace])|([bdf]))*/ and $^N eq "f"), $message);
ok(($x =~ /(([ace])|([bd]))*/ and $^N eq "e"), $message);
{ok(($x =~ /(([ace])|([bdf]))*/ and $^N eq "f"), $message);}
## Test to see if $^N is automatically localized -- it should now
## have the value set in the previous test.
is($^N, "e", '$^N is automatically localized');
# Now test inside (?{ ... })
$message = '$^N usage inside (?{ ... })';
our ($y, $z);
ok(($x =~ /a([abc])(?{$y=$^N})c/ and $y eq "b"), $message);
ok(($x =~ /a([abc]+)(?{$y=$^N})d/ and $y eq "bc"), $message);
ok(($x =~ /a([abcdefg]+)(?{$y=$^N})d/ and $y eq "bc"), $message);
ok(($x =~ /(a([abcdefg]+)(?{$y=$^N})d)(?{$z=$^N})e/ and $y eq "bc"
and $z eq "abcd"), $message);
ok(($x =~ /(a([abcdefg]+)(?{$y=$^N})de)(?{$z=$^N})/ and $y eq "bc"
and $z eq "abcde"), $message);
}
SKIP:
{
## Should probably put in tests for all the POSIX stuff,
## but not sure how to guarantee a specific locale......
my $message = 'Test [[:cntrl:]]';
my $AllBytes = join "" => map {chr} 0 .. 255;
(my $x = $AllBytes) =~ s/[[:cntrl:]]//g;
$x = join "", sort { $a cmp $b }
map { chr utf8::native_to_unicode(ord $_) } split "", $x;
is($x, join("", map {chr} 0x20 .. 0x7E, 0x80 .. 0xFF), $message);
($x = $AllBytes) =~ s/[^[:cntrl:]]//g;
$x = join "", sort { $a cmp $b }
map { chr utf8::native_to_unicode(ord $_) } split "", $x;
is($x, (join "", map {chr} 0x00 .. 0x1F, 0x7F), $message);
}
{
# With /s modifier UTF8 chars were interpreted as bytes
my $message = "UTF-8 chars aren't bytes";
my $a = "Hello \x{263A} World";
my @a = ($a =~ /./gs);
is($#a, 12, $message);
}
{
no warnings 'digit';
# Check that \x## works. 5.6.1 and 5.005_03 fail some of these.
my $x;
$x = "\x4e" . "E";
like ($x, qr/^\x4EE$/, "Check only 2 bytes of hex are matched.");
$x = "\x4e" . "i";
like ($x, qr/^\x4Ei$/, "Check that invalid hex digit stops it (2)");
$x = "\x4" . "j";
like ($x, qr/^\x4j$/, "Check that invalid hex digit stops it (1)");
$x = "\x0" . "k";
like ($x, qr/^\xk$/, "Check that invalid hex digit stops it (0)");
$x = "\x0" . "x";
like ($x, qr/^\xx$/, "\\xx isn't to be treated as \\0");
$x = "\x0" . "xa";
like ($x, qr/^\xxa$/, "\\xxa isn't to be treated as \\xa");
$x = "\x9" . "_b";
like ($x, qr/^\x9_b$/, "\\x9_b isn't to be treated as \\x9b");
# and now again in [] ranges
$x = "\x4e" . "E";
like ($x, qr/^[\x4EE]{2}$/, "Check only 2 bytes of hex are matched.");
$x = "\x4e" . "i";
like ($x, qr/^[\x4Ei]{2}$/, "Check that invalid hex digit stops it (2)");
$x = "\x4" . "j";
like ($x, qr/^[\x4j]{2}$/, "Check that invalid hex digit stops it (1)");
$x = "\x0" . "k";
like ($x, qr/^[\xk]{2}$/, "Check that invalid hex digit stops it (0)");
$x = "\x0" . "x";
like ($x, qr/^[\xx]{2}$/, "\\xx isn't to be treated as \\0");
$x = "\x0" . "xa";
like ($x, qr/^[\xxa]{3}$/, "\\xxa isn't to be treated as \\xa");
$x = "\x9" . "_b";
like ($x, qr/^[\x9_b]{3}$/, "\\x9_b isn't to be treated as \\x9b");
# Check that \x{##} works. 5.6.1 fails quite a few of these.
$x = "\x9b";
like ($x, qr/^\x{9_b}$/, "\\x{9_b} is to be treated as \\x9b");
$x = "\x9b" . "y";
like ($x, qr/^\x{9_b}y$/, "\\x{9_b} is to be treated as \\x9b (again)");
$x = "\x9b" . "y";
like ($x, qr/^\x{9b_}y$/, "\\x{9b_} is to be treated as \\x9b");
$x = "\x9b" . "y";
like ($x, qr/^\x{9_bq}y$/, "\\x{9_bc} is to be treated as \\x9b");
$x = "\x0" . "y";
like ($x, qr/^\x{x9b}y$/, "\\x{x9b} is to be treated as \\x0");
$x = "\x0" . "y";
like ($x, qr/^\x{0x9b}y$/, "\\x{0x9b} is to be treated as \\x0");
$x = "\x9b" . "y";
like ($x, qr/^\x{09b}y$/, "\\x{09b} is to be treated as \\x9b");
$x = "\x9b";
like ($x, qr/^[\x{9_b}]$/, "\\x{9_b} is to be treated as \\x9b");
$x = "\x9b" . "y";
like ($x, qr/^[\x{9_b}y]{2}$/,
"\\x{9_b} is to be treated as \\x9b (again)");
$x = "\x9b" . "y";
like ($x, qr/^[\x{9b_}y]{2}$/, "\\x{9b_} is to be treated as \\x9b");
$x = "\x9b" . "y";
like ($x, qr/^[\x{9_bq}y]{2}$/, "\\x{9_bc} is to be treated as \\x9b");
$x = "\x0" . "y";
like ($x, qr/^[\x{x9b}y]{2}$/, "\\x{x9b} is to be treated as \\x0");
$x = "\x0" . "y";
like ($x, qr/^[\x{0x9b}y]{2}$/, "\\x{0x9b} is to be treated as \\x0");
$x = "\x9b" . "y";
like ($x, qr/^[\x{09b}y]{2}$/, "\\x{09b} is to be treated as \\x9b");
}
{
# High bit bug -- japhy
my $x = "ab\200d";
like $x, qr/.*?\200/, "High bit fine";
}
{
# The basic character classes and Unicode
like "\x{0100}", qr/\w/, 'LATIN CAPITAL LETTER A WITH MACRON in /\w/';
like "\x{0660}", qr/\d/, 'ARABIC-INDIC DIGIT ZERO in /\d/';
like "\x{1680}", qr/\s/, 'OGHAM SPACE MARK in /\s/';
}
{
my $message = "Folding matches and Unicode";
like("a\x{100}", qr/A/i, $message);
like("A\x{100}", qr/a/i, $message);
like("a\x{100}", qr/a/i, $message);
like("A\x{100}", qr/A/i, $message);
like("\x{101}a", qr/\x{100}/i, $message);
like("\x{100}a", qr/\x{100}/i, $message);
like("\x{101}a", qr/\x{101}/i, $message);
like("\x{100}a", qr/\x{101}/i, $message);
like("a\x{100}", qr/A\x{100}/i, $message);
like("A\x{100}", qr/a\x{100}/i, $message);
like("a\x{100}", qr/a\x{100}/i, $message);
like("A\x{100}", qr/A\x{100}/i, $message);
like("a\x{100}", qr/[A]/i, $message);
like("A\x{100}", qr/[a]/i, $message);
like("a\x{100}", qr/[a]/i, $message);
like("A\x{100}", qr/[A]/i, $message);
like("\x{101}a", qr/[\x{100}]/i, $message);
like("\x{100}a", qr/[\x{100}]/i, $message);
like("\x{101}a", qr/[\x{101}]/i, $message);
like("\x{100}a", qr/[\x{101}]/i, $message);
}
{
use charnames ':full';
my $message = "Folding 'LATIN LETTER A WITH GRAVE'";
my $lower = "\N{LATIN SMALL LETTER A WITH GRAVE}";
my $UPPER = "\N{LATIN CAPITAL LETTER A WITH GRAVE}";
like($lower, qr/$UPPER/i, $message);
like($UPPER, qr/$lower/i, $message);
like($lower, qr/[$UPPER]/i, $message);
like($UPPER, qr/[$lower]/i, $message);
$message = "Folding 'GREEK LETTER ALPHA WITH VRACHY'";
$lower = "\N{GREEK CAPITAL LETTER ALPHA WITH VRACHY}";
$UPPER = "\N{GREEK SMALL LETTER ALPHA WITH VRACHY}";
like($lower, qr/$UPPER/i, $message);
like($UPPER, qr/$lower/i, $message);
like($lower, qr/[$UPPER]/i, $message);
like($UPPER, qr/[$lower]/i, $message);
$message = "Folding 'LATIN LETTER Y WITH DIAERESIS'";
$lower = "\N{LATIN SMALL LETTER Y WITH DIAERESIS}";
$UPPER = "\N{LATIN CAPITAL LETTER Y WITH DIAERESIS}";
like($lower, qr/$UPPER/i, $message);
like($UPPER, qr/$lower/i, $message);
like($lower, qr/[$UPPER]/i, $message);
like($UPPER, qr/[$lower]/i, $message);
}
{
use charnames ':full';
my $message = "GREEK CAPITAL LETTER SIGMA vs " .
"COMBINING GREEK PERISPOMENI";
my $SIGMA = "\N{GREEK CAPITAL LETTER SIGMA}";
my $char = "\N{COMBINING GREEK PERISPOMENI}";
warning_is(sub {unlike("_:$char:_", qr/_:$SIGMA:_/i, $message)}, undef,
'Did not warn [change a5961de5f4215b5c]');
}
{
my $message = '\X';
use charnames ':full';
ok("a!" =~ /^(\X)!/ && $1 eq "a", $message);
ok("\xDF!" =~ /^(\X)!/ && $1 eq "\xDF", $message);
ok("\x{100}!" =~ /^(\X)!/ && $1 eq "\x{100}", $message);
ok("\x{100}\x{300}!" =~ /^(\X)!/ && $1 eq "\x{100}\x{300}", $message);
ok("\N{LATIN CAPITAL LETTER E}!" =~ /^(\X)!/ &&
$1 eq "\N{LATIN CAPITAL LETTER E}", $message);
ok("\N{LATIN CAPITAL LETTER E}\N{COMBINING GRAVE ACCENT}!"
=~ /^(\X)!/ &&
$1 eq "\N{LATIN CAPITAL LETTER E}\N{COMBINING GRAVE ACCENT}", $message);
}
{
my $message = "Final Sigma";
my $SIGMA = "\x{03A3}"; # CAPITAL
my $Sigma = "\x{03C2}"; # SMALL FINAL
my $sigma = "\x{03C3}"; # SMALL
like($SIGMA, qr/$SIGMA/i, $message);
like($SIGMA, qr/$Sigma/i, $message);
like($SIGMA, qr/$sigma/i, $message);
like($Sigma, qr/$SIGMA/i, $message);
like($Sigma, qr/$Sigma/i, $message);
like($Sigma, qr/$sigma/i, $message);
like($sigma, qr/$SIGMA/i, $message);
like($sigma, qr/$Sigma/i, $message);
like($sigma, qr/$sigma/i, $message);
like($SIGMA, qr/[$SIGMA]/i, $message);
like($SIGMA, qr/[$Sigma]/i, $message);
like($SIGMA, qr/[$sigma]/i, $message);
like($Sigma, qr/[$SIGMA]/i, $message);
like($Sigma, qr/[$Sigma]/i, $message);
like($Sigma, qr/[$sigma]/i, $message);
like($sigma, qr/[$SIGMA]/i, $message);
like($sigma, qr/[$Sigma]/i, $message);
like($sigma, qr/[$sigma]/i, $message);
$message = "More final Sigma";
my $S3 = "$SIGMA$Sigma$sigma";
ok(":$S3:" =~ /:(($SIGMA)+):/i && $1 eq $S3 && $2 eq $sigma, $message);
ok(":$S3:" =~ /:(($Sigma)+):/i && $1 eq $S3 && $2 eq $sigma, $message);
ok(":$S3:" =~ /:(($sigma)+):/i && $1 eq $S3 && $2 eq $sigma, $message);
ok(":$S3:" =~ /:(([$SIGMA])+):/i && $1 eq $S3 && $2 eq $sigma, $message);
ok(":$S3:" =~ /:(([$Sigma])+):/i && $1 eq $S3 && $2 eq $sigma, $message);
ok(":$S3:" =~ /:(([$sigma])+):/i && $1 eq $S3 && $2 eq $sigma, $message);
}
{
use charnames ':full';
my $message = "Parlez-Vous " .
"Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais?";
ok("Fran\N{LATIN SMALL LETTER C}ais" =~ /Fran.ais/ &&
$& eq "Francais", $message);
ok("Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" =~ /Fran.ais/ &&
$& eq "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais", $message);
ok("Fran\N{LATIN SMALL LETTER C}ais" =~ /Fran\Xais/ &&
$& eq "Francais", $message);
ok("Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" =~ /Fran\Xais/ &&
$& eq "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais", $message);
ok("Franc\N{COMBINING CEDILLA}ais" =~ /Fran\Xais/ &&
$& eq "Franc\N{COMBINING CEDILLA}ais", $message);
ok("Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais" =~
/Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais/ &&
$& eq "Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais", $message);
ok("Franc\N{COMBINING CEDILLA}ais" =~ /Franc\N{COMBINING CEDILLA}ais/ &&
$& eq "Franc\N{COMBINING CEDILLA}ais", $message);
my @f = (
["Fran\N{LATIN SMALL LETTER C}ais", "Francais"],
["Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais",
"Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais"],
["Franc\N{COMBINING CEDILLA}ais", "Franc\N{COMBINING CEDILLA}ais"],
);
foreach my $entry (@f) {
my ($subject, $match) = @$entry;
ok($subject =~ /Fran(?:c\N{COMBINING CEDILLA}?|
\N{LATIN SMALL LETTER C WITH CEDILLA})ais/x &&
$& eq $match, $message);
}
}
{
my $message = "Lingering (and useless) UTF8 flag doesn't mess up /i";
my $pat = "ABcde";
my $str = "abcDE\x{100}";
chop $str;
like($str, qr/$pat/i, $message);
$pat = "ABcde\x{100}";
$str = "abcDE";
chop $pat;
like($str, qr/$pat/i, $message);
$pat = "ABcde\x{100}";
$str = "abcDE\x{100}";
chop $pat;
chop $str;
like($str, qr/$pat/i, $message);
}
{
use charnames ':full';
my $message = "LATIN SMALL LETTER SHARP S " .
"(\N{LATIN SMALL LETTER SHARP S})";
like("\N{LATIN SMALL LETTER SHARP S}",
qr/\N{LATIN SMALL LETTER SHARP S}/, $message);
like("\N{LATIN SMALL LETTER SHARP S}",
qr'\N{LATIN SMALL LETTER SHARP S}', $message);
like("\N{LATIN SMALL LETTER SHARP S}",
qr/\N{LATIN SMALL LETTER SHARP S}/i, $message);
like("\N{LATIN SMALL LETTER SHARP S}",
qr'\N{LATIN SMALL LETTER SHARP S}'i, $message);
like("\N{LATIN SMALL LETTER SHARP S}",
qr/[\N{LATIN SMALL LETTER SHARP S}]/, $message);
like("\N{LATIN SMALL LETTER SHARP S}",
qr'[\N{LATIN SMALL LETTER SHARP S}]', $message);
like("\N{LATIN SMALL LETTER SHARP S}",
qr/[\N{LATIN SMALL LETTER SHARP S}]/i, $message);
like("\N{LATIN SMALL LETTER SHARP S}",
qr'[\N{LATIN SMALL LETTER SHARP S}]'i, $message);
like("ss", qr /\N{LATIN SMALL LETTER SHARP S}/i, $message);
like("ss", qr '\N{LATIN SMALL LETTER SHARP S}'i, $message);
like("SS", qr /\N{LATIN SMALL LETTER SHARP S}/i, $message);
like("SS", qr '\N{LATIN SMALL LETTER SHARP S}'i, $message);
like("ss", qr/[\N{LATIN SMALL LETTER SHARP S}]/i, $message);
like("ss", qr'[\N{LATIN SMALL LETTER SHARP S}]'i, $message);
like("SS", qr/[\N{LATIN SMALL LETTER SHARP S}]/i, $message);
like("SS", qr'[\N{LATIN SMALL LETTER SHARP S}]'i, $message);
like("\N{LATIN SMALL LETTER SHARP S}", qr/ss/i, $message);
like("\N{LATIN SMALL LETTER SHARP S}", qr/SS/i, $message);
$message = "Unoptimized named sequence in class";
like("ss", qr/[\N{LATIN SMALL LETTER SHARP S}x]/i, $message);
like("ss", qr'[\N{LATIN SMALL LETTER SHARP S}x]'i, $message);
like("SS", qr/[\N{LATIN SMALL LETTER SHARP S}x]/i, $message);
like("SS", qr'[\N{LATIN SMALL LETTER SHARP S}x]'i, $message);
like("\N{LATIN SMALL LETTER SHARP S}",
qr/[\N{LATIN SMALL LETTER SHARP S}x]/, $message);
like("\N{LATIN SMALL LETTER SHARP S}",
qr'[\N{LATIN SMALL LETTER SHARP S}x]', $message);
like("\N{LATIN SMALL LETTER SHARP S}",
qr/[\N{LATIN SMALL LETTER SHARP S}x]/i, $message);
like("\N{LATIN SMALL LETTER SHARP S}",
qr'[\N{LATIN SMALL LETTER SHARP S}x]'i, $message);
}
{
# More whitespace: U+0085, U+2028, U+2029\n";
# U+0085, U+00A0 need to be forced to be Unicode, the \x{100} does that.
like "<\x{100}" . uni_to_native("\x{0085}") . ">", qr/<\x{100}\s>/, '\x{0085} in \s';
like "<" . uni_to_native("\x{0085}") . ">", qr/<\v>/, '\x{0085} in \v';
like "<\x{100}" . uni_to_native("\x{00A0}") . ">", qr/<\x{100}\s>/, '\x{00A0} in \s';
like "<" . uni_to_native("\x{00A0}") . ">", qr/<\h>/, '\x{00A0} in \h';
my @h = map {sprintf "%05x" => $_} 0x01680, 0x02000 .. 0x0200A,
0x0202F, 0x0205F, 0x03000;
my @v = map {sprintf "%05x" => $_} 0x02028, 0x02029;
my @H = map {sprintf "%05x" => $_} 0x01361, 0x0200B, 0x02408, 0x02420,
0x0303F, 0xE0020, 0x180E;
my @V = map {sprintf "%05x" => $_} 0x0008A .. 0x0008D, 0x00348, 0x10100,
0xE005F, 0xE007C, 0x180E;
for my $hex (@h) {
my $str = eval qq ["<\\x{$hex}>"];
like $str, qr/<\s>/, "\\x{$hex} in \\s";
like $str, qr/<\h>/, "\\x{$hex} in \\h";
unlike $str, qr/<\v>/, "\\x{$hex} not in \\v";
}
for my $hex (@v) {
my $str = eval qq ["<\\x{$hex}>"];
like $str, qr/<\s>/, "\\x{$hex} in \\s";
like $str, qr/<\v>/, "\\x{$hex} in \\v";
unlike $str, qr/<\h>/, "\\x{$hex} not in \\h";
}
for my $hex (@H) {
my $str = eval qq ["<\\x{$hex}>"];
like $str, qr/<\S>/, "\\x{$hex} in \\S";
like $str, qr/<\H>/, "\\x{$hex} in \\H";
}
for my $hex (@V) {
my $str = eval qq ["<\\x{$hex}>"];
like $str, qr/<\S>/, "\\x{$hex} in \\S";
like $str, qr/<\V>/, "\\x{$hex} in \\V";
}
}
{
# . with /s should work on characters, as opposed to bytes
my $message = ". with /s works on characters, not bytes";
my $s = "\x{e4}\x{100}";
# This is not expected to match: the point is that
# neither should we get "Malformed UTF-8" warnings.
warning_is(sub {$s =~ /\G(.+?)\n/gcs}, undef,
"No 'Malformed UTF-8' warning");
my @c;
push @c => $1 while $s =~ /\G(.)/gs;
local $" = "";
is("@c", $s, $message);
# Test only chars < 256
my $t1 = "Q003\n\n\x{e4}\x{f6}\n\nQ004\n\n\x{e7}";
my $r1 = "";
while ($t1 =~ / \G ( .+? ) \n\s+ ( .+? ) ( $ | \n\s+ ) /xgcs) {
$r1 .= $1 . $2;
}
my $t2 = $t1 . "\x{100}"; # Repeat with a larger char
my $r2 = "";
while ($t2 =~ / \G ( .+? ) \n\s+ ( .+? ) ( $ | \n\s+ ) /xgcs) {
$r2 .= $1 . $2;
}
$r2 =~ s/\x{100}//;
is($r1, $r2, $message);
}
{
my $message = "Unicode lookbehind";
like("A\x{100}B", qr/(?<=A.)B/, $message);
like("A\x{200}\x{300}B", qr/(?<=A..)B/, $message);
like("\x{400}AB", qr/(?<=\x{400}.)B/, $message);
like("\x{500}\x{600}B", qr/(?<=\x{500}.)B/, $message);
# Original code also contained:
# ok "\x{500\x{600}}B" =~ /(?<=\x{500}.)B/;
# but that looks like a typo.
}
{
my $message = 'UTF-8 hash keys and /$/';
# http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters
# /2002-01/msg01327.html
my $u = "a\x{100}";
my $v = substr ($u, 0, 1);
my $w = substr ($u, 1, 1);
my %u = ($u => $u, $v => $v, $w => $w);
for (keys %u) {
my $m1 = /^\w*$/ ? 1 : 0;
my $m2 = $u {$_} =~ /^\w*$/ ? 1 : 0;
is($m1, $m2, $message);
}
}
{
my $message = "No SEGV in s/// and UTF-8";
my $s = "s#\x{100}" x 4;
ok($s =~ s/[^\w]/ /g, $message);
if ( 1 or $ENV{PERL_TEST_LEGACY_POSIX_CC} ) {
is($s, "s \x{100}" x 4, $message);
}
else {
is($s, "s " x 4, $message);
}
}
{
my $message = "UTF-8 bug (maybe already known?)";
my $u = "foo";
$u =~ s/./\x{100}/g;
is($u, "\x{100}\x{100}\x{100}", $message);
$u = "foobar";
$u =~ s/[ao]/\x{100}/g;
is($u, "f\x{100}\x{100}b\x{100}r", $message);
$u =~ s/\x{100}/e/g;
is($u, "feeber", $message);
}
{
my $message = "UTF-8 bug with s///";
# check utf8/non-utf8 mixtures
# try to force all float/anchored check combinations
my $c = "\x{100}";
my $subst;
for my $re ("xx.*$c", "x.*$c$c", "$c.*xx", "$c$c.*x",
"xx.*(?=$c)", "(?=$c).*xx",) {
unlike("xxx", qr/$re/, $message);
ok(+($subst = "xxx") !~ s/$re//, $message);
}
for my $re ("xx.*$c*", "$c*.*xx") {
like("xxx", qr/$re/, $message);
ok(+($subst = "xxx") =~ s/$re//, $message);
is($subst, "", $message);
}
for my $re ("xxy*", "y*xx") {
like("xx$c", qr/$re/, $message);
ok(+($subst = "xx$c") =~ s/$re//, $message);
is($subst, $c, $message);
unlike("xy$c", qr/$re/, $message);
ok(+($subst = "xy$c") !~ s/$re//, $message);
}
for my $re ("xy$c*z", "x$c*yz") {
like("xyz", qr/$re/, $message);
ok(+($subst = "xyz") =~ s/$re//, $message);
is($subst, "", $message);
}
}
{
# The second half of RT #114808
warning_is(sub {'aa' =~ /.+\x{100}/}, undef,
'utf8-only floating substr, non-utf8 target, no warning');
}
{
my $message = "qr /.../x";
my $R = qr / A B C # D E/x;
ok("ABCDE" =~ $R && $& eq "ABC", $message);
ok("ABCDE" =~ /$R/ && $& eq "ABC", $message);
ok("ABCDE" =~ m/$R/ && $& eq "ABC", $message);
ok("ABCDE" =~ /($R)/ && $1 eq "ABC", $message);
ok("ABCDE" =~ m/($R)/ && $1 eq "ABC", $message);
}
{
local $\;
$_ = 'aaaaaaaaaa';
utf8::upgrade($_); chop $_; $\="\n";
ok /[^\s]+/, 'm/[^\s]/ utf8';
ok /[^\d]+/, 'm/[^\d]/ utf8';
ok +($a = $_, $_ =~ s/[^\s]+/./g), 's/[^\s]/ utf8';
ok +($a = $_, $a =~ s/[^\d]+/./g), 's/[^\s]/ utf8';
}
{
# Subject: Odd regexp behavior
# From: Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>
# Date: Wed, 26 Feb 2003 16:53:12 +0000
# Message-Id: <E18o4nw-0008Ly-00@wisbech.cl.cam.ac.uk>
# To: perl-unicode@perl.org
my $message = 'Markus Kuhn 2003-02-26';
my $x = "\x{2019}\nk";
ok($x =~ s/(\S)\n(\S)/$1 $2/sg, $message);
is($x, "\x{2019} k", $message);
$x = "b\nk";
ok($x =~ s/(\S)\n(\S)/$1 $2/sg, $message);
is($x, "b k", $message);
like("\x{2019}", qr/\S/, $message);
}
{
like "\x{100}\n", qr/\x{100}\n$/, "UTF-8 length cache and fbm_compile";
}
{
package Str;
use overload q /""/ => sub {${$_ [0]};};
sub new {my ($c, $v) = @_; bless \$v, $c;}
package main;
$_ = Str -> new ("a\x{100}/\x{100}b");
ok join (":", /\b(.)\x{100}/g) eq "a:/", "re_intuit_start and PL_bostr";
}
{
my $re = qq /^([^X]*)X/;
utf8::upgrade ($re);
like "\x{100}X", qr/$re/, "S_cl_and ANYOF_UNICODE & ANYOF_INVERTED";
my $loc_re = qq /(?l:^([^X]*)X)/;
utf8::upgrade ($loc_re);
no warnings 'locale';
like "\x{100}X", qr/$loc_re/, "locale, S_cl_and ANYOF_UNICODE & ANYOF_INVERTED";
}
{
like "123\x{100}", qr/^.*1.*23\x{100}$/,
'UTF-8 + multiple floating substr';
}
{
my $message = '<20030808193656.5109.1@llama.ni-s.u-net.com>';
# LATIN SMALL/CAPITAL LETTER A WITH MACRON
like(" \x{101}", qr/\x{100}/i, $message);
# LATIN SMALL/CAPITAL LETTER A WITH RING BELOW
like(" \x{1E01}", qr/\x{1E00}/i, $message);
# DESERET SMALL/CAPITAL LETTER LONG I
like(" \x{10428}", qr/\x{10400}/i, $message);
# LATIN SMALL/CAPITAL LETTER A WITH RING BELOW + 'X'
like(" \x{1E01}x", qr/\x{1E00}X/i, $message);
}
{
for (120 .. 130, 240 .. 260) {
my $head = 'x' x $_;
my $message = q [Don't misparse \x{...} in regexp ] .
q [near EXACT char count limit];
for my $tail ('\x{0061}', '\x{1234}', '\x61') {
eval qq{like("$head$tail", qr/$head$tail/, \$message)};
is($@, '', $message);
}
$message = q [Don't misparse \N{...} in regexp ] .
q [near EXACT char count limit];
for my $tail ('\N{SNOWFLAKE}') {
eval qq {use charnames ':full';
like("$head$tail", qr/$head$tail/, \$message)};
eval qq {use charnames ':full';
like("$head$tail", qr'$head$tail', \$message)};
is($@, '', $message);
}
}
}
{ # TRIE related
our @got = ();
"words" =~ /(word|word|word)(?{push @got, $1})s$/;
is(@got, 1, "TRIE optimisation");
@got = ();
"words" =~ /(word|word|word)(?{push @got,$1})s$/i;
is(@got, 1,"TRIEF optimisation");
my @nums = map {int rand 1000} 1 .. 100;
my $re = "(" . (join "|", @nums) . ")";
$re = qr/\b$re\b/;
foreach (@nums) {
like $_, qr/$re/, "Trie nums";
}
$_ = join " ", @nums;
@got = ();
push @got, $1 while /$re/g;
my %count;
$count {$_} ++ for @got;
my $ok = 1;
for (@nums) {
$ok = 0 if --$count {$_} < 0;
}
ok $ok, "Trie min count matches";
}
{
# TRIE related
# LATIN SMALL/CAPITAL LETTER A WITH MACRON
ok "foba \x{101}foo" =~ qr/(foo|\x{100}foo|bar)/i &&
$1 eq "\x{101}foo",
"TRIEF + LATIN SMALL/CAPITAL LETTER A WITH MACRON";
# LATIN SMALL/CAPITAL LETTER A WITH RING BELOW
ok "foba \x{1E01}foo" =~ qr/(foo|\x{1E00}foo|bar)/i &&
$1 eq "\x{1E01}foo",
"TRIEF + LATIN SMALL/CAPITAL LETTER A WITH RING BELOW";
# DESERET SMALL/CAPITAL LETTER LONG I
ok "foba \x{10428}foo" =~ qr/(foo|\x{10400}foo|bar)/i &&
$1 eq "\x{10428}foo",
"TRIEF + DESERET SMALL/CAPITAL LETTER LONG I";
# LATIN SMALL/CAPITAL LETTER A WITH RING BELOW + 'X'
ok "foba \x{1E01}xfoo" =~ qr/(foo|\x{1E00}Xfoo|bar)/i &&
$1 eq "\x{1E01}xfoo",
"TRIEF + LATIN SMALL/CAPITAL LETTER A WITH RING BELOW + 'X'";
use charnames ':full';
my $s = "\N{LATIN SMALL LETTER SHARP S}";
ok "foba ba$s" =~ qr/(foo|Ba$s|bar)/i && $1 eq "ba$s",
"TRIEF + LATIN SMALL LETTER SHARP S =~ ss";
ok "foba ba$s" =~ qr/(Ba$s|foo|bar)/i && $1 eq "ba$s",
"TRIEF + LATIN SMALL LETTER SHARP S =~ ss";
ok "foba ba$s" =~ qr/(foo|bar|Ba$s)/i && $1 eq "ba$s",
"TRIEF + LATIN SMALL LETTER SHARP S =~ ss";
ok "foba ba$s" =~ qr/(foo|Bass|bar)/i && $1 eq "ba$s",
"TRIEF + LATIN SMALL LETTER SHARP S =~ ss";
ok "foba ba$s" =~ qr/(foo|BaSS|bar)/i && $1 eq "ba$s",
"TRIEF + LATIN SMALL LETTER SHARP S =~ SS";
ok "foba ba${s}pxySS$s$s" =~ qr/(b(?:a${s}t|a${s}f|a${s}p)[xy]+$s*)/i
&& $1 eq "ba${s}pxySS$s$s",
"COMMON PREFIX TRIEF + LATIN SMALL LETTER SHARP S";
}
{
BEGIN {
unshift @INC, 'lib';
}
use Cname; # Our custom charname plugin, currently found in
# t/lib/Cname.pm
like 'fooB', qr/\N{foo}[\N{B}\N{b}]/, "Passthrough charname";
my $name = "foo\xDF";
my $result = eval "'A${name}B' =~ /^A\\N{$name}B\$/";
ok !$@ && $result, "Passthrough charname of non-ASCII, Latin1";
eval "qr/\\p{name=foo}/";
like($@, qr/Can't find Unicode property definition "name=foo"/,
'\p{name=} doesn\'t see a cumstom charnames translator');
#
# Why doesn't must_warn work here?
#
my $w;
local $SIG {__WARN__} = sub {$w .= "@_"};
$result = eval 'q(WARN) =~ /^[\N{WARN}]$/';
ok !$@ && $result && ! $w, '\N{} returning multi-char works';
undef $w;
eval q [unlike "\0", qr/[\N{EMPTY-STR}XY]/,
"Zerolength charname in charclass doesn't match \\\\0"];
ok $w && $w =~ /Ignoring zero length/,
'Ignoring zero length \N{} in character class warning';
undef $w;
eval q [like 'xy', qr/x[\N{EMPTY-STR} y]/x,
'Empty string charname in [] is ignored; finds a following character'];
ok $w && $w =~ /Ignoring zero length/,
'Ignoring zero length \N{} in character class warning';
undef $w;
eval q [like 'x ', qr/x[\N{EMPTY-STR} y]/,
'Empty string charname in [] is ignored; finds a following blank under /x'];
like $w, qr/Ignoring zero length/,
'Ignoring zero length \N{} in character class warning';
# EVIL keeps track of its calls, and appends a new character each
# time: A AB ABC ABCD ...
ok 'AB' =~ /(\N{EVIL})/ && $1 eq 'A', 'Charname caching $1';
like 'ABC', qr/(\N{EVIL})/, 'Charname caching $1';
ok 'ABCD' =~ m'(\N{EVIL})' && $1 eq 'ABC', 'Charname caching $1';
ok 'ABCDE' =~ m'(\N{EVIL})', 'Charname caching $1';
like 'xy', qr/x\N{EMPTY-STR}y/,
'Empty string charname produces NOTHING node';
ok 'xy' =~ 'x\N{EMPTY-STR}y',
'Empty string charname produces NOTHING node';
like '', qr/\N{EMPTY-STR}/,
'Empty string charname produces NOTHING node';
like "\N{LONG-STR}", qr/^\N{LONG-STR}$/, 'Verify that long string works';
like "\N{LONG-STR}", qr/^\N{LONG-STR}$/i, 'Verify under folding that long string works';
# perlhacktips points out that these work on both ASCII and EBCDIC
like "\xfc", qr/\N{EMPTY-STR}\xdc/i, 'Empty \N{} should change /d to /u';
like "\xfc", qr'\N{EMPTY-STR}\xdc'i, 'Empty \N{} should change /d to /u';
eval '/(?[[\N{EMPTY-STR}]])/';
like $@, qr/Zero length \\N\{\}/, 'Verify zero-length return from \N{} correctly fails';
ok "\N{LONG-STR}" =~ /^\N{LONG-STR}$/, 'Verify that long string works';
ok "\N{LONG-STR}" =~ '^\N{LONG-STR}$', 'Verify that long string works';
ok "\N{LONG-STR}" =~ /^\N{LONG-STR}$/i, 'Verify under folding that long string works';
ok "\N{LONG-STR}" =~ m'^\N{LONG-STR}$'i, 'Verify under folding that long string works';
undef $w;
{
() = eval q ["\N{TOO MANY SPACES}"];
like ($@, qr/charnames alias definitions may not contain a sequence of multiple spaces/, "Multiple spaces in a row in a charnames alias is fatal");
eval q [use utf8; () = "\N{TOO MANY SPACES}"];
like ($@, qr/charnames alias definitions may not contain a sequence of multiple spaces/, "... same under utf8");
}
undef $w;
my $Cedilla_Latin1 = "GAR"
. uni_to_native("\xC7")