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