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