11 our (%PropertyAlias, %PA_reverse, %PropValueAlias, %PVA_reverse, %PVA_abbr_map);
13 sub croak
{ require Carp
; Carp
::croak
(@_) }
16 ## "SWASH" == "SWATCH HASH". A "swatch" is a swatch of the Unicode landscape.
17 ## It's a data structure that encodes a set of Unicode characters.
21 my ($class, $type, $list, $minbits, $none) = @_;
24 print STDERR
"SWASHNEW @_\n" if DEBUG
;
27 ## Get the list of codepoints for the type.
30 ## Given a $type, our goal is to fill $list with the set of codepoint
33 ## To make the parsing of $type clear, this code takes the a rather
34 ## unorthodox approach of last'ing out of the block once we have the
35 ## info we need. Were this to be a subroutine, the 'last' would just
38 my $file; ## file to load data from, and also part of the %Cache key.
46 print "type = $type\n" if DEBUG
;
51 ## It could be a user-defined property.
54 my $caller1 = $type =~ s/(.+)::// ?
$1 : caller(1);
56 if (defined $caller1 && $type =~ /^(?:\w+)$/) {
57 my $prop = "${caller1}::$type";
58 if (exists &{$prop}) {
68 ($wasIs = $type =~ s/^Is(?:\s+|[-_])?//i)
70 $type =~ s/^(?:(?:General(?:\s+|_)?)?Category|gc)\s*[:=]\s*//i
72 $type =~ s/^(?:Script|sc)\s*[:=]\s*//i
74 $type =~ s/^Block\s*[:=]\s*/In/i;
78 ## See if it's in some enumeration.
80 require "unicore/PVA.pl";
81 if ($type =~ /^([\w\s]+)[:=]\s*(.*)/) {
82 my ($enum, $val) = (lc $1, lc $2);
86 my $pa = $PropertyAlias{$enum} ?
$enum : $PA_reverse{$enum};
87 my $f = $PropValueAlias{$pa}{$val} ?
$val : $PVA_reverse{$pa}{lc $val};
90 $pa = "gc_sc" if $pa eq "gc" or $pa eq "sc";
91 $file = "unicore/lib/$pa/$PVA_abbr_map{$pa}{lc $f}.pl";
99 if ($PropValueAlias{gc
}{$t} or $PropValueAlias{sc
}{$t}) {
100 $file = "unicore/lib/gc_sc/$PVA_abbr_map{gc_sc}{$t}.pl";
106 ## See if it's in the direct mapping table.
108 require "unicore/Exact.pl";
109 if (my $base = $utf8::Exact
{$type}) {
110 $file = "unicore/lib/gc_sc/$base.pl";
115 ## If not there exactly, try the canonical form. The canonical
116 ## form is lowercased, with any separators (\s+|[-_]) removed.
118 my $canonical = lc $type;
119 $canonical =~ s/(?<=[a-z\d])(?:\s+|[-_])(?=[a-z\d])//g;
120 print "canonical = $canonical\n" if DEBUG
;
122 require "unicore/Canonical.pl";
123 if (my $base = ($utf8::Canonical
{$canonical} || $utf8::Canonical
{ lc $utf8::PropertyAlias
{$canonical} })) {
124 $file = "unicore/lib/gc_sc/$base.pl";
129 ## See if it's a user-level "To".
132 my $caller0 = caller(0);
134 if (defined $caller0 && $type =~ /^To(?:\w+)$/) {
135 my $map = $caller0 . "::" . $type;
137 if (exists &{$map}) {
146 ## Last attempt -- see if it's a standard "To" name
147 ## (e.g. "ToLower") ToTitle is used by ucfirst().
148 ## The user-level way to access ToDigit() and ToFold()
149 ## is to use Unicode::UCD.
151 if ($type =~ /^To(Digit|Fold|Lower|Title|Upper)$/)
153 $file = "unicore/To/$1.pl";
154 ## would like to test to see if $file actually exists....
159 ## If we reach this line, it's because we couldn't figure
160 ## out what to do with $type. Ouch.
167 print "found it (file='$file')\n" if DEBUG
;
170 ## If we reach here, it was due to a 'last GETFILE' above
171 ## (exception: user-defined properties and mappings), so we
172 ## have a filename, so now we load it if we haven't already.
173 ## If we have, return the cached results. The cache key is the
176 if ($Cache{$file} and ref($Cache{$file}) eq $class)
178 print "Returning cached '$file' for \\p{$type}\n" if DEBUG
;
179 return $Cache{$class, $file};
185 $ListSorted = 1; ## we know that these lists are sorted
193 my @tmp = split(/^/m, $list);
196 $extras = join '', grep /^[^0-9a-fA-F]/, @tmp;
199 sort { $a->[0] <=> $b->[0] }
200 map { /^([0-9a-fA-F]+)/; [ hex($1), $_ ] }
201 grep { /^([0-9a-fA-F]+)/ and not $seen{$1}++ } @tmp; # XXX doesn't do ranges right
205 my $hextra = sprintf "%04x", $none + 1;
206 $list =~ s/\tXXXX$/\t$hextra/mg;
211 while ($list =~ /^([0-9a-fA-F]+)(?:[\t]([0-9a-fA-F]+)?)(?:[ \t]([0-9a-fA-F]+))?/mg) {
213 my $max = defined $2 ?
hex $2 : $min;
214 my $val = defined $3 ?
hex $3 : 0;
215 $val += $max - $min if defined $3;
216 $top = $val if $val > $top;
223 $bits = $minbits if $bits < $minbits;
226 for my $x ($extras) {
228 while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
231 print STDERR
"$1 => $2\n" if DEBUG
;
232 if ($char =~ /[-+!&]/) {
233 my ($c,$t) = split(/::/, $name, 2); # bogus use of ::, really
236 $subobj = utf8
->SWASHNEW($t, "", 0, 0, 0);
238 elsif (exists &$name) {
239 $subobj = utf8
->SWASHNEW($name, "", 0, 0, 0);
241 elsif ($c =~ /^([0-9a-fA-F]+)/) {
242 $subobj = utf8
->SWASHNEW("", $c, 0, 0, 0);
244 return $subobj unless ref $subobj;
245 push @extras, $name => $subobj;
246 $bits = $subobj->{BITS
} if $bits < $subobj->{BITS
};
251 print STDERR
"CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG
;
263 $Cache{$class, $file} = $SWASH;
269 # NOTE: utf8.c:swash_init() assumes entries are never modified once generated.
272 # See utf8.c:Perl_swash_fetch for problems with this interface.
273 my ($self, $start, $len) = @_;
274 local $^D
= 0 if $^D
;
275 my $type = $self->{TYPE
};
276 my $bits = $self->{BITS
};
277 my $none = $self->{NONE
};
278 print STDERR
"SWASHGET @_ [$type/$bits/$none]\n" if DEBUG
;
279 my $end = $start + $len;
282 vec($swatch, $len - 1, $bits) = 0; # Extend to correct length.
284 for $key (0 .. $len - 1) { vec($swatch, $key, $bits) = $none }
287 for ($self->{LIST
}) {
291 while (/^([0-9a-fA-F]+)(?:[ \t]([0-9a-fA-F]+)?)?(?:[ \t]([0-9a-fA-F]+))?/mg) {
293 my ($a, $b, $c) = ($1, $2, $3);
294 croak
"$type: illegal mapping '$_'"
296 !(defined $a && defined $c);
298 my $max = defined $b ?
hex $b : $min;
299 my $val = defined $c ?
hex $c : 0;
300 next if $max < $start;
301 print "$min $max $val\n" if DEBUG
;
304 $val += $start - $min if $val < $none;
307 for ($key = $min; $key <= $max; $key++) {
308 last LINE
if $key >= $end;
309 print STDERR
"$key => $val\n" if DEBUG
;
310 vec($swatch, $key - $start, $bits) = $val;
311 ++$val if $val < $none;
316 $val += $start - $min;
319 for ($key = $min; $key <= $max; $key++, $val++) {
320 last LINE
if $key >= $end;
321 print STDERR
"$key => $val\n" if DEBUG
;
322 vec($swatch, $key - $start, $bits) = $val;
329 while (/^([0-9a-fA-F]+)(?:[ \t]+([0-9a-fA-F]+))?/mg) {
332 my $max = defined $2 ?
hex $2 : $min;
333 next if $max < $start;
337 for ($key = $min; $key <= $max; $key++) {
338 last LINE
if $key >= $end;
339 print STDERR
"$key => 1\n" if DEBUG
;
340 vec($swatch, $key - $start, 1) = 1;
345 for my $x ($self->{EXTRAS
}) {
347 while ($x =~ /^([-+!&])(.*)/mg) {
350 print STDERR
"INDIRECT $1 $2\n" if DEBUG
;
351 my $otherbits = $self->{$name}->{BITS
};
352 croak
("SWASHGET size mismatch") if $bits < $otherbits;
353 my $other = $self->{$name}->SWASHGET($start, $len);
355 if ($bits == 1 and $otherbits == 1) {
359 for ($key = 0; $key < $len; $key++) {
360 vec($swatch, $key, $bits) = vec($other, $key, $otherbits);
364 elsif ($char eq '!') {
365 if ($bits == 1 and $otherbits == 1) {
369 for ($key = 0; $key < $len; $key++) {
370 if (!vec($other, $key, $otherbits)) {
371 vec($swatch, $key, $bits) = 1;
376 elsif ($char eq '-') {
377 if ($bits == 1 and $otherbits == 1) {
381 for ($key = 0; $key < $len; $key++) {
382 if (vec($other, $key, $otherbits)) {
383 vec($swatch, $key, $bits) = 0;
388 elsif ($char eq '&') {
389 if ($bits == 1 and $otherbits == 1) {
393 for ($key = 0; $key < $len; $key++) {
394 if (!vec($other, $key, $otherbits)) {
395 vec($swatch, $key, $bits) = 0;
403 print STDERR
"CELLS ";
404 for ($key = 0; $key < $len; $key++) {
405 print STDERR
vec($swatch, $key, $bits), " ";