perl #17453
[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 my %Cache;
10
11 sub croak { require Carp; Carp::croak(@_) }
12
13 ##
14 ## "SWASH" == "SWATCH HASH". A "swatch" is a swatch of the Unicode landscape
15 ##
16
17 sub SWASHNEW {
18     my ($class, $type, $list, $minbits, $none) = @_;
19     local $^D = 0 if $^D;
20
21     print STDERR "SWASHNEW @_\n" if DEBUG;
22
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
28     ## ranges.
29     ##
30     ## To make the parsing of $type clear, this code takes the a rather
31     ## unorthodox 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     ##
35     my $file; ## file to load data from, and also part of the %Cache key.
36     my $ListSorted = 0;
37
38     if ($type)
39     {
40         $type =~ s/^\s+//;
41         $type =~ s/\s+$//;
42
43         print "type = $type\n" if DEBUG;
44
45       GETFILE:
46         {
47             ##
48             ## 'Is' is always optional, so if it's there, remove it.
49             ## Same with 'Category=' and 'Script='.
50             ##
51             ## 'Block=' is replaced by 'In'.
52             ##
53             my $wasIs;
54
55             ($wasIs = $type =~ s/^Is(?:\s+|[-_])?//i)
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;
62
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;
70             }
71
72             ##
73             ## If not there exactly, try the canonical form. The canonical
74             ## form is lowercased, with any separators (\s+|[-_]) removed.
75             ##
76             my $canonical = lc $type;
77             $canonical =~ s/(?<=[a-z\d])(?:\s+|[-_])(?=[a-z\d])//g;
78             print "canonical = $canonical\n" if DEBUG;
79
80             require "unicore/Canonical.pl";
81             if (my $base = $utf8::Canonical{$canonical}) {
82                 $file = "unicore/lib/$base.pl";
83                 last GETFILE;
84             }
85
86             ##
87             ## It could be a user-defined property.
88             ##
89
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             }
101
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             ##
116
117             return $type;
118         }
119
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         }
138
139         $ListSorted = 1; ## we know that these lists are sorted
140     }
141
142     my $extras;
143     my $bits;
144
145     my $ORIG = $list;
146     if ($list) {
147         my @tmp = split(/^/m, $list);
148         my %seen;
149         no warnings;
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;
180         while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
181             my $char = $1;
182             my $name = $2;
183             print STDERR "$1 => $2\n" if DEBUG;
184             if ($char =~ /[-+!]/) {
185                 my ($c,$t) = split(/::/, $name, 2);     # bogus use of ::, really
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                 }
193                 return $subobj unless ref $subobj;
194                 push @extras, $name => $subobj;
195                 $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
196             }
197         }
198     }
199
200     print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG;
201
202     my $SWASH = bless {
203         TYPE => $type,
204         BITS => $bits,
205         EXTRAS => $extras,
206         LIST => $list,
207         NONE => $none,
208         @extras,
209     } => $class;
210
211     if ($file) {
212         $Cache{$class, $file} = $SWASH;
213     }
214
215     return $SWASH;
216 }
217
218 # NOTE: utf8.c:swash_init() assumes entries are never modified once generated.
219
220 sub SWASHGET {
221     # See utf8.c:Perl_swash_fetch for problems with this interface.
222     my ($self, $start, $len) = @_;
223     local $^D = 0 if $^D;
224     my $type = $self->{TYPE};
225     my $bits = $self->{BITS};
226     my $none = $self->{NONE};
227     print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if DEBUG;
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;
245                 print "$min $max $val\n" if DEBUG;
246                 if ($none) {
247                     if ($min < $start) {
248                         $val += $start - $min if $val < $none;
249                         $min = $start;
250                     }
251                     for ($key = $min; $key <= $max; $key++) {
252                         last LINE if $key >= $end;
253                         print STDERR "$key => $val\n" if DEBUG;
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;
265                         print STDERR "$key => $val\n" if DEBUG;
266                         vec($swatch, $key - $start, $bits) = $val;
267                     }
268                 }
269             }
270         }
271         else {
272           LINE:
273             while (/^([0-9a-fA-F]+)(?:[ \t]+([0-9a-fA-F]+))?/mg) {
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;
282                     print STDERR "$key => 1\n" if DEBUG;
283                     vec($swatch, $key - $start, 1) = 1;
284                 }
285             }
286         }
287     }
288     for my $x ($self->{EXTRAS}) {
289         pos $x = 0;
290         while ($x =~ /^([-+!])(.*)/mg) {
291             my $char = $1;
292             my $name = $2;
293             print STDERR "INDIRECT $1 $2\n" if DEBUG;
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;
300                 }
301                 else {
302                     for ($key = 0; $key < $len; $key++) {
303                         vec($swatch, $key, $bits) = vec($other, $key, $otherbits);
304                     }
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;
315                         }
316                     }
317                 }
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;
327                         }
328                     }
329                 }
330             }
331         }
332     }
333     if (DEBUG) {
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;