Commit | Line | Data |
---|---|---|
a0ed51b3 LW |
1 | package utf8; |
2 | ||
15732964 | 3 | sub DEBUG () { 0 } |
a0ed51b3 LW |
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 JH |
12 | |
13 | print STDERR "SWASHNEW @_\n" if DEBUG; | |
14 | ||
a0ed51b3 | 15 | if ($type and ref ${"${class}::{$type}"} eq $class) { |
15732964 | 16 | warn qq/Found \${"${class}::{$type}"}\n/ if DEBUG; |
a0ed51b3 LW |
17 | return ${"${class}::{$type}"}; # Already there... |
18 | } | |
19 | ||
15732964 | 20 | if ($type) { |
d73e5302 JH |
21 | $type =~ s/^\s+//; |
22 | $type =~ s/\s+$//; | |
15732964 | 23 | |
d73e5302 | 24 | print "type = $type\n" if DEBUG; |
15732964 | 25 | |
d73e5302 | 26 | my $file; |
15732964 | 27 | |
d73e5302 JH |
28 | unless (defined $file) { |
29 | defined %utf8::Is || do "unicore/Is.pl"; | |
71d929cb | 30 | if ($type =~ /^(?:Is|Category\s*=\s*)?[- _]?([A-Z].*)$/i) { |
d73e5302 JH |
31 | my $istype = $1; |
32 | print "istype = $istype\n" if DEBUG; | |
33 | unless ($list = do "unicore/Is/$istype.pl") { | |
34 | if (exists $utf8::Is{$istype}) { | |
35 | $file = "unicore/Is/$utf8::Is{$istype}"; | |
36 | } else { | |
37 | my $isprefix = substr(lc($istype), 0, 2); | |
38 | print "isprefix = $isprefix\n" if DEBUG; | |
39 | if (exists $utf8::IsPat{$isprefix}) { | |
40 | my $Is = $istype; | |
41 | print "isprefix = $isprefix, Is = $Is\n" if DEBUG; | |
42 | for my $k (keys %{$utf8::IsPat{$isprefix}}) { | |
43 | print "isprefix = $isprefix, Is = $Is, k = $k\n" if DEBUG; | |
44 | if ($Is =~ /^$k$/i) { | |
45 | $file = "unicore/Is/$utf8::IsPat{$isprefix}->{$k}"; | |
46 | print "isprefix = $isprefix, Is = $Is, k = $k, file = $file\n" if DEBUG; | |
47 | last; | |
48 | } | |
49 | } | |
50 | } | |
51 | } | |
52 | } | |
53 | } | |
fa689c23 | 54 | |
d73e5302 JH |
55 | unless (defined $file) { |
56 | defined %utf8::In || do "unicore/In.pl"; | |
57 | $type = 'Lampersand' if $type =~ /^(?:Is)?L&$/; | |
76ccdbe2 JH |
58 | $type = 'Assigned' if $type =~ /^(?:Is)?Assigned$/i; |
59 | $type = 'Unassigned' if $type =~ /^(?:Is)?Unassigned$/i; | |
71d929cb | 60 | if ($type =~ /^(In|(?:Script|Block)\s*=\s*)?[- _]?(?!herited$)(.+)/i) { |
fa689c23 | 61 | my $incat = $1 || ''; |
71d929cb JH |
62 | my $intype = $2; |
63 | print "incat = $incat, intype = $intype\n" if DEBUG; | |
64 | if (exists $utf8::In{$intype}) { | |
d73e5302 JH |
65 | $file = "unicore/In/$utf8::In{$intype}"; |
66 | } else { | |
67 | my $inprefix = substr(lc($intype), 0, 2); | |
68 | print "inprefix = $inprefix\n" if DEBUG; | |
69 | if (exists $utf8::InPat{$inprefix}) { | |
70 | my $In = $intype; | |
71 | print "inprefix = $inprefix, In = $In\n" if DEBUG; | |
72 | for my $k (keys %{$utf8::InPat{$inprefix}}) { | |
73 | print "inprefix = $inprefix, In = $In, k = $k\n" if DEBUG; | |
74 | if ($In =~ /^$k$/i) { | |
71d929cb JH |
75 | my $i = $utf8::InPat{$inprefix}->{$k}; |
76 | print "inprefix = $inprefix, In = $In, k = $k, i = $i\n" if DEBUG; | |
77 | next if $incat =~ /^S/ && | |
78 | !exists $utf8::InScript{$i}; | |
79 | next if $incat =~ /^B/ && | |
80 | !exists $utf8::InBlock{$i}; | |
81 | $file = "unicore/In/$i"; | |
d73e5302 JH |
82 | print "inprefix = $inprefix, In = $In, k = $k, file = $file\n" if DEBUG; |
83 | last; | |
84 | } | |
85 | } | |
86 | } | |
d9efae67 JH |
87 | } |
88 | } | |
89 | } | |
15732964 | 90 | |
d73e5302 JH |
91 | unless (defined $file) { |
92 | if ($type =~ /^To([A-Z][A-Za-z]+)$/) { | |
93 | $file = "unicore/To/$1"; | |
94 | } | |
dee4fd60 | 95 | } |
15732964 | 96 | } |
886ba366 | 97 | |
d73e5302 JH |
98 | if (defined $file) { |
99 | $list = do "$file.pl"; | |
100 | } | |
101 | ||
102 | croak("Can't find Unicode character property \"$type\"") | |
103 | unless $list; | |
886ba366 | 104 | } |
a0ed51b3 | 105 | |
15732964 JH |
106 | my $extras; |
107 | my $bits; | |
108 | ||
a0ed51b3 LW |
109 | if ($list) { |
110 | my @tmp = split(/^/m, $list); | |
111 | my %seen; | |
db376a24 | 112 | no warnings; |
a0ed51b3 LW |
113 | $extras = join '', grep /^[^0-9a-fA-F]/, @tmp; |
114 | $list = join '', | |
115 | sort { hex $a <=> hex $b } | |
116 | grep {/^([0-9a-fA-F]+)/ and not $seen{$1}++} @tmp; # XXX doesn't do ranges right | |
117 | } | |
118 | ||
119 | if ($none) { | |
120 | my $hextra = sprintf "%04x", $none + 1; | |
121 | $list =~ s/\tXXXX$/\t$hextra/mg; | |
122 | } | |
123 | ||
124 | if ($minbits < 32) { | |
125 | my $top = 0; | |
126 | while ($list =~ /^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) { | |
127 | my $min = hex $1; | |
128 | my $max = hex(defined $2 ? $2 : $1); | |
129 | my $val = hex(defined $3 ? $3 : ""); | |
130 | $val += $max - $min if defined $3; | |
131 | $top = $val if $val > $top; | |
132 | } | |
133 | $bits = | |
134 | $top > 0xffff ? 32 : | |
135 | $top > 0xff ? 16 : | |
136 | $top > 1 ? 8 : 1 | |
137 | } | |
138 | $bits = $minbits if $bits < $minbits; | |
139 | ||
140 | my @extras; | |
141 | for my $x ($extras) { | |
142 | pos $x = 0; | |
1fef36c7 | 143 | while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) { |
a0ed51b3 LW |
144 | my $char = $1; |
145 | my $name = $2; | |
6d6ae962 | 146 | print STDERR "$1 => $2\n" if DEBUG; |
a0ed51b3 LW |
147 | if ($char =~ /[-+!]/) { |
148 | my ($c,$t) = split(/::/, $name, 2); # bogus use of ::, really | |
149 | my $subobj = $c->SWASHNEW($t, "", 0, 0, 0); | |
150 | push @extras, $name => $subobj; | |
151 | $bits = $subobj->{BITS} if $bits < $subobj->{BITS}; | |
152 | } | |
153 | } | |
154 | } | |
155 | ||
15732964 | 156 | print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG; |
a0ed51b3 LW |
157 | |
158 | ${"${class}::{$type}"} = bless { | |
159 | TYPE => $type, | |
160 | BITS => $bits, | |
161 | EXTRAS => $extras, | |
162 | LIST => $list, | |
163 | NONE => $none, | |
164 | @extras, | |
165 | } => $class; | |
166 | } | |
167 | ||
168 | # NOTE: utf8.c:swash_init() assumes entries are never modified once generated. | |
169 | ||
170 | sub SWASHGET { | |
035d37be | 171 | # See utf8.c:Perl_swash_fetch for problems with this interface. |
a0ed51b3 LW |
172 | my ($self, $start, $len) = @_; |
173 | local $^D = 0 if $^D; | |
a0ed51b3 LW |
174 | my $type = $self->{TYPE}; |
175 | my $bits = $self->{BITS}; | |
176 | my $none = $self->{NONE}; | |
15732964 | 177 | print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if DEBUG; |
a0ed51b3 LW |
178 | my $end = $start + $len; |
179 | my $swatch = ""; | |
180 | my $key; | |
181 | vec($swatch, $len - 1, $bits) = 0; # Extend to correct length. | |
182 | if ($none) { | |
183 | for $key (0 .. $len - 1) { vec($swatch, $key, $bits) = $none } | |
184 | } | |
185 | ||
186 | for ($self->{LIST}) { | |
187 | pos $_ = 0; | |
188 | if ($bits > 1) { | |
189 | LINE: | |
190 | while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) { | |
191 | my $min = hex $1; | |
192 | my $max = (defined $2 ? hex $2 : $min); | |
193 | my $val = hex $3; | |
194 | next if $max < $start; | |
6d6ae962 | 195 | print "$min $max $val\n" if DEBUG; |
a0ed51b3 LW |
196 | if ($none) { |
197 | if ($min < $start) { | |
710250cb | 198 | $val += $start - $min if $val < $none; |
a0ed51b3 LW |
199 | $min = $start; |
200 | } | |
201 | for ($key = $min; $key <= $max; $key++) { | |
202 | last LINE if $key >= $end; | |
6d6ae962 | 203 | print STDERR "$key => $val\n" if DEBUG; |
a0ed51b3 LW |
204 | vec($swatch, $key - $start, $bits) = $val; |
205 | ++$val if $val < $none; | |
206 | } | |
207 | } | |
208 | else { | |
209 | if ($min < $start) { | |
210 | $val += $start - $min; | |
211 | $min = $start; | |
212 | } | |
213 | for ($key = $min; $key <= $max; $key++, $val++) { | |
214 | last LINE if $key >= $end; | |
6d6ae962 | 215 | print STDERR "$key => $val\n" if DEBUG; |
a0ed51b3 LW |
216 | vec($swatch, $key - $start, $bits) = $val; |
217 | } | |
218 | } | |
219 | } | |
220 | } | |
221 | else { | |
222 | LINE: | |
223 | while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+))?/mg) { | |
224 | my $min = hex $1; | |
225 | my $max = (defined $2 ? hex $2 : $min); | |
226 | next if $max < $start; | |
227 | if ($min < $start) { | |
228 | $min = $start; | |
229 | } | |
230 | for ($key = $min; $key <= $max; $key++) { | |
231 | last LINE if $key >= $end; | |
6d6ae962 | 232 | print STDERR "$key => 1\n" if DEBUG; |
a0ed51b3 LW |
233 | vec($swatch, $key - $start, 1) = 1; |
234 | } | |
235 | } | |
236 | } | |
237 | } | |
238 | for my $x ($self->{EXTRAS}) { | |
239 | pos $x = 0; | |
1fef36c7 | 240 | while ($x =~ /^([-+!])(.*)/mg) { |
a0ed51b3 LW |
241 | my $char = $1; |
242 | my $name = $2; | |
15732964 | 243 | print STDERR "INDIRECT $1 $2\n" if DEBUG; |
1fef36c7 GA |
244 | my $otherbits = $self->{$name}->{BITS}; |
245 | croak("SWASHGET size mismatch") if $bits < $otherbits; | |
246 | my $other = $self->{$name}->SWASHGET($start, $len); | |
247 | if ($char eq '+') { | |
248 | if ($bits == 1 and $otherbits == 1) { | |
249 | $swatch |= $other; | |
a0ed51b3 | 250 | } |
1fef36c7 GA |
251 | else { |
252 | for ($key = 0; $key < $len; $key++) { | |
253 | vec($swatch, $key, $bits) = vec($other, $key, $otherbits); | |
a0ed51b3 | 254 | } |
1fef36c7 GA |
255 | } |
256 | } | |
257 | elsif ($char eq '!') { | |
258 | if ($bits == 1 and $otherbits == 1) { | |
259 | $swatch |= ~$other; | |
260 | } | |
261 | else { | |
262 | for ($key = 0; $key < $len; $key++) { | |
263 | if (!vec($other, $key, $otherbits)) { | |
264 | vec($swatch, $key, $bits) = 1; | |
a0ed51b3 LW |
265 | } |
266 | } | |
267 | } | |
1fef36c7 GA |
268 | } |
269 | elsif ($char eq '-') { | |
270 | if ($bits == 1 and $otherbits == 1) { | |
271 | $swatch &= ~$other; | |
272 | } | |
273 | else { | |
274 | for ($key = 0; $key < $len; $key++) { | |
275 | if (vec($other, $key, $otherbits)) { | |
276 | vec($swatch, $key, $bits) = 0; | |
a0ed51b3 LW |
277 | } |
278 | } | |
279 | } | |
280 | } | |
281 | } | |
282 | } | |
15732964 | 283 | if (DEBUG) { |
a0ed51b3 LW |
284 | print STDERR "CELLS "; |
285 | for ($key = 0; $key < $len; $key++) { | |
286 | print STDERR vec($swatch, $key, $bits), " "; | |
287 | } | |
288 | print STDERR "\n"; | |
289 | } | |
290 | $swatch; | |
291 | } | |
292 | ||
293 | 1; |