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