This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
reg_fold.t: Allow to work on early Unicodes
[perl5.git] / t / re / reg_fold.t
CommitLineData
24df86f6
RGS
1#!perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
0214bff6 6 require './test.pl';
164766b2 7 skip_all_if_miniperl("no dynamic loading on miniperl, no File::Spec");
24df86f6
RGS
8}
9
a0a388a1
YO
10use strict;
11use warnings;
a0a388a1 12my @tests;
a0a388a1
YO
13
14my $file="../lib/unicore/CaseFolding.txt";
10329c47
KW
15my @folds;
16use Unicode::UCD;
17
18# Use the Unicode data file if we are on an ASCII platform (which its data is
19# for), and it is in the modern format (starting in Unicode 3.1.0) and it is
20# available. This avoids being affected by potential bugs introduced by other
21# layers of Perl
22if (ord('A') == 65
23 && pack "C*", split /\./, Unicode::UCD::UnicodeVersion() ge v3.1.0
24 && open my $fh, "<", $file)
25{
26 @folds = <$fh>;
27}
28else {
29 my ($invlist_ref, $invmap_ref, undef, $default)
30 = Unicode::UCD::prop_invmap('Case_Folding');
31 for my $i (0 .. @$invlist_ref - 1 - 1) {
32 next if $invmap_ref->[$i] == $default;
33 my $adjust = -1;
34 for my $j ($invlist_ref->[$i] .. $invlist_ref->[$i+1] -1) {
35 $adjust++;
36
37 # Single-code point maps go to a 'C' type
38 if (! ref $invmap_ref->[$i]) {
39 push @folds, sprintf("%04X; C; %04X\n",
40 $j,
41 $invmap_ref->[$i] + $adjust);
42 }
43 else { # Multi-code point maps go to 'F'. prop_invmap()
44 # guarantees that no adjustment is needed for these,
45 # as the range will contain just one element
46 push @folds, sprintf("%04X; F; %s\n",
47 $j,
48 join " ", map { sprintf "%04X", $_ }
49 @{$invmap_ref->[$i]});
50 }
51 }
52 }
53}
54
55for (@folds) {
a0a388a1
YO
56 chomp;
57 my ($line,$comment)= split/\s+#\s+/, $_;
10329c47 58 $comment = "" unless defined $comment;
1443f10d 59 my ($cp,$type,@folded)=split/[\s;]+/,$line||'';
a0a388a1 60 next unless $type and ($type eq 'F' or $type eq 'C');
1443f10d
KW
61 my $fold_above_latin1 = grep { hex("0x$_") > 255 } @folded;
62 $_="\\x{$_}" for @folded;
a0a388a1 63 my $cpv=hex("0x$cp");
1443f10d 64 my $chr="\\x{$cp}";
a0a388a1 65 my @str;
1443f10d 66 foreach my $swap (0, 1) { # swap lhs and rhs, or not.
8edd0c7f 67 foreach my $charclass (0, 1) { # Put rhs in [...], or not
1443f10d
KW
68 my $lhs;
69 my $rhs;
70 if ($swap) {
71 $lhs = join "", @folded;
72 $rhs = $chr;
73 $rhs = "[$rhs]" if $charclass;
74 } else {
8edd0c7f 75 #next if $charclass && @folded > 1;
1443f10d
KW
76 $lhs = $chr;
77 $rhs = "";
78 foreach my $rhs_char (@folded) {
79 $rhs .= '[' if $charclass;
80 $rhs .= $rhs_char;
81 $rhs .= ']' if $charclass;
82 }
83 }
84 $lhs = "\"$lhs\"";
6cdc48f8 85 $rhs = "/^$rhs\$/iu";
24df86f6 86
1443f10d
KW
87 # Try both Latin1 and Unicode for code points below 256
88 foreach my $upgrade ("", 'utf8::upgrade($c); ') {
59aef945
KW
89 if ($upgrade) { # No need to upgrade if already must be in
90 # utf8
1443f10d
KW
91 next if $swap && $fold_above_latin1;
92 next if !$swap && $cpv > 255;
93 }
94 my $eval = "my \$c = $lhs; $upgrade\$c =~ $rhs";
95 #print __LINE__, ": $eval\n";
96 push @tests, qq[ok(eval '$eval', '$eval - $comment')];
67fcf67c 97 if (! $swap && $charclass && @folded > 1)
2f7760b5 98 {
67fcf67c 99 $tests[-1]="TODO: { local \$::TODO='A multi-char fold \"foo\", doesnt work for /[f][o][o]/i';\n$tests[-1] }"
1443f10d 100 }
1443f10d
KW
101 }
102 }
a0a388a1 103 }
24df86f6 104}
2726813d 105
fad448f4
KW
106# Now verify the case folding tables. First compute the mappings without
107# resorting to the functions we're testing.
108
109# Initialize the array so each $i maps to itself.
110my @fold_ascii;
111for my $i (0 .. 255) {
112 $fold_ascii[$i] = $i;
113}
114my @fold_latin1 = @fold_ascii;
115
116# Override the uppercase elements to fold to their lower case equivalents,
117# using the fact that 'A' in ASCII is 0x41, 'a' is 0x41+32, 'B' is 0x42, and
118# so on. The same paradigm applies for most of the Latin1 range cased
119# characters, but in posix anything outside ASCII maps to itself, as we've
120# already set up.
121for my $i (0x41 .. 0x5A, 0xC0 .. 0xD6, 0xD8 .. 0xDE) {
122 my $upper_ord = ord_latin1_to_native($i);
123 my $lower_ord = ord_latin1_to_native($i + 32);
124
125 $fold_latin1[$upper_ord] = $lower_ord;
126
127 next if $i > 127;
128 $fold_ascii[$upper_ord] = $lower_ord;
129}
130
131# Same for folding lower to the upper equivalents
132for my $i (0x61 .. 0x7A, 0xE0 .. 0xF6, 0xF8 .. 0xFE) {
133 my $lower_ord = ord_latin1_to_native($i);
134 my $upper_ord = ord_latin1_to_native($i - 32);
135
136 $fold_latin1[$lower_ord] = $upper_ord;
137
138 next if $i > 127;
139 $fold_ascii[$lower_ord] = $upper_ord;
140}
141
59aef945 142# Test every latin1 character for the correct values in both /u and /d
fad448f4
KW
143for my $i (0 .. 255) {
144 my $chr = sprintf "\\x%02X", $i;
145 my $hex_fold_ascii = sprintf "0x%02X", $fold_ascii[$i];
146 my $hex_fold_latin1 = sprintf "0x%02X", $fold_latin1[$i];
147 push @tests, qq[like chr($hex_fold_ascii), qr/(?d:$chr)/i, 'chr($hex_fold_ascii) =~ qr/(?d:$chr)/i'];
fad448f4 148 push @tests, qq[like chr($hex_fold_latin1), qr/(?u:$chr)/i, 'chr($hex_fold_latin1) =~ qr/(?u:$chr)/i'];
fad448f4
KW
149}
150
151
2726813d 152push @tests, qq[like chr(0x0430), qr/[=\x{0410}-\x{0411}]/i, 'Bug #71752 Unicode /i char in a range'];
2726813d 153push @tests, qq[like 'a', qr/\\p{Upper}/i, "'a' =~ /\\\\p{Upper}/i"];
8951c461 154push @tests, q[my $c = "\x{212A}"; my $p = qr/(?:^[\x{004B}_]+$)/i; utf8::upgrade($p); like $c, $p, 'Bug #78994: my $c = "\x{212A}"; my $p = qr/(?:^[\x{004B}_]+$)/i; utf8::upgrade($p); $c =~ $p'];
2726813d 155
7b98bc43
KW
156use charnames ":full";
157push @tests, q[my $re1 = "\N{WHITE SMILING FACE}";like "\xE8", qr/[\w$re1]/, 'my $re = "\N{WHITE SMILING FACE}"; "\xE8" =~ qr/[\w$re]/'];
158push @tests, q[my $re2 = "\N{WHITE SMILING FACE}";like "\xE8", qr/\w|$re2/, 'my $re = "\N{WHITE SMILING FACE}"; "\xE8" =~ qr/\w|$re/'];
159
b2a1b324 160eval join ";\n","plan tests=>". (scalar @tests), @tests, "1"
a0a388a1
YO
161 or die $@;
162__DATA__