11 sub croak { require Carp; Carp::croak(@_) }
14 ## "SWASH" == "SWATCH HASH". A "swatch" is a swatch of the Unicode landscape.
15 ## It's a data structure that encodes a set of Unicode characters.
19 # If a floating point number is within this distance from the value of a
20 # fraction, it is considered to be that fraction, even if many more digits
21 # are specified that don't exactly match.
22 my $min_floating_slop;
25 my ($class, $type, $list, $minbits, $none) = @_;
28 print STDERR __LINE__, ": class=$class, type=$type, list=$list, minbits=$minbits, none=$none\n" if DEBUG;
31 ## Get the list of codepoints for the type.
32 ## Called from swash_init (see utf8.c) or SWASHNEW itself.
34 ## Callers of swash_init:
35 ## op.c:pmtrans -- for tr/// and y///
36 ## regexec.c:regclass_swash -- for /[]/, \p, and \P
37 ## utf8.c:is_utf8_common -- for common Unicode properties
38 ## utf8.c:to_utf8_case -- for lc, uc, ucfirst, etc. and //i
40 ## Given a $type, our goal is to fill $list with the set of codepoint
41 ## ranges. If $type is false, $list passed is used.
44 ## For binary properties, $minbits must be 1.
45 ## For character mappings (case and transliteration), $minbits must
46 ## be a number except 1.
48 ## $list (or that filled according to $type):
49 ## Refer to perlunicode.pod, "User-Defined Character Properties."
51 ## For binary properties, only characters with the property value
52 ## of True should be listed. The 3rd column, if any, will be ignored
54 ## $none is undocumented, so I'm (khw) trying to do some documentation
55 ## of it now. It appears to be if there is a mapping in an input file
56 ## that maps to 'XXXX', then that is replaced by $none+1, expressed in
57 ## hexadecimal. The only place I found it possibly used was in
60 ## To make the parsing of $type clear, this code takes the a rather
61 ## unorthodox approach of last'ing out of the block once we have the
62 ## info we need. Were this to be a subroutine, the 'last' would just
65 my $file; ## file to load data from, and also part of the %Cache key.
68 # Change this to get a different set of Unicode tables
69 my $unicore_dir = 'unicore';
76 print STDERR __LINE__, ": type = $type\n" if DEBUG;
81 ## It could be a user-defined property. Look in current
82 ## package if no package given
85 my $caller1 = $type =~ s/(.+)::// ? $1 : caller(1);
87 if (defined $caller1 && $type =~ /^(?:\w+)$/) {
88 my $prop = "${caller1}::$type";
89 if (exists &{$prop}) {
97 # During Perl's compilation, this routine may be called before
98 # the tables are constructed. If so, we have a chicken/egg
99 # problem. If we die, the tables never get constructed, so
100 # keep going, but return an empty table so only what the code
101 # has compiled in internally (currently ASCII/Latin1 range
102 # matching) will work.
104 # Poor man's constant, to avoid a run-time check.
106 = \! defined &DynaLoader::boot_DynaLoader;
109 eval "require '$unicore_dir/Heavy.pl'";
113 require "$unicore_dir/Heavy.pl";
115 BEGIN { delete $utf8::{miniperl} }
117 # Everything is caseless matching
118 my $property_and_table = lc $type;
119 print STDERR __LINE__, ": $property_and_table\n" if DEBUG;
121 # See if is of the compound form 'property=value', where the
122 # value indicates the table we should use.
123 my ($property, $table, @remainder) =
124 split /\s*[:=]\s*/, $property_and_table, -1;
125 return $type if @remainder;
128 if (! defined $table) {
130 # Here, is the single form. The property becomes empty, and
131 # the whole value is the table.
133 $prefix = $property = "";
135 print STDERR __LINE__, ": $property\n" if DEBUG;
137 # Here it is the compound property=table form. The property
138 # name is always loosely matched, which means remove any of
140 $property =~ s/[_\s-]//g;
142 # And convert to canonical form. Quit if not valid.
143 $property = $utf8::loose_property_name_of{$property};
144 return $type unless defined $property;
146 $prefix = "$property=";
148 # If the rhs looks like it is a number...
149 print STDERR __LINE__, ": table=$table\n" if DEBUG;
150 if ($table =~ qr{ ^ [ \s 0-9 _ + / . -]+ $ }x) {
151 print STDERR __LINE__, ": table=$table\n" if DEBUG;
153 # Don't allow leading nor trailing slashes
154 return $type if $table =~ / ^ \/ | \/ $ /x;
156 # Split on slash, in case it is a rational, like \p{1/5}
157 my @parts = split qr{ \s* / \s* }x, $table, -1;
158 print __LINE__, ": $type\n" if @parts > 2 && DEBUG;
160 # Can have maximum of one slash
161 return $type if @parts > 2;
163 foreach my $part (@parts) {
164 print __LINE__, ": part=$part\n" if DEBUG;
166 $part =~ s/^\+\s*//; # Remove leading plus
167 $part =~ s/^-\s*/-/; # Remove blanks after unary
170 # Remove underscores between digits.
171 $part =~ s/( ?<= [0-9] ) _ (?= [0-9] ) //xg;
173 # No leading zeros (but don't make a single '0'
174 # into a null string)
175 $part =~ s/ ^ ( -? ) 0+ /$1/x;
176 $part .= '0' if $part eq '-' || $part eq "";
178 # No trailing zeros after a decimal point
179 $part =~ s/ ( \. .*? ) 0+ $ /$1/x;
181 # Begin with a 0 if a leading decimal point
182 $part =~ s/ ^ ( -? ) \. /${1}0./x;
184 # Ensure not a trailing decimal point: turn into an
186 $part =~ s/ \. $ //x;
188 print STDERR __LINE__, ": part=$part\n" if DEBUG;
189 #return $type if $part eq "";
191 # Result better look like a number. (This test is
192 # needed because, for example could have a plus in
194 return $type if $part
195 !~ / ^ -? [0-9]+ ( \. [0-9]+)? $ /x;
201 # If denominator is negative, get rid of it, and ...
202 if ($parts[1] =~ s/^-//) {
204 # If numerator is also negative, convert the
205 # whole thing to positive, or move the minus to
207 if ($parts[0] !~ s/^-//) {
208 $parts[0] = '-' . $parts[0];
211 $table = join '/', @parts;
213 elsif ($property ne 'nv' || $parts[0] !~ /\./) {
215 # Here is not numeric value, or doesn't have a
216 # decimal point. No further manipulation is
217 # necessary. (Note the hard-coded property name.
218 # This could fail if other properties eventually
219 # had fractions as well; perhaps the cjk ones
220 # could evolve to do that. This hard-coding could
221 # be fixed by mktables generating a list of
222 # properties that could have fractions.)
226 # Here is a floating point numeric_value. Try to
227 # convert to rational. First see if is in the list
229 if (exists $utf8::nv_floating_to_rational{$parts[0]}) {
230 $table = $utf8::nv_floating_to_rational{$parts[0]};
233 # Here not in the list. See if is close
234 # enough to something in the list. First
235 # determine what 'close enough' means. It has
236 # to be as tight as what mktables says is the
237 # maximum slop, and as tight as how many
238 # digits we were passed. That is, if the user
239 # said .667, .6667, .66667, etc. we match as
240 # many digits as they passed until get to
241 # where it doesn't matter any more due to the
242 # machine's precision. If they said .6666668,
244 (my $fraction = $parts[0]) =~ s/^.*\.//;
245 my $epsilon = 10 ** - (length($fraction));
246 if ($epsilon > $utf8::max_floating_slop) {
247 $epsilon = $utf8::max_floating_slop;
250 # But it can't be tighter than the minimum
251 # precision for this machine. If haven't
252 # already calculated that minimum, do so now.
253 if (! defined $min_floating_slop) {
255 # Keep going down an order of magnitude
256 # until find that adding this quantity to
257 # 1 remains 1; but put an upper limit on
258 # this so in case this algorithm doesn't
259 # work properly on some platform, that we
260 # won't loop forever.
262 $min_floating_slop = 1;
263 while (1+ $min_floating_slop != 1
266 my $next = $min_floating_slop / 10;
267 last if $next == 0; # If underflows,
269 $min_floating_slop = $next;
270 print STDERR __LINE__, ": min_float_slop=$min_floating_slop\n" if DEBUG;
273 # Back off a couple orders of magnitude,
275 $min_floating_slop *= 100;
278 if ($epsilon < $min_floating_slop) {
279 $epsilon = $min_floating_slop;
281 print STDERR __LINE__, ": fraction=.$fraction; epsilon=$epsilon\n" if DEBUG;
285 # And for each possible rational in the table,
286 # see if it is within epsilon of the input.
288 (keys %utf8::nv_floating_to_rational)
290 print STDERR __LINE__, ": epsilon=$epsilon, official=$official, diff=", abs($parts[0] - $official), "\n" if DEBUG;
291 if (abs($parts[0] - $official) < $epsilon) {
293 $utf8::nv_floating_to_rational{$official};
298 # Quit if didn't find one.
299 return $type unless defined $table;
302 print STDERR __LINE__, ": $property=$table\n" if DEBUG;
306 # Combine lhs (if any) and rhs to get something that matches
307 # the syntax of the lookups.
308 $property_and_table = "$prefix$table";
309 print STDERR __LINE__, ": $property_and_table\n" if DEBUG;
311 # First try stricter matching.
312 $file = $utf8::stricter_to_file_of{$property_and_table};
314 # If didn't find it, try again with looser matching by editing
315 # out the applicable characters on the rhs and looking up
317 if (! defined $file) {
318 $table =~ s/ [_\s-] //xg;
319 $property_and_table = "$prefix$table";
320 print STDERR __LINE__, ": $property_and_table\n" if DEBUG;
321 $file = $utf8::loose_to_file_of{$property_and_table};
324 # Add the constant and go fetch it in.
326 if ($utf8::why_deprecated{$file}) {
327 warnings::warnif('deprecated', "Use of '$type' in \\p{} or \\P{} is deprecated because: $utf8::why_deprecated{$file};");
329 $file= "$unicore_dir/lib/$file.pl";
332 print STDERR __LINE__, ": didn't find $property_and_table\n" if DEBUG;
335 ## See if it's a user-level "To".
338 my $caller0 = caller(0);
340 if (defined $caller0 && $type =~ /^To(?:\w+)$/) {
341 my $map = $caller0 . "::" . $type;
343 if (exists &{$map}) {
352 ## Last attempt -- see if it's a standard "To" name
353 ## (e.g. "ToLower") ToTitle is used by ucfirst().
354 ## The user-level way to access ToDigit() and ToFold()
355 ## is to use Unicode::UCD.
357 if ($type =~ /^To(Digit|Fold|Lower|Title|Upper)$/) {
358 $file = "$unicore_dir/To/$1.pl";
359 ## would like to test to see if $file actually exists....
364 ## If we reach this line, it's because we couldn't figure
365 ## out what to do with $type. Ouch.
372 print STDERR __LINE__, ": found it (file='$file')\n" if DEBUG;
375 ## If we reach here, it was due to a 'last GETFILE' above
376 ## (exception: user-defined properties and mappings), so we
377 ## have a filename, so now we load it if we haven't already.
378 ## If we have, return the cached results. The cache key is the
379 ## class and file to load.
381 my $found = $Cache{$class, $file};
382 if ($found and ref($found) eq $class) {
383 print STDERR __LINE__, ": Returning cached '$file' for \\p{$type}\n" if DEBUG;
389 $list = do $file; die $@ if $@;
392 $ListSorted = 1; ## we know that these lists are sorted
400 my @tmp = split(/^/m, $list);
403 $extras = join '', grep /^[^0-9a-fA-F]/, @tmp;
406 sort { $a->[0] <=> $b->[0] }
407 map { /^([0-9a-fA-F]+)/; [ CORE::hex($1), $_ ] }
408 grep { /^([0-9a-fA-F]+)/ and not $seen{$1}++ } @tmp; # XXX doesn't do ranges right
412 my $hextra = sprintf "%04x", $none + 1;
413 $list =~ s/\tXXXX$/\t$hextra/mg;
416 if ($minbits != 1 && $minbits < 32) { # not binary property
418 while ($list =~ /^([0-9a-fA-F]+)(?:[\t]([0-9a-fA-F]+)?)(?:[ \t]([0-9a-fA-F]+))?/mg) {
419 my $min = CORE::hex $1;
420 my $max = defined $2 ? CORE::hex $2 : $min;
421 my $val = defined $3 ? CORE::hex $3 : 0;
422 $val += $max - $min if defined $3;
423 $top = $val if $val > $top;
427 $top > 0xff ? 16 : 8;
428 $bits = $topbits if $bits < $topbits;
433 for my $x ($extras) {
435 while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
438 print STDERR __LINE__, ": $1 => $2\n" if DEBUG;
439 if ($char =~ /[-+!&]/) {
440 my ($c,$t) = split(/::/, $name, 2); # bogus use of ::, really
443 $subobj = utf8->SWASHNEW($t, "", $minbits, 0);
445 elsif (exists &$name) {
446 $subobj = utf8->SWASHNEW($name, "", $minbits, 0);
448 elsif ($c =~ /^([0-9a-fA-F]+)/) {
449 $subobj = utf8->SWASHNEW("", $c, $minbits, 0);
451 return $subobj unless ref $subobj;
452 push @extras, $name => $subobj;
453 $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
460 print STDERR __LINE__, ": CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none";
461 print STDERR "\nLIST =>\n$list" if defined $list;
462 print STDERR "\nEXTRAS =>\n$extras" if defined $extras;
476 $Cache{$class, $file} = $SWASH;
483 # Now SWASHGET is recasted into a C function S_swash_get (see utf8.c).