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