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