Commit | Line | Data |
a0ed51b3 |
1 | package utf8; |
cf25bb62 |
2 | use strict; |
3 | use warnings; |
a0ed51b3 |
4 | |
15732964 |
5 | sub DEBUG () { 0 } |
a0ed51b3 |
6 | |
7 | sub DESTROY {} |
8 | |
5beb625e |
9 | my %Cache; |
10 | |
12ac2576 |
11 | our (%PropertyAlias, %PA_reverse, %PropValueAlias, %PVA_reverse, %PVA_abbr_map); |
12 | |
86916d66 |
13 | sub croak { require Carp; Carp::croak(@_) } |
14 | |
5beb625e |
15 | ## |
3a2263fe |
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 |
18 | ## |
19 | |
a0ed51b3 |
20 | sub SWASHNEW { |
21 | my ($class, $type, $list, $minbits, $none) = @_; |
22 | local $^D = 0 if $^D; |
15732964 |
23 | |
24 | print STDERR "SWASHNEW @_\n" if DEBUG; |
25 | |
cf25bb62 |
26 | ## |
27 | ## Get the list of codepoints for the type. |
28 | ## Called from utf8.c |
29 | ## |
30 | ## Given a $type, our goal is to fill $list with the set of codepoint |
5beb625e |
31 | ## ranges. |
cf25bb62 |
32 | ## |
33 | ## To make the parsing of $type clear, this code takes the a rather |
2d92f8a0 |
34 | ## unorthodox approach of last'ing out of the block once we have the |
cf25bb62 |
35 | ## info we need. Were this to be a subroutine, the 'last' would just |
36 | ## be a 'return'. |
37 | ## |
5beb625e |
38 | my $file; ## file to load data from, and also part of the %Cache key. |
39 | my $ListSorted = 0; |
40 | |
cf25bb62 |
41 | if ($type) |
42 | { |
43 | $type =~ s/^\s+//; |
44 | $type =~ s/\s+$//; |
45 | |
46 | print "type = $type\n" if DEBUG; |
47 | |
cf25bb62 |
48 | GETFILE: |
49 | { |
12ac2576 |
50 | ## |
51 | ## It could be a user-defined property. |
52 | ## |
53 | |
54 | my $caller1 = $type =~ s/(.+)::// ? $1 : caller(1); |
55 | |
56 | if (defined $caller1 && $type =~ /^(?:\w+)$/) { |
57 | my $prop = "${caller1}::$type"; |
58 | if (exists &{$prop}) { |
59 | no strict 'refs'; |
60 | |
61 | $list = &{$prop}; |
62 | last GETFILE; |
63 | } |
64 | } |
65 | |
2d92f8a0 |
66 | my $wasIs; |
67 | |
68 | ($wasIs = $type =~ s/^Is(?:\s+|[-_])?//i) |
5beb625e |
69 | or |
12ac2576 |
70 | $type =~ s/^(?:(?:General(?:\s+|_)?)?Category|gc)\s*[:=]\s*//i |
5beb625e |
71 | or |
12ac2576 |
72 | $type =~ s/^(?:Script|sc)\s*[:=]\s*//i |
5beb625e |
73 | or |
12ac2576 |
74 | $type =~ s/^Block\s*[:=]\s*/In/i; |
75 | |
76 | |
77 | ## |
78 | ## See if it's in some enumeration. |
79 | ## |
8aa03e5d |
80 | require "utf8_pva.pl"; |
12ac2576 |
81 | if ($type =~ /^([\w\s]+)[:=]\s*(.*)/) { |
12ac2576 |
82 | my ($enum, $val) = (lc $1, lc $2); |
83 | $enum =~ tr/ _-//d; |
84 | $val =~ tr/ _-//d; |
85 | |
86 | my $pa = $PropertyAlias{$enum} ? $enum : $PA_reverse{$enum}; |
87 | my $f = $PropValueAlias{$pa}{$val} ? $val : $PVA_reverse{$pa}{lc $val}; |
88 | |
89 | if ($pa and $f) { |
90 | $pa = "gc_sc" if $pa eq "gc" or $pa eq "sc"; |
91 | $file = "unicore/lib/$pa/$PVA_abbr_map{$pa}{lc $f}.pl"; |
92 | last GETFILE; |
93 | } |
94 | } |
95 | else { |
96 | my $t = lc $type; |
97 | $t =~ tr/ _-//d; |
98 | |
99 | if ($PropValueAlias{gc}{$t} or $PropValueAlias{sc}{$t}) { |
100 | $file = "unicore/lib/gc_sc/$PVA_abbr_map{gc_sc}{$t}.pl"; |
101 | last GETFILE; |
102 | } |
103 | } |
cf25bb62 |
104 | |
5beb625e |
105 | ## |
106 | ## See if it's in the direct mapping table. |
107 | ## |
108 | require "unicore/Exact.pl"; |
109 | if (my $base = $utf8::Exact{$type}) { |
12ac2576 |
110 | $file = "unicore/lib/gc_sc/$base.pl"; |
5beb625e |
111 | last GETFILE; |
cf25bb62 |
112 | } |
113 | |
114 | ## |
5beb625e |
115 | ## If not there exactly, try the canonical form. The canonical |
116 | ## form is lowercased, with any separators (\s+|[-_]) removed. |
cf25bb62 |
117 | ## |
5beb625e |
118 | my $canonical = lc $type; |
119 | $canonical =~ s/(?<=[a-z\d])(?:\s+|[-_])(?=[a-z\d])//g; |
120 | print "canonical = $canonical\n" if DEBUG; |
cf25bb62 |
121 | |
5beb625e |
122 | require "unicore/Canonical.pl"; |
12ac2576 |
123 | if (my $base = ($utf8::Canonical{$canonical} || $utf8::Canonical{ lc $utf8::PropertyAlias{$canonical} })) { |
124 | $file = "unicore/lib/gc_sc/$base.pl"; |
5beb625e |
125 | last GETFILE; |
cf25bb62 |
126 | } |
127 | |
491fd90a |
128 | ## |
3a2263fe |
129 | ## See if it's a user-level "To". |
130 | ## |
131 | |
132 | my $caller0 = caller(0); |
133 | |
134 | if (defined $caller0 && $type =~ /^To(?:\w+)$/) { |
135 | my $map = $caller0 . "::" . $type; |
09e0265a |
136 | |
3a2263fe |
137 | if (exists &{$map}) { |
138 | no strict 'refs'; |
139 | |
140 | $list = &{$map}; |
141 | last GETFILE; |
142 | } |
143 | } |
144 | |
cf25bb62 |
145 | ## |
3a2263fe |
146 | ## Last attempt -- see if it's a standard "To" name |
147 | ## (e.g. "ToLower") ToTitle is used by ucfirst(). |
148 | ## The user-level way to access ToDigit() and ToFold() |
149 | ## is to use Unicode::UCD. |
cf25bb62 |
150 | ## |
3a2263fe |
151 | if ($type =~ /^To(Digit|Fold|Lower|Title|Upper)$/) |
cf25bb62 |
152 | { |
153 | $file = "unicore/To/$1.pl"; |
154 | ## would like to test to see if $file actually exists.... |
155 | last GETFILE; |
156 | } |
157 | |
158 | ## |
159 | ## If we reach this line, it's because we couldn't figure |
160 | ## out what to do with $type. Ouch. |
161 | ## |
bc45ce41 |
162 | |
163 | return $type; |
cf25bb62 |
164 | } |
165 | |
491fd90a |
166 | if (defined $file) { |
167 | print "found it (file='$file')\n" if DEBUG; |
168 | |
169 | ## |
170 | ## If we reach here, it was due to a 'last GETFILE' above |
3a2263fe |
171 | ## (exception: user-defined properties and mappings), so we |
491fd90a |
172 | ## have a filename, so now we load it if we haven't already. |
173 | ## If we have, return the cached results. The cache key is the |
174 | ## file to load. |
175 | ## |
176 | if ($Cache{$file} and ref($Cache{$file}) eq $class) |
177 | { |
178 | print "Returning cached '$file' for \\p{$type}\n" if DEBUG; |
179 | return $Cache{$class, $file}; |
180 | } |
181 | |
182 | $list = do $file; |
183 | } |
5beb625e |
184 | |
5beb625e |
185 | $ListSorted = 1; ## we know that these lists are sorted |
886ba366 |
186 | } |
a0ed51b3 |
187 | |
15732964 |
188 | my $extras; |
485aafd9 |
189 | my $bits = 0; |
cf25bb62 |
190 | |
5beb625e |
191 | my $ORIG = $list; |
a0ed51b3 |
192 | if ($list) { |
193 | my @tmp = split(/^/m, $list); |
194 | my %seen; |
db376a24 |
195 | no warnings; |
a0ed51b3 |
196 | $extras = join '', grep /^[^0-9a-fA-F]/, @tmp; |
197 | $list = join '', |
df68b396 |
198 | map { $_->[1] } |
199 | sort { $a->[0] <=> $b->[0] } |
200 | map { /^([0-9a-fA-F]+)/; [ hex($1), $_ ] } |
201 | grep { /^([0-9a-fA-F]+)/ and not $seen{$1}++ } @tmp; # XXX doesn't do ranges right |
a0ed51b3 |
202 | } |
203 | |
204 | if ($none) { |
205 | my $hextra = sprintf "%04x", $none + 1; |
206 | $list =~ s/\tXXXX$/\t$hextra/mg; |
207 | } |
208 | |
209 | if ($minbits < 32) { |
210 | my $top = 0; |
3a2263fe |
211 | while ($list =~ /^([0-9a-fA-F]+)(?:[\t]([0-9a-fA-F]+)?)(?:[ \t]([0-9a-fA-F]+))?/mg) { |
a0ed51b3 |
212 | my $min = hex $1; |
3a2263fe |
213 | my $max = defined $2 ? hex $2 : $min; |
214 | my $val = defined $3 ? hex $3 : 0; |
a0ed51b3 |
215 | $val += $max - $min if defined $3; |
216 | $top = $val if $val > $top; |
217 | } |
218 | $bits = |
219 | $top > 0xffff ? 32 : |
220 | $top > 0xff ? 16 : |
221 | $top > 1 ? 8 : 1 |
222 | } |
223 | $bits = $minbits if $bits < $minbits; |
224 | |
225 | my @extras; |
226 | for my $x ($extras) { |
227 | pos $x = 0; |
1fef36c7 |
228 | while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) { |
a0ed51b3 |
229 | my $char = $1; |
230 | my $name = $2; |
6d6ae962 |
231 | print STDERR "$1 => $2\n" if DEBUG; |
09e0265a |
232 | if ($char =~ /[-+!&]/) { |
a0ed51b3 |
233 | my ($c,$t) = split(/::/, $name, 2); # bogus use of ::, really |
11ef8fdd |
234 | my $subobj; |
235 | if ($c eq 'utf8') { |
09e0265a |
236 | $subobj = utf8->SWASHNEW($t, "", 0, 0, 0); |
237 | } |
238 | elsif (exists &$name) { |
239 | $subobj = utf8->SWASHNEW($name, "", 0, 0, 0); |
11ef8fdd |
240 | } |
241 | elsif ($c =~ /^([0-9a-fA-F]+)/) { |
242 | $subobj = utf8->SWASHNEW("", $c, 0, 0, 0); |
243 | } |
bc45ce41 |
244 | return $subobj unless ref $subobj; |
a0ed51b3 |
245 | push @extras, $name => $subobj; |
246 | $bits = $subobj->{BITS} if $bits < $subobj->{BITS}; |
247 | } |
248 | } |
249 | } |
250 | |
15732964 |
251 | print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG; |
a0ed51b3 |
252 | |
5beb625e |
253 | my $SWASH = bless { |
a0ed51b3 |
254 | TYPE => $type, |
255 | BITS => $bits, |
256 | EXTRAS => $extras, |
257 | LIST => $list, |
258 | NONE => $none, |
259 | @extras, |
260 | } => $class; |
5beb625e |
261 | |
262 | if ($file) { |
263 | $Cache{$class, $file} = $SWASH; |
264 | } |
265 | |
266 | return $SWASH; |
a0ed51b3 |
267 | } |
268 | |
269 | # NOTE: utf8.c:swash_init() assumes entries are never modified once generated. |
270 | |
271 | sub SWASHGET { |
035d37be |
272 | # See utf8.c:Perl_swash_fetch for problems with this interface. |
a0ed51b3 |
273 | my ($self, $start, $len) = @_; |
274 | local $^D = 0 if $^D; |
a0ed51b3 |
275 | my $type = $self->{TYPE}; |
276 | my $bits = $self->{BITS}; |
277 | my $none = $self->{NONE}; |
15732964 |
278 | print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if DEBUG; |
a0ed51b3 |
279 | my $end = $start + $len; |
280 | my $swatch = ""; |
281 | my $key; |
282 | vec($swatch, $len - 1, $bits) = 0; # Extend to correct length. |
283 | if ($none) { |
284 | for $key (0 .. $len - 1) { vec($swatch, $key, $bits) = $none } |
285 | } |
286 | |
287 | for ($self->{LIST}) { |
288 | pos $_ = 0; |
289 | if ($bits > 1) { |
290 | LINE: |
3a2263fe |
291 | while (/^([0-9a-fA-F]+)(?:[ \t]([0-9a-fA-F]+)?)?(?:[ \t]([0-9a-fA-F]+))?/mg) { |
292 | chomp; |
293 | my ($a, $b, $c) = ($1, $2, $3); |
294 | croak "$type: illegal mapping '$_'" |
295 | if $type =~ /^To/ && |
296 | !(defined $a && defined $c); |
297 | my $min = hex $a; |
298 | my $max = defined $b ? hex $b : $min; |
299 | my $val = defined $c ? hex $c : 0; |
a0ed51b3 |
300 | next if $max < $start; |
6d6ae962 |
301 | print "$min $max $val\n" if DEBUG; |
a0ed51b3 |
302 | if ($none) { |
303 | if ($min < $start) { |
710250cb |
304 | $val += $start - $min if $val < $none; |
a0ed51b3 |
305 | $min = $start; |
306 | } |
307 | for ($key = $min; $key <= $max; $key++) { |
308 | last LINE if $key >= $end; |
6d6ae962 |
309 | print STDERR "$key => $val\n" if DEBUG; |
a0ed51b3 |
310 | vec($swatch, $key - $start, $bits) = $val; |
311 | ++$val if $val < $none; |
312 | } |
313 | } |
314 | else { |
315 | if ($min < $start) { |
316 | $val += $start - $min; |
317 | $min = $start; |
318 | } |
319 | for ($key = $min; $key <= $max; $key++, $val++) { |
320 | last LINE if $key >= $end; |
6d6ae962 |
321 | print STDERR "$key => $val\n" if DEBUG; |
a0ed51b3 |
322 | vec($swatch, $key - $start, $bits) = $val; |
323 | } |
324 | } |
325 | } |
326 | } |
327 | else { |
328 | LINE: |
99a6b1f0 |
329 | while (/^([0-9a-fA-F]+)(?:[ \t]+([0-9a-fA-F]+))?/mg) { |
3a2263fe |
330 | chomp; |
a0ed51b3 |
331 | my $min = hex $1; |
3a2263fe |
332 | my $max = defined $2 ? hex $2 : $min; |
a0ed51b3 |
333 | next if $max < $start; |
334 | if ($min < $start) { |
335 | $min = $start; |
336 | } |
337 | for ($key = $min; $key <= $max; $key++) { |
338 | last LINE if $key >= $end; |
6d6ae962 |
339 | print STDERR "$key => 1\n" if DEBUG; |
a0ed51b3 |
340 | vec($swatch, $key - $start, 1) = 1; |
341 | } |
342 | } |
343 | } |
344 | } |
345 | for my $x ($self->{EXTRAS}) { |
346 | pos $x = 0; |
09e0265a |
347 | while ($x =~ /^([-+!&])(.*)/mg) { |
a0ed51b3 |
348 | my $char = $1; |
349 | my $name = $2; |
15732964 |
350 | print STDERR "INDIRECT $1 $2\n" if DEBUG; |
1fef36c7 |
351 | my $otherbits = $self->{$name}->{BITS}; |
352 | croak("SWASHGET size mismatch") if $bits < $otherbits; |
353 | my $other = $self->{$name}->SWASHGET($start, $len); |
354 | if ($char eq '+') { |
355 | if ($bits == 1 and $otherbits == 1) { |
356 | $swatch |= $other; |
a0ed51b3 |
357 | } |
1fef36c7 |
358 | else { |
359 | for ($key = 0; $key < $len; $key++) { |
360 | vec($swatch, $key, $bits) = vec($other, $key, $otherbits); |
a0ed51b3 |
361 | } |
1fef36c7 |
362 | } |
363 | } |
364 | elsif ($char eq '!') { |
365 | if ($bits == 1 and $otherbits == 1) { |
366 | $swatch |= ~$other; |
367 | } |
368 | else { |
369 | for ($key = 0; $key < $len; $key++) { |
370 | if (!vec($other, $key, $otherbits)) { |
371 | vec($swatch, $key, $bits) = 1; |
a0ed51b3 |
372 | } |
373 | } |
374 | } |
1fef36c7 |
375 | } |
376 | elsif ($char eq '-') { |
377 | if ($bits == 1 and $otherbits == 1) { |
378 | $swatch &= ~$other; |
379 | } |
380 | else { |
381 | for ($key = 0; $key < $len; $key++) { |
382 | if (vec($other, $key, $otherbits)) { |
383 | vec($swatch, $key, $bits) = 0; |
a0ed51b3 |
384 | } |
385 | } |
386 | } |
387 | } |
09e0265a |
388 | elsif ($char eq '&') { |
389 | if ($bits == 1 and $otherbits == 1) { |
390 | $swatch &= $other; |
391 | } |
392 | else { |
393 | for ($key = 0; $key < $len; $key++) { |
394 | if (!vec($other, $key, $otherbits)) { |
395 | vec($swatch, $key, $bits) = 0; |
396 | } |
397 | } |
398 | } |
399 | } |
a0ed51b3 |
400 | } |
401 | } |
15732964 |
402 | if (DEBUG) { |
a0ed51b3 |
403 | print STDERR "CELLS "; |
404 | for ($key = 0; $key < $len; $key++) { |
405 | print STDERR vec($swatch, $key, $bits), " "; |
406 | } |
407 | print STDERR "\n"; |
408 | } |
409 | $swatch; |
410 | } |
411 | |
412 | 1; |