File::Copy Pod
[p5sagit/p5-mst-13.2.git] / lib / utf8_heavy.pl
1 package utf8;
2 use strict;
3 use warnings;
4 require "utf8_pva.pl";
5
6 sub DEBUG () { 0 }
7
8 sub DESTROY {}
9
10 my %Cache;
11
12 our (%PropertyAlias, %PA_reverse, %PropValueAlias, %PVA_reverse, %PVA_abbr_map);
13
14 sub croak { require Carp; Carp::croak(@_) }
15
16 ##
17 ## "SWASH" == "SWATCH HASH". A "swatch" is a swatch of the Unicode landscape.
18 ## It's a data structure that encodes a set of Unicode characters.
19 ##
20
21 sub SWASHNEW {
22     my ($class, $type, $list, $minbits, $none) = @_;
23     local $^D = 0 if $^D;
24
25     print STDERR "SWASHNEW @_\n" if DEBUG;
26
27     ##
28     ## Get the list of codepoints for the type.
29     ## Called from utf8.c
30     ##
31     ## Given a $type, our goal is to fill $list with the set of codepoint
32     ## ranges.
33     ##
34     ## To make the parsing of $type clear, this code takes the a rather
35     ## unorthodox approach of last'ing out of the block once we have the
36     ## info we need. Were this to be a subroutine, the 'last' would just
37     ## be a 'return'.
38     ##
39     my $file; ## file to load data from, and also part of the %Cache key.
40     my $ListSorted = 0;
41
42     if ($type)
43     {
44         $type =~ s/^\s+//;
45         $type =~ s/\s+$//;
46
47         print "type = $type\n" if DEBUG;
48
49       GETFILE:
50         {
51             ##
52             ## It could be a user-defined property.
53             ##
54
55             my $caller1 = $type =~ s/(.+)::// ? $1 : caller(1);
56
57             if (defined $caller1 && $type =~ /^(?:\w+)$/) {
58                 my $prop = "${caller1}::$type";
59                 if (exists &{$prop}) {
60                     no strict 'refs';
61                     
62                     $list = &{$prop};
63                     last GETFILE;
64                 }
65             }
66
67             my $wasIs;
68
69             ($wasIs = $type =~ s/^Is(?:\s+|[-_])?//i)
70               or
71             $type =~ s/^(?:(?:General(?:\s+|_)?)?Category|gc)\s*[:=]\s*//i
72               or
73             $type =~ s/^(?:Script|sc)\s*[:=]\s*//i
74               or
75             $type =~ s/^Block\s*[:=]\s*/In/i;
76
77
78             ##
79             ## See if it's in some enumeration.
80             ##
81             if ($type =~ /^([\w\s]+)[:=]\s*(.*)/) {
82                 require "utf8_pva.pl";
83                 my ($enum, $val) = (lc $1, lc $2);
84                 $enum =~ tr/ _-//d;
85                 $val =~ tr/ _-//d;
86
87                 my $pa = $PropertyAlias{$enum} ? $enum : $PA_reverse{$enum};
88                 my $f = $PropValueAlias{$pa}{$val} ? $val : $PVA_reverse{$pa}{lc $val};
89
90                 if ($pa and $f) {
91                     $pa = "gc_sc" if $pa eq "gc" or $pa eq "sc";
92                     $file = "unicore/lib/$pa/$PVA_abbr_map{$pa}{lc $f}.pl";
93                     last GETFILE;
94                 }
95             }
96             else {
97                 my $t = lc $type;
98                 $t =~ tr/ _-//d;
99
100                 if ($PropValueAlias{gc}{$t} or $PropValueAlias{sc}{$t}) {
101                     $file = "unicore/lib/gc_sc/$PVA_abbr_map{gc_sc}{$t}.pl";
102                     last GETFILE;
103                 }
104             }
105
106             ##
107             ## See if it's in the direct mapping table.
108             ##
109             require "unicore/Exact.pl";
110             if (my $base = $utf8::Exact{$type}) {
111                 $file = "unicore/lib/gc_sc/$base.pl";
112                 last GETFILE;
113             }
114
115             ##
116             ## If not there exactly, try the canonical form. The canonical
117             ## form is lowercased, with any separators (\s+|[-_]) removed.
118             ##
119             my $canonical = lc $type;
120             $canonical =~ s/(?<=[a-z\d])(?:\s+|[-_])(?=[a-z\d])//g;
121             print "canonical = $canonical\n" if DEBUG;
122
123             require "unicore/Canonical.pl";
124             if (my $base = ($utf8::Canonical{$canonical} || $utf8::Canonical{ lc $utf8::PropertyAlias{$canonical} })) {
125                 $file = "unicore/lib/gc_sc/$base.pl";
126                 last GETFILE;
127             }
128
129             ##
130             ## See if it's a user-level "To".
131             ##
132
133             my $caller0 = caller(0);
134
135             if (defined $caller0 && $type =~ /^To(?:\w+)$/) {
136                 my $map = $caller0 . "::" . $type;
137
138                 if (exists &{$map}) {
139                     no strict 'refs';
140                     
141                     $list = &{$map};
142                     last GETFILE;
143                 }
144             }
145
146             ##
147             ## Last attempt -- see if it's a standard "To" name
148             ## (e.g. "ToLower")  ToTitle is used by ucfirst().
149             ## The user-level way to access ToDigit() and ToFold()
150             ## is to use Unicode::UCD.
151             ##
152             if ($type =~ /^To(Digit|Fold|Lower|Title|Upper)$/)
153             {
154                 $file = "unicore/To/$1.pl";
155                 ## would like to test to see if $file actually exists....
156                 last GETFILE;
157             }
158
159             ##
160             ## If we reach this line, it's because we couldn't figure
161             ## out what to do with $type. Ouch.
162             ##
163
164             return $type;
165         }
166
167         if (defined $file) {
168             print "found it (file='$file')\n" if DEBUG;
169
170             ##
171             ## If we reach here, it was due to a 'last GETFILE' above
172             ## (exception: user-defined properties and mappings), so we
173             ## have a filename, so now we load it if we haven't already.
174             ## If we have, return the cached results. The cache key is the
175             ## file to load.
176             ##
177             if ($Cache{$file} and ref($Cache{$file}) eq $class)
178             {
179                 print "Returning cached '$file' for \\p{$type}\n" if DEBUG;
180                 return $Cache{$class, $file};
181             }
182
183             $list = do $file;
184         }
185
186         $ListSorted = 1; ## we know that these lists are sorted
187     }
188
189     my $extras;
190     my $bits = 0;
191
192     my $ORIG = $list;
193     if ($list) {
194         my @tmp = split(/^/m, $list);
195         my %seen;
196         no warnings;
197         $extras = join '', grep /^[^0-9a-fA-F]/, @tmp;
198         $list = join '',
199             map  { $_->[1] }
200             sort { $a->[0] <=> $b->[0] }
201             map  { /^([0-9a-fA-F]+)/; [ hex($1), $_ ] }
202             grep { /^([0-9a-fA-F]+)/ and not $seen{$1}++ } @tmp; # XXX doesn't do ranges right
203     }
204
205     if ($none) {
206         my $hextra = sprintf "%04x", $none + 1;
207         $list =~ s/\tXXXX$/\t$hextra/mg;
208     }
209
210     if ($minbits < 32) {
211         my $top = 0;
212         while ($list =~ /^([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 = defined $3 ? hex $3 : 0;
216             $val += $max - $min if defined $3;
217             $top = $val if $val > $top;
218         }
219         $bits =
220             $top > 0xffff ? 32 :
221             $top > 0xff ? 16 :
222             $top > 1 ? 8 : 1
223     }
224     $bits = $minbits if $bits < $minbits;
225
226     my @extras;
227     for my $x ($extras) {
228         pos $x = 0;
229         while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
230             my $char = $1;
231             my $name = $2;
232             print STDERR "$1 => $2\n" if DEBUG;
233             if ($char =~ /[-+!&]/) {
234                 my ($c,$t) = split(/::/, $name, 2);     # bogus use of ::, really
235                 my $subobj;
236                 if ($c eq 'utf8') {
237                     $subobj = utf8->SWASHNEW($t, "", 0, 0, 0);
238                 }
239                 elsif (exists &$name) {
240                     $subobj = utf8->SWASHNEW($name, "", 0, 0, 0);
241                 }
242                 elsif ($c =~ /^([0-9a-fA-F]+)/) {
243                     $subobj = utf8->SWASHNEW("", $c, 0, 0, 0);
244                 }
245                 return $subobj unless ref $subobj;
246                 push @extras, $name => $subobj;
247                 $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
248             }
249         }
250     }
251
252     print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG;
253
254     my $SWASH = bless {
255         TYPE => $type,
256         BITS => $bits,
257         EXTRAS => $extras,
258         LIST => $list,
259         NONE => $none,
260         @extras,
261     } => $class;
262
263     if ($file) {
264         $Cache{$class, $file} = $SWASH;
265     }
266
267     return $SWASH;
268 }
269
270 # NOTE: utf8.c:swash_init() assumes entries are never modified once generated.
271
272 sub SWASHGET {
273     # See utf8.c:Perl_swash_fetch for problems with this interface.
274     my ($self, $start, $len) = @_;
275     local $^D = 0 if $^D;
276     my $type = $self->{TYPE};
277     my $bits = $self->{BITS};
278     my $none = $self->{NONE};
279     print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if DEBUG;
280     my $end = $start + $len;
281     my $swatch = "";
282     my $key;
283     vec($swatch, $len - 1, $bits) = 0;  # Extend to correct length.
284     if ($none) {
285         for $key (0 .. $len - 1) { vec($swatch, $key, $bits) = $none }
286     }
287
288     for ($self->{LIST}) {
289         pos $_ = 0;
290         if ($bits > 1) {
291           LINE:
292             while (/^([0-9a-fA-F]+)(?:[ \t]([0-9a-fA-F]+)?)?(?:[ \t]([0-9a-fA-F]+))?/mg) {
293                 chomp;
294                 my ($a, $b, $c) = ($1, $2, $3);
295                 croak "$type: illegal mapping '$_'"
296                     if $type =~ /^To/ &&
297                        !(defined $a && defined $c);
298                 my $min = hex $a;
299                 my $max = defined $b ? hex $b : $min;
300                 my $val = defined $c ? hex $c : 0;
301                 next if $max < $start;
302                 print "$min $max $val\n" if DEBUG;
303                 if ($none) {
304                     if ($min < $start) {
305                         $val += $start - $min if $val < $none;
306                         $min = $start;
307                     }
308                     for ($key = $min; $key <= $max; $key++) {
309                         last LINE if $key >= $end;
310                         print STDERR "$key => $val\n" if DEBUG;
311                         vec($swatch, $key - $start, $bits) = $val;
312                         ++$val if $val < $none;
313                     }
314                 }
315                 else {
316                     if ($min < $start) {
317                         $val += $start - $min;
318                         $min = $start;
319                     }
320                     for ($key = $min; $key <= $max; $key++, $val++) {
321                         last LINE if $key >= $end;
322                         print STDERR "$key => $val\n" if DEBUG;
323                         vec($swatch, $key - $start, $bits) = $val;
324                     }
325                 }
326             }
327         }
328         else {
329           LINE:
330             while (/^([0-9a-fA-F]+)(?:[ \t]+([0-9a-fA-F]+))?/mg) {
331                 chomp;
332                 my $min = hex $1;
333                 my $max = defined $2 ? hex $2 : $min;
334                 next if $max < $start;
335                 if ($min < $start) {
336                     $min = $start;
337                 }
338                 for ($key = $min; $key <= $max; $key++) {
339                     last LINE if $key >= $end;
340                     print STDERR "$key => 1\n" if DEBUG;
341                     vec($swatch, $key - $start, 1) = 1;
342                 }
343             }
344         }
345     }
346     for my $x ($self->{EXTRAS}) {
347         pos $x = 0;
348         while ($x =~ /^([-+!&])(.*)/mg) {
349             my $char = $1;
350             my $name = $2;
351             print STDERR "INDIRECT $1 $2\n" if DEBUG;
352             my $otherbits = $self->{$name}->{BITS};
353             croak("SWASHGET size mismatch") if $bits < $otherbits;
354             my $other = $self->{$name}->SWASHGET($start, $len);
355             if ($char eq '+') {
356                 if ($bits == 1 and $otherbits == 1) {
357                     $swatch |= $other;
358                 }
359                 else {
360                     for ($key = 0; $key < $len; $key++) {
361                         vec($swatch, $key, $bits) = vec($other, $key, $otherbits);
362                     }
363                 }
364             }
365             elsif ($char eq '!') {
366                 if ($bits == 1 and $otherbits == 1) {
367                     $swatch |= ~$other;
368                 }
369                 else {
370                     for ($key = 0; $key < $len; $key++) {
371                         if (!vec($other, $key, $otherbits)) {
372                             vec($swatch, $key, $bits) = 1;
373                         }
374                     }
375                 }
376             }
377             elsif ($char eq '-') {
378                 if ($bits == 1 and $otherbits == 1) {
379                     $swatch &= ~$other;
380                 }
381                 else {
382                     for ($key = 0; $key < $len; $key++) {
383                         if (vec($other, $key, $otherbits)) {
384                             vec($swatch, $key, $bits) = 0;
385                         }
386                     }
387                 }
388             }
389             elsif ($char eq '&') {
390                 if ($bits == 1 and $otherbits == 1) {
391                     $swatch &= $other;
392                 }
393                 else {
394                     for ($key = 0; $key < $len; $key++) {
395                         if (!vec($other, $key, $otherbits)) {
396                             vec($swatch, $key, $bits) = 0;
397                         }
398                     }
399                 }
400             }
401         }
402     }
403     if (DEBUG) {
404         print STDERR "CELLS ";
405         for ($key = 0; $key < $len; $key++) {
406             print STDERR vec($swatch, $key, $bits), " ";
407         }
408         print STDERR "\n";
409     }
410     $swatch;
411 }
412
413 1;