9 sub croak { require Carp; Carp::croak(@_) }
12 my ($class, $type, $list, $minbits, $none) = @_;
15 print STDERR "SWASHNEW @_\n" if DEBUG;
17 ## check to see if we've already got it.
20 if ($type and ref ${"${class}::{$type}"} eq $class) {
21 warn qq/Found \${"${class}::{$type}"}\n/ if DEBUG;
22 return ${"${class}::{$type}"};
27 ## Get the list of codepoints for the type.
30 ## Given a $type, our goal is to fill $list with the set of codepoint
31 ## ranges. As we try various interpretations of $type, sometimes we'll
32 ## end up with the $list directly, and sometimes we'll end up with a
33 ## $file name that holds the list data.
35 ## To make the parsing of $type clear, this code takes the a rather
36 ## unorthadox approach of last'ing out of the block once we have the
37 ## info we need. Were this to be a subroutine, the 'last' would just
45 print "type = $type\n" if DEBUG;
48 ## Figure out what file to load to get the data....
52 ## First, see if it's an "Is" name (the 'Is' is optional)
54 ## Because we check "Is" names first, they have precidence over
55 ## "In" names. For example, "Greek" is both a script and a
56 ## block. "IsGreek" always gets the script, while "InGreek"
57 ## always gets the block. "Greek" gets the script because we
58 ## check "Is" names first.
61 ## "Is" prefix, or "Script=" or "Category="
62 (?: Is [- _]? | (?:Script|Category)\s*=\s* )?
63 ## name to check in the "Is" symbol table.
70 ## Input ($type) Name To Check ($istype)
71 ## ------------- -----------------------
76 ## Script = Greek Greek
79 print "istype = $istype\n" if DEBUG;
81 ## Load "Is" mapping data, if not yet loaded.
82 do "unicore/Is.pl" if not defined %utf8::Is;
85 ## If the "Is" mapping data has an exact match, it points
86 ## to the file we need.
88 if (exists $utf8::Is{$istype})
90 $file = "unicore/Is/$utf8::Is{$istype}.pl";
95 ## Need to look at %utf8::IsPat (loaded from "unicore/Is.pl")
96 ## to see if there's a regex that matches this $istype.
97 ## If so, the associated name is the file we need.
99 my $prefix = substr(lc($istype), 0, 2);
100 if (exists $utf8::IsPat{$prefix})
102 while (my ($pat, $name) = each %{$utf8::IsPat{$prefix}})
104 print "isprefix = $prefix, Is = $istype, pat = $pat\n" if DEBUG;
106 ## The following regex probably need not be cached,
107 ## since every time there's a match, the results of
108 ## the entire call to SWASHNEW() is cached, so there's
109 ## a very limited number of times any one $pat will
110 ## be evaluated as a regex, at least with "reasonable"
111 ## code that doesn't try a baziilion \p{Random} names.
113 if ($istype =~ /^$pat$/i)
115 $file = "unicore/Is/$name.pl";
123 ## Couldn't find via "Is" -- let's try via "In".....
126 ( In(?!herited$)[- _]? | Block\s*=\s*)?
132 print "intype = $intype\n" if DEBUG;
135 ## Input ($type) Name To Check ($intype)
136 ## ------------- -----------------------
137 ## Inherited Inherited
139 ## Block = Greek Greek
142 ## Load "In" mapping data, if not yet loaded.
143 do "unicore/In.pl" if not defined %utf8::In;
145 ## If there's a direct match, it points to the file we need
146 if (exists $utf8::In{$intype}) {
147 $file = "unicore/In/$utf8::In{$intype}.pl";
151 my $prefix = substr(lc($intype), 0, 2);
152 if (exists $utf8::InPat{$prefix})
154 print "inprefix = $prefix, In = $intype\n" if DEBUG;
155 while (my ($pat, $name) = each %{$utf8::InPat{$prefix}})
157 print "inprefix = $prefix, In = $intype, k = $pat\n" if DEBUG;
158 if ($intype =~ /^$pat$/i) {
159 $file = "unicore/In/$name.pl";
160 print "inprefix = $prefix, In = $intype, k = $pat, file = $file\n" if DEBUG;
168 ## Last attempt -- see if it's a "To" name (e.g. "ToLower")
170 if ($type =~ /^To([A-Z][A-Za-z]+)$/)
172 $file = "unicore/To/$1.pl";
173 ## would like to test to see if $file actually exists....
178 ## If we reach this line, it's because we couldn't figure
179 ## out what to do with $type. Ouch.
181 croak("Can't find Unicode character property \"$type\"");
185 ## If we reach here, it was due to a 'last GETFILE' above, so we
186 ## have a filename, so now we load it.
195 my @tmp = split(/^/m, $list);
198 $extras = join '', grep /^[^0-9a-fA-F]/, @tmp;
200 sort { hex $a <=> hex $b }
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 = hex(defined $2 ? $2 : $1);
214 my $val = hex(defined $3 ? $3 : "");
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
234 my $subobj = $c->SWASHNEW($t, "", 0, 0, 0);
235 push @extras, $name => $subobj;
236 $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
241 print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG;
244 ${"${class}::{$type}"} = bless {
254 # NOTE: utf8.c:swash_init() assumes entries are never modified once generated.
257 # See utf8.c:Perl_swash_fetch for problems with this interface.
258 my ($self, $start, $len) = @_;
259 local $^D = 0 if $^D;
260 my $type = $self->{TYPE};
261 my $bits = $self->{BITS};
262 my $none = $self->{NONE};
263 print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if DEBUG;
264 my $end = $start + $len;
267 vec($swatch, $len - 1, $bits) = 0; # Extend to correct length.
269 for $key (0 .. $len - 1) { vec($swatch, $key, $bits) = $none }
272 for ($self->{LIST}) {
276 while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
278 my $max = (defined $2 ? hex $2 : $min);
280 next if $max < $start;
281 print "$min $max $val\n" if DEBUG;
284 $val += $start - $min if $val < $none;
287 for ($key = $min; $key <= $max; $key++) {
288 last LINE if $key >= $end;
289 print STDERR "$key => $val\n" if DEBUG;
290 vec($swatch, $key - $start, $bits) = $val;
291 ++$val if $val < $none;
296 $val += $start - $min;
299 for ($key = $min; $key <= $max; $key++, $val++) {
300 last LINE if $key >= $end;
301 print STDERR "$key => $val\n" if DEBUG;
302 vec($swatch, $key - $start, $bits) = $val;
309 while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+))?/mg) {
311 my $max = (defined $2 ? hex $2 : $min);
312 next if $max < $start;
316 for ($key = $min; $key <= $max; $key++) {
317 last LINE if $key >= $end;
318 print STDERR "$key => 1\n" if DEBUG;
319 vec($swatch, $key - $start, 1) = 1;
324 for my $x ($self->{EXTRAS}) {
326 while ($x =~ /^([-+!])(.*)/mg) {
329 print STDERR "INDIRECT $1 $2\n" if DEBUG;
330 my $otherbits = $self->{$name}->{BITS};
331 croak("SWASHGET size mismatch") if $bits < $otherbits;
332 my $other = $self->{$name}->SWASHGET($start, $len);
334 if ($bits == 1 and $otherbits == 1) {
338 for ($key = 0; $key < $len; $key++) {
339 vec($swatch, $key, $bits) = vec($other, $key, $otherbits);
343 elsif ($char eq '!') {
344 if ($bits == 1 and $otherbits == 1) {
348 for ($key = 0; $key < $len; $key++) {
349 if (!vec($other, $key, $otherbits)) {
350 vec($swatch, $key, $bits) = 1;
355 elsif ($char eq '-') {
356 if ($bits == 1 and $otherbits == 1) {
360 for ($key = 0; $key < $len; $key++) {
361 if (vec($other, $key, $otherbits)) {
362 vec($swatch, $key, $bits) = 0;
370 print STDERR "CELLS ";
371 for ($key = 0; $key < $len; $key++) {
372 print STDERR vec($swatch, $key, $bits), " ";