Unicode properties: fix L& (the #12319 didn't allow L&,
[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     my $file;
16
17     if ($type and ref ${"${class}::{$type}"} eq $class) {
18         warn qq/Found \${"${class}::{$type}"}\n/ if DEBUG;
19         return ${"${class}::{$type}"};  # Already there...
20     }
21
22     if ($type) {
23
24         defined %utf8::In || do "unicore/In.pl";
25
26         $type =~ s/^In(?:[-_]|\s+)?(?!herited$)//i;
27         $type =~ s/\s+$//;
28
29         my $inprefix = substr(lc($type), 0, 3);
30         if (exists $utf8::InPat{$inprefix}) {
31             my $In = $type;
32             for my $k (keys %{$utf8::InPat{$inprefix}}) {
33                 if ($In =~ /^$k$/i) {
34                     $In = $utf8::InPat{$inprefix}->{$k};
35                     if (exists $utf8::In{$In}) {
36                         $file = "unicore/In/$utf8::In{$In}";
37                         print "inprefix = $inprefix, In = $In, k = $k, file = $file\n" if DEBUG;
38                         last;
39                     }
40                 }
41             }
42         }
43
44         unless (defined $file) {
45             # This is separate from 'To' in preparation of Is.pl (a la In.pl).
46             if ($type =~ /^Is([A-Z][A-Za-z]*|L&)$/ || $type =~ /^(L&)$/ ) {
47                 my $cat = $1 eq 'L&' ? 'L' : $1;
48                 $file = "unicore/Is/$cat";
49             } elsif ((not defined $file) && $type =~ /^To([A-Z][A-Za-z]*)$/) {
50                 $file = "unicore/To/$1";
51             }
52         }
53     }
54
55     {
56         $list ||= do "$file.pl"
57               ||  do "unicore/Is/$type.pl"
58               ||  croak("Can't find Unicode character property \"$type\"");
59     }
60
61     my $extras;
62     my $bits;
63  
64     if ($list) {
65         my @tmp = split(/^/m, $list);
66         my %seen;
67         no warnings;
68         $extras = join '', grep /^[^0-9a-fA-F]/, @tmp;
69         $list = join '',
70             sort { hex $a <=> hex $b }
71             grep {/^([0-9a-fA-F]+)/ and not $seen{$1}++} @tmp; # XXX doesn't do ranges right
72     }
73
74     if ($none) {
75         my $hextra = sprintf "%04x", $none + 1;
76         $list =~ s/\tXXXX$/\t$hextra/mg;
77     }
78
79     if ($minbits < 32) {
80         my $top = 0;
81         while ($list =~ /^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
82             my $min = hex $1;
83             my $max = hex(defined $2 ? $2 : $1);
84             my $val = hex(defined $3 ? $3 : "");
85             $val += $max - $min if defined $3;
86             $top = $val if $val > $top;
87         }
88         $bits =
89             $top > 0xffff ? 32 :
90             $top > 0xff ? 16 :
91             $top > 1 ? 8 : 1
92     }
93     $bits = $minbits if $bits < $minbits;
94
95     my @extras;
96     for my $x ($extras) {
97         pos $x = 0;
98         while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
99             my $char = $1;
100             my $name = $2;
101 #           print STDERR "$1 => $2\n" if DEBUG;
102             if ($char =~ /[-+!]/) {
103                 my ($c,$t) = split(/::/, $name, 2);     # bogus use of ::, really
104                 my $subobj = $c->SWASHNEW($t, "", 0, 0, 0);
105                 push @extras, $name => $subobj;
106                 $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
107             }
108         }
109     }
110
111     print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG;
112
113     ${"${class}::{$type}"} = bless {
114         TYPE => $type,
115         BITS => $bits,
116         EXTRAS => $extras,
117         LIST => $list,
118         NONE => $none,
119         @extras,
120     } => $class;
121 }
122
123 # NOTE: utf8.c:swash_init() assumes entries are never modified once generated.
124
125 sub SWASHGET {
126     my ($self, $start, $len) = @_;
127     local $^D = 0 if $^D;
128     my $type = $self->{TYPE};
129     my $bits = $self->{BITS};
130     my $none = $self->{NONE};
131     print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if DEBUG;
132     my $end = $start + $len;
133     my $swatch = "";
134     my $key;
135     vec($swatch, $len - 1, $bits) = 0;  # Extend to correct length.
136     if ($none) {
137         for $key (0 .. $len - 1) { vec($swatch, $key, $bits) = $none }
138     }
139
140     for ($self->{LIST}) {
141         pos $_ = 0;
142         if ($bits > 1) {
143           LINE:
144             while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
145                 my $min = hex $1;
146                 my $max = (defined $2 ? hex $2 : $min);
147                 my $val = hex $3;
148                 next if $max < $start;
149 #               print "$min $max $val\n";
150                 if ($none) {
151                     if ($min < $start) {
152                         $val += $start - $min if $val < $none;
153                         $min = $start;
154                     }
155                     for ($key = $min; $key <= $max; $key++) {
156                         last LINE if $key >= $end;
157 #                       print STDERR "$key => $val\n" if DEBUG;
158                         vec($swatch, $key - $start, $bits) = $val;
159                         ++$val if $val < $none;
160                     }
161                 }
162                 else {
163                     if ($min < $start) {
164                         $val += $start - $min;
165                         $min = $start;
166                     }
167                     for ($key = $min; $key <= $max; $key++, $val++) {
168                         last LINE if $key >= $end;
169 #                       print STDERR "$key => $val\n" if DEBUG;
170                         vec($swatch, $key - $start, $bits) = $val;
171                     }
172                 }
173             }
174         }
175         else {
176           LINE:
177             while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+))?/mg) {
178                 my $min = hex $1;
179                 my $max = (defined $2 ? hex $2 : $min);
180                 next if $max < $start;
181                 if ($min < $start) {
182                     $min = $start;
183                 }
184                 for ($key = $min; $key <= $max; $key++) {
185                     last LINE if $key >= $end;
186 #                   print STDERR "$key => 1\n" if DEBUG;
187                     vec($swatch, $key - $start, 1) = 1;
188                 }
189             }
190         }
191     }
192     for my $x ($self->{EXTRAS}) {
193         pos $x = 0;
194         while ($x =~ /^([-+!])(.*)/mg) {
195             my $char = $1;
196             my $name = $2;
197             print STDERR "INDIRECT $1 $2\n" if DEBUG;
198             my $otherbits = $self->{$name}->{BITS};
199             croak("SWASHGET size mismatch") if $bits < $otherbits;
200             my $other = $self->{$name}->SWASHGET($start, $len);
201             if ($char eq '+') {
202                 if ($bits == 1 and $otherbits == 1) {
203                     $swatch |= $other;
204                 }
205                 else {
206                     for ($key = 0; $key < $len; $key++) {
207                         vec($swatch, $key, $bits) = vec($other, $key, $otherbits);
208                     }
209                 }
210             }
211             elsif ($char eq '!') {
212                 if ($bits == 1 and $otherbits == 1) {
213                     $swatch |= ~$other;
214                 }
215                 else {
216                     for ($key = 0; $key < $len; $key++) {
217                         if (!vec($other, $key, $otherbits)) {
218                             vec($swatch, $key, $bits) = 1;
219                         }
220                     }
221                 }
222             }
223             elsif ($char eq '-') {
224                 if ($bits == 1 and $otherbits == 1) {
225                     $swatch &= ~$other;
226                 }
227                 else {
228                     for ($key = 0; $key < $len; $key++) {
229                         if (vec($other, $key, $otherbits)) {
230                             vec($swatch, $key, $bits) = 0;
231                         }
232                     }
233                 }
234             }
235         }
236     }
237     if (DEBUG) {
238         print STDERR "CELLS ";
239         for ($key = 0; $key < $len; $key++) {
240             print STDERR vec($swatch, $key, $bits), " ";
241         }
242         print STDERR "\n";
243     }
244     $swatch;
245 }
246
247 1;