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;
24 # To guard against this program calling something that in turn ends up
25 # calling this program with the same inputs, and hence infinitely
26 # recursing, we keep a stack of the properties that are currently in
27 # progress, pushed upon entry, popped upon return.
31 my ($class, $type, $list, $minbits, $none) = @_;
34 $class = "" unless defined $class;
35 print STDERR __LINE__, ": class=$class, type=$type, list=",
36 (defined $list) ? $list : ':undef:',
37 ", minbits=$minbits, none=$none\n" if DEBUG;
40 ## Get the list of codepoints for the type.
41 ## Called from swash_init (see utf8.c) or SWASHNEW itself.
43 ## Callers of swash_init:
44 ## op.c:pmtrans -- for tr/// and y///
45 ## regexec.c:regclass_swash -- for /[]/, \p, and \P
46 ## utf8.c:is_utf8_common -- for common Unicode properties
47 ## utf8.c:to_utf8_case -- for lc, uc, ucfirst, etc. and //i
49 ## Given a $type, our goal is to fill $list with the set of codepoint
50 ## ranges. If $type is false, $list passed is used.
53 ## For binary properties, $minbits must be 1.
54 ## For character mappings (case and transliteration), $minbits must
55 ## be a number except 1.
57 ## $list (or that filled according to $type):
58 ## Refer to perlunicode.pod, "User-Defined Character Properties."
60 ## For binary properties, only characters with the property value
61 ## of True should be listed. The 3rd column, if any, will be ignored
63 ## $none is undocumented, so I'm (khw) trying to do some documentation
64 ## of it now. It appears to be if there is a mapping in an input file
65 ## that maps to 'XXXX', then that is replaced by $none+1, expressed in
66 ## hexadecimal. The only place I found it possibly used was in
69 ## To make the parsing of $type clear, this code takes the a rather
70 ## unorthodox approach of last'ing out of the block once we have the
71 ## info we need. Were this to be a subroutine, the 'last' would just
74 my $file; ## file to load data from, and also part of the %Cache key.
77 # Change this to get a different set of Unicode tables
78 my $unicore_dir = 'unicore';
83 # Verify that this isn't a recursive call for this property.
84 # Can't use croak, as it may try to recurse here itself.
85 my $class_type = $class . "::$type";
86 if (grep { $_ eq $class_type } @recursed) {
87 CORE::die "panic: Infinite recursion in SWASHNEW for '$type'\n";
89 push @recursed, $class_type;
94 # regcomp.c surrounds the property name with '__" and '_i' if this
95 # is to be caseless matching.
96 my $caseless = $type =~ s/^__(.*)_i$/$1/;
98 print STDERR __LINE__, ": type=$type, caseless=$caseless\n" if DEBUG;
103 ## It could be a user-defined property. Look in current
104 ## package if no package given
107 my $caller1 = $type =~ s/(.+)::// ? $1 : caller(1);
109 if (defined $caller1 && $type =~ /^I[ns]\w+$/) {
110 my $prop = "${caller1}::$type";
111 if (exists &{$prop}) {
112 # stolen from Scalar::Util::PP::tainted()
115 local($@, $SIG{__DIE__}, $SIG{__WARN__});
118 eval { kill 0 * $prop };
119 $tainted = 1 if $@ =~ /^Insecure/;
121 die "Insecure user-defined property \\p{$prop}\n"
124 $list = &{$prop}($caseless);
129 # During Perl's compilation, this routine may be called before
130 # the tables are constructed. If so, we have a chicken/egg
131 # problem. If we die, the tables never get constructed, so
132 # keep going, but return an empty table so only what the code
133 # has compiled in internally (currently ASCII/Latin1 range
134 # matching) will work.
136 # Poor man's constant, to avoid a run-time check.
138 = \! defined &DynaLoader::boot_DynaLoader;
141 eval "require '$unicore_dir/Heavy.pl'";
145 require "$unicore_dir/Heavy.pl";
147 BEGIN { delete $utf8::{miniperl} }
149 # All property names are matched caselessly
150 my $property_and_table = lc $type;
151 print STDERR __LINE__, ": $property_and_table\n" if DEBUG;
153 # See if is of the compound form 'property=value', where the
154 # value indicates the table we should use.
155 my ($property, $table, @remainder) =
156 split /\s*[:=]\s*/, $property_and_table, -1;
158 pop @recursed if @recursed;
163 if (! defined $table) {
165 # Here, is the single form. The property becomes empty, and
166 # the whole value is the table.
168 $prefix = $property = "";
170 print STDERR __LINE__, ": $property\n" if DEBUG;
172 # Here it is the compound property=table form. The property
173 # name is always loosely matched, which means remove any of
175 $property =~ s/[_\s-]//g;
177 # And convert to canonical form. Quit if not valid.
178 $property = $utf8::loose_property_name_of{$property};
179 if (! defined $property) {
180 pop @recursed if @recursed;
184 $prefix = "$property=";
186 # If the rhs looks like it is a number...
187 print STDERR __LINE__, ": table=$table\n" if DEBUG;
188 if ($table =~ qr{ ^ [ \s 0-9 _ + / . -]+ $ }x) {
189 print STDERR __LINE__, ": table=$table\n" if DEBUG;
191 # Don't allow leading nor trailing slashes
192 if ($table =~ / ^ \/ | \/ $ /x) {
193 pop @recursed if @recursed;
197 # Split on slash, in case it is a rational, like \p{1/5}
198 my @parts = split qr{ \s* / \s* }x, $table, -1;
199 print __LINE__, ": $type\n" if @parts > 2 && DEBUG;
201 # Can have maximum of one slash
203 pop @recursed if @recursed;
207 foreach my $part (@parts) {
208 print __LINE__, ": part=$part\n" if DEBUG;
210 $part =~ s/^\+\s*//; # Remove leading plus
211 $part =~ s/^-\s*/-/; # Remove blanks after unary
214 # Remove underscores between digits.
215 $part =~ s/( ?<= [0-9] ) _ (?= [0-9] ) //xg;
217 # No leading zeros (but don't make a single '0'
218 # into a null string)
219 $part =~ s/ ^ ( -? ) 0+ /$1/x;
220 $part .= '0' if $part eq '-' || $part eq "";
222 # No trailing zeros after a decimal point
223 $part =~ s/ ( \. .*? ) 0+ $ /$1/x;
225 # Begin with a 0 if a leading decimal point
226 $part =~ s/ ^ ( -? ) \. /${1}0./x;
228 # Ensure not a trailing decimal point: turn into an
230 $part =~ s/ \. $ //x;
232 print STDERR __LINE__, ": part=$part\n" if DEBUG;
233 #return $type if $part eq "";
235 # Result better look like a number. (This test is
236 # needed because, for example could have a plus in
238 if ($part !~ / ^ -? [0-9]+ ( \. [0-9]+)? $ /x) {
239 pop @recursed if @recursed;
247 # If denominator is negative, get rid of it, and ...
248 if ($parts[1] =~ s/^-//) {
250 # If numerator is also negative, convert the
251 # whole thing to positive, or move the minus to
253 if ($parts[0] !~ s/^-//) {
254 $parts[0] = '-' . $parts[0];
257 $table = join '/', @parts;
259 elsif ($property ne 'nv' || $parts[0] !~ /\./) {
261 # Here is not numeric value, or doesn't have a
262 # decimal point. No further manipulation is
263 # necessary. (Note the hard-coded property name.
264 # This could fail if other properties eventually
265 # had fractions as well; perhaps the cjk ones
266 # could evolve to do that. This hard-coding could
267 # be fixed by mktables generating a list of
268 # properties that could have fractions.)
272 # Here is a floating point numeric_value. Try to
273 # convert to rational. First see if is in the list
275 if (exists $utf8::nv_floating_to_rational{$parts[0]}) {
276 $table = $utf8::nv_floating_to_rational{$parts[0]};
279 # Here not in the list. See if is close
280 # enough to something in the list. First
281 # determine what 'close enough' means. It has
282 # to be as tight as what mktables says is the
283 # maximum slop, and as tight as how many
284 # digits we were passed. That is, if the user
285 # said .667, .6667, .66667, etc. we match as
286 # many digits as they passed until get to
287 # where it doesn't matter any more due to the
288 # machine's precision. If they said .6666668,
290 (my $fraction = $parts[0]) =~ s/^.*\.//;
291 my $epsilon = 10 ** - (length($fraction));
292 if ($epsilon > $utf8::max_floating_slop) {
293 $epsilon = $utf8::max_floating_slop;
296 # But it can't be tighter than the minimum
297 # precision for this machine. If haven't
298 # already calculated that minimum, do so now.
299 if (! defined $min_floating_slop) {
301 # Keep going down an order of magnitude
302 # until find that adding this quantity to
303 # 1 remains 1; but put an upper limit on
304 # this so in case this algorithm doesn't
305 # work properly on some platform, that we
306 # won't loop forever.
308 $min_floating_slop = 1;
309 while (1+ $min_floating_slop != 1
312 my $next = $min_floating_slop / 10;
313 last if $next == 0; # If underflows,
315 $min_floating_slop = $next;
316 print STDERR __LINE__, ": min_float_slop=$min_floating_slop\n" if DEBUG;
319 # Back off a couple orders of magnitude,
321 $min_floating_slop *= 100;
324 if ($epsilon < $min_floating_slop) {
325 $epsilon = $min_floating_slop;
327 print STDERR __LINE__, ": fraction=.$fraction; epsilon=$epsilon\n" if DEBUG;
331 # And for each possible rational in the table,
332 # see if it is within epsilon of the input.
334 (keys %utf8::nv_floating_to_rational)
336 print STDERR __LINE__, ": epsilon=$epsilon, official=$official, diff=", abs($parts[0] - $official), "\n" if DEBUG;
337 if (abs($parts[0] - $official) < $epsilon) {
339 $utf8::nv_floating_to_rational{$official};
344 # Quit if didn't find one.
345 if (! defined $table) {
346 pop @recursed if @recursed;
351 print STDERR __LINE__, ": $property=$table\n" if DEBUG;
355 # Combine lhs (if any) and rhs to get something that matches
356 # the syntax of the lookups.
357 $property_and_table = "$prefix$table";
358 print STDERR __LINE__, ": $property_and_table\n" if DEBUG;
360 # First try stricter matching.
361 $file = $utf8::stricter_to_file_of{$property_and_table};
363 # If didn't find it, try again with looser matching by editing
364 # out the applicable characters on the rhs and looking up
366 if (! defined $file) {
367 $table =~ s/ [_\s-] //xg;
368 $property_and_table = "$prefix$table";
369 print STDERR __LINE__, ": $property_and_table\n" if DEBUG;
370 $file = $utf8::loose_to_file_of{$property_and_table};
373 # Add the constant and go fetch it in.
375 if ($utf8::why_deprecated{$file}) {
376 warnings::warnif('deprecated', "Use of '$type' in \\p{} or \\P{} is deprecated because: $utf8::why_deprecated{$file};");
380 && exists $utf8::caseless_equivalent{$property_and_table})
382 $file = $utf8::caseless_equivalent{$property_and_table};
384 $file= "$unicore_dir/lib/$file.pl";
387 print STDERR __LINE__, ": didn't find $property_and_table\n" if DEBUG;
390 ## See if it's a user-level "To".
393 my $caller0 = caller(0);
395 if (defined $caller0 && $type =~ /^To(?:\w+)$/) {
396 my $map = $caller0 . "::" . $type;
398 if (exists &{$map}) {
402 warnings::warnif('deprecated', "User-defined case-mapping '$type' is deprecated");
408 ## Last attempt -- see if it's a standard "To" name
409 ## (e.g. "ToLower") ToTitle is used by ucfirst().
410 ## The user-level way to access ToDigit() and ToFold()
411 ## is to use Unicode::UCD.
413 if ($type =~ /^To(Digit|Fold|Lower|Title|Upper)$/) {
414 $file = "$unicore_dir/To/$1.pl";
415 ## would like to test to see if $file actually exists....
420 ## If we reach this line, it's because we couldn't figure
421 ## out what to do with $type. Ouch.
424 pop @recursed if @recursed;
429 print STDERR __LINE__, ": found it (file='$file')\n" if DEBUG;
432 ## If we reach here, it was due to a 'last GETFILE' above
433 ## (exception: user-defined properties and mappings), so we
434 ## have a filename, so now we load it if we haven't already.
435 ## If we have, return the cached results. The cache key is the
436 ## class and file to load.
438 my $found = $Cache{$class, $file};
439 if ($found and ref($found) eq $class) {
440 print STDERR __LINE__, ": Returning cached '$file' for \\p{$type}\n" if DEBUG;
441 pop @recursed if @recursed;
447 $list = do $file; die $@ if $@;
450 $ListSorted = 1; ## we know that these lists are sorted
457 my $taint = substr($list,0,0); # maintain taint
458 my @tmp = split(/^/m, $list);
461 $extras = join '', $taint, grep /^[^0-9a-fA-F]/, @tmp;
462 $list = join '', $taint,
464 sort { $a->[0] <=> $b->[0] }
465 map { /^([0-9a-fA-F]+)/; [ CORE::hex($1), $_ ] }
466 grep { /^([0-9a-fA-F]+)/ and not $seen{$1}++ } @tmp; # XXX doesn't do ranges right
470 my $hextra = sprintf "%04x", $none + 1;
471 $list =~ s/\tXXXX$/\t$hextra/mg;
474 if ($minbits != 1 && $minbits < 32) { # not binary property
476 while ($list =~ /^([0-9a-fA-F]+)(?:[\t]([0-9a-fA-F]+)?)(?:[ \t]([0-9a-fA-F]+))?/mg) {
477 my $min = CORE::hex $1;
478 my $max = defined $2 ? CORE::hex $2 : $min;
479 my $val = defined $3 ? CORE::hex $3 : 0;
480 $val += $max - $min if defined $3;
481 $top = $val if $val > $top;
485 $top > 0xff ? 16 : 8;
486 $bits = $topbits if $bits < $topbits;
491 for my $x ($extras) {
492 my $taint = substr($x,0,0); # maintain taint
494 while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
495 my $char = "$1$taint";
496 my $name = "$2$taint";
497 print STDERR __LINE__, ": char [$char] => name [$name]\n"
499 if ($char =~ /[-+!&]/) {
500 my ($c,$t) = split(/::/, $name, 2); # bogus use of ::, really
503 $subobj = utf8->SWASHNEW($t, "", $minbits, 0);
505 elsif (exists &$name) {
506 $subobj = utf8->SWASHNEW($name, "", $minbits, 0);
508 elsif ($c =~ /^([0-9a-fA-F]+)/) {
509 $subobj = utf8->SWASHNEW("", $c, $minbits, 0);
512 pop @recursed if @recursed && $type;
515 push @extras, $name => $subobj;
516 $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
523 print STDERR __LINE__, ": CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none";
524 print STDERR "\nLIST =>\n$list" if defined $list;
525 print STDERR "\nEXTRAS =>\n$extras" if defined $extras;
539 $Cache{$class, $file} = $SWASH;
542 pop @recursed if @recursed && $type;
548 # Now SWASHGET is recasted into a C function S_swash_get (see utf8.c).