Don't expect ASCII ordering.
[p5sagit/p5-mst-13.2.git] / lib / utf8_heavy.pl
1 package utf8;
2
3 sub DEBUG () { 0 }
4
5 sub DESTROY {}
6
7 sub croak { require Carp; Carp::croak(@_) }
8
9 sub SWASHNEW {
10     my ($class, $type, $list, $minbits, $none) = @_;
11     local $^D = 0 if $^D;
12
13     print STDERR "SWASHNEW @_\n" if DEBUG;
14
15     if ($type and ref ${"${class}::{$type}"} eq $class) {
16         warn qq/Found \${"${class}::{$type}"}\n/ if DEBUG;
17         return ${"${class}::{$type}"};  # Already there...
18     }
19
20     if ($type) {
21         $type =~ s/^\s+//;
22         $type =~ s/\s+$//;
23
24         print "type = $type\n" if DEBUG;
25
26         my $file;
27
28         unless (defined $file) {
29             defined %utf8::Is || do "unicore/Is.pl";
30             if ($type =~ /^(?:Is|Category\s*=\s*)?[- _]?([A-Z].*)$/i) {
31                 my $istype = $1;
32                 print "istype = $istype\n" if DEBUG;
33                 unless ($list = do "unicore/Is/$istype.pl") {
34                     if (exists $utf8::Is{$istype}) {
35                         $file = "unicore/Is/$utf8::Is{$istype}";
36                     } else {
37                         my $isprefix = substr(lc($istype), 0, 2);
38                         print "isprefix = $isprefix\n" if DEBUG;
39                         if (exists $utf8::IsPat{$isprefix}) {
40                             my $Is = $istype;
41                             print "isprefix = $isprefix, Is = $Is\n" if DEBUG;
42                             for my $k (keys %{$utf8::IsPat{$isprefix}}) {
43                                 print "isprefix = $isprefix, Is = $Is, k = $k\n" if DEBUG;
44                                 if ($Is =~ /^$k$/i) {
45                                     $file = "unicore/Is/$utf8::IsPat{$isprefix}->{$k}";
46                                     print "isprefix = $isprefix, Is = $Is, k = $k, file = $file\n" if DEBUG;
47                                     last;
48                                 }
49                             }
50                         }
51                     }
52                 }
53             }
54                         
55             unless (defined $file) {
56                 defined %utf8::In || do "unicore/In.pl";
57                 $type = 'Lampersand' if $type =~ /^(?:Is)?L&$/;
58                 if ($type =~ /^(In|(?:Script|Block)\s*=\s*)?[- _]?(?!herited$)(.+)/i) {
59                     my $incat  = $1;
60                     my $intype = $2;
61                     print "incat = $incat, intype = $intype\n" if DEBUG;
62                     if (exists $utf8::In{$intype}) {
63                         $file = "unicore/In/$utf8::In{$intype}";
64                     } else {
65                         my $inprefix = substr(lc($intype), 0, 2);
66                         print "inprefix = $inprefix\n" if DEBUG;
67                         if (exists $utf8::InPat{$inprefix}) {
68                             my $In = $intype;
69                             print "inprefix = $inprefix, In = $In\n" if DEBUG;
70                             for my $k (keys %{$utf8::InPat{$inprefix}}) {
71                                 print "inprefix = $inprefix, In = $In, k = $k\n" if DEBUG;
72                                 if ($In =~ /^$k$/i) {
73                                     my $i = $utf8::InPat{$inprefix}->{$k};
74                                     print "inprefix = $inprefix, In = $In, k = $k, i = $i\n" if DEBUG;
75                                     next if $incat =~ /^S/ &&
76                                             !exists $utf8::InScript{$i};
77                                     next if $incat =~ /^B/ &&
78                                             !exists $utf8::InBlock{$i};
79                                     $file = "unicore/In/$i";
80                                     print "inprefix = $inprefix, In = $In, k = $k, file = $file\n" if DEBUG;
81                                     last;
82                                 }
83                             }
84                         }
85                     }
86                 }
87             }
88
89             unless (defined $file) {
90                 if ($type =~ /^To([A-Z][A-Za-z]+)$/) {
91                     $file = "unicore/To/$1";
92                 }
93             }
94         }
95
96         if (defined $file) {
97             $list = do "$file.pl";
98         }
99
100         croak("Can't find Unicode character property \"$type\"")
101             unless $list;
102     }
103
104     my $extras;
105     my $bits;
106  
107     if ($list) {
108         my @tmp = split(/^/m, $list);
109         my %seen;
110         no warnings;
111         $extras = join '', grep /^[^0-9a-fA-F]/, @tmp;
112         $list = join '',
113             sort { hex $a <=> hex $b }
114             grep {/^([0-9a-fA-F]+)/ and not $seen{$1}++} @tmp; # XXX doesn't do ranges right
115     }
116
117     if ($none) {
118         my $hextra = sprintf "%04x", $none + 1;
119         $list =~ s/\tXXXX$/\t$hextra/mg;
120     }
121
122     if ($minbits < 32) {
123         my $top = 0;
124         while ($list =~ /^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
125             my $min = hex $1;
126             my $max = hex(defined $2 ? $2 : $1);
127             my $val = hex(defined $3 ? $3 : "");
128             $val += $max - $min if defined $3;
129             $top = $val if $val > $top;
130         }
131         $bits =
132             $top > 0xffff ? 32 :
133             $top > 0xff ? 16 :
134             $top > 1 ? 8 : 1
135     }
136     $bits = $minbits if $bits < $minbits;
137
138     my @extras;
139     for my $x ($extras) {
140         pos $x = 0;
141         while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
142             my $char = $1;
143             my $name = $2;
144             print STDERR "$1 => $2\n" if DEBUG;
145             if ($char =~ /[-+!]/) {
146                 my ($c,$t) = split(/::/, $name, 2);     # bogus use of ::, really
147                 my $subobj = $c->SWASHNEW($t, "", 0, 0, 0);
148                 push @extras, $name => $subobj;
149                 $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
150             }
151         }
152     }
153
154     print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG;
155
156     ${"${class}::{$type}"} = bless {
157         TYPE => $type,
158         BITS => $bits,
159         EXTRAS => $extras,
160         LIST => $list,
161         NONE => $none,
162         @extras,
163     } => $class;
164 }
165
166 # NOTE: utf8.c:swash_init() assumes entries are never modified once generated.
167
168 sub SWASHGET {
169     # See utf8.c:Perl_swash_fetch for problems with this interface.
170     my ($self, $start, $len) = @_;
171     local $^D = 0 if $^D;
172     my $type = $self->{TYPE};
173     my $bits = $self->{BITS};
174     my $none = $self->{NONE};
175     print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if DEBUG;
176     my $end = $start + $len;
177     my $swatch = "";
178     my $key;
179     vec($swatch, $len - 1, $bits) = 0;  # Extend to correct length.
180     if ($none) {
181         for $key (0 .. $len - 1) { vec($swatch, $key, $bits) = $none }
182     }
183
184     for ($self->{LIST}) {
185         pos $_ = 0;
186         if ($bits > 1) {
187           LINE:
188             while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
189                 my $min = hex $1;
190                 my $max = (defined $2 ? hex $2 : $min);
191                 my $val = hex $3;
192                 next if $max < $start;
193                 print "$min $max $val\n" if DEBUG;
194                 if ($none) {
195                     if ($min < $start) {
196                         $val += $start - $min if $val < $none;
197                         $min = $start;
198                     }
199                     for ($key = $min; $key <= $max; $key++) {
200                         last LINE if $key >= $end;
201                         print STDERR "$key => $val\n" if DEBUG;
202                         vec($swatch, $key - $start, $bits) = $val;
203                         ++$val if $val < $none;
204                     }
205                 }
206                 else {
207                     if ($min < $start) {
208                         $val += $start - $min;
209                         $min = $start;
210                     }
211                     for ($key = $min; $key <= $max; $key++, $val++) {
212                         last LINE if $key >= $end;
213                         print STDERR "$key => $val\n" if DEBUG;
214                         vec($swatch, $key - $start, $bits) = $val;
215                     }
216                 }
217             }
218         }
219         else {
220           LINE:
221             while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+))?/mg) {
222                 my $min = hex $1;
223                 my $max = (defined $2 ? hex $2 : $min);
224                 next if $max < $start;
225                 if ($min < $start) {
226                     $min = $start;
227                 }
228                 for ($key = $min; $key <= $max; $key++) {
229                     last LINE if $key >= $end;
230                     print STDERR "$key => 1\n" if DEBUG;
231                     vec($swatch, $key - $start, 1) = 1;
232                 }
233             }
234         }
235     }
236     for my $x ($self->{EXTRAS}) {
237         pos $x = 0;
238         while ($x =~ /^([-+!])(.*)/mg) {
239             my $char = $1;
240             my $name = $2;
241             print STDERR "INDIRECT $1 $2\n" if DEBUG;
242             my $otherbits = $self->{$name}->{BITS};
243             croak("SWASHGET size mismatch") if $bits < $otherbits;
244             my $other = $self->{$name}->SWASHGET($start, $len);
245             if ($char eq '+') {
246                 if ($bits == 1 and $otherbits == 1) {
247                     $swatch |= $other;
248                 }
249                 else {
250                     for ($key = 0; $key < $len; $key++) {
251                         vec($swatch, $key, $bits) = vec($other, $key, $otherbits);
252                     }
253                 }
254             }
255             elsif ($char eq '!') {
256                 if ($bits == 1 and $otherbits == 1) {
257                     $swatch |= ~$other;
258                 }
259                 else {
260                     for ($key = 0; $key < $len; $key++) {
261                         if (!vec($other, $key, $otherbits)) {
262                             vec($swatch, $key, $bits) = 1;
263                         }
264                     }
265                 }
266             }
267             elsif ($char eq '-') {
268                 if ($bits == 1 and $otherbits == 1) {
269                     $swatch &= ~$other;
270                 }
271                 else {
272                     for ($key = 0; $key < $len; $key++) {
273                         if (vec($other, $key, $otherbits)) {
274                             vec($swatch, $key, $bits) = 0;
275                         }
276                     }
277                 }
278             }
279         }
280     }
281     if (DEBUG) {
282         print STDERR "CELLS ";
283         for ($key = 0; $key < $len; $key++) {
284             print STDERR vec($swatch, $key, $bits), " ";
285         }
286         print STDERR "\n";
287     }
288     $swatch;
289 }
290
291 1;