Big mktables rewrite from Jeffrey;
[p5sagit/p5-mst-13.2.git] / lib / utf8_heavy.pl
CommitLineData
a0ed51b3 1package utf8;
cf25bb62 2use strict;
3use warnings;
a0ed51b3 4
15732964 5sub DEBUG () { 0 }
a0ed51b3 6
7sub DESTROY {}
8
9sub croak { require Carp; Carp::croak(@_) }
10
11sub SWASHNEW {
12 my ($class, $type, $list, $minbits, $none) = @_;
13 local $^D = 0 if $^D;
15732964 14
15 print STDERR "SWASHNEW @_\n" if DEBUG;
16
cf25bb62 17 ## check to see if we've already got it.
18 {
19 no strict 'refs';
20 if ($type and ref ${"${class}::{$type}"} eq $class) {
21 warn qq/Found \${"${class}::{$type}"}\n/ if DEBUG;
22 return ${"${class}::{$type}"};
23 }
a0ed51b3 24 }
25
cf25bb62 26 ##
27 ## Get the list of codepoints for the type.
28 ## Called from utf8.c
29 ##
30 ## Given a $type, our goal is to fill $list with the set of codepoint
31 ## ranges. As we try various interpretations of $type, sometimes we'll
32 ## end up with the $list directly, and sometimes we'll end up with a
33 ## $file name that holds the list data.
34 ##
35 ## To make the parsing of $type clear, this code takes the a rather
36 ## unorthadox approach of last'ing out of the block once we have the
37 ## info we need. Were this to be a subroutine, the 'last' would just
38 ## be a 'return'.
39 ##
40 if ($type)
41 {
42 $type =~ s/^\s+//;
43 $type =~ s/\s+$//;
44
45 print "type = $type\n" if DEBUG;
46
47 my $file;
48 ## Figure out what file to load to get the data....
49 GETFILE:
50 {
51 ##
52 ## First, see if it's an "Is" name (the 'Is' is optional)
53 ##
54 ## Because we check "Is" names first, they have precidence over
55 ## "In" names. For example, "Greek" is both a script and a
56 ## block. "IsGreek" always gets the script, while "InGreek"
57 ## always gets the block. "Greek" gets the script because we
58 ## check "Is" names first.
59 ##
60 if ($type =~ m{^
61 ## "Is" prefix, or "Script=" or "Category="
62 (?: Is [- _]? | (?:Script|Category)\s*=\s* )?
63 ## name to check in the "Is" symbol table.
64 ([A-Z].*)
65 $
66 }ix)
67 {
68 my $istype = $1;
69 ##
70 ## Input ($type) Name To Check ($istype)
71 ## ------------- -----------------------
72 ## IsLu Lu
73 ## Lu Lu
74 ## Category = Lu Lu
75 ## Foo Foo
76 ## Script = Greek Greek
77 ##
78
79 print "istype = $istype\n" if DEBUG;
80
81 ## Load "Is" mapping data, if not yet loaded.
82 do "unicore/Is.pl" if not defined %utf8::Is;
83
84 ##
85 ## If the "Is" mapping data has an exact match, it points
86 ## to the file we need.
87 ##
88 if (exists $utf8::Is{$istype})
89 {
90 $file = "unicore/Is/$utf8::Is{$istype}.pl";
91 last GETFILE;
92 }
93
94 ##
95 ## Need to look at %utf8::IsPat (loaded from "unicore/Is.pl")
96 ## to see if there's a regex that matches this $istype.
97 ## If so, the associated name is the file we need.
98 ##
99 my $prefix = substr(lc($istype), 0, 2);
100 if (exists $utf8::IsPat{$prefix})
101 {
102 while (my ($pat, $name) = each %{$utf8::IsPat{$prefix}})
103 {
104 print "isprefix = $prefix, Is = $istype, pat = $pat\n" if DEBUG;
105 ##
106 ## The following regex probably need not be cached,
107 ## since every time there's a match, the results of
108 ## the entire call to SWASHNEW() is cached, so there's
109 ## a very limited number of times any one $pat will
110 ## be evaluated as a regex, at least with "reasonable"
111 ## code that doesn't try a baziilion \p{Random} names.
112 ##
113 if ($istype =~ /^$pat$/i)
114 {
115 $file = "unicore/Is/$name.pl";
116 last GETFILE;
117 }
118 }
119 }
120 }
121
122 ##
123 ## Couldn't find via "Is" -- let's try via "In".....
124 ##
125 if ($type =~ m{^
126 ( In(?!herited$)[- _]? | Block\s*=\s*)?
127 ([A-Z].*)
128 $
129 }xi)
130 {
131 my $intype = $2;
132 print "intype = $intype\n" if DEBUG;
133
134 ##
135 ## Input ($type) Name To Check ($intype)
136 ## ------------- -----------------------
137 ## Inherited Inherited
138 ## InGreek Greek
139 ## Block = Greek Greek
140 ##
141
142 ## Load "In" mapping data, if not yet loaded.
143 do "unicore/In.pl" if not defined %utf8::In;
144
145 ## If there's a direct match, it points to the file we need
146 if (exists $utf8::In{$intype}) {
147 $file = "unicore/In/$utf8::In{$intype}.pl";
148 last GETFILE;
149 }
150
151 my $prefix = substr(lc($intype), 0, 2);
152 if (exists $utf8::InPat{$prefix})
153 {
154 print "inprefix = $prefix, In = $intype\n" if DEBUG;
155 while (my ($pat, $name) = each %{$utf8::InPat{$prefix}})
156 {
157 print "inprefix = $prefix, In = $intype, k = $pat\n" if DEBUG;
158 if ($intype =~ /^$pat$/i) {
159 $file = "unicore/In/$name.pl";
160 print "inprefix = $prefix, In = $intype, k = $pat, file = $file\n" if DEBUG;
161 last GETFILE;
162 }
163 }
164 }
165 }
166
167 ##
168 ## Last attempt -- see if it's a "To" name (e.g. "ToLower")
169 ##
170 if ($type =~ /^To([A-Z][A-Za-z]+)$/)
171 {
172 $file = "unicore/To/$1.pl";
173 ## would like to test to see if $file actually exists....
174 last GETFILE;
175 }
176
177 ##
178 ## If we reach this line, it's because we couldn't figure
179 ## out what to do with $type. Ouch.
180 ##
181 croak("Can't find Unicode character property \"$type\"");
182 }
183
184 ##
185 ## If we reach here, it was due to a 'last GETFILE' above, so we
186 ## have a filename, so now we load it.
187 ##
188 $list = do $file;
886ba366 189 }
a0ed51b3 190
15732964 191 my $extras;
192 my $bits;
cf25bb62 193
a0ed51b3 194 if ($list) {
195 my @tmp = split(/^/m, $list);
196 my %seen;
db376a24 197 no warnings;
a0ed51b3 198 $extras = join '', grep /^[^0-9a-fA-F]/, @tmp;
199 $list = join '',
200 sort { hex $a <=> hex $b }
201 grep {/^([0-9a-fA-F]+)/ and not $seen{$1}++} @tmp; # XXX doesn't do ranges right
202 }
203
204 if ($none) {
205 my $hextra = sprintf "%04x", $none + 1;
206 $list =~ s/\tXXXX$/\t$hextra/mg;
207 }
208
209 if ($minbits < 32) {
210 my $top = 0;
211 while ($list =~ /^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
212 my $min = hex $1;
213 my $max = hex(defined $2 ? $2 : $1);
214 my $val = hex(defined $3 ? $3 : "");
215 $val += $max - $min if defined $3;
216 $top = $val if $val > $top;
217 }
218 $bits =
219 $top > 0xffff ? 32 :
220 $top > 0xff ? 16 :
221 $top > 1 ? 8 : 1
222 }
223 $bits = $minbits if $bits < $minbits;
224
225 my @extras;
226 for my $x ($extras) {
227 pos $x = 0;
1fef36c7 228 while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
a0ed51b3 229 my $char = $1;
230 my $name = $2;
6d6ae962 231 print STDERR "$1 => $2\n" if DEBUG;
a0ed51b3 232 if ($char =~ /[-+!]/) {
233 my ($c,$t) = split(/::/, $name, 2); # bogus use of ::, really
234 my $subobj = $c->SWASHNEW($t, "", 0, 0, 0);
235 push @extras, $name => $subobj;
236 $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
237 }
238 }
239 }
240
15732964 241 print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG;
a0ed51b3 242
cf25bb62 243 no strict 'refs';
a0ed51b3 244 ${"${class}::{$type}"} = bless {
245 TYPE => $type,
246 BITS => $bits,
247 EXTRAS => $extras,
248 LIST => $list,
249 NONE => $none,
250 @extras,
251 } => $class;
252}
253
254# NOTE: utf8.c:swash_init() assumes entries are never modified once generated.
255
256sub SWASHGET {
035d37be 257 # See utf8.c:Perl_swash_fetch for problems with this interface.
a0ed51b3 258 my ($self, $start, $len) = @_;
259 local $^D = 0 if $^D;
a0ed51b3 260 my $type = $self->{TYPE};
261 my $bits = $self->{BITS};
262 my $none = $self->{NONE};
15732964 263 print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if DEBUG;
a0ed51b3 264 my $end = $start + $len;
265 my $swatch = "";
266 my $key;
267 vec($swatch, $len - 1, $bits) = 0; # Extend to correct length.
268 if ($none) {
269 for $key (0 .. $len - 1) { vec($swatch, $key, $bits) = $none }
270 }
271
272 for ($self->{LIST}) {
273 pos $_ = 0;
274 if ($bits > 1) {
275 LINE:
276 while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
277 my $min = hex $1;
278 my $max = (defined $2 ? hex $2 : $min);
279 my $val = hex $3;
280 next if $max < $start;
6d6ae962 281 print "$min $max $val\n" if DEBUG;
a0ed51b3 282 if ($none) {
283 if ($min < $start) {
710250cb 284 $val += $start - $min if $val < $none;
a0ed51b3 285 $min = $start;
286 }
287 for ($key = $min; $key <= $max; $key++) {
288 last LINE if $key >= $end;
6d6ae962 289 print STDERR "$key => $val\n" if DEBUG;
a0ed51b3 290 vec($swatch, $key - $start, $bits) = $val;
291 ++$val if $val < $none;
292 }
293 }
294 else {
295 if ($min < $start) {
296 $val += $start - $min;
297 $min = $start;
298 }
299 for ($key = $min; $key <= $max; $key++, $val++) {
300 last LINE if $key >= $end;
6d6ae962 301 print STDERR "$key => $val\n" if DEBUG;
a0ed51b3 302 vec($swatch, $key - $start, $bits) = $val;
303 }
304 }
305 }
306 }
307 else {
308 LINE:
309 while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+))?/mg) {
310 my $min = hex $1;
311 my $max = (defined $2 ? hex $2 : $min);
312 next if $max < $start;
313 if ($min < $start) {
314 $min = $start;
315 }
316 for ($key = $min; $key <= $max; $key++) {
317 last LINE if $key >= $end;
6d6ae962 318 print STDERR "$key => 1\n" if DEBUG;
a0ed51b3 319 vec($swatch, $key - $start, 1) = 1;
320 }
321 }
322 }
323 }
324 for my $x ($self->{EXTRAS}) {
325 pos $x = 0;
1fef36c7 326 while ($x =~ /^([-+!])(.*)/mg) {
a0ed51b3 327 my $char = $1;
328 my $name = $2;
15732964 329 print STDERR "INDIRECT $1 $2\n" if DEBUG;
1fef36c7 330 my $otherbits = $self->{$name}->{BITS};
331 croak("SWASHGET size mismatch") if $bits < $otherbits;
332 my $other = $self->{$name}->SWASHGET($start, $len);
333 if ($char eq '+') {
334 if ($bits == 1 and $otherbits == 1) {
335 $swatch |= $other;
a0ed51b3 336 }
1fef36c7 337 else {
338 for ($key = 0; $key < $len; $key++) {
339 vec($swatch, $key, $bits) = vec($other, $key, $otherbits);
a0ed51b3 340 }
1fef36c7 341 }
342 }
343 elsif ($char eq '!') {
344 if ($bits == 1 and $otherbits == 1) {
345 $swatch |= ~$other;
346 }
347 else {
348 for ($key = 0; $key < $len; $key++) {
349 if (!vec($other, $key, $otherbits)) {
350 vec($swatch, $key, $bits) = 1;
a0ed51b3 351 }
352 }
353 }
1fef36c7 354 }
355 elsif ($char eq '-') {
356 if ($bits == 1 and $otherbits == 1) {
357 $swatch &= ~$other;
358 }
359 else {
360 for ($key = 0; $key < $len; $key++) {
361 if (vec($other, $key, $otherbits)) {
362 vec($swatch, $key, $bits) = 0;
a0ed51b3 363 }
364 }
365 }
366 }
367 }
368 }
15732964 369 if (DEBUG) {
a0ed51b3 370 print STDERR "CELLS ";
371 for ($key = 0; $key < $len; $key++) {
372 print STDERR vec($swatch, $key, $bits), " ";
373 }
374 print STDERR "\n";
375 }
376 $swatch;
377}
378
3791;