This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Resort MANIFEST - Latest Module::Build MANIFEST update broke sorting.
[perl5.git] / lib / utf8_heavy.pl
CommitLineData
a0ed51b3 1package utf8;
cf25bb62
JH
2use strict;
3use warnings;
a0ed51b3 4
15732964 5sub DEBUG () { 0 }
a0ed51b3
LW
6
7sub DESTROY {}
8
5beb625e
JH
9my %Cache;
10
12ac2576
JP
11our (%PropertyAlias, %PA_reverse, %PropValueAlias, %PVA_reverse, %PVA_abbr_map);
12
86916d66
JH
13sub croak { require Carp; Carp::croak(@_) }
14
5beb625e 15##
3a2263fe
RGS
16## "SWASH" == "SWATCH HASH". A "swatch" is a swatch of the Unicode landscape.
17## It's a data structure that encodes a set of Unicode characters.
5beb625e
JH
18##
19
a0ed51b3
LW
20sub SWASHNEW {
21 my ($class, $type, $list, $minbits, $none) = @_;
22 local $^D = 0 if $^D;
15732964
JH
23
24 print STDERR "SWASHNEW @_\n" if DEBUG;
25
cf25bb62
JH
26 ##
27 ## Get the list of codepoints for the type.
711a919c
ST
28 ## Called from swash_init (see utf8.c) or SWASHNEW itself.
29 ##
30 ## Callers of swash_init:
31 ## op.c:pmtrans -- for tr/// and y///
32 ## regexec.c:regclass_swash -- for /[]/, \p, and \P
33 ## utf8.c:is_utf8_common -- for common Unicode properties
34 ## utf8.c:to_utf8_case -- for lc, uc, ucfirst, etc. and //i
cf25bb62
JH
35 ##
36 ## Given a $type, our goal is to fill $list with the set of codepoint
711a919c
ST
37 ## ranges. If $type is false, $list passed is used.
38 ##
39 ## $minbits:
40 ## For binary properties, $minbits must be 1.
41 ## For character mappings (case and transliteration), $minbits must
42 ## be a number except 1.
43 ##
44 ## $list (or that filled according to $type):
45 ## Refer to perlunicode.pod, "User-Defined Character Properties."
46 ##
47 ## For binary properties, only characters with the property value
48 ## of True should be listed. The 3rd column, if any, will be ignored.
cf25bb62
JH
49 ##
50 ## To make the parsing of $type clear, this code takes the a rather
2d92f8a0 51 ## unorthodox approach of last'ing out of the block once we have the
cf25bb62
JH
52 ## info we need. Were this to be a subroutine, the 'last' would just
53 ## be a 'return'.
54 ##
5beb625e
JH
55 my $file; ## file to load data from, and also part of the %Cache key.
56 my $ListSorted = 0;
57
cf25bb62
JH
58 if ($type)
59 {
60 $type =~ s/^\s+//;
61 $type =~ s/\s+$//;
62
711a919c 63 print STDERR "type = $type\n" if DEBUG;
cf25bb62 64
cf25bb62
JH
65 GETFILE:
66 {
12ac2576
JP
67 ##
68 ## It could be a user-defined property.
69 ##
70
71 my $caller1 = $type =~ s/(.+)::// ? $1 : caller(1);
72
73 if (defined $caller1 && $type =~ /^(?:\w+)$/) {
74 my $prop = "${caller1}::$type";
75 if (exists &{$prop}) {
76 no strict 'refs';
77
78 $list = &{$prop};
79 last GETFILE;
80 }
81 }
82
2d92f8a0
JH
83 my $wasIs;
84
85 ($wasIs = $type =~ s/^Is(?:\s+|[-_])?//i)
5beb625e 86 or
12ac2576 87 $type =~ s/^(?:(?:General(?:\s+|_)?)?Category|gc)\s*[:=]\s*//i
5beb625e 88 or
12ac2576 89 $type =~ s/^(?:Script|sc)\s*[:=]\s*//i
5beb625e 90 or
12ac2576
JP
91 $type =~ s/^Block\s*[:=]\s*/In/i;
92
93
94 ##
95 ## See if it's in some enumeration.
96 ##
7ebf06b3 97 require "unicore/PVA.pl";
12ac2576 98 if ($type =~ /^([\w\s]+)[:=]\s*(.*)/) {
12ac2576
JP
99 my ($enum, $val) = (lc $1, lc $2);
100 $enum =~ tr/ _-//d;
101 $val =~ tr/ _-//d;
102
103 my $pa = $PropertyAlias{$enum} ? $enum : $PA_reverse{$enum};
104 my $f = $PropValueAlias{$pa}{$val} ? $val : $PVA_reverse{$pa}{lc $val};
105
106 if ($pa and $f) {
107 $pa = "gc_sc" if $pa eq "gc" or $pa eq "sc";
108 $file = "unicore/lib/$pa/$PVA_abbr_map{$pa}{lc $f}.pl";
109 last GETFILE;
110 }
111 }
112 else {
113 my $t = lc $type;
114 $t =~ tr/ _-//d;
115
116 if ($PropValueAlias{gc}{$t} or $PropValueAlias{sc}{$t}) {
117 $file = "unicore/lib/gc_sc/$PVA_abbr_map{gc_sc}{$t}.pl";
118 last GETFILE;
119 }
120 }
cf25bb62 121
5beb625e
JH
122 ##
123 ## See if it's in the direct mapping table.
124 ##
125 require "unicore/Exact.pl";
126 if (my $base = $utf8::Exact{$type}) {
12ac2576 127 $file = "unicore/lib/gc_sc/$base.pl";
5beb625e 128 last GETFILE;
cf25bb62
JH
129 }
130
131 ##
5beb625e
JH
132 ## If not there exactly, try the canonical form. The canonical
133 ## form is lowercased, with any separators (\s+|[-_]) removed.
cf25bb62 134 ##
5beb625e
JH
135 my $canonical = lc $type;
136 $canonical =~ s/(?<=[a-z\d])(?:\s+|[-_])(?=[a-z\d])//g;
711a919c 137 print STDERR "canonical = $canonical\n" if DEBUG;
cf25bb62 138
5beb625e 139 require "unicore/Canonical.pl";
0a0ffbce 140 { no warnings "uninitialized";
12ac2576
JP
141 if (my $base = ($utf8::Canonical{$canonical} || $utf8::Canonical{ lc $utf8::PropertyAlias{$canonical} })) {
142 $file = "unicore/lib/gc_sc/$base.pl";
5beb625e 143 last GETFILE;
cf25bb62 144 }
0a0ffbce 145 }
cf25bb62 146
491fd90a 147 ##
3a2263fe
RGS
148 ## See if it's a user-level "To".
149 ##
150
151 my $caller0 = caller(0);
152
153 if (defined $caller0 && $type =~ /^To(?:\w+)$/) {
154 my $map = $caller0 . "::" . $type;
09e0265a 155
3a2263fe
RGS
156 if (exists &{$map}) {
157 no strict 'refs';
158
159 $list = &{$map};
160 last GETFILE;
161 }
162 }
163
cf25bb62 164 ##
3a2263fe
RGS
165 ## Last attempt -- see if it's a standard "To" name
166 ## (e.g. "ToLower") ToTitle is used by ucfirst().
167 ## The user-level way to access ToDigit() and ToFold()
168 ## is to use Unicode::UCD.
cf25bb62 169 ##
711a919c 170 if ($type =~ /^To(Digit|Fold|Lower|Title|Upper)$/) {
cf25bb62
JH
171 $file = "unicore/To/$1.pl";
172 ## would like to test to see if $file actually exists....
173 last GETFILE;
174 }
175
176 ##
177 ## If we reach this line, it's because we couldn't figure
178 ## out what to do with $type. Ouch.
179 ##
bc45ce41
JH
180
181 return $type;
cf25bb62
JH
182 }
183
491fd90a 184 if (defined $file) {
711a919c 185 print STDERR "found it (file='$file')\n" if DEBUG;
491fd90a
JH
186
187 ##
188 ## If we reach here, it was due to a 'last GETFILE' above
3a2263fe 189 ## (exception: user-defined properties and mappings), so we
491fd90a
JH
190 ## have a filename, so now we load it if we haven't already.
191 ## If we have, return the cached results. The cache key is the
c49d5ed7 192 ## class and file to load.
491fd90a 193 ##
c49d5ed7
JS
194 my $found = $Cache{$class, $file};
195 if ($found and ref($found) eq $class) {
711a919c 196 print STDERR "Returning cached '$file' for \\p{$type}\n" if DEBUG;
c49d5ed7 197 return $found;
491fd90a
JH
198 }
199
2096afc4
RGS
200 local $@;
201 local $!;
e81465be 202 $list = do $file; die $@ if $@;
491fd90a 203 }
5beb625e 204
5beb625e 205 $ListSorted = 1; ## we know that these lists are sorted
886ba366 206 }
a0ed51b3 207
15732964 208 my $extras;
711a919c 209 my $bits = $minbits;
cf25bb62 210
5beb625e 211 my $ORIG = $list;
a0ed51b3
LW
212 if ($list) {
213 my @tmp = split(/^/m, $list);
214 my %seen;
db376a24 215 no warnings;
a0ed51b3
LW
216 $extras = join '', grep /^[^0-9a-fA-F]/, @tmp;
217 $list = join '',
df68b396
JH
218 map { $_->[1] }
219 sort { $a->[0] <=> $b->[0] }
075d4edd 220 map { /^([0-9a-fA-F]+)/; [ CORE::hex($1), $_ ] }
df68b396 221 grep { /^([0-9a-fA-F]+)/ and not $seen{$1}++ } @tmp; # XXX doesn't do ranges right
a0ed51b3
LW
222 }
223
224 if ($none) {
225 my $hextra = sprintf "%04x", $none + 1;
226 $list =~ s/\tXXXX$/\t$hextra/mg;
227 }
228
711a919c 229 if ($minbits != 1 && $minbits < 32) { # not binary property
a0ed51b3 230 my $top = 0;
3a2263fe 231 while ($list =~ /^([0-9a-fA-F]+)(?:[\t]([0-9a-fA-F]+)?)(?:[ \t]([0-9a-fA-F]+))?/mg) {
075d4edd
RGS
232 my $min = CORE::hex $1;
233 my $max = defined $2 ? CORE::hex $2 : $min;
234 my $val = defined $3 ? CORE::hex $3 : 0;
a0ed51b3
LW
235 $val += $max - $min if defined $3;
236 $top = $val if $val > $top;
237 }
711a919c 238 my $topbits =
a0ed51b3 239 $top > 0xffff ? 32 :
711a919c
ST
240 $top > 0xff ? 16 : 8;
241 $bits = $topbits if $bits < $topbits;
a0ed51b3 242 }
a0ed51b3
LW
243
244 my @extras;
245 for my $x ($extras) {
246 pos $x = 0;
1fef36c7 247 while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
a0ed51b3
LW
248 my $char = $1;
249 my $name = $2;
6d6ae962 250 print STDERR "$1 => $2\n" if DEBUG;
09e0265a 251 if ($char =~ /[-+!&]/) {
a0ed51b3 252 my ($c,$t) = split(/::/, $name, 2); # bogus use of ::, really
11ef8fdd
JH
253 my $subobj;
254 if ($c eq 'utf8') {
711a919c 255 $subobj = utf8->SWASHNEW($t, "", $minbits, 0);
09e0265a
JP
256 }
257 elsif (exists &$name) {
711a919c 258 $subobj = utf8->SWASHNEW($name, "", $minbits, 0);
11ef8fdd
JH
259 }
260 elsif ($c =~ /^([0-9a-fA-F]+)/) {
711a919c 261 $subobj = utf8->SWASHNEW("", $c, $minbits, 0);
11ef8fdd 262 }
bc45ce41 263 return $subobj unless ref $subobj;
a0ed51b3
LW
264 push @extras, $name => $subobj;
265 $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
266 }
267 }
268 }
269
15732964 270 print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG;
a0ed51b3 271
5beb625e 272 my $SWASH = bless {
a0ed51b3
LW
273 TYPE => $type,
274 BITS => $bits,
275 EXTRAS => $extras,
276 LIST => $list,
277 NONE => $none,
278 @extras,
279 } => $class;
5beb625e
JH
280
281 if ($file) {
282 $Cache{$class, $file} = $SWASH;
283 }
284
285 return $SWASH;
a0ed51b3
LW
286}
287
979f2922 288# Now SWASHGET is recasted into a C function S_swash_get (see utf8.c).
a0ed51b3
LW
289
2901;