Commit | Line | Data |
a0ed51b3 |
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; |
9fdf68be |
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 | } |
886ba366 |
37 | |
38 | { |
0064a8a9 |
39 | $list ||= |
40 | ( exists &{"${caller}::${type}"} && |
41 | eval { $caller->$type() } ) |
886ba366 |
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 | } |
a0ed51b3 |
47 | |
48 | $| = 1; |
49 | |
50 | if ($list) { |
51 | my @tmp = split(/^/m, $list); |
52 | my %seen; |
db376a24 |
53 | no warnings; |
a0ed51b3 |
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; |
1fef36c7 |
84 | while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) { |
a0ed51b3 |
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; |
a0ed51b3 |
114 | my $type = $self->{TYPE}; |
115 | my $bits = $self->{BITS}; |
116 | my $none = $self->{NONE}; |
1fef36c7 |
117 | print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if $DEBUG; |
a0ed51b3 |
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) { |
710250cb |
138 | $val += $start - $min if $val < $none; |
a0ed51b3 |
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; |
1fef36c7 |
180 | while ($x =~ /^([-+!])(.*)/mg) { |
a0ed51b3 |
181 | my $char = $1; |
182 | my $name = $2; |
183 | print STDERR "INDIRECT $1 $2\n" if $DEBUG; |
1fef36c7 |
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; |
a0ed51b3 |
190 | } |
1fef36c7 |
191 | else { |
192 | for ($key = 0; $key < $len; $key++) { |
193 | vec($swatch, $key, $bits) = vec($other, $key, $otherbits); |
a0ed51b3 |
194 | } |
1fef36c7 |
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; |
a0ed51b3 |
205 | } |
206 | } |
207 | } |
1fef36c7 |
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; |
a0ed51b3 |
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; |