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