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