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