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