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