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