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