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