This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
The non-MakeMaker.pm parts of
[perl5.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 my %Cache;
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     ## unorthadox 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             $type =~ s/^Is(?:\s+|[-_])?//i
54               or
55             $type =~ s/^Category\s*=\s*//i
56               or
57             $type =~ s/^Script\s*=\s*//i
58               or
59             $type =~ s/^Block\s*=\s*/In/i;
60
61             ##
62             ## See if it's in the direct mapping table.
63             ##
64             require "unicore/Exact.pl";
65             if (my $base = $utf8::Exact{$type}) {
66                 $file = "unicore/lib/$base.pl";
67                 last GETFILE;
68             }
69
70             ##
71             ## If not there exactly, try the canonical form. The canonical
72             ## form is lowercased, with any separators (\s+|[-_]) removed.
73             ##
74             my $canonical = lc $type;
75             $canonical =~ s/(?<=[a-z\d])(?:\s+|[-_])(?=[a-z\d])//g;
76             print "canonical = $canonical\n" if DEBUG;
77
78             require "unicore/Canonical.pl";
79             if (my $base = $utf8::Canonical{$canonical}) {
80                 $file = "unicore/lib/$base.pl";
81                 last GETFILE;
82             }
83
84             ##
85             ## Last attempt -- see if it's a "To" name (e.g. "ToLower")
86             ##
87             if ($type =~ /^To([A-Z][A-Za-z]+)$/)
88             {
89                 $file = "unicore/To/$1.pl";
90                 ## would like to test to see if $file actually exists....
91                 last GETFILE;
92             }
93
94             ##
95             ## If we reach this line, it's because we couldn't figure
96             ## out what to do with $type. Ouch.
97             ##
98             croak("Can't find Unicode character property \"$type\"");
99         }
100
101         print "found it (file='$file')\n" if DEBUG;
102
103         ##
104         ## If we reach here, it was due to a 'last GETFILE' above, so we
105         ## have a filename, so now we load it if we haven't already.
106         ## If we have, return the cached results. The cache key is the
107         ## file to load.
108         ##
109         if ($Cache{$file} and ref($Cache{$file}) eq $class)
110         {
111             print "Returning cached '$file' for \\p{$type}\n" if DEBUG;
112             return $Cache{$class, $file};
113         }
114
115         $list = do $file;
116         $ListSorted = 1; ## we know that these lists are sorted
117     }
118
119     my $extras;
120     my $bits;
121
122     my $ORIG = $list;
123     if ($list) {
124         my @tmp = split(/^/m, $list);
125         my %seen;
126         no warnings;
127         $extras = join '', grep /^[^0-9a-fA-F]/, @tmp;
128         $list = join '',
129             sort { hex $a <=> hex $b }
130             grep {/^([0-9a-fA-F]+)/ and not $seen{$1}++} @tmp; # XXX doesn't do ranges right
131     }
132
133     if ($none) {
134         my $hextra = sprintf "%04x", $none + 1;
135         $list =~ s/\tXXXX$/\t$hextra/mg;
136     }
137
138     if ($minbits < 32) {
139         my $top = 0;
140         while ($list =~ /^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
141             my $min = hex $1;
142             my $max = hex(defined $2 ? $2 : $1);
143             my $val = hex(defined $3 ? $3 : "");
144             $val += $max - $min if defined $3;
145             $top = $val if $val > $top;
146         }
147         $bits =
148             $top > 0xffff ? 32 :
149             $top > 0xff ? 16 :
150             $top > 1 ? 8 : 1
151     }
152     $bits = $minbits if $bits < $minbits;
153
154     my @extras;
155     for my $x ($extras) {
156         pos $x = 0;
157         while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
158             my $char = $1;
159             my $name = $2;
160             print STDERR "$1 => $2\n" if DEBUG;
161             if ($char =~ /[-+!]/) {
162                 my ($c,$t) = split(/::/, $name, 2);     # bogus use of ::, really
163                 my $subobj = $c->SWASHNEW($t, "", 0, 0, 0);
164                 push @extras, $name => $subobj;
165                 $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
166             }
167         }
168     }
169
170     print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG;
171
172     my $SWASH = bless {
173         TYPE => $type,
174         BITS => $bits,
175         EXTRAS => $extras,
176         LIST => $list,
177         NONE => $none,
178         @extras,
179     } => $class;
180
181     if ($file) {
182         $Cache{$class, $file} = $SWASH;
183     }
184
185     return $SWASH;
186 }
187
188 # NOTE: utf8.c:swash_init() assumes entries are never modified once generated.
189
190 sub SWASHGET {
191     # See utf8.c:Perl_swash_fetch for problems with this interface.
192     my ($self, $start, $len) = @_;
193     local $^D = 0 if $^D;
194     my $type = $self->{TYPE};
195     my $bits = $self->{BITS};
196     my $none = $self->{NONE};
197     print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if DEBUG;
198     my $end = $start + $len;
199     my $swatch = "";
200     my $key;
201     vec($swatch, $len - 1, $bits) = 0;  # Extend to correct length.
202     if ($none) {
203         for $key (0 .. $len - 1) { vec($swatch, $key, $bits) = $none }
204     }
205
206     for ($self->{LIST}) {
207         pos $_ = 0;
208         if ($bits > 1) {
209           LINE:
210             while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
211                 my $min = hex $1;
212                 my $max = (defined $2 ? hex $2 : $min);
213                 my $val = hex $3;
214                 next if $max < $start;
215                 print "$min $max $val\n" if DEBUG;
216                 if ($none) {
217                     if ($min < $start) {
218                         $val += $start - $min if $val < $none;
219                         $min = $start;
220                     }
221                     for ($key = $min; $key <= $max; $key++) {
222                         last LINE if $key >= $end;
223                         print STDERR "$key => $val\n" if DEBUG;
224                         vec($swatch, $key - $start, $bits) = $val;
225                         ++$val if $val < $none;
226                     }
227                 }
228                 else {
229                     if ($min < $start) {
230                         $val += $start - $min;
231                         $min = $start;
232                     }
233                     for ($key = $min; $key <= $max; $key++, $val++) {
234                         last LINE if $key >= $end;
235                         print STDERR "$key => $val\n" if DEBUG;
236                         vec($swatch, $key - $start, $bits) = $val;
237                     }
238                 }
239             }
240         }
241         else {
242           LINE:
243             while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+))?/mg) {
244                 my $min = hex $1;
245                 my $max = (defined $2 ? hex $2 : $min);
246                 next if $max < $start;
247                 if ($min < $start) {
248                     $min = $start;
249                 }
250                 for ($key = $min; $key <= $max; $key++) {
251                     last LINE if $key >= $end;
252                     print STDERR "$key => 1\n" if DEBUG;
253                     vec($swatch, $key - $start, 1) = 1;
254                 }
255             }
256         }
257     }
258     for my $x ($self->{EXTRAS}) {
259         pos $x = 0;
260         while ($x =~ /^([-+!])(.*)/mg) {
261             my $char = $1;
262             my $name = $2;
263             print STDERR "INDIRECT $1 $2\n" if DEBUG;
264             my $otherbits = $self->{$name}->{BITS};
265             croak("SWASHGET size mismatch") if $bits < $otherbits;
266             my $other = $self->{$name}->SWASHGET($start, $len);
267             if ($char eq '+') {
268                 if ($bits == 1 and $otherbits == 1) {
269                     $swatch |= $other;
270                 }
271                 else {
272                     for ($key = 0; $key < $len; $key++) {
273                         vec($swatch, $key, $bits) = vec($other, $key, $otherbits);
274                     }
275                 }
276             }
277             elsif ($char eq '!') {
278                 if ($bits == 1 and $otherbits == 1) {
279                     $swatch |= ~$other;
280                 }
281                 else {
282                     for ($key = 0; $key < $len; $key++) {
283                         if (!vec($other, $key, $otherbits)) {
284                             vec($swatch, $key, $bits) = 1;
285                         }
286                     }
287                 }
288             }
289             elsif ($char eq '-') {
290                 if ($bits == 1 and $otherbits == 1) {
291                     $swatch &= ~$other;
292                 }
293                 else {
294                     for ($key = 0; $key < $len; $key++) {
295                         if (vec($other, $key, $otherbits)) {
296                             vec($swatch, $key, $bits) = 0;
297                         }
298                     }
299                 }
300             }
301         }
302     }
303     if (DEBUG) {
304         print STDERR "CELLS ";
305         for ($key = 0; $key < $len; $key++) {
306             print STDERR vec($swatch, $key, $bits), " ";
307         }
308         print STDERR "\n";
309     }
310     $swatch;
311 }
312
313 1;