3f14afece222415fac44222bed1c0313b7451a9e
[p5sagit/p5-mst-13.2.git] / lib / utf8_heavy.pl
1 package utf8;
2 use strict;
3 use warnings;
4
5 sub DEBUG () { 0 }
6
7 sub DESTROY {}
8
9 sub croak { require Carp; Carp::croak(@_) }
10
11 sub SWASHNEW {
12     my ($class, $type, $list, $minbits, $none) = @_;
13     local $^D = 0 if $^D;
14
15     print STDERR "SWASHNEW @_\n" if DEBUG;
16
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         }
24     }
25
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 (my $hashref = $utf8::IsPat{$prefix})
101                 {
102                     while (my ($pat, $name) = each %{$hashref})
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                             keys %{$hashref}; ## reset the 'each' above
117                             last GETFILE;
118                         }
119                     }
120                 }
121             }
122
123             ##
124             ## Couldn't find via "Is" -- let's try via "In".....
125             ##
126             if ($type =~ m{^
127                            ( In(?!herited$)[- _]? | Block\s*=\s*)?
128                            ([A-Z].*)
129                            $
130                           }xi)
131             {
132                 my $intype = $2;
133                 print "intype = $intype\n" if DEBUG;
134
135                 ##
136                 ## Input ($type)      Name To Check ($intype)
137                 ## -------------      -----------------------
138                 ## Inherited             Inherited
139                 ## InGreek               Greek
140                 ## Block = Greek         Greek
141                 ##
142
143                 ## Load "In" mapping data, if not yet loaded.
144                 do "unicore/In.pl" if not defined %utf8::In;
145
146                 ## If there's a direct match, it points to the file we need
147                 if (exists $utf8::In{$intype}) {
148                     $file = "unicore/In/$utf8::In{$intype}.pl";
149                     last GETFILE;
150                 }
151
152                 ##
153                 ## Need to look at %utf8::InPat (loaded from "unicore/In.pl")
154                 ## to see if there's a regex that matches this $intype.
155                 ## If so, the associated name is the file we need.
156                 ##
157                 my $prefix = substr(lc($intype), 0, 2);
158                 if (my $hashref = $utf8::InPat{$prefix})
159                 {
160                     print "inprefix = $prefix, In = $intype\n" if DEBUG;
161                     while (my ($pat, $name) = each %{$hashref})
162                     {
163                         print "inprefix = $prefix, In = $intype, k = $pat\n" if DEBUG;
164                         if ($intype =~ /^$pat$/i) {
165                             $file = "unicore/In/$name.pl";
166                             print "inprefix = $prefix, In = $intype, k = $pat, file = $file\n" if DEBUG;
167                             keys %{$hashref}; ## reset the 'each' above
168                             last GETFILE;
169                         }
170                     }
171                 }
172             }
173
174             ##
175             ## Last attempt -- see if it's a "To" name (e.g. "ToLower")
176             ##
177             if ($type =~ /^To([A-Z][A-Za-z]+)$/)
178             {
179                 $file = "unicore/To/$1.pl";
180                 ## would like to test to see if $file actually exists....
181                 last GETFILE;
182             }
183
184             ##
185             ## If we reach this line, it's because we couldn't figure
186             ## out what to do with $type. Ouch.
187             ##
188             croak("Can't find Unicode character property \"$type\"");
189         }
190
191         ##
192         ## If we reach here, it was due to a 'last GETFILE' above, so we
193         ## have a filename, so now we load it.
194         ##
195         $list = do $file;
196     }
197
198     my $extras;
199     my $bits;
200
201     if ($list) {
202         my @tmp = split(/^/m, $list);
203         my %seen;
204         no warnings;
205         $extras = join '', grep /^[^0-9a-fA-F]/, @tmp;
206         $list = join '',
207             sort { hex $a <=> hex $b }
208             grep {/^([0-9a-fA-F]+)/ and not $seen{$1}++} @tmp; # XXX doesn't do ranges right
209     }
210
211     if ($none) {
212         my $hextra = sprintf "%04x", $none + 1;
213         $list =~ s/\tXXXX$/\t$hextra/mg;
214     }
215
216     if ($minbits < 32) {
217         my $top = 0;
218         while ($list =~ /^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
219             my $min = hex $1;
220             my $max = hex(defined $2 ? $2 : $1);
221             my $val = hex(defined $3 ? $3 : "");
222             $val += $max - $min if defined $3;
223             $top = $val if $val > $top;
224         }
225         $bits =
226             $top > 0xffff ? 32 :
227             $top > 0xff ? 16 :
228             $top > 1 ? 8 : 1
229     }
230     $bits = $minbits if $bits < $minbits;
231
232     my @extras;
233     for my $x ($extras) {
234         pos $x = 0;
235         while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
236             my $char = $1;
237             my $name = $2;
238             print STDERR "$1 => $2\n" if DEBUG;
239             if ($char =~ /[-+!]/) {
240                 my ($c,$t) = split(/::/, $name, 2);     # bogus use of ::, really
241                 my $subobj = $c->SWASHNEW($t, "", 0, 0, 0);
242                 push @extras, $name => $subobj;
243                 $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
244             }
245         }
246     }
247
248     print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG;
249
250     no strict 'refs';
251     ${"${class}::{$type}"} = bless {
252         TYPE => $type,
253         BITS => $bits,
254         EXTRAS => $extras,
255         LIST => $list,
256         NONE => $none,
257         @extras,
258     } => $class;
259 }
260
261 # NOTE: utf8.c:swash_init() assumes entries are never modified once generated.
262
263 sub SWASHGET {
264     # See utf8.c:Perl_swash_fetch for problems with this interface.
265     my ($self, $start, $len) = @_;
266     local $^D = 0 if $^D;
267     my $type = $self->{TYPE};
268     my $bits = $self->{BITS};
269     my $none = $self->{NONE};
270     print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if DEBUG;
271     my $end = $start + $len;
272     my $swatch = "";
273     my $key;
274     vec($swatch, $len - 1, $bits) = 0;  # Extend to correct length.
275     if ($none) {
276         for $key (0 .. $len - 1) { vec($swatch, $key, $bits) = $none }
277     }
278
279     for ($self->{LIST}) {
280         pos $_ = 0;
281         if ($bits > 1) {
282           LINE:
283             while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
284                 my $min = hex $1;
285                 my $max = (defined $2 ? hex $2 : $min);
286                 my $val = hex $3;
287                 next if $max < $start;
288                 print "$min $max $val\n" if DEBUG;
289                 if ($none) {
290                     if ($min < $start) {
291                         $val += $start - $min if $val < $none;
292                         $min = $start;
293                     }
294                     for ($key = $min; $key <= $max; $key++) {
295                         last LINE if $key >= $end;
296                         print STDERR "$key => $val\n" if DEBUG;
297                         vec($swatch, $key - $start, $bits) = $val;
298                         ++$val if $val < $none;
299                     }
300                 }
301                 else {
302                     if ($min < $start) {
303                         $val += $start - $min;
304                         $min = $start;
305                     }
306                     for ($key = $min; $key <= $max; $key++, $val++) {
307                         last LINE if $key >= $end;
308                         print STDERR "$key => $val\n" if DEBUG;
309                         vec($swatch, $key - $start, $bits) = $val;
310                     }
311                 }
312             }
313         }
314         else {
315           LINE:
316             while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+))?/mg) {
317                 my $min = hex $1;
318                 my $max = (defined $2 ? hex $2 : $min);
319                 next if $max < $start;
320                 if ($min < $start) {
321                     $min = $start;
322                 }
323                 for ($key = $min; $key <= $max; $key++) {
324                     last LINE if $key >= $end;
325                     print STDERR "$key => 1\n" if DEBUG;
326                     vec($swatch, $key - $start, 1) = 1;
327                 }
328             }
329         }
330     }
331     for my $x ($self->{EXTRAS}) {
332         pos $x = 0;
333         while ($x =~ /^([-+!])(.*)/mg) {
334             my $char = $1;
335             my $name = $2;
336             print STDERR "INDIRECT $1 $2\n" if DEBUG;
337             my $otherbits = $self->{$name}->{BITS};
338             croak("SWASHGET size mismatch") if $bits < $otherbits;
339             my $other = $self->{$name}->SWASHGET($start, $len);
340             if ($char eq '+') {
341                 if ($bits == 1 and $otherbits == 1) {
342                     $swatch |= $other;
343                 }
344                 else {
345                     for ($key = 0; $key < $len; $key++) {
346                         vec($swatch, $key, $bits) = vec($other, $key, $otherbits);
347                     }
348                 }
349             }
350             elsif ($char eq '!') {
351                 if ($bits == 1 and $otherbits == 1) {
352                     $swatch |= ~$other;
353                 }
354                 else {
355                     for ($key = 0; $key < $len; $key++) {
356                         if (!vec($other, $key, $otherbits)) {
357                             vec($swatch, $key, $bits) = 1;
358                         }
359                     }
360                 }
361             }
362             elsif ($char eq '-') {
363                 if ($bits == 1 and $otherbits == 1) {
364                     $swatch &= ~$other;
365                 }
366                 else {
367                     for ($key = 0; $key < $len; $key++) {
368                         if (vec($other, $key, $otherbits)) {
369                             vec($swatch, $key, $bits) = 0;
370                         }
371                     }
372                 }
373             }
374         }
375     }
376     if (DEBUG) {
377         print STDERR "CELLS ";
378         for ($key = 0; $key < $len; $key++) {
379             print STDERR vec($swatch, $key, $bits), " ";
380         }
381         print STDERR "\n";
382     }
383     $swatch;
384 }
385
386 1;