This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
don't depend on threads to do a watchdog when testing threads
[perl5.git] / lib / utf8_heavy.pl
1 package utf8;
2 use strict;
3 use warnings;
4
5 sub DEBUG () { 0 }
6 $|=1 if DEBUG;
7
8 sub DESTROY {}
9
10 my %Cache;
11
12 sub croak { require Carp; Carp::croak(@_) }
13
14 sub _loose_name ($) {
15     # Given a lowercase property or property-value name, return its
16     # standardized version that is expected for look-up in the 'loose' hashes
17     # in Heavy.pl (hence, this depends on what mktables does).  This squeezes
18     # out blanks, underscores and dashes.  The complication stems from the
19     # grandfathered-in 'L_', which retains a single trailing underscore.
20
21     my $loose = $_[0] =~ s/[-\s_]//rg;
22
23     return $loose if $loose !~ / ^ (?: is | to )? l $/x;
24     return 'l_' if $_[0] =~ / l .* _ /x;    # If original had a trailing '_'
25     return $loose;
26 }
27
28 ##
29 ## "SWASH" == "SWATCH HASH". A "swatch" is a swatch of the Unicode landscape.
30 ## It's a data structure that encodes a set of Unicode characters.
31 ##
32
33 {
34     # If a floating point number is within this distance from the value of a
35     # fraction, it is considered to be that fraction, even if many more digits
36     # are specified that don't exactly match.
37     my $min_floating_slop;
38
39     # To guard against this program calling something that in turn ends up
40     # calling this program with the same inputs, and hence infinitely
41     # recursing, we keep a stack of the properties that are currently in
42     # progress, pushed upon entry, popped upon return.
43     my @recursed;
44
45     sub SWASHNEW {
46         my ($class, $type, $list, $minbits, $none) = @_;
47         my $user_defined = 0;
48         local $^D = 0 if $^D;
49
50         $class = "" unless defined $class;
51         print STDERR __LINE__, ": class=$class, type=$type, list=",
52                                 (defined $list) ? $list : ':undef:',
53                                 ", minbits=$minbits, none=$none\n" if DEBUG;
54
55         ##
56         ## Get the list of codepoints for the type.
57         ## Called from swash_init (see utf8.c) or SWASHNEW itself.
58         ##
59         ## Callers of swash_init:
60         ##     op.c:pmtrans             -- for tr/// and y///
61         ##     regexec.c:regclass_swash -- for /[]/, \p, and \P
62         ##     utf8.c:is_utf8_common    -- for common Unicode properties
63         ##     utf8.c:to_utf8_case      -- for lc, uc, ucfirst, etc. and //i
64         ##     Unicode::UCD::prop_invlist
65         ##     Unicode::UCD::prop_invmap
66         ##
67         ## Given a $type, our goal is to fill $list with the set of codepoint
68         ## ranges. If $type is false, $list passed is used.
69         ##
70         ## $minbits:
71         ##     For binary properties, $minbits must be 1.
72         ##     For character mappings (case and transliteration), $minbits must
73         ##     be a number except 1.
74         ##
75         ## $list (or that filled according to $type):
76         ##     Refer to perlunicode.pod, "User-Defined Character Properties."
77         ##     
78         ##     For binary properties, only characters with the property value
79         ##     of True should be listed. The 3rd column, if any, will be ignored
80         ##
81         ## $none is undocumented, so I'm (khw) trying to do some documentation
82         ## of it now.  It appears to be if there is a mapping in an input file
83         ## that maps to 'XXXX', then that is replaced by $none+1, expressed in
84         ## hexadecimal.  It is used somehow in tr///.
85         ##
86         ## To make the parsing of $type clear, this code takes the a rather
87         ## unorthodox approach of last'ing out of the block once we have the
88         ## info we need. Were this to be a subroutine, the 'last' would just
89         ## be a 'return'.
90         ##
91         #   If a problem is found $type is returned;
92         #   Upon success, a new (or cached) blessed object is returned with
93         #   keys TYPE, BITS, EXTRAS, LIST, and NONE with values having the
94         #   same meanings as the input parameters.
95         #   SPECIALS contains a reference to any special-treatment hash in the
96         #   INVERT_IT is non-zero if the result should be inverted before use
97         #   USER_DEFINED is non-zero if the result came from a user-defined
98         #       property.
99         my $file; ## file to load data from, and also part of the %Cache key.
100         my $ListSorted = 0;
101
102         # Change this to get a different set of Unicode tables
103         my $unicore_dir = 'unicore';
104         my $invert_it = 0;
105
106         if ($type)
107         {
108
109             # Verify that this isn't a recursive call for this property.
110             # Can't use croak, as it may try to recurse here itself.
111             my $class_type = $class . "::$type";
112             if (grep { $_ eq $class_type } @recursed) {
113                 CORE::die "panic: Infinite recursion in SWASHNEW for '$type'\n";
114             }
115             push @recursed, $class_type;
116
117             $type =~ s/^\s+//;
118             $type =~ s/\s+$//;
119
120             # regcomp.c surrounds the property name with '__" and '_i' if this
121             # is to be caseless matching.
122             my $caseless = $type =~ s/^__(.*)_i$/$1/;
123
124             print STDERR __LINE__, ": type=$type, caseless=$caseless\n" if DEBUG;
125
126         GETFILE:
127             {
128                 ##
129                 ## It could be a user-defined property.  Look in current
130                 ## package if no package given
131                 ##
132
133                 my $caller1 = $type =~ s/(.+)::// ? $1 : caller(1);
134
135                 if (defined $caller1 && $type =~ /^I[ns]\w+$/) {
136                     my $prop = "${caller1}::$type";
137                     if (exists &{$prop}) {
138                         # stolen from Scalar::Util::PP::tainted()
139                         my $tainted;
140                         {
141                             local($@, $SIG{__DIE__}, $SIG{__WARN__});
142                             local $^W = 0;
143                             no warnings;
144                             eval { kill 0 * $prop };
145                             $tainted = 1 if $@ =~ /^Insecure/;
146                         }
147                         die "Insecure user-defined property \\p{$prop}\n"
148                             if $tainted;
149                         no strict 'refs';
150                         $list = &{$prop}($caseless);
151                         $user_defined = 1;
152                         last GETFILE;
153                     }
154                 }
155
156                 # During Perl's compilation, this routine may be called before
157                 # the tables are constructed.  If so, we have a chicken/egg
158                 # problem.  If we die, the tables never get constructed, so
159                 # keep going, but return an empty table so only what the code
160                 # has compiled in internally (currently ASCII/Latin1 range
161                 # matching) will work.
162                 BEGIN {
163                     # Poor man's constant, to avoid a run-time check.
164                     $utf8::{miniperl}
165                         = \! defined &DynaLoader::boot_DynaLoader;
166                 }
167                 if (miniperl) {
168                     eval "require '$unicore_dir/Heavy.pl'";
169                     last GETFILE if $@;
170                 }
171                 else {
172                     require "$unicore_dir/Heavy.pl";
173                 }
174                 BEGIN { delete $utf8::{miniperl} }
175
176                 # All property names are matched caselessly
177                 my $property_and_table = CORE::lc $type;
178                 print STDERR __LINE__, ": $property_and_table\n" if DEBUG;
179
180                 # See if is of the compound form 'property=value', where the
181                 # value indicates the table we should use.
182                 my ($property, $table, @remainder) =
183                                     split /\s*[:=]\s*/, $property_and_table, -1;
184                 if (@remainder) {
185                     pop @recursed if @recursed;
186                     return $type;
187                 }
188
189                 my $prefix;
190                 if (! defined $table) {
191                         
192                     # Here, is the single form.  The property becomes empty, and
193                     # the whole value is the table.
194                     $table = $property;
195                     $prefix = $property = "";
196                 } else {
197                     print STDERR __LINE__, ": $property\n" if DEBUG;
198
199                     # Here it is the compound property=table form.  The property
200                     # name is always loosely matched, and always can have an
201                     # optional 'is' prefix (which isn't true in the single
202                     # form).
203                     $property = _loose_name($property) =~ s/^is//r;
204
205                     # And convert to canonical form.  Quit if not valid.
206                     $property = $utf8::loose_property_name_of{$property};
207                     if (! defined $property) {
208                         pop @recursed if @recursed;
209                         return $type;
210                     }
211
212                     $prefix = "$property=";
213
214                     # If the rhs looks like it is a number...
215                     print STDERR __LINE__, ": table=$table\n" if DEBUG;
216                     if ($table =~ qr{ ^ [ \s 0-9 _  + / . -]+ $ }x) {
217                         print STDERR __LINE__, ": table=$table\n" if DEBUG;
218
219                         # Don't allow leading nor trailing slashes 
220                         if ($table =~ / ^ \/ | \/ $ /x) {
221                             pop @recursed if @recursed;
222                             return $type;
223                         }
224
225                         # Split on slash, in case it is a rational, like \p{1/5}
226                         my @parts = split qr{ \s* / \s* }x, $table, -1;
227                         print __LINE__, ": $type\n" if @parts > 2 && DEBUG;
228
229                         # Can have maximum of one slash
230                         if (@parts > 2) {
231                             pop @recursed if @recursed;
232                             return $type;
233                         }
234
235                         foreach my $part (@parts) {
236                             print __LINE__, ": part=$part\n" if DEBUG;
237
238                             $part =~ s/^\+\s*//;    # Remove leading plus
239                             $part =~ s/^-\s*/-/;    # Remove blanks after unary
240                                                     # minus
241
242                             # Remove underscores between digits.
243                             $part =~ s/( ?<= [0-9] ) _ (?= [0-9] ) //xg;
244
245                             # No leading zeros (but don't make a single '0'
246                             # into a null string)
247                             $part =~ s/ ^ ( -? ) 0+ /$1/x;
248                             $part .= '0' if $part eq '-' || $part eq "";
249
250                             # No trailing zeros after a decimal point
251                             $part =~ s/ ( \. .*? ) 0+ $ /$1/x;
252
253                             # Begin with a 0 if a leading decimal point
254                             $part =~ s/ ^ ( -? ) \. /${1}0./x;
255
256                             # Ensure not a trailing decimal point: turn into an
257                             # integer
258                             $part =~ s/ \. $ //x;
259
260                             print STDERR __LINE__, ": part=$part\n" if DEBUG;
261                             #return $type if $part eq "";
262                             
263                             # Result better look like a number.  (This test is
264                             # needed because, for example could have a plus in
265                             # the middle.)
266                             if ($part !~ / ^ -? [0-9]+ ( \. [0-9]+)? $ /x) {
267                                 pop @recursed if @recursed;
268                                 return $type;
269                             }
270                         }
271
272                         #  If a rational...
273                         if (@parts == 2) {
274
275                             # If denominator is negative, get rid of it, and ...
276                             if ($parts[1] =~ s/^-//) {
277
278                                 # If numerator is also negative, convert the
279                                 # whole thing to positive, or move the minus to
280                                 # the numerator
281                                 if ($parts[0] !~ s/^-//) {
282                                     $parts[0] = '-' . $parts[0];
283                                 }
284                             }
285                             $table = join '/', @parts;
286                         }
287                         elsif ($property ne 'nv' || $parts[0] !~ /\./) {
288
289                             # Here is not numeric value, or doesn't have a
290                             # decimal point.  No further manipulation is
291                             # necessary.  (Note the hard-coded property name.
292                             # This could fail if other properties eventually
293                             # had fractions as well; perhaps the cjk ones
294                             # could evolve to do that.  This hard-coding could
295                             # be fixed by mktables generating a list of
296                             # properties that could have fractions.)
297                             $table = $parts[0];
298                         } else {
299
300                             # Here is a floating point numeric_value.  Try to
301                             # convert to rational.  First see if is in the list
302                             # of known ones.
303                             if (exists $utf8::nv_floating_to_rational{$parts[0]}) {
304                                 $table = $utf8::nv_floating_to_rational{$parts[0]};
305                             } else {
306
307                                 # Here not in the list.  See if is close
308                                 # enough to something in the list.  First
309                                 # determine what 'close enough' means.  It has
310                                 # to be as tight as what mktables says is the
311                                 # maximum slop, and as tight as how many
312                                 # digits we were passed.  That is, if the user
313                                 # said .667, .6667, .66667, etc.  we match as
314                                 # many digits as they passed until get to
315                                 # where it doesn't matter any more due to the
316                                 # machine's precision.  If they said .6666668,
317                                 # we fail.
318                                 (my $fraction = $parts[0]) =~ s/^.*\.//;
319                                 my $epsilon = 10 ** - (length($fraction));
320                                 if ($epsilon > $utf8::max_floating_slop) {
321                                     $epsilon = $utf8::max_floating_slop;
322                                 }
323
324                                 # But it can't be tighter than the minimum
325                                 # precision for this machine.  If haven't
326                                 # already calculated that minimum, do so now.
327                                 if (! defined $min_floating_slop) {
328
329                                     # Keep going down an order of magnitude
330                                     # until find that adding this quantity to
331                                     # 1 remains 1; but put an upper limit on
332                                     # this so in case this algorithm doesn't
333                                     # work properly on some platform, that we
334                                     # won't loop forever.
335                                     my $count = 0;
336                                     $min_floating_slop = 1;
337                                     while (1+ $min_floating_slop != 1
338                                            && $count++ < 50)
339                                     {
340                                         my $next = $min_floating_slop / 10;
341                                         last if $next == 0; # If underflows,
342                                                             # use previous one
343                                         $min_floating_slop = $next;
344                                         print STDERR __LINE__, ": min_float_slop=$min_floating_slop\n" if DEBUG;
345                                     }
346
347                                     # Back off a couple orders of magnitude,
348                                     # just to be safe.
349                                     $min_floating_slop *= 100;
350                                 }
351                                     
352                                 if ($epsilon < $min_floating_slop) {
353                                     $epsilon = $min_floating_slop;
354                                 }
355                                 print STDERR __LINE__, ": fraction=.$fraction; epsilon=$epsilon\n" if DEBUG;
356
357                                 undef $table;
358
359                                 # And for each possible rational in the table,
360                                 # see if it is within epsilon of the input.
361                                 foreach my $official
362                                         (keys %utf8::nv_floating_to_rational)
363                                 {
364                                     print STDERR __LINE__, ": epsilon=$epsilon, official=$official, diff=", abs($parts[0] - $official), "\n" if DEBUG;
365                                     if (abs($parts[0] - $official) < $epsilon) {
366                                       $table =
367                                       $utf8::nv_floating_to_rational{$official};
368                                         last;
369                                     }
370                                 }
371
372                                 # Quit if didn't find one.
373                                 if (! defined $table) {
374                                     pop @recursed if @recursed;
375                                     return $type;
376                                 }
377                             }
378                         }
379                         print STDERR __LINE__, ": $property=$table\n" if DEBUG;
380                     }
381                 }
382
383                 # Combine lhs (if any) and rhs to get something that matches
384                 # the syntax of the lookups.
385                 $property_and_table = "$prefix$table";
386                 print STDERR __LINE__, ": $property_and_table\n" if DEBUG;
387
388                 # First try stricter matching.
389                 $file = $utf8::stricter_to_file_of{$property_and_table};
390
391                 # If didn't find it, try again with looser matching by editing
392                 # out the applicable characters on the rhs and looking up
393                 # again.
394                 if (! defined $file) {
395                     $table = _loose_name($table);
396                     $property_and_table = "$prefix$table";
397                     print STDERR __LINE__, ": $property_and_table\n" if DEBUG;
398                     $file = $utf8::loose_to_file_of{$property_and_table};
399                 }
400
401                 # Add the constant and go fetch it in.
402                 if (defined $file) {
403
404                     # A beginning ! means to invert
405                     $invert_it = $file =~ s/^!//;
406
407                     if ($utf8::why_deprecated{$file}) {
408                         warnings::warnif('deprecated', "Use of '$type' in \\p{} or \\P{} is deprecated because: $utf8::why_deprecated{$file};");
409                     }
410
411                     if ($caseless
412                         && exists $utf8::caseless_equivalent{$property_and_table})
413                     {
414                         $file = $utf8::caseless_equivalent{$property_and_table};
415                     }
416                     $file= "$unicore_dir/lib/$file.pl";
417                     last GETFILE;
418                 }
419                 print STDERR __LINE__, ": didn't find $property_and_table\n" if DEBUG;
420
421                 ##
422                 ## Last attempt -- see if it's a standard "To" name
423                 ## (e.g. "ToLower")  ToTitle is used by ucfirst().
424                 ## The user-level way to access ToDigit() and ToFold()
425                 ## is to use Unicode::UCD.
426                 ##
427                 # Only check if caller wants non-binary
428                 my $retried = 0;
429                 if ($minbits != 1 && $property_and_table =~ s/^to//) {{
430                     # Look input up in list of properties for which we have
431                     # mapping files.
432                     if (defined ($file =
433                           $utf8::loose_property_to_file_of{$property_and_table}))
434                     {
435                         $type = $utf8::file_to_swash_name{$file};
436                         print STDERR __LINE__, ": type set to $type\n" if DEBUG;
437                         $file = "$unicore_dir/$file.pl";
438                         last GETFILE;
439                     }   # If that fails see if there is a corresponding binary
440                         # property file
441                     elsif (defined ($file =
442                                    $utf8::loose_to_file_of{$property_and_table}))
443                     {
444
445                         # Here, there is no map file for the property we are
446                         # trying to get the map of, but this is a binary
447                         # property, and there is a file for it that can easily
448                         # be translated to a mapping.
449
450                         # In the case of properties that are forced to binary,
451                         # they are a combination.  We return the actual
452                         # mapping instead of the binary.  If the input is
453                         # something like 'Tocjkkiicore', it will be found in
454                         # %loose_property_to_file_of above as => 'To/kIICore'.
455                         # But the form like ToIskiicore won't be.  To fix
456                         # this, it was easiest to do it here.  These
457                         # properties are the complements of the default
458                         # property, so there is an entry in %loose_to_file_of
459                         # that is 'iskiicore' => '!kIICore/N', If we find such
460                         # an entry, strip off things and try again, which
461                         # should find the entry in %loose_property_to_file_of.
462                         # Actual binary properties that are of this form, such
463                         # as this entry: 'ishrkt' => '!Perl/Any' will also be
464                         # retried, but won't be in %loose_property_to_file_of,
465                         # and instead the next time through, it will find
466                         # 'hrkt' => '!Perl/Any' and proceed.
467                         redo if ! $retried
468                                 && $file =~ /^!/
469                                 && $property_and_table =~ s/^is//;
470
471                         # This is a binary property.  Setting this here causes
472                         # it to be stored as such in the cache, so if someone
473                         # comes along later looking for just a binary, they
474                         # get it.
475                         $minbits = 1;
476
477                         $invert_it = $file =~ s/^!//;
478                         $file = "$unicore_dir/lib/$file.pl";
479                         last GETFILE;
480                     }
481                 } }
482
483                 ##
484                 ## If we reach this line, it's because we couldn't figure
485                 ## out what to do with $type. Ouch.
486                 ##
487
488                 pop @recursed if @recursed;
489                 return $type;
490             } # end of GETFILE block
491
492             if (defined $file) {
493                 print STDERR __LINE__, ": found it (file='$file')\n" if DEBUG;
494
495                 ##
496                 ## If we reach here, it was due to a 'last GETFILE' above
497                 ## (exception: user-defined properties and mappings), so we
498                 ## have a filename, so now we load it if we haven't already.
499                 ## If we have, return the cached results. The cache key is the
500                 ## class and file to load.
501                 ##
502                 my $found = $Cache{$class, $file};
503                 if ($found and ref($found) eq $class) {
504                     print STDERR __LINE__, ": Returning cached '$file' for \\p{$type}; invert_it=$invert_it\n" if DEBUG;
505                     pop @recursed if @recursed;
506                     $found->{'INVERT_IT'} = $invert_it;
507                     return $found;
508                 }
509
510                 local $@;
511                 local $!;
512                 $list = do $file; die $@ if $@;
513             }
514
515             $ListSorted = 1; ## we know that these lists are sorted
516         } # End of $type is non-null
517
518         # Here, either $type was null, or we found the requested property and
519         # read it into $list
520
521         my $extras;
522
523         my $bits = $minbits;
524
525         if ($list) {
526             my $taint = substr($list,0,0); # maintain taint
527             my @tmp = split(/^/m, $list);
528             my %seen;
529             no warnings;
530             $extras = join '', $taint, grep /^[^0-9a-fA-F]/, @tmp;
531             $list = join '', $taint,
532                 map  { $_->[1] }
533                 sort { $a->[0] <=> $b->[0] }
534                 map  { /^([0-9a-fA-F]+)/; [ CORE::hex($1), $_ ] }
535                 grep { /^([0-9a-fA-F]+)/ and not $seen{$1}++ } @tmp; # XXX doesn't do ranges right
536         }
537
538         if ($none) {
539             my $hextra = sprintf "%04x", $none + 1;
540             $list =~ s/\tXXXX$/\t$hextra/mg;
541         }
542
543         if ($minbits != 1 && $minbits < 32) { # not binary property
544             my $top = 0;
545             while ($list =~ /^([0-9a-fA-F]+)(?:[\t]([0-9a-fA-F]+)?)(?:[ \t]([0-9a-fA-F]+))?/mg) {
546                 my $min = CORE::hex $1;
547                 my $max = defined $2 ? CORE::hex $2 : $min;
548                 my $val = defined $3 ? CORE::hex $3 : 0;
549                 $val += $max - $min if defined $3;
550                 $top = $val if $val > $top;
551             }
552             my $topbits =
553                 $top > 0xffff ? 32 :
554                 $top > 0xff ? 16 : 8;
555             $bits = $topbits if $bits < $topbits;
556         }
557
558         my @extras;
559         if ($extras) {
560             for my $x ($extras) {
561                 my $taint = substr($x,0,0); # maintain taint
562                 pos $x = 0;
563                 while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
564                     my $char = "$1$taint";
565                     my $name = "$2$taint";
566                     print STDERR __LINE__, ": char [$char] => name [$name]\n"
567                         if DEBUG;
568                     if ($char =~ /[-+!&]/) {
569                         my ($c,$t) = split(/::/, $name, 2);     # bogus use of ::, really
570                         my $subobj;
571                         if ($c eq 'utf8') {
572                             $subobj = utf8->SWASHNEW($t, "", $minbits, 0);
573                         }
574                         elsif (exists &$name) {
575                             $subobj = utf8->SWASHNEW($name, "", $minbits, 0);
576                         }
577                         elsif ($c =~ /^([0-9a-fA-F]+)/) {
578                             $subobj = utf8->SWASHNEW("", $c, $minbits, 0);
579                         }
580                         if (! ref $subobj) {
581                             pop @recursed if @recursed && $type;
582                             return $subobj;
583                         }
584                         push @extras, $name => $subobj;
585                         $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
586                         $user_defined = $subobj->{USER_DEFINED}
587                                               if $subobj->{USER_DEFINED};
588                     }
589                 }
590             }
591         }
592
593         if (DEBUG) {
594             print STDERR __LINE__, ": CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none, INVERT_IT => $invert_it, USER_DEFINED => $user_defined";
595             print STDERR "\nLIST =>\n$list" if defined $list;
596             print STDERR "\nEXTRAS =>\n$extras" if defined $extras;
597             print STDERR "\n";
598         }
599
600         my $SWASH = bless {
601             TYPE => $type,
602             BITS => $bits,
603             EXTRAS => $extras,
604             LIST => $list,
605             NONE => $none,
606             USER_DEFINED => $user_defined,
607             @extras,
608         } => $class;
609
610         if ($file) {
611             $Cache{$class, $file} = $SWASH;
612             if ($type
613                 && exists $utf8::SwashInfo{$type}
614                 && exists $utf8::SwashInfo{$type}{'specials_name'})
615             {
616                 my $specials_name = $utf8::SwashInfo{$type}{'specials_name'};
617                 no strict "refs";
618                 print STDERR "\nspecials_name => $specials_name\n" if DEBUG;
619                 $SWASH->{'SPECIALS'} = \%$specials_name;
620             }
621             $SWASH->{'INVERT_IT'} = $invert_it;
622         }
623
624         pop @recursed if @recursed && $type;
625
626         return $SWASH;
627     }
628 }
629
630 # Now SWASHGET is recasted into a C function S_swash_get (see utf8.c).
631
632 1;