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