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