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