Re: [perl #18888] $Exporter::Verbose=1 does not work for testing, $Heavy::Verbose...
[p5sagit/p5-mst-13.2.git] / lib / utf8_heavy.pl
CommitLineData
a0ed51b3 1package utf8;
cf25bb62 2use strict;
3use warnings;
a0ed51b3 4
15732964 5sub DEBUG () { 0 }
a0ed51b3 6
7sub DESTROY {}
8
5beb625e 9my %Cache;
10
86916d66 11sub croak { require Carp; Carp::croak(@_) }
12
5beb625e 13##
14## "SWASH" == "SWATCH HASH". A "swatch" is a swatch of the Unicode landscape
15##
16
a0ed51b3 17sub SWASHNEW {
18 my ($class, $type, $list, $minbits, $none) = @_;
19 local $^D = 0 if $^D;
15732964 20
21 print STDERR "SWASHNEW @_\n" if DEBUG;
22
cf25bb62 23 ##
24 ## Get the list of codepoints for the type.
25 ## Called from utf8.c
26 ##
27 ## Given a $type, our goal is to fill $list with the set of codepoint
5beb625e 28 ## ranges.
cf25bb62 29 ##
30 ## To make the parsing of $type clear, this code takes the a rather
2d92f8a0 31 ## unorthodox approach of last'ing out of the block once we have the
cf25bb62 32 ## info we need. Were this to be a subroutine, the 'last' would just
33 ## be a 'return'.
34 ##
5beb625e 35 my $file; ## file to load data from, and also part of the %Cache key.
36 my $ListSorted = 0;
37
cf25bb62 38 if ($type)
39 {
40 $type =~ s/^\s+//;
41 $type =~ s/\s+$//;
42
43 print "type = $type\n" if DEBUG;
44
cf25bb62 45 GETFILE:
46 {
47 ##
5beb625e 48 ## 'Is' is always optional, so if it's there, remove it.
49 ## Same with 'Category=' and 'Script='.
cf25bb62 50 ##
5beb625e 51 ## 'Block=' is replaced by 'In'.
cf25bb62 52 ##
2d92f8a0 53 my $wasIs;
54
55 ($wasIs = $type =~ s/^Is(?:\s+|[-_])?//i)
5beb625e 56 or
57 $type =~ s/^Category\s*=\s*//i
58 or
59 $type =~ s/^Script\s*=\s*//i
60 or
61 $type =~ s/^Block\s*=\s*/In/i;
cf25bb62 62
5beb625e 63 ##
64 ## See if it's in the direct mapping table.
65 ##
66 require "unicore/Exact.pl";
67 if (my $base = $utf8::Exact{$type}) {
68 $file = "unicore/lib/$base.pl";
69 last GETFILE;
cf25bb62 70 }
71
72 ##
5beb625e 73 ## If not there exactly, try the canonical form. The canonical
74 ## form is lowercased, with any separators (\s+|[-_]) removed.
cf25bb62 75 ##
5beb625e 76 my $canonical = lc $type;
77 $canonical =~ s/(?<=[a-z\d])(?:\s+|[-_])(?=[a-z\d])//g;
78 print "canonical = $canonical\n" if DEBUG;
cf25bb62 79
5beb625e 80 require "unicore/Canonical.pl";
81 if (my $base = $utf8::Canonical{$canonical}) {
82 $file = "unicore/lib/$base.pl";
83 last GETFILE;
cf25bb62 84 }
85
491fd90a 86 ##
87 ## It could be a user-defined property.
88 ##
89
2d92f8a0 90 my $caller = caller(1);
91
92 if (defined $caller && $type =~ /^(?:\w+)$/) {
93 my $prop = $caller . "::" . ( $wasIs ? "Is" : "" ) . $type;
94 if (exists &{$prop}) {
95 no strict 'refs';
96
97 $list = &{$prop};
98 last GETFILE;
99 }
100 }
491fd90a 101
cf25bb62 102 ##
103 ## Last attempt -- see if it's a "To" name (e.g. "ToLower")
104 ##
105 if ($type =~ /^To([A-Z][A-Za-z]+)$/)
106 {
107 $file = "unicore/To/$1.pl";
108 ## would like to test to see if $file actually exists....
109 last GETFILE;
110 }
111
112 ##
113 ## If we reach this line, it's because we couldn't figure
114 ## out what to do with $type. Ouch.
115 ##
bc45ce41 116
117 return $type;
cf25bb62 118 }
119
491fd90a 120 if (defined $file) {
121 print "found it (file='$file')\n" if DEBUG;
122
123 ##
124 ## If we reach here, it was due to a 'last GETFILE' above
125 ## (exception: user-defined properties), so we
126 ## have a filename, so now we load it if we haven't already.
127 ## If we have, return the cached results. The cache key is the
128 ## file to load.
129 ##
130 if ($Cache{$file} and ref($Cache{$file}) eq $class)
131 {
132 print "Returning cached '$file' for \\p{$type}\n" if DEBUG;
133 return $Cache{$class, $file};
134 }
135
136 $list = do $file;
137 }
5beb625e 138
5beb625e 139 $ListSorted = 1; ## we know that these lists are sorted
886ba366 140 }
a0ed51b3 141
15732964 142 my $extras;
143 my $bits;
cf25bb62 144
5beb625e 145 my $ORIG = $list;
a0ed51b3 146 if ($list) {
147 my @tmp = split(/^/m, $list);
148 my %seen;
db376a24 149 no warnings;
a0ed51b3 150 $extras = join '', grep /^[^0-9a-fA-F]/, @tmp;
151 $list = join '',
df68b396 152 map { $_->[1] }
153 sort { $a->[0] <=> $b->[0] }
154 map { /^([0-9a-fA-F]+)/; [ hex($1), $_ ] }
155 grep { /^([0-9a-fA-F]+)/ and not $seen{$1}++ } @tmp; # XXX doesn't do ranges right
a0ed51b3 156 }
157
158 if ($none) {
159 my $hextra = sprintf "%04x", $none + 1;
160 $list =~ s/\tXXXX$/\t$hextra/mg;
161 }
162
163 if ($minbits < 32) {
164 my $top = 0;
165 while ($list =~ /^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
166 my $min = hex $1;
167 my $max = hex(defined $2 ? $2 : $1);
168 my $val = hex(defined $3 ? $3 : "");
169 $val += $max - $min if defined $3;
170 $top = $val if $val > $top;
171 }
172 $bits =
173 $top > 0xffff ? 32 :
174 $top > 0xff ? 16 :
175 $top > 1 ? 8 : 1
176 }
177 $bits = $minbits if $bits < $minbits;
178
179 my @extras;
180 for my $x ($extras) {
181 pos $x = 0;
1fef36c7 182 while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
a0ed51b3 183 my $char = $1;
184 my $name = $2;
6d6ae962 185 print STDERR "$1 => $2\n" if DEBUG;
a0ed51b3 186 if ($char =~ /[-+!]/) {
187 my ($c,$t) = split(/::/, $name, 2); # bogus use of ::, really
11ef8fdd 188 my $subobj;
189 if ($c eq 'utf8') {
190 $subobj = $c->SWASHNEW($t, "", 0, 0, 0);
191 }
192 elsif ($c =~ /^([0-9a-fA-F]+)/) {
193 $subobj = utf8->SWASHNEW("", $c, 0, 0, 0);
194 }
bc45ce41 195 return $subobj unless ref $subobj;
a0ed51b3 196 push @extras, $name => $subobj;
197 $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
198 }
199 }
200 }
201
15732964 202 print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG;
a0ed51b3 203
5beb625e 204 my $SWASH = bless {
a0ed51b3 205 TYPE => $type,
206 BITS => $bits,
207 EXTRAS => $extras,
208 LIST => $list,
209 NONE => $none,
210 @extras,
211 } => $class;
5beb625e 212
213 if ($file) {
214 $Cache{$class, $file} = $SWASH;
215 }
216
217 return $SWASH;
a0ed51b3 218}
219
220# NOTE: utf8.c:swash_init() assumes entries are never modified once generated.
221
222sub SWASHGET {
035d37be 223 # See utf8.c:Perl_swash_fetch for problems with this interface.
a0ed51b3 224 my ($self, $start, $len) = @_;
225 local $^D = 0 if $^D;
a0ed51b3 226 my $type = $self->{TYPE};
227 my $bits = $self->{BITS};
228 my $none = $self->{NONE};
15732964 229 print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if DEBUG;
a0ed51b3 230 my $end = $start + $len;
231 my $swatch = "";
232 my $key;
233 vec($swatch, $len - 1, $bits) = 0; # Extend to correct length.
234 if ($none) {
235 for $key (0 .. $len - 1) { vec($swatch, $key, $bits) = $none }
236 }
237
238 for ($self->{LIST}) {
239 pos $_ = 0;
240 if ($bits > 1) {
241 LINE:
242 while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
243 my $min = hex $1;
244 my $max = (defined $2 ? hex $2 : $min);
245 my $val = hex $3;
246 next if $max < $start;
6d6ae962 247 print "$min $max $val\n" if DEBUG;
a0ed51b3 248 if ($none) {
249 if ($min < $start) {
710250cb 250 $val += $start - $min if $val < $none;
a0ed51b3 251 $min = $start;
252 }
253 for ($key = $min; $key <= $max; $key++) {
254 last LINE if $key >= $end;
6d6ae962 255 print STDERR "$key => $val\n" if DEBUG;
a0ed51b3 256 vec($swatch, $key - $start, $bits) = $val;
257 ++$val if $val < $none;
258 }
259 }
260 else {
261 if ($min < $start) {
262 $val += $start - $min;
263 $min = $start;
264 }
265 for ($key = $min; $key <= $max; $key++, $val++) {
266 last LINE if $key >= $end;
6d6ae962 267 print STDERR "$key => $val\n" if DEBUG;
a0ed51b3 268 vec($swatch, $key - $start, $bits) = $val;
269 }
270 }
271 }
272 }
273 else {
274 LINE:
99a6b1f0 275 while (/^([0-9a-fA-F]+)(?:[ \t]+([0-9a-fA-F]+))?/mg) {
a0ed51b3 276 my $min = hex $1;
277 my $max = (defined $2 ? hex $2 : $min);
278 next if $max < $start;
279 if ($min < $start) {
280 $min = $start;
281 }
282 for ($key = $min; $key <= $max; $key++) {
283 last LINE if $key >= $end;
6d6ae962 284 print STDERR "$key => 1\n" if DEBUG;
a0ed51b3 285 vec($swatch, $key - $start, 1) = 1;
286 }
287 }
288 }
289 }
290 for my $x ($self->{EXTRAS}) {
291 pos $x = 0;
1fef36c7 292 while ($x =~ /^([-+!])(.*)/mg) {
a0ed51b3 293 my $char = $1;
294 my $name = $2;
15732964 295 print STDERR "INDIRECT $1 $2\n" if DEBUG;
1fef36c7 296 my $otherbits = $self->{$name}->{BITS};
297 croak("SWASHGET size mismatch") if $bits < $otherbits;
298 my $other = $self->{$name}->SWASHGET($start, $len);
299 if ($char eq '+') {
300 if ($bits == 1 and $otherbits == 1) {
301 $swatch |= $other;
a0ed51b3 302 }
1fef36c7 303 else {
304 for ($key = 0; $key < $len; $key++) {
305 vec($swatch, $key, $bits) = vec($other, $key, $otherbits);
a0ed51b3 306 }
1fef36c7 307 }
308 }
309 elsif ($char eq '!') {
310 if ($bits == 1 and $otherbits == 1) {
311 $swatch |= ~$other;
312 }
313 else {
314 for ($key = 0; $key < $len; $key++) {
315 if (!vec($other, $key, $otherbits)) {
316 vec($swatch, $key, $bits) = 1;
a0ed51b3 317 }
318 }
319 }
1fef36c7 320 }
321 elsif ($char eq '-') {
322 if ($bits == 1 and $otherbits == 1) {
323 $swatch &= ~$other;
324 }
325 else {
326 for ($key = 0; $key < $len; $key++) {
327 if (vec($other, $key, $otherbits)) {
328 vec($swatch, $key, $bits) = 0;
a0ed51b3 329 }
330 }
331 }
332 }
333 }
334 }
15732964 335 if (DEBUG) {
a0ed51b3 336 print STDERR "CELLS ";
337 for ($key = 0; $key < $len; $key++) {
338 print STDERR vec($swatch, $key, $bits), " ";
339 }
340 print STDERR "\n";
341 }
342 $swatch;
343}
344
3451;