| 1 | #!perl -w |
| 2 | use 5.015; |
| 3 | use strict; |
| 4 | use warnings; |
| 5 | use Unicode::UCD qw(prop_aliases |
| 6 | prop_values |
| 7 | prop_value_aliases |
| 8 | prop_invlist |
| 9 | prop_invmap search_invlist |
| 10 | charprop |
| 11 | num |
| 12 | ); |
| 13 | require './regen/regen_lib.pl'; |
| 14 | require './regen/charset_translations.pl'; |
| 15 | require './lib/unicore/Heavy.pl'; |
| 16 | use re "/aa"; |
| 17 | |
| 18 | # This program outputs charclass_invlists.h, which contains various inversion |
| 19 | # lists in the form of C arrays that are to be used as-is for inversion lists. |
| 20 | # Thus, the lists it contains are essentially pre-compiled, and need only a |
| 21 | # light-weight fast wrapper to make them usable at run-time. |
| 22 | |
| 23 | # As such, this code knows about the internal structure of these lists, and |
| 24 | # any change made to that has to be done here as well. A random number stored |
| 25 | # in the headers is used to minimize the possibility of things getting |
| 26 | # out-of-sync, or the wrong data structure being passed. Currently that |
| 27 | # random number is: |
| 28 | |
| 29 | my $VERSION_DATA_STRUCTURE_TYPE = 148565664; |
| 30 | |
| 31 | # charclass_invlists.h now also contains inversion maps and enum definitions |
| 32 | # for those maps that have a finite number of possible values |
| 33 | |
| 34 | # integer or float |
| 35 | my $numeric_re = qr/ ^ -? \d+ (:? \. \d+ )? $ /x; |
| 36 | |
| 37 | # More than one code point may have the same code point as their fold. This |
| 38 | # gives the maximum number in the current Unicode release. (The folded-to |
| 39 | # code point is not included in this count.) Most folds are pairs of code |
| 40 | # points, like 'B' and 'b', so this number is at least one. |
| 41 | my $max_fold_froms = 1; |
| 42 | |
| 43 | my %keywords; |
| 44 | my $table_name_prefix = "UNI_"; |
| 45 | |
| 46 | # Matches valid C language enum names: begins with ASCII alphabetic, then any |
| 47 | # ASCII \w |
| 48 | my $enum_name_re = qr / ^ [[:alpha:]] \w* $ /ax; |
| 49 | |
| 50 | my $out_fh = open_new('charclass_invlists.h', '>', |
| 51 | {style => '*', by => 'regen/mk_invlists.pl', |
| 52 | from => "Unicode::UCD"}); |
| 53 | |
| 54 | my $in_file_pound_if = ""; |
| 55 | |
| 56 | my $max_hdr_len = 3; # In headings, how wide a name is allowed? |
| 57 | |
| 58 | print $out_fh "/* See the generating file for comments */\n\n"; |
| 59 | |
| 60 | # enums that should be made public |
| 61 | my %public_enums = ( |
| 62 | _Perl_SCX => 1 |
| 63 | ); |
| 64 | |
| 65 | # The symbols generated by this program are all currently defined only in a |
| 66 | # single dot c each. The code knows where most of them go, but this hash |
| 67 | # gives overrides for the exceptions to the typical place |
| 68 | my %exceptions_to_where_to_define = |
| 69 | ( |
| 70 | #_Perl_IVCF => 'PERL_IN_REGCOMP_C', |
| 71 | ); |
| 72 | |
| 73 | my %where_to_define_enums = (); |
| 74 | |
| 75 | my $applies_to_all_charsets_text = "all charsets"; |
| 76 | |
| 77 | my %gcb_enums; |
| 78 | my @gcb_short_enums; |
| 79 | my %gcb_abbreviations; |
| 80 | my %lb_enums; |
| 81 | my @lb_short_enums; |
| 82 | my %lb_abbreviations; |
| 83 | my %wb_enums; |
| 84 | my @wb_short_enums; |
| 85 | my %wb_abbreviations; |
| 86 | |
| 87 | my @a2n; |
| 88 | |
| 89 | my %prop_name_aliases; |
| 90 | # Invert this hash so that for each canonical name, we get a list of things |
| 91 | # that map to it (excluding itself) |
| 92 | foreach my $name (sort keys %utf8::loose_property_name_of) { |
| 93 | my $canonical = $utf8::loose_property_name_of{$name}; |
| 94 | push @{$prop_name_aliases{$canonical}}, $name if $canonical ne $name; |
| 95 | } |
| 96 | |
| 97 | # Output these tables in the same vicinity as each other, so that will get |
| 98 | # paged in at about the same time. These are also assumed to be the exact |
| 99 | # same list as those properties used internally by perl. |
| 100 | my %keep_together = ( |
| 101 | assigned => 1, |
| 102 | ascii => 1, |
| 103 | upper => 1, |
| 104 | lower => 1, |
| 105 | title => 1, |
| 106 | cased => 1, |
| 107 | uppercaseletter => 1, |
| 108 | lowercaseletter => 1, |
| 109 | titlecaseletter => 1, |
| 110 | casedletter => 1, |
| 111 | vertspace => 1, |
| 112 | xposixalnum => 1, |
| 113 | xposixalpha => 1, |
| 114 | xposixblank => 1, |
| 115 | xposixcntrl => 1, |
| 116 | xposixdigit => 1, |
| 117 | xposixgraph => 1, |
| 118 | xposixlower => 1, |
| 119 | xposixprint => 1, |
| 120 | xposixpunct => 1, |
| 121 | xposixspace => 1, |
| 122 | xposixupper => 1, |
| 123 | xposixword => 1, |
| 124 | xposixxdigit => 1, |
| 125 | posixalnum => 1, |
| 126 | posixalpha => 1, |
| 127 | posixblank => 1, |
| 128 | posixcntrl => 1, |
| 129 | posixdigit => 1, |
| 130 | posixgraph => 1, |
| 131 | posixlower => 1, |
| 132 | posixprint => 1, |
| 133 | posixpunct => 1, |
| 134 | posixspace => 1, |
| 135 | posixupper => 1, |
| 136 | posixword => 1, |
| 137 | posixxdigit => 1, |
| 138 | _perl_any_folds => 1, |
| 139 | _perl_folds_to_multi_char => 1, |
| 140 | _perl_is_in_multi_char_fold => 1, |
| 141 | _perl_non_final_folds => 1, |
| 142 | _perl_idstart => 1, |
| 143 | _perl_idcont => 1, |
| 144 | _perl_charname_begin => 1, |
| 145 | _perl_charname_continue => 1, |
| 146 | _perl_problematic_locale_foldeds_start => 1, |
| 147 | _perl_problematic_locale_folds => 1, |
| 148 | _perl_quotemeta => 1, |
| 149 | ); |
| 150 | my %perl_tags; # So can find synonyms of the above properties |
| 151 | |
| 152 | my $unused_table_hdr = 'u'; # Heading for row or column for unused values |
| 153 | |
| 154 | sub uniques { |
| 155 | # Returns non-duplicated input values. From "Perl Best Practices: |
| 156 | # Encapsulated Cleverness". p. 455 in first edition. |
| 157 | |
| 158 | my %seen; |
| 159 | return grep { ! $seen{$_}++ } @_; |
| 160 | } |
| 161 | |
| 162 | sub a2n($) { |
| 163 | my $cp = shift; |
| 164 | |
| 165 | # Returns the input Unicode code point translated to native. |
| 166 | |
| 167 | return $cp if $cp !~ $numeric_re || $cp > 255; |
| 168 | return $a2n[$cp]; |
| 169 | } |
| 170 | |
| 171 | sub end_file_pound_if { |
| 172 | if ($in_file_pound_if) { |
| 173 | print $out_fh "\n#endif\t/* $in_file_pound_if */\n"; |
| 174 | $in_file_pound_if = ""; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | sub end_charset_pound_if { |
| 179 | print $out_fh "\n" . get_conditional_compile_line_end(); |
| 180 | } |
| 181 | |
| 182 | sub switch_pound_if ($$;$) { |
| 183 | my $name = shift; |
| 184 | my $new_pound_if = shift; |
| 185 | my $charset = shift; |
| 186 | |
| 187 | my @new_pound_if = ref ($new_pound_if) |
| 188 | ? sort @$new_pound_if |
| 189 | : $new_pound_if; |
| 190 | |
| 191 | # Switch to new #if given by the 2nd argument. If there is an override |
| 192 | # for this, it instead switches to that. The 1st argument is the |
| 193 | # static's name, used only to check if there is an override for this |
| 194 | # |
| 195 | # The 'charset' parmameter, if present, is used to first end the charset |
| 196 | # #if if we actually do a switch, and then restart it afterwards. This |
| 197 | # code, then assumes that the charset #if's are enclosed in the file ones. |
| 198 | |
| 199 | if (exists $exceptions_to_where_to_define{$name}) { |
| 200 | @new_pound_if = $exceptions_to_where_to_define{$name}; |
| 201 | } |
| 202 | |
| 203 | foreach my $element (@new_pound_if) { |
| 204 | |
| 205 | # regcomp.c is arranged so that the tables are not compiled in |
| 206 | # re_comp.c */ |
| 207 | my $no_xsub = 1 if $element =~ / PERL_IN_ (?: REGCOMP ) _C /x; |
| 208 | $element = "defined($element)"; |
| 209 | $element = "($element && ! defined(PERL_IN_XSUB_RE))" if $no_xsub; |
| 210 | } |
| 211 | $new_pound_if = join " || ", @new_pound_if; |
| 212 | |
| 213 | # Change to the new one if different from old |
| 214 | if ($in_file_pound_if ne $new_pound_if) { |
| 215 | |
| 216 | end_charset_pound_if() if defined $charset; |
| 217 | |
| 218 | # Exit any current #if |
| 219 | if ($in_file_pound_if) { |
| 220 | end_file_pound_if; |
| 221 | } |
| 222 | |
| 223 | $in_file_pound_if = $new_pound_if; |
| 224 | print $out_fh "\n#if $in_file_pound_if\n"; |
| 225 | |
| 226 | start_charset_pound_if ($charset, 1) if defined $charset; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | sub start_charset_pound_if ($;$) { |
| 231 | print $out_fh "\n" . get_conditional_compile_line_start(shift, shift); |
| 232 | } |
| 233 | |
| 234 | { # Closure |
| 235 | my $fh; |
| 236 | my $in_doinit = 0; |
| 237 | |
| 238 | sub output_table_header($$$;$@) { |
| 239 | |
| 240 | # Output to $fh the heading for a table given by the other inputs |
| 241 | |
| 242 | $fh = shift; |
| 243 | my ($type, # typedef of table, like UV, UV* |
| 244 | $name, # name of table |
| 245 | $comment, # Optional comment to put on header line |
| 246 | @sizes # Optional sizes of each array index. If omitted, |
| 247 | # there is a single index whose size is computed by |
| 248 | # the C compiler. |
| 249 | ) = @_; |
| 250 | |
| 251 | $type =~ s/ \s+ $ //x; |
| 252 | |
| 253 | # If a the typedef is a ptr, add in an extra const |
| 254 | $type .= " const" if $type =~ / \* $ /x; |
| 255 | |
| 256 | $comment = "" unless defined $comment; |
| 257 | $comment = " /* $comment */" if $comment; |
| 258 | |
| 259 | my $array_declaration; |
| 260 | if (@sizes) { |
| 261 | $array_declaration = ""; |
| 262 | $array_declaration .= "[$_]" for @sizes; |
| 263 | } |
| 264 | else { |
| 265 | $array_declaration = '[]'; |
| 266 | } |
| 267 | |
| 268 | my $declaration = "$type ${name}$array_declaration"; |
| 269 | |
| 270 | # Things not matching this are static. Otherwise, it is an external |
| 271 | # constant, initialized only under DOINIT. |
| 272 | # |
| 273 | # (Currently everything is static) |
| 274 | if ($in_file_pound_if !~ / PERL_IN_ (?: ) _C /x) { |
| 275 | $in_doinit = 0; |
| 276 | print $fh "\nstatic const $declaration = {$comment\n"; |
| 277 | } |
| 278 | else { |
| 279 | $in_doinit = 1; |
| 280 | print $fh <<EOF; |
| 281 | |
| 282 | # ifndef DOINIT |
| 283 | |
| 284 | EXTCONST $declaration; |
| 285 | |
| 286 | # else |
| 287 | |
| 288 | EXTCONST $declaration = {$comment |
| 289 | EOF |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | sub output_table_trailer() { |
| 294 | |
| 295 | # Close out a table started by output_table_header() |
| 296 | |
| 297 | print $fh "};\n"; |
| 298 | if ($in_doinit) { |
| 299 | print $fh "\n# endif /* DOINIT */\n\n"; |
| 300 | $in_doinit = 0; |
| 301 | } |
| 302 | } |
| 303 | } # End closure |
| 304 | |
| 305 | |
| 306 | sub output_invlist ($$;$) { |
| 307 | my $name = shift; |
| 308 | my $invlist = shift; # Reference to inversion list array |
| 309 | my $charset = shift // ""; # name of character set for comment |
| 310 | |
| 311 | die "No inversion list for $name" unless defined $invlist |
| 312 | && ref $invlist eq 'ARRAY'; |
| 313 | |
| 314 | # Output the inversion list $invlist using the name $name for it. |
| 315 | # It is output in the exact internal form for inversion lists. |
| 316 | |
| 317 | # Is the last element of the header 0, or 1 ? |
| 318 | my $zero_or_one = 0; |
| 319 | if (@$invlist && $invlist->[0] != 0) { |
| 320 | unshift @$invlist, 0; |
| 321 | $zero_or_one = 1; |
| 322 | } |
| 323 | |
| 324 | $charset = "for $charset" if $charset; |
| 325 | output_table_header($out_fh, "UV", "${name}_invlist", $charset); |
| 326 | |
| 327 | my $count = @$invlist; |
| 328 | print $out_fh <<EOF; |
| 329 | \t$count,\t/* Number of elements */ |
| 330 | \t$VERSION_DATA_STRUCTURE_TYPE, /* Version and data structure type */ |
| 331 | \t$zero_or_one,\t/* 0 if the list starts at 0; |
| 332 | \t\t 1 if it starts at the element beyond 0 */ |
| 333 | EOF |
| 334 | |
| 335 | # The main body are the UVs passed in to this routine. Do the final |
| 336 | # element separately |
| 337 | for my $i (0 .. @$invlist - 1) { |
| 338 | printf $out_fh "\t0x%X", $invlist->[$i]; |
| 339 | print $out_fh "," if $i < @$invlist - 1; |
| 340 | print $out_fh "\n"; |
| 341 | } |
| 342 | |
| 343 | output_table_trailer(); |
| 344 | } |
| 345 | |
| 346 | sub output_invmap ($$$$$$$) { |
| 347 | my $name = shift; |
| 348 | my $invmap = shift; # Reference to inversion map array |
| 349 | my $prop_name = shift; |
| 350 | my $input_format = shift; # The inversion map's format |
| 351 | my $default = shift; # The property value for code points who |
| 352 | # otherwise don't have a value specified. |
| 353 | my $extra_enums = shift; # comma-separated list of our additions to the |
| 354 | # property's standard possible values |
| 355 | my $charset = shift // ""; # name of character set for comment |
| 356 | |
| 357 | # Output the inversion map $invmap for property $prop_name, but use $name |
| 358 | # as the actual data structure's name. |
| 359 | |
| 360 | my $count = @$invmap; |
| 361 | |
| 362 | my $output_format; |
| 363 | my $invmap_declaration_type; |
| 364 | my $enum_declaration_type; |
| 365 | my $aux_declaration_type; |
| 366 | my %enums; |
| 367 | my $name_prefix; |
| 368 | |
| 369 | if ($input_format =~ / ^ [as] l? $ /x) { |
| 370 | $prop_name = (prop_aliases($prop_name))[1] // $prop_name =~ s/^_Perl_//r; # Get full name |
| 371 | my $short_name = (prop_aliases($prop_name))[0] // $prop_name; |
| 372 | my @input_enums; |
| 373 | |
| 374 | # Find all the possible input values. These become the enum names |
| 375 | # that comprise the inversion map. For inputs that don't have sub |
| 376 | # lists, we can just get the unique values. Otherwise, we have to |
| 377 | # expand the sublists first. |
| 378 | if ($input_format !~ / ^ a /x) { |
| 379 | if ($input_format ne 'sl') { |
| 380 | @input_enums = sort(uniques(@$invmap)); |
| 381 | } |
| 382 | else { |
| 383 | foreach my $element (@$invmap) { |
| 384 | if (ref $element) { |
| 385 | push @input_enums, @$element; |
| 386 | } |
| 387 | else { |
| 388 | push @input_enums, $element; |
| 389 | } |
| 390 | } |
| 391 | @input_enums = sort(uniques(@input_enums)); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | # The internal enums come last, and in the order specified. |
| 396 | # |
| 397 | # The internal one named EDGE is also used a marker. Any ones that |
| 398 | # come after it are used in the algorithms below, and so must be |
| 399 | # defined, even if the release of Unicode this is being compiled for |
| 400 | # doesn't use them. But since no code points are assigned to them in |
| 401 | # such a release, those values will never be accessed. We collapse |
| 402 | # all of them into a single placholder row and a column. The |
| 403 | # algorithms below will fill in those cells with essentially garbage, |
| 404 | # but they are never read, so it doesn't matter. This allows the |
| 405 | # algorithm to remain the same from release to release. |
| 406 | # |
| 407 | # In one case, regexec.c also uses a placeholder which must be defined |
| 408 | # here, and we put it in the unused row and column as its value is |
| 409 | # never read. |
| 410 | # |
| 411 | my @enums = @input_enums; |
| 412 | my @extras; |
| 413 | my @unused_enums; |
| 414 | my $unused_enum_value = @enums; |
| 415 | if ($extra_enums ne "") { |
| 416 | @extras = split /,/, $extra_enums; |
| 417 | my $seen_EDGE = 0; |
| 418 | |
| 419 | # Don't add if already there. |
| 420 | foreach my $this_extra (@extras) { |
| 421 | next if grep { $_ eq $this_extra } @enums; |
| 422 | if ($this_extra eq 'EDGE') { |
| 423 | push @enums, $this_extra; |
| 424 | $seen_EDGE = 1; |
| 425 | } |
| 426 | elsif ($seen_EDGE) { |
| 427 | push @unused_enums, $this_extra; |
| 428 | } |
| 429 | else { |
| 430 | push @enums, $this_extra; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | @unused_enums = sort @unused_enums; |
| 435 | $unused_enum_value = @enums; # All unused have the same value, |
| 436 | # one beyond the final used one |
| 437 | } |
| 438 | |
| 439 | # Assign a value to each element of the enum type we are creating. |
| 440 | # The default value always gets 0; the others are arbitrarily |
| 441 | # assigned. |
| 442 | my $enum_val = 0; |
| 443 | my $canonical_default = prop_value_aliases($prop_name, $default); |
| 444 | $default = $canonical_default if defined $canonical_default; |
| 445 | $enums{$default} = $enum_val++; |
| 446 | |
| 447 | for my $enum (@enums) { |
| 448 | $enums{$enum} = $enum_val++ unless exists $enums{$enum}; |
| 449 | } |
| 450 | |
| 451 | # Calculate the data for the special tables output for these properties. |
| 452 | if ($name =~ / ^ _Perl_ (?: GCB | LB | WB ) $ /x) { |
| 453 | |
| 454 | # The data includes the hashes %gcb_enums, %lb_enums, etc. |
| 455 | # Similarly we calculate column headings for the tables. |
| 456 | # |
| 457 | # We use string evals to allow the same code to work on |
| 458 | # all the tables |
| 459 | my $type = lc $prop_name; |
| 460 | |
| 461 | # Skip if we've already done this code, which populated |
| 462 | # this hash |
| 463 | if (eval "! \%${type}_enums") { |
| 464 | |
| 465 | # For each enum in the type ... |
| 466 | foreach my $enum (sort keys %enums) { |
| 467 | my $value = $enums{$enum}; |
| 468 | my $short; |
| 469 | my $abbreviated_from; |
| 470 | |
| 471 | # Special case this wb property value to make the |
| 472 | # name more clear |
| 473 | if ($enum eq 'Perl_Tailored_HSpace') { |
| 474 | $short = 'hs'; |
| 475 | $abbreviated_from = $enum; |
| 476 | } |
| 477 | else { |
| 478 | |
| 479 | # Use the official short name, if found. |
| 480 | ($short) = prop_value_aliases($type, $enum); |
| 481 | |
| 482 | if (! defined $short) { |
| 483 | |
| 484 | # But if there is no official name, use the name |
| 485 | # that came from the data (if any). Otherwise, |
| 486 | # the name had to come from the extras list. |
| 487 | # There are two types of values in that list. |
| 488 | # |
| 489 | # First are those enums that are not part of the |
| 490 | # property, but are defined by this code. By |
| 491 | # convention these have all-caps names. We use |
| 492 | # the lowercased name for these. |
| 493 | # |
| 494 | # Second are enums that are needed to get the |
| 495 | # algorithms below to work and/or to get regexec.c |
| 496 | # to compile, but don't exist in all Unicode |
| 497 | # releases. These are handled outside this loop |
| 498 | # as 'unused_enums' |
| 499 | if (grep { $_ eq $enum } @input_enums) { |
| 500 | $short = $enum |
| 501 | } |
| 502 | else { |
| 503 | $short = lc $enum; |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | # If our short name is too long, or we already |
| 509 | # know that the name is an abbreviation, truncate |
| 510 | # to make sure it's short enough, and remember |
| 511 | # that we did this so we can later add a comment in the |
| 512 | # generated file |
| 513 | if ( $abbreviated_from |
| 514 | || length $short > $max_hdr_len) |
| 515 | { |
| 516 | $short = substr($short, 0, $max_hdr_len); |
| 517 | $abbreviated_from = $enum |
| 518 | unless $abbreviated_from; |
| 519 | # If the name we are to display conflicts, try |
| 520 | # another. |
| 521 | while (eval "exists |
| 522 | \$${type}_abbreviations{$short}") |
| 523 | { |
| 524 | die $@ if $@; |
| 525 | |
| 526 | # The increment operator on strings doesn't work |
| 527 | # on those containing an '_', so just use the |
| 528 | # final portion. |
| 529 | my @short = split '_', $short; |
| 530 | $short[-1]++; |
| 531 | $short = join "_", @short; |
| 532 | } |
| 533 | |
| 534 | eval "\$${type}_abbreviations{$short} = '$enum'"; |
| 535 | die $@ if $@; |
| 536 | } |
| 537 | |
| 538 | # Remember the mapping from the property value |
| 539 | # (enum) name to its value. |
| 540 | eval "\$${type}_enums{$enum} = $value"; |
| 541 | die $@ if $@; |
| 542 | |
| 543 | # Remember the inverse mapping to the short name |
| 544 | # so that we can properly label the generated |
| 545 | # table's rows and columns |
| 546 | eval "\$${type}_short_enums[$value] = '$short'"; |
| 547 | die $@ if $@; |
| 548 | } |
| 549 | |
| 550 | # Each unused enum has the same value. They all are collapsed |
| 551 | # into one row and one column, named $unused_table_hdr. |
| 552 | if (@unused_enums) { |
| 553 | eval "\$${type}_short_enums['$unused_enum_value'] = '$unused_table_hdr'"; |
| 554 | die $@ if $@; |
| 555 | |
| 556 | foreach my $enum (@unused_enums) { |
| 557 | eval "\$${type}_enums{$enum} = $unused_enum_value"; |
| 558 | die $@ if $@; |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | # The short names tend to be two lower case letters, but it looks |
| 565 | # better for those if they are upper. XXX |
| 566 | $short_name = uc($short_name) if length($short_name) < 3 |
| 567 | || substr($short_name, 0, 1) =~ /[[:lower:]]/; |
| 568 | $name_prefix = "${short_name}_"; |
| 569 | |
| 570 | # Start the enum definition for this map |
| 571 | my @enum_definition; |
| 572 | my @enum_list; |
| 573 | foreach my $enum (keys %enums) { |
| 574 | $enum_list[$enums{$enum}] = $enum; |
| 575 | } |
| 576 | foreach my $i (0 .. @enum_list - 1) { |
| 577 | push @enum_definition, ",\n" if $i > 0; |
| 578 | |
| 579 | my $name = $enum_list[$i]; |
| 580 | push @enum_definition, "\t${name_prefix}$name = $i"; |
| 581 | } |
| 582 | if (@unused_enums) { |
| 583 | foreach my $unused (@unused_enums) { |
| 584 | push @enum_definition, |
| 585 | ",\n\t${name_prefix}$unused = $unused_enum_value"; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | # For an 'l' property, we need extra enums, because some of the |
| 590 | # elements are lists. Each such distinct list is placed in its own |
| 591 | # auxiliary map table. Here, we go through the inversion map, and for |
| 592 | # each distinct list found, create an enum value for it, numbered -1, |
| 593 | # -2, .... |
| 594 | my %multiples; |
| 595 | my $aux_table_prefix = "AUX_TABLE_"; |
| 596 | if ($input_format =~ /l/) { |
| 597 | foreach my $element (@$invmap) { |
| 598 | |
| 599 | # A regular scalar is not one of the lists we're looking for |
| 600 | # at this stage. |
| 601 | next unless ref $element; |
| 602 | |
| 603 | my $joined; |
| 604 | if ($input_format =~ /a/) { # These are already ordered |
| 605 | $joined = join ",", @$element; |
| 606 | } |
| 607 | else { |
| 608 | $joined = join ",", sort @$element; |
| 609 | } |
| 610 | my $already_found = exists $multiples{$joined}; |
| 611 | |
| 612 | my $i; |
| 613 | if ($already_found) { # Use any existing one |
| 614 | $i = $multiples{$joined}; |
| 615 | } |
| 616 | else { # Otherwise increment to get a new table number |
| 617 | $i = keys(%multiples) + 1; |
| 618 | $multiples{$joined} = $i; |
| 619 | } |
| 620 | |
| 621 | # This changes the inversion map for this entry to not be the |
| 622 | # list |
| 623 | $element = "use_$aux_table_prefix$i"; |
| 624 | |
| 625 | # And add to the enum values |
| 626 | if (! $already_found) { |
| 627 | push @enum_definition, ",\n\t${name_prefix}$element = -$i"; |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | $enum_declaration_type = "${name_prefix}enum"; |
| 633 | |
| 634 | # Finished with the enum definition. Inversion map stuff is used only |
| 635 | # by regexec or utf-8 (if it is for code points) , unless it is in the |
| 636 | # enum exception list |
| 637 | my $where = (exists $where_to_define_enums{$name}) |
| 638 | ? $where_to_define_enums{$name} |
| 639 | : ($input_format =~ /a/) |
| 640 | ? 'PERL_IN_UTF8_C' |
| 641 | : 'PERL_IN_REGEXEC_C'; |
| 642 | |
| 643 | if (! exists $public_enums{$name}) { |
| 644 | switch_pound_if($name, $where, $charset); |
| 645 | } |
| 646 | else { |
| 647 | end_charset_pound_if; |
| 648 | end_file_pound_if; |
| 649 | start_charset_pound_if($charset, 1); |
| 650 | } |
| 651 | |
| 652 | # If the enum only contains one element, that is a dummy, default one |
| 653 | if (scalar @enum_definition > 1) { |
| 654 | |
| 655 | # Currently unneeded |
| 656 | #print $out_fh "\n#define ${name_prefix}ENUM_COUNT ", |
| 657 | # ..scalar keys %enums, "\n"; |
| 658 | |
| 659 | if ($input_format =~ /l/) { |
| 660 | print $out_fh |
| 661 | "\n", |
| 662 | "/* Negative enum values indicate the need to use an", |
| 663 | " auxiliary table\n", |
| 664 | " * consisting of the list of enums this one expands to.", |
| 665 | " The absolute\n", |
| 666 | " * values of the negative enums are indices into a table", |
| 667 | " of the auxiliary\n", |
| 668 | " * tables' addresses */"; |
| 669 | } |
| 670 | print $out_fh "\ntypedef enum {\n"; |
| 671 | print $out_fh join "", @enum_definition; |
| 672 | print $out_fh "\n"; |
| 673 | print $out_fh "} $enum_declaration_type;\n"; |
| 674 | } |
| 675 | |
| 676 | switch_pound_if($name, $where, $charset); |
| 677 | |
| 678 | $invmap_declaration_type = ($input_format =~ /s/) |
| 679 | ? $enum_declaration_type |
| 680 | : "int"; |
| 681 | $aux_declaration_type = ($input_format =~ /s/) |
| 682 | ? $enum_declaration_type |
| 683 | : "unsigned int"; |
| 684 | |
| 685 | $output_format = "${name_prefix}%s"; |
| 686 | |
| 687 | # If there are auxiliary tables, output them. |
| 688 | if (%multiples) { |
| 689 | |
| 690 | print $out_fh "\n#define HAS_${name_prefix}AUX_TABLES\n"; |
| 691 | |
| 692 | # Invert keys and values |
| 693 | my %inverted_mults; |
| 694 | while (my ($key, $value) = each %multiples) { |
| 695 | $inverted_mults{$value} = $key; |
| 696 | } |
| 697 | |
| 698 | # Output them in sorted order |
| 699 | my @sorted_table_list = sort { $a <=> $b } keys %inverted_mults; |
| 700 | |
| 701 | # Keep track of how big each aux table is |
| 702 | my @aux_counts; |
| 703 | |
| 704 | # Output each aux table. |
| 705 | foreach my $table_number (@sorted_table_list) { |
| 706 | my $table = $inverted_mults{$table_number}; |
| 707 | output_table_header($out_fh, |
| 708 | $aux_declaration_type, |
| 709 | "$name_prefix$aux_table_prefix$table_number"); |
| 710 | |
| 711 | # Earlier, we joined the elements of this table together with a comma |
| 712 | my @elements = split ",", $table; |
| 713 | |
| 714 | $aux_counts[$table_number] = scalar @elements; |
| 715 | for my $i (0 .. @elements - 1) { |
| 716 | print $out_fh ",\n" if $i > 0; |
| 717 | if ($input_format =~ /a/) { |
| 718 | printf $out_fh "\t0x%X", $elements[$i]; |
| 719 | } |
| 720 | else { |
| 721 | print $out_fh "\t${name_prefix}$elements[$i]"; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | print $out_fh "\n"; |
| 726 | output_table_trailer(); |
| 727 | } |
| 728 | |
| 729 | # Output the table that is indexed by the absolute value of the |
| 730 | # aux table enum and contains pointers to the tables output just |
| 731 | # above |
| 732 | output_table_header($out_fh, "$aux_declaration_type *", |
| 733 | "${name_prefix}${aux_table_prefix}ptrs"); |
| 734 | print $out_fh "\tNULL,\t/* Placeholder */\n"; |
| 735 | for my $i (1 .. @sorted_table_list) { |
| 736 | print $out_fh ",\n" if $i > 1; |
| 737 | print $out_fh "\t$name_prefix$aux_table_prefix$i"; |
| 738 | } |
| 739 | print $out_fh "\n"; |
| 740 | output_table_trailer(); |
| 741 | |
| 742 | print $out_fh |
| 743 | "\n/* Parallel table to the above, giving the number of elements" |
| 744 | . " in each table\n * pointed to */\n"; |
| 745 | output_table_header($out_fh, "U8", |
| 746 | "${name_prefix}${aux_table_prefix}lengths"); |
| 747 | print $out_fh "\t0,\t/* Placeholder */\n"; |
| 748 | for my $i (1 .. @sorted_table_list) { |
| 749 | print $out_fh ",\n" if $i > 1; |
| 750 | print $out_fh "\t$aux_counts[$i]\t/* $name_prefix$aux_table_prefix$i */"; |
| 751 | } |
| 752 | print $out_fh "\n"; |
| 753 | output_table_trailer(); |
| 754 | } # End of outputting the auxiliary and associated tables |
| 755 | |
| 756 | # The scx property used in regexec.c needs a specialized table which |
| 757 | # is most convenient to output here, while the data structures set up |
| 758 | # above are still extant. This table contains the code point that is |
| 759 | # the zero digit of each script, indexed by script enum value. |
| 760 | if (lc $short_name eq 'scx') { |
| 761 | my @decimals_invlist = prop_invlist("Numeric_Type=Decimal"); |
| 762 | my %script_zeros; |
| 763 | |
| 764 | # Find all the decimal digits. The 0 of each range is always the |
| 765 | # 0th element, except in some early Unicode releases, so check for |
| 766 | # that. |
| 767 | for (my $i = 0; $i < @decimals_invlist; $i += 2) { |
| 768 | my $code_point = $decimals_invlist[$i]; |
| 769 | next if num(chr($code_point)) ne '0'; |
| 770 | |
| 771 | # Turn the scripts this zero is in into a list. |
| 772 | my @scripts = split ",", |
| 773 | charprop($code_point, "_Perl_SCX", '_perl_core_internal_ok'); |
| 774 | $code_point = sprintf("0x%x", $code_point); |
| 775 | |
| 776 | foreach my $script (@scripts) { |
| 777 | if (! exists $script_zeros{$script}) { |
| 778 | $script_zeros{$script} = $code_point; |
| 779 | } |
| 780 | elsif (ref $script_zeros{$script}) { |
| 781 | push $script_zeros{$script}->@*, $code_point; |
| 782 | } |
| 783 | else { # Turn into a list if this is the 2nd zero of the |
| 784 | # script |
| 785 | my $existing = $script_zeros{$script}; |
| 786 | undef $script_zeros{$script}; |
| 787 | push $script_zeros{$script}->@*, $existing, $code_point; |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | # @script_zeros contains the zero, sorted by the script's enum |
| 793 | # value |
| 794 | my @script_zeros; |
| 795 | foreach my $script (keys %script_zeros) { |
| 796 | my $enum_value = $enums{$script}; |
| 797 | $script_zeros[$enum_value] = $script_zeros{$script}; |
| 798 | } |
| 799 | |
| 800 | print $out_fh |
| 801 | "\n/* This table, indexed by the script enum, gives the zero" |
| 802 | . " code point for that\n * script; 0 if the script has multiple" |
| 803 | . " digit sequences. Scripts without a\n * digit sequence use" |
| 804 | . " ASCII [0-9], hence are marked '0' */\n"; |
| 805 | output_table_header($out_fh, "UV", "script_zeros"); |
| 806 | for my $i (0 .. @script_zeros - 1) { |
| 807 | my $code_point = $script_zeros[$i]; |
| 808 | if (defined $code_point) { |
| 809 | $code_point = " 0" if ref $code_point; |
| 810 | print $out_fh "\t$code_point"; |
| 811 | } |
| 812 | elsif (lc $enum_list[$i] eq 'inherited') { |
| 813 | print $out_fh "\t 0"; |
| 814 | } |
| 815 | else { # The only digits a script without its own set accepts |
| 816 | # is [0-9] |
| 817 | print $out_fh "\t'0'"; |
| 818 | } |
| 819 | print $out_fh "," if $i < @script_zeros - 1; |
| 820 | print $out_fh "\t/* $enum_list[$i] */"; |
| 821 | print $out_fh "\n"; |
| 822 | } |
| 823 | output_table_trailer(); |
| 824 | } # End of special handling of scx |
| 825 | } |
| 826 | else { |
| 827 | die "'$input_format' invmap() format for '$prop_name' unimplemented"; |
| 828 | } |
| 829 | |
| 830 | die "No inversion map for $prop_name" unless defined $invmap |
| 831 | && ref $invmap eq 'ARRAY' |
| 832 | && $count; |
| 833 | |
| 834 | # Now output the inversion map proper |
| 835 | $charset = "for $charset" if $charset; |
| 836 | output_table_header($out_fh, $invmap_declaration_type, |
| 837 | "${name}_invmap", |
| 838 | $charset); |
| 839 | |
| 840 | # The main body are the scalars passed in to this routine. |
| 841 | for my $i (0 .. $count - 1) { |
| 842 | my $element = $invmap->[$i]; |
| 843 | my $full_element_name = prop_value_aliases($prop_name, $element); |
| 844 | if ($input_format =~ /a/ && $element !~ /\D/) { |
| 845 | $element = ($element == 0) |
| 846 | ? 0 |
| 847 | : sprintf("0x%X", $element); |
| 848 | } |
| 849 | else { |
| 850 | $element = $full_element_name if defined $full_element_name; |
| 851 | $element = $name_prefix . $element; |
| 852 | } |
| 853 | print $out_fh "\t$element"; |
| 854 | print $out_fh "," if $i < $count - 1; |
| 855 | print $out_fh "\n"; |
| 856 | } |
| 857 | output_table_trailer(); |
| 858 | } |
| 859 | |
| 860 | sub mk_invlist_from_sorted_cp_list { |
| 861 | |
| 862 | # Returns an inversion list constructed from the sorted input array of |
| 863 | # code points |
| 864 | |
| 865 | my $list_ref = shift; |
| 866 | |
| 867 | return unless @$list_ref; |
| 868 | |
| 869 | # Initialize to just the first element |
| 870 | my @invlist = ( $list_ref->[0], $list_ref->[0] + 1); |
| 871 | |
| 872 | # For each succeeding element, if it extends the previous range, adjust |
| 873 | # up, otherwise add it. |
| 874 | for my $i (1 .. @$list_ref - 1) { |
| 875 | if ($invlist[-1] == $list_ref->[$i]) { |
| 876 | $invlist[-1]++; |
| 877 | } |
| 878 | else { |
| 879 | push @invlist, $list_ref->[$i], $list_ref->[$i] + 1; |
| 880 | } |
| 881 | } |
| 882 | return @invlist; |
| 883 | } |
| 884 | |
| 885 | # Read in the Case Folding rules, and construct arrays of code points for the |
| 886 | # properties we need. |
| 887 | my ($cp_ref, $folds_ref, $format, $default) = prop_invmap("Case_Folding"); |
| 888 | die "Could not find inversion map for Case_Folding" unless defined $format; |
| 889 | die "Incorrect format '$format' for Case_Folding inversion map" |
| 890 | unless $format eq 'al' |
| 891 | || $format eq 'a'; |
| 892 | sub _Perl_IVCF { |
| 893 | |
| 894 | # This creates a map of the inversion of case folding. i.e., given a |
| 895 | # character, it gives all the other characters that fold to it. |
| 896 | # |
| 897 | # Inversion maps function kind of like a hash, with the inversion list |
| 898 | # specifying the buckets (keys) and the inversion maps specifying the |
| 899 | # contents of the corresponding bucket. Effectively this function just |
| 900 | # swaps the keys and values of the case fold hash. But there are |
| 901 | # complications. Most importantly, More than one character can each have |
| 902 | # the same fold. This is solved by having a list of characters that fold |
| 903 | # to a given one. |
| 904 | |
| 905 | my %new; |
| 906 | |
| 907 | # Go through the inversion list. |
| 908 | for (my $i = 0; $i < @$cp_ref; $i++) { |
| 909 | |
| 910 | # Skip if nothing folds to this |
| 911 | next if $folds_ref->[$i] == 0; |
| 912 | |
| 913 | # This entry which is valid from here to up (but not including) the |
| 914 | # next entry is for the next $count characters, so that, for example, |
| 915 | # A-Z is represented by one entry. |
| 916 | my $cur_list = $cp_ref->[$i]; |
| 917 | my $count = $cp_ref->[$i+1] - $cur_list; |
| 918 | |
| 919 | # The fold of [$i] can be not just a single character, but a sequence |
| 920 | # of multiple ones. We deal with those here by just creating a string |
| 921 | # consisting of them. Otherwise, we use the single code point [$i] |
| 922 | # folds to. |
| 923 | my $cur_map = (ref $folds_ref->[$i]) |
| 924 | ? join "", map { chr } $folds_ref->[$i]->@* |
| 925 | : $folds_ref->[$i]; |
| 926 | |
| 927 | # Expand out this range |
| 928 | while ($count > 0) { |
| 929 | push @{$new{$cur_map}}, $cur_list; |
| 930 | |
| 931 | # A multiple-character fold is a string, and shouldn't need |
| 932 | # incrementing anyway |
| 933 | if (ref $folds_ref->[$i]) { |
| 934 | die sprintf("Case fold for %x is multiple chars; should have" |
| 935 | . " a count of 1, but instead it was $count", $count) |
| 936 | unless $count == 1; |
| 937 | } |
| 938 | else { |
| 939 | $cur_map++; |
| 940 | $cur_list++; |
| 941 | } |
| 942 | $count--; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | # Now go through and make some adjustments. We add synthetic entries for |
| 947 | # two cases. |
| 948 | # 1) Two or more code points can fold to the same multiple character, |
| 949 | # sequence, as U+FB05 and U+FB06 both fold to 'st'. This code is only |
| 950 | # for single character folds, but FB05 and FB06 are single characters |
| 951 | # that are equivalent folded, so we add entries so that they are |
| 952 | # considered to fold to each other |
| 953 | # 2) If two or more above-Latin1 code points fold to the same Latin1 range |
| 954 | # one, we also add entries so that they are considered to fold to each |
| 955 | # other. This is so that under /aa or /l matching, where folding to |
| 956 | # their Latin1 range code point is illegal, they still can fold to each |
| 957 | # other. This situation happens in Unicode 3.0.1, but probably no |
| 958 | # other version. |
| 959 | foreach my $fold (keys %new) { |
| 960 | my $folds_to_string = $fold =~ /\D/; |
| 961 | |
| 962 | # If the bucket contains only one element, convert from an array to a |
| 963 | # scalar |
| 964 | if (scalar $new{$fold}->@* == 1) { |
| 965 | $new{$fold} = $new{$fold}[0]; |
| 966 | } |
| 967 | else { |
| 968 | |
| 969 | # Otherwise, sort numerically. This places the highest code point |
| 970 | # in the list at the tail end. This is because Unicode keeps the |
| 971 | # lowercase code points as higher ordinals than the uppercase, at |
| 972 | # least for the ones that matter so far. These are synthetic |
| 973 | # entries, and we want to predictably have the lowercase (which is |
| 974 | # more likely to be what gets folded to) in the same corresponding |
| 975 | # position, so that other code can rely on that. If some new |
| 976 | # version of Unicode came along that violated this, we might have |
| 977 | # to change so that the sort is based on upper vs lower instead. |
| 978 | # (The lower-comes-after isn't true of native EBCDIC, but here we |
| 979 | # are dealing strictly with Unicode values). |
| 980 | @{$new{$fold}} = sort { $a <=> $b } $new{$fold}->@* |
| 981 | unless $folds_to_string; |
| 982 | # We will be working with a copy of this sorted entry. |
| 983 | my @source_list = $new{$fold}->@*; |
| 984 | if (! $folds_to_string) { |
| 985 | |
| 986 | # This handles situation 2) listed above, which only arises if |
| 987 | # what is being folded-to (the fold) is in the Latin1 range. |
| 988 | if ($fold > 255 ) { |
| 989 | undef @source_list; |
| 990 | } |
| 991 | else { |
| 992 | # And it only arises if there are two or more folders that |
| 993 | # fold to it above Latin1. We look at just those. |
| 994 | @source_list = grep { $_ > 255 } @source_list; |
| 995 | undef @source_list if @source_list == 1; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | # Here, we've found the items we want to set up synthetic folds |
| 1000 | # for. Add entries so that each folds to each other. |
| 1001 | foreach my $cp (@source_list) { |
| 1002 | my @rest = grep { $cp != $_ } @source_list; |
| 1003 | if (@rest == 1) { |
| 1004 | $new{$cp} = $rest[0]; |
| 1005 | } |
| 1006 | else { |
| 1007 | push @{$new{$cp}}, @rest; |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | # We don't otherwise deal with multiple-character folds |
| 1013 | delete $new{$fold} if $folds_to_string; |
| 1014 | } |
| 1015 | |
| 1016 | |
| 1017 | # Now we have a hash that is the inversion of the case fold property. |
| 1018 | # First find the maximum number of code points that fold to the same one. |
| 1019 | foreach my $fold_to (keys %new) { |
| 1020 | if (ref $new{$fold_to}) { |
| 1021 | my $folders_count = scalar @{$new{$fold_to}}; |
| 1022 | $max_fold_froms = $folders_count if $folders_count > $max_fold_froms; |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | # Then convert the hash to an inversion map. |
| 1027 | my @sorted_folds = sort { $a <=> $b } keys %new; |
| 1028 | my (@invlist, @invmap); |
| 1029 | |
| 1030 | # We know that nothing folds to the controls (whose ordinals start at 0). |
| 1031 | # And the first real entries are the lowest in the hash. |
| 1032 | push @invlist, 0, $sorted_folds[0]; |
| 1033 | push @invmap, 0, $new{$sorted_folds[0]}; |
| 1034 | |
| 1035 | # Go through the remainder of the hash keys (which are the folded code |
| 1036 | # points) |
| 1037 | for (my $i = 1; $i < @sorted_folds; $i++) { |
| 1038 | |
| 1039 | # Get the current one, and the one prior to it. |
| 1040 | my $fold = $sorted_folds[$i]; |
| 1041 | my $prev_fold = $sorted_folds[$i-1]; |
| 1042 | |
| 1043 | # If the current one is not just 1 away from the prior one, we close |
| 1044 | # out the range containing the previous fold, and know that the gap |
| 1045 | # doesn't have anything that folds. |
| 1046 | if ($fold - 1 != $prev_fold) { |
| 1047 | push @invlist, $prev_fold + 1; |
| 1048 | push @invmap, 0; |
| 1049 | |
| 1050 | # And start a new range |
| 1051 | push @invlist, $fold; |
| 1052 | push @invmap, $new{$fold}; |
| 1053 | } |
| 1054 | elsif ($new{$fold} - 1 != $new{$prev_fold}) { |
| 1055 | |
| 1056 | # Here the current fold is just 1 greater than the previous, but |
| 1057 | # the new map isn't correspondingly 1 greater than the previous, |
| 1058 | # the old range is ended, but since there is no gap, we don't have |
| 1059 | # to insert anything else. |
| 1060 | push @invlist, $fold; |
| 1061 | push @invmap, $new{$fold}; |
| 1062 | |
| 1063 | } # else { Otherwise, this new entry just extends the previous } |
| 1064 | |
| 1065 | die "In IVCF: $invlist[-1] <= $invlist[-2]" |
| 1066 | if $invlist[-1] <= $invlist[-2]; |
| 1067 | } |
| 1068 | |
| 1069 | # And add an entry that indicates that everything above this, to infinity, |
| 1070 | # does not have a case fold. |
| 1071 | push @invlist, $sorted_folds[-1] + 1; |
| 1072 | push @invmap, 0; |
| 1073 | |
| 1074 | # All Unicode versions have some places where multiple code points map to |
| 1075 | # the same one, so the format always has an 'l' |
| 1076 | return \@invlist, \@invmap, 'al', $default; |
| 1077 | } |
| 1078 | |
| 1079 | sub prop_name_for_cmp ($) { # Sort helper |
| 1080 | my $name = shift; |
| 1081 | |
| 1082 | # Returns the input lowercased, with non-alphas removed, as well as |
| 1083 | # everything starting with a comma |
| 1084 | |
| 1085 | $name =~ s/,.*//; |
| 1086 | $name =~ s/[[:^alpha:]]//g; |
| 1087 | return lc $name; |
| 1088 | } |
| 1089 | |
| 1090 | sub UpperLatin1 { |
| 1091 | my @return = mk_invlist_from_sorted_cp_list([ 128 .. 255 ]); |
| 1092 | return \@return; |
| 1093 | } |
| 1094 | |
| 1095 | sub _Perl_CCC_non0_non230 { |
| 1096 | |
| 1097 | # Create an inversion list of code points with non-zero canonical |
| 1098 | # combining class that also don't have 230 as the class number. This is |
| 1099 | # part of a Unicode Standard rule |
| 1100 | |
| 1101 | my @nonzeros = prop_invlist("ccc=0"); |
| 1102 | shift @nonzeros; # Invert so is "ccc != 0" |
| 1103 | |
| 1104 | my @return; |
| 1105 | |
| 1106 | # Expand into list of code points, while excluding those with ccc == 230 |
| 1107 | for (my $i = 0; $i < @nonzeros; $i += 2) { |
| 1108 | my $upper = ($i + 1) < @nonzeros |
| 1109 | ? $nonzeros[$i+1] - 1 # In range |
| 1110 | : $Unicode::UCD::MAX_CP; # To infinity. |
| 1111 | for my $j ($nonzeros[$i] .. $upper) { |
| 1112 | my @ccc_names = prop_value_aliases("ccc", charprop($j, "ccc")); |
| 1113 | |
| 1114 | # Final element in @ccc_names will be all numeric |
| 1115 | push @return, $j if $ccc_names[-1] != 230; |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | @return = sort { $a <=> $b } @return; |
| 1120 | @return = mk_invlist_from_sorted_cp_list(\@return); |
| 1121 | return \@return; |
| 1122 | } |
| 1123 | |
| 1124 | sub output_table_common { |
| 1125 | |
| 1126 | # Common subroutine to actually output the generated rules table. |
| 1127 | |
| 1128 | my ($property, |
| 1129 | $table_value_defines_ref, |
| 1130 | $table_ref, |
| 1131 | $names_ref, |
| 1132 | $abbreviations_ref) = @_; |
| 1133 | my $size = @$table_ref; |
| 1134 | |
| 1135 | # Output the #define list, sorted by numeric value |
| 1136 | if ($table_value_defines_ref) { |
| 1137 | my $max_name_length = 0; |
| 1138 | my @defines; |
| 1139 | |
| 1140 | # Put in order, and at the same time find the longest name |
| 1141 | while (my ($enum, $value) = each %$table_value_defines_ref) { |
| 1142 | $defines[$value] = $enum; |
| 1143 | |
| 1144 | my $length = length $enum; |
| 1145 | $max_name_length = $length if $length > $max_name_length; |
| 1146 | } |
| 1147 | |
| 1148 | print $out_fh "\n"; |
| 1149 | |
| 1150 | # Output, so that the values are vertically aligned in a column after |
| 1151 | # the longest name |
| 1152 | foreach my $i (0 .. @defines - 1) { |
| 1153 | next unless defined $defines[$i]; |
| 1154 | printf $out_fh "#define %-*s %2d\n", |
| 1155 | $max_name_length, |
| 1156 | $defines[$i], |
| 1157 | $i; |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | my $column_width = 2; # We currently allow 2 digits for the number |
| 1162 | |
| 1163 | # Being above a U8 is not currently handled |
| 1164 | my $table_type = 'U8'; |
| 1165 | |
| 1166 | # If a name is longer than the width set aside for a column, its column |
| 1167 | # needs to have increased spacing so that the name doesn't get truncated |
| 1168 | # nor run into an adjacent column |
| 1169 | my @spacers; |
| 1170 | |
| 1171 | # Is there a row and column for unused values in this release? |
| 1172 | my $has_unused = $names_ref->[$size-1] eq $unused_table_hdr; |
| 1173 | |
| 1174 | for my $i (0 .. $size - 1) { |
| 1175 | no warnings 'numeric'; |
| 1176 | $spacers[$i] = " " x (length($names_ref->[$i]) - $column_width); |
| 1177 | } |
| 1178 | |
| 1179 | output_table_header($out_fh, $table_type, "${property}_table", undef, $size, $size); |
| 1180 | |
| 1181 | # Calculate the column heading line |
| 1182 | my $header_line = "/* " |
| 1183 | . (" " x $max_hdr_len) # We let the row heading meld to |
| 1184 | # the '*/' for those that are at |
| 1185 | # the max |
| 1186 | . " " x 3; # Space for '*/ ' |
| 1187 | # Now each column |
| 1188 | for my $i (0 .. $size - 1) { |
| 1189 | $header_line .= sprintf "%s%*s", |
| 1190 | $spacers[$i], |
| 1191 | $column_width + 1, # 1 for the ',' |
| 1192 | $names_ref->[$i]; |
| 1193 | } |
| 1194 | $header_line .= " */\n"; |
| 1195 | |
| 1196 | # If we have annotations, output it now. |
| 1197 | if ($has_unused || scalar %$abbreviations_ref) { |
| 1198 | my $text = ""; |
| 1199 | foreach my $abbr (sort keys %$abbreviations_ref) { |
| 1200 | $text .= "; " if $text; |
| 1201 | $text .= "'$abbr' stands for '$abbreviations_ref->{$abbr}'"; |
| 1202 | } |
| 1203 | if ($has_unused) { |
| 1204 | $text .= "; $unused_table_hdr stands for 'unused in this Unicode" |
| 1205 | . " release (and the data in the row or column are garbage)" |
| 1206 | } |
| 1207 | |
| 1208 | my $indent = " " x 3; |
| 1209 | $text = $indent . "/* $text */"; |
| 1210 | |
| 1211 | # Wrap the text so that it is no wider than the table, which the |
| 1212 | # header line gives. |
| 1213 | my $output_width = length $header_line; |
| 1214 | while (length $text > $output_width) { |
| 1215 | my $cur_line = substr($text, 0, $output_width); |
| 1216 | |
| 1217 | # Find the first blank back from the right end to wrap at. |
| 1218 | for (my $i = $output_width -1; $i > 0; $i--) { |
| 1219 | if (substr($text, $i, 1) eq " ") { |
| 1220 | print $out_fh substr($text, 0, $i), "\n"; |
| 1221 | |
| 1222 | # Set so will look at just the remaining tail (which will |
| 1223 | # be indented and have a '*' after the indent |
| 1224 | $text = $indent . " * " . substr($text, $i + 1); |
| 1225 | last; |
| 1226 | } |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | # And any remaining |
| 1231 | print $out_fh $text, "\n" if $text; |
| 1232 | } |
| 1233 | |
| 1234 | # We calculated the header line earlier just to get its width so that we |
| 1235 | # could make sure the annotations fit into that. |
| 1236 | print $out_fh $header_line; |
| 1237 | |
| 1238 | # Now output the bulk of the table. |
| 1239 | for my $i (0 .. $size - 1) { |
| 1240 | |
| 1241 | # First the row heading. |
| 1242 | printf $out_fh "/* %-*s*/ ", $max_hdr_len, $names_ref->[$i]; |
| 1243 | print $out_fh "{"; # Then the brace for this row |
| 1244 | |
| 1245 | # Then each column |
| 1246 | for my $j (0 .. $size -1) { |
| 1247 | print $out_fh $spacers[$j]; |
| 1248 | printf $out_fh "%*d", $column_width, $table_ref->[$i][$j]; |
| 1249 | print $out_fh "," if $j < $size - 1; |
| 1250 | } |
| 1251 | print $out_fh " }"; |
| 1252 | print $out_fh "," if $i < $size - 1; |
| 1253 | print $out_fh "\n"; |
| 1254 | } |
| 1255 | |
| 1256 | output_table_trailer(); |
| 1257 | } |
| 1258 | |
| 1259 | sub output_GCB_table() { |
| 1260 | |
| 1261 | # Create and output the pair table for use in determining Grapheme Cluster |
| 1262 | # Breaks, given in http://www.unicode.org/reports/tr29/. |
| 1263 | my %gcb_actions = ( |
| 1264 | GCB_NOBREAK => 0, |
| 1265 | GCB_BREAKABLE => 1, |
| 1266 | GCB_RI_then_RI => 2, # Rules 12 and 13 |
| 1267 | GCB_EX_then_EM => 3, # Rule 10 |
| 1268 | GCB_Maybe_Emoji_NonBreak => 4, |
| 1269 | ); |
| 1270 | |
| 1271 | # The table is constructed in reverse order of the rules, to make the |
| 1272 | # lower-numbered, higher priority ones override the later ones, as the |
| 1273 | # algorithm stops at the earliest matching rule |
| 1274 | |
| 1275 | my @gcb_table; |
| 1276 | my $table_size = @gcb_short_enums; |
| 1277 | |
| 1278 | # Otherwise, break everywhere. |
| 1279 | # GB99 Any ÷ Any |
| 1280 | for my $i (0 .. $table_size - 1) { |
| 1281 | for my $j (0 .. $table_size - 1) { |
| 1282 | $gcb_table[$i][$j] = 1; |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | # Do not break within emoji flag sequences. That is, do not break between |
| 1287 | # regional indicator (RI) symbols if there is an odd number of RI |
| 1288 | # characters before the break point. Must be resolved in runtime code. |
| 1289 | # |
| 1290 | # GB12 sot (RI RI)* RI × RI |
| 1291 | # GB13 [^RI] (RI RI)* RI × RI |
| 1292 | $gcb_table[$gcb_enums{'Regional_Indicator'}] |
| 1293 | [$gcb_enums{'Regional_Indicator'}] = $gcb_actions{GCB_RI_then_RI}; |
| 1294 | |
| 1295 | # Post 11.0: GB11 \p{Extended_Pictographic} Extend* ZWJ |
| 1296 | # × \p{Extended_Pictographic} |
| 1297 | $gcb_table[$gcb_enums{'ZWJ'}][$gcb_enums{'XPG_XX'}] = |
| 1298 | $gcb_actions{GCB_Maybe_Emoji_NonBreak}; |
| 1299 | |
| 1300 | # This and the rule GB10 obsolete starting with Unicode 11.0, can be left |
| 1301 | # in as there are no code points that match, so the code won't ever get |
| 1302 | # executed. |
| 1303 | # Do not break within emoji modifier sequences or emoji zwj sequences. |
| 1304 | # Pre 11.0: GB11 ZWJ × ( Glue_After_Zwj | E_Base_GAZ ) |
| 1305 | $gcb_table[$gcb_enums{'ZWJ'}][$gcb_enums{'Glue_After_Zwj'}] = 0; |
| 1306 | $gcb_table[$gcb_enums{'ZWJ'}][$gcb_enums{'E_Base_GAZ'}] = 0; |
| 1307 | |
| 1308 | # GB10 ( E_Base | E_Base_GAZ ) Extend* × E_Modifier |
| 1309 | $gcb_table[$gcb_enums{'Extend'}][$gcb_enums{'E_Modifier'}] |
| 1310 | = $gcb_actions{GCB_EX_then_EM}; |
| 1311 | $gcb_table[$gcb_enums{'E_Base'}][$gcb_enums{'E_Modifier'}] = 0; |
| 1312 | $gcb_table[$gcb_enums{'E_Base_GAZ'}][$gcb_enums{'E_Modifier'}] = 0; |
| 1313 | |
| 1314 | # Do not break before extending characters or ZWJ. |
| 1315 | # Do not break before SpacingMarks, or after Prepend characters. |
| 1316 | # GB9b Prepend × |
| 1317 | # GB9a × SpacingMark |
| 1318 | # GB9 × ( Extend | ZWJ ) |
| 1319 | for my $i (0 .. @gcb_table - 1) { |
| 1320 | $gcb_table[$gcb_enums{'Prepend'}][$i] = 0; |
| 1321 | $gcb_table[$i][$gcb_enums{'SpacingMark'}] = 0; |
| 1322 | $gcb_table[$i][$gcb_enums{'Extend'}] = 0; |
| 1323 | $gcb_table[$i][$gcb_enums{'ZWJ'}] = 0; |
| 1324 | } |
| 1325 | |
| 1326 | # Do not break Hangul syllable sequences. |
| 1327 | # GB8 ( LVT | T) × T |
| 1328 | $gcb_table[$gcb_enums{'LVT'}][$gcb_enums{'T'}] = 0; |
| 1329 | $gcb_table[$gcb_enums{'T'}][$gcb_enums{'T'}] = 0; |
| 1330 | |
| 1331 | # GB7 ( LV | V ) × ( V | T ) |
| 1332 | $gcb_table[$gcb_enums{'LV'}][$gcb_enums{'V'}] = 0; |
| 1333 | $gcb_table[$gcb_enums{'LV'}][$gcb_enums{'T'}] = 0; |
| 1334 | $gcb_table[$gcb_enums{'V'}][$gcb_enums{'V'}] = 0; |
| 1335 | $gcb_table[$gcb_enums{'V'}][$gcb_enums{'T'}] = 0; |
| 1336 | |
| 1337 | # GB6 L × ( L | V | LV | LVT ) |
| 1338 | $gcb_table[$gcb_enums{'L'}][$gcb_enums{'L'}] = 0; |
| 1339 | $gcb_table[$gcb_enums{'L'}][$gcb_enums{'V'}] = 0; |
| 1340 | $gcb_table[$gcb_enums{'L'}][$gcb_enums{'LV'}] = 0; |
| 1341 | $gcb_table[$gcb_enums{'L'}][$gcb_enums{'LVT'}] = 0; |
| 1342 | |
| 1343 | # Do not break between a CR and LF. Otherwise, break before and after |
| 1344 | # controls. |
| 1345 | # GB5 ÷ ( Control | CR | LF ) |
| 1346 | # GB4 ( Control | CR | LF ) ÷ |
| 1347 | for my $i (0 .. @gcb_table - 1) { |
| 1348 | $gcb_table[$i][$gcb_enums{'Control'}] = 1; |
| 1349 | $gcb_table[$i][$gcb_enums{'CR'}] = 1; |
| 1350 | $gcb_table[$i][$gcb_enums{'LF'}] = 1; |
| 1351 | $gcb_table[$gcb_enums{'Control'}][$i] = 1; |
| 1352 | $gcb_table[$gcb_enums{'CR'}][$i] = 1; |
| 1353 | $gcb_table[$gcb_enums{'LF'}][$i] = 1; |
| 1354 | } |
| 1355 | |
| 1356 | # GB3 CR × LF |
| 1357 | $gcb_table[$gcb_enums{'CR'}][$gcb_enums{'LF'}] = 0; |
| 1358 | |
| 1359 | # Break at the start and end of text, unless the text is empty |
| 1360 | # GB1 sot ÷ |
| 1361 | # GB2 ÷ eot |
| 1362 | for my $i (0 .. @gcb_table - 1) { |
| 1363 | $gcb_table[$i][$gcb_enums{'EDGE'}] = 1; |
| 1364 | $gcb_table[$gcb_enums{'EDGE'}][$i] = 1; |
| 1365 | } |
| 1366 | $gcb_table[$gcb_enums{'EDGE'}][$gcb_enums{'EDGE'}] = 0; |
| 1367 | |
| 1368 | output_table_common('GCB', \%gcb_actions, |
| 1369 | \@gcb_table, \@gcb_short_enums, \%gcb_abbreviations); |
| 1370 | } |
| 1371 | |
| 1372 | sub output_LB_table() { |
| 1373 | |
| 1374 | # Create and output the enums, #defines, and pair table for use in |
| 1375 | # determining Line Breaks. This uses the default line break algorithm, |
| 1376 | # given in http://www.unicode.org/reports/tr14/, but tailored by example 7 |
| 1377 | # in that page, as the Unicode-furnished tests assume that tailoring. |
| 1378 | |
| 1379 | # The result is really just true or false. But we follow along with tr14, |
| 1380 | # creating a rule which is false for something like X SP* X. That gets |
| 1381 | # encoding 2. The rest of the actions are synthetic ones that indicate |
| 1382 | # some context handling is required. These each are added to the |
| 1383 | # underlying 0, 1, or 2, instead of replacing them, so that the underlying |
| 1384 | # value can be retrieved. Actually only rules from 7 through 18 (which |
| 1385 | # are the ones where space matter) are possible to have 2 added to them. |
| 1386 | # The others below add just 0 or 1. It might be possible for one |
| 1387 | # synthetic rule to be added to another, yielding a larger value. This |
| 1388 | # doesn't happen in the Unicode 8.0 rule set, and as you can see from the |
| 1389 | # names of the middle grouping below, it is impossible for that to occur |
| 1390 | # for them because they all start with mutually exclusive classes. That |
| 1391 | # the final rule can't be added to any of the others isn't obvious from |
| 1392 | # its name, so it is assigned a power of 2 higher than the others can get |
| 1393 | # to so any addition would preserve all data. (And the code will reach an |
| 1394 | # assert(0) on debugging builds should this happen.) |
| 1395 | my %lb_actions = ( |
| 1396 | LB_NOBREAK => 0, |
| 1397 | LB_BREAKABLE => 1, |
| 1398 | LB_NOBREAK_EVEN_WITH_SP_BETWEEN => 2, |
| 1399 | |
| 1400 | LB_CM_ZWJ_foo => 3, # Rule 9 |
| 1401 | LB_SP_foo => 6, # Rule 18 |
| 1402 | LB_PR_or_PO_then_OP_or_HY => 9, # Rule 25 |
| 1403 | LB_SY_or_IS_then_various => 11, # Rule 25 |
| 1404 | LB_HY_or_BA_then_foo => 13, # Rule 21 |
| 1405 | LB_RI_then_RI => 15, # Rule 30a |
| 1406 | |
| 1407 | LB_various_then_PO_or_PR => (1<<5), # Rule 25 |
| 1408 | ); |
| 1409 | |
| 1410 | # Construct the LB pair table. This is based on the rules in |
| 1411 | # http://www.unicode.org/reports/tr14/, but modified as those rules are |
| 1412 | # designed for someone taking a string of text and sequentially going |
| 1413 | # through it to find the break opportunities, whereas, Perl requires |
| 1414 | # determining if a given random spot is a break opportunity, without |
| 1415 | # knowing all the entire string before it. |
| 1416 | # |
| 1417 | # The table is constructed in reverse order of the rules, to make the |
| 1418 | # lower-numbered, higher priority ones override the later ones, as the |
| 1419 | # algorithm stops at the earliest matching rule |
| 1420 | |
| 1421 | my @lb_table; |
| 1422 | my $table_size = @lb_short_enums; |
| 1423 | |
| 1424 | # LB31. Break everywhere else |
| 1425 | for my $i (0 .. $table_size - 1) { |
| 1426 | for my $j (0 .. $table_size - 1) { |
| 1427 | $lb_table[$i][$j] = $lb_actions{'LB_BREAKABLE'}; |
| 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | # LB30b Do not break between an emoji base and an emoji modifier. |
| 1432 | # EB × EM |
| 1433 | $lb_table[$lb_enums{'E_Base'}][$lb_enums{'E_Modifier'}] |
| 1434 | = $lb_actions{'LB_NOBREAK'}; |
| 1435 | |
| 1436 | # LB30a Break between two regional indicator symbols if and only if there |
| 1437 | # are an even number of regional indicators preceding the position of the |
| 1438 | # break. |
| 1439 | # sot (RI RI)* RI × RI |
| 1440 | # [^RI] (RI RI)* RI × RI |
| 1441 | $lb_table[$lb_enums{'Regional_Indicator'}] |
| 1442 | [$lb_enums{'Regional_Indicator'}] = $lb_actions{'LB_RI_then_RI'}; |
| 1443 | |
| 1444 | # LB30 Do not break between letters, numbers, or ordinary symbols and |
| 1445 | # opening or closing parentheses. |
| 1446 | # (AL | HL | NU) × OP |
| 1447 | $lb_table[$lb_enums{'Alphabetic'}][$lb_enums{'Open_Punctuation'}] |
| 1448 | = $lb_actions{'LB_NOBREAK'}; |
| 1449 | $lb_table[$lb_enums{'Hebrew_Letter'}][$lb_enums{'Open_Punctuation'}] |
| 1450 | = $lb_actions{'LB_NOBREAK'}; |
| 1451 | $lb_table[$lb_enums{'Numeric'}][$lb_enums{'Open_Punctuation'}] |
| 1452 | = $lb_actions{'LB_NOBREAK'}; |
| 1453 | |
| 1454 | # CP × (AL | HL | NU) |
| 1455 | $lb_table[$lb_enums{'Close_Parenthesis'}][$lb_enums{'Alphabetic'}] |
| 1456 | = $lb_actions{'LB_NOBREAK'}; |
| 1457 | $lb_table[$lb_enums{'Close_Parenthesis'}][$lb_enums{'Hebrew_Letter'}] |
| 1458 | = $lb_actions{'LB_NOBREAK'}; |
| 1459 | $lb_table[$lb_enums{'Close_Parenthesis'}][$lb_enums{'Numeric'}] |
| 1460 | = $lb_actions{'LB_NOBREAK'}; |
| 1461 | |
| 1462 | # LB29 Do not break between numeric punctuation and alphabetics (“e.g.”). |
| 1463 | # IS × (AL | HL) |
| 1464 | $lb_table[$lb_enums{'Infix_Numeric'}][$lb_enums{'Alphabetic'}] |
| 1465 | = $lb_actions{'LB_NOBREAK'}; |
| 1466 | $lb_table[$lb_enums{'Infix_Numeric'}][$lb_enums{'Hebrew_Letter'}] |
| 1467 | = $lb_actions{'LB_NOBREAK'}; |
| 1468 | |
| 1469 | # LB28 Do not break between alphabetics (“at”). |
| 1470 | # (AL | HL) × (AL | HL) |
| 1471 | $lb_table[$lb_enums{'Alphabetic'}][$lb_enums{'Alphabetic'}] |
| 1472 | = $lb_actions{'LB_NOBREAK'}; |
| 1473 | $lb_table[$lb_enums{'Hebrew_Letter'}][$lb_enums{'Alphabetic'}] |
| 1474 | = $lb_actions{'LB_NOBREAK'}; |
| 1475 | $lb_table[$lb_enums{'Alphabetic'}][$lb_enums{'Hebrew_Letter'}] |
| 1476 | = $lb_actions{'LB_NOBREAK'}; |
| 1477 | $lb_table[$lb_enums{'Hebrew_Letter'}][$lb_enums{'Hebrew_Letter'}] |
| 1478 | = $lb_actions{'LB_NOBREAK'}; |
| 1479 | |
| 1480 | # LB27 Treat a Korean Syllable Block the same as ID. |
| 1481 | # (JL | JV | JT | H2 | H3) × IN |
| 1482 | $lb_table[$lb_enums{'JL'}][$lb_enums{'Inseparable'}] |
| 1483 | = $lb_actions{'LB_NOBREAK'}; |
| 1484 | $lb_table[$lb_enums{'JV'}][$lb_enums{'Inseparable'}] |
| 1485 | = $lb_actions{'LB_NOBREAK'}; |
| 1486 | $lb_table[$lb_enums{'JT'}][$lb_enums{'Inseparable'}] |
| 1487 | = $lb_actions{'LB_NOBREAK'}; |
| 1488 | $lb_table[$lb_enums{'H2'}][$lb_enums{'Inseparable'}] |
| 1489 | = $lb_actions{'LB_NOBREAK'}; |
| 1490 | $lb_table[$lb_enums{'H3'}][$lb_enums{'Inseparable'}] |
| 1491 | = $lb_actions{'LB_NOBREAK'}; |
| 1492 | |
| 1493 | # (JL | JV | JT | H2 | H3) × PO |
| 1494 | $lb_table[$lb_enums{'JL'}][$lb_enums{'Postfix_Numeric'}] |
| 1495 | = $lb_actions{'LB_NOBREAK'}; |
| 1496 | $lb_table[$lb_enums{'JV'}][$lb_enums{'Postfix_Numeric'}] |
| 1497 | = $lb_actions{'LB_NOBREAK'}; |
| 1498 | $lb_table[$lb_enums{'JT'}][$lb_enums{'Postfix_Numeric'}] |
| 1499 | = $lb_actions{'LB_NOBREAK'}; |
| 1500 | $lb_table[$lb_enums{'H2'}][$lb_enums{'Postfix_Numeric'}] |
| 1501 | = $lb_actions{'LB_NOBREAK'}; |
| 1502 | $lb_table[$lb_enums{'H3'}][$lb_enums{'Postfix_Numeric'}] |
| 1503 | = $lb_actions{'LB_NOBREAK'}; |
| 1504 | |
| 1505 | # PR × (JL | JV | JT | H2 | H3) |
| 1506 | $lb_table[$lb_enums{'Prefix_Numeric'}][$lb_enums{'JL'}] |
| 1507 | = $lb_actions{'LB_NOBREAK'}; |
| 1508 | $lb_table[$lb_enums{'Prefix_Numeric'}][$lb_enums{'JV'}] |
| 1509 | = $lb_actions{'LB_NOBREAK'}; |
| 1510 | $lb_table[$lb_enums{'Prefix_Numeric'}][$lb_enums{'JT'}] |
| 1511 | = $lb_actions{'LB_NOBREAK'}; |
| 1512 | $lb_table[$lb_enums{'Prefix_Numeric'}][$lb_enums{'H2'}] |
| 1513 | = $lb_actions{'LB_NOBREAK'}; |
| 1514 | $lb_table[$lb_enums{'Prefix_Numeric'}][$lb_enums{'H3'}] |
| 1515 | = $lb_actions{'LB_NOBREAK'}; |
| 1516 | |
| 1517 | # LB26 Do not break a Korean syllable. |
| 1518 | # JL × (JL | JV | H2 | H3) |
| 1519 | $lb_table[$lb_enums{'JL'}][$lb_enums{'JL'}] = $lb_actions{'LB_NOBREAK'}; |
| 1520 | $lb_table[$lb_enums{'JL'}][$lb_enums{'JV'}] = $lb_actions{'LB_NOBREAK'}; |
| 1521 | $lb_table[$lb_enums{'JL'}][$lb_enums{'H2'}] = $lb_actions{'LB_NOBREAK'}; |
| 1522 | $lb_table[$lb_enums{'JL'}][$lb_enums{'H3'}] = $lb_actions{'LB_NOBREAK'}; |
| 1523 | |
| 1524 | # (JV | H2) × (JV | JT) |
| 1525 | $lb_table[$lb_enums{'JV'}][$lb_enums{'JV'}] = $lb_actions{'LB_NOBREAK'}; |
| 1526 | $lb_table[$lb_enums{'H2'}][$lb_enums{'JV'}] = $lb_actions{'LB_NOBREAK'}; |
| 1527 | $lb_table[$lb_enums{'JV'}][$lb_enums{'JT'}] = $lb_actions{'LB_NOBREAK'}; |
| 1528 | $lb_table[$lb_enums{'H2'}][$lb_enums{'JT'}] = $lb_actions{'LB_NOBREAK'}; |
| 1529 | |
| 1530 | # (JT | H3) × JT |
| 1531 | $lb_table[$lb_enums{'JT'}][$lb_enums{'JT'}] = $lb_actions{'LB_NOBREAK'}; |
| 1532 | $lb_table[$lb_enums{'H3'}][$lb_enums{'JT'}] = $lb_actions{'LB_NOBREAK'}; |
| 1533 | |
| 1534 | # LB25 Do not break between the following pairs of classes relevant to |
| 1535 | # numbers, as tailored by example 7 in |
| 1536 | # http://www.unicode.org/reports/tr14/#Examples |
| 1537 | # We follow that tailoring because Unicode's test cases expect it |
| 1538 | # (PR | PO) × ( OP | HY )? NU |
| 1539 | $lb_table[$lb_enums{'Prefix_Numeric'}][$lb_enums{'Numeric'}] |
| 1540 | = $lb_actions{'LB_NOBREAK'}; |
| 1541 | $lb_table[$lb_enums{'Postfix_Numeric'}][$lb_enums{'Numeric'}] |
| 1542 | = $lb_actions{'LB_NOBREAK'}; |
| 1543 | |
| 1544 | # Given that (OP | HY )? is optional, we have to test for it in code. |
| 1545 | # We add in the action (instead of overriding) for this, so that in |
| 1546 | # the code we can recover the underlying break value. |
| 1547 | $lb_table[$lb_enums{'Prefix_Numeric'}][$lb_enums{'Open_Punctuation'}] |
| 1548 | += $lb_actions{'LB_PR_or_PO_then_OP_or_HY'}; |
| 1549 | $lb_table[$lb_enums{'Postfix_Numeric'}][$lb_enums{'Open_Punctuation'}] |
| 1550 | += $lb_actions{'LB_PR_or_PO_then_OP_or_HY'}; |
| 1551 | $lb_table[$lb_enums{'Prefix_Numeric'}][$lb_enums{'Hyphen'}] |
| 1552 | += $lb_actions{'LB_PR_or_PO_then_OP_or_HY'}; |
| 1553 | $lb_table[$lb_enums{'Postfix_Numeric'}][$lb_enums{'Hyphen'}] |
| 1554 | += $lb_actions{'LB_PR_or_PO_then_OP_or_HY'}; |
| 1555 | |
| 1556 | # ( OP | HY ) × NU |
| 1557 | $lb_table[$lb_enums{'Open_Punctuation'}][$lb_enums{'Numeric'}] |
| 1558 | = $lb_actions{'LB_NOBREAK'}; |
| 1559 | $lb_table[$lb_enums{'Hyphen'}][$lb_enums{'Numeric'}] |
| 1560 | = $lb_actions{'LB_NOBREAK'}; |
| 1561 | |
| 1562 | # NU (NU | SY | IS)* × (NU | SY | IS | CL | CP ) |
| 1563 | # which can be rewritten as: |
| 1564 | # NU (SY | IS)* × (NU | SY | IS | CL | CP ) |
| 1565 | $lb_table[$lb_enums{'Numeric'}][$lb_enums{'Numeric'}] |
| 1566 | = $lb_actions{'LB_NOBREAK'}; |
| 1567 | $lb_table[$lb_enums{'Numeric'}][$lb_enums{'Break_Symbols'}] |
| 1568 | = $lb_actions{'LB_NOBREAK'}; |
| 1569 | $lb_table[$lb_enums{'Numeric'}][$lb_enums{'Infix_Numeric'}] |
| 1570 | = $lb_actions{'LB_NOBREAK'}; |
| 1571 | $lb_table[$lb_enums{'Numeric'}][$lb_enums{'Close_Punctuation'}] |
| 1572 | = $lb_actions{'LB_NOBREAK'}; |
| 1573 | $lb_table[$lb_enums{'Numeric'}][$lb_enums{'Close_Parenthesis'}] |
| 1574 | = $lb_actions{'LB_NOBREAK'}; |
| 1575 | |
| 1576 | # Like earlier where we have to test in code, we add in the action so |
| 1577 | # that we can recover the underlying values. This is done in rules |
| 1578 | # below, as well. The code assumes that we haven't added 2 actions. |
| 1579 | # Shoul a later Unicode release break that assumption, then tests |
| 1580 | # should start failing. |
| 1581 | $lb_table[$lb_enums{'Break_Symbols'}][$lb_enums{'Numeric'}] |
| 1582 | += $lb_actions{'LB_SY_or_IS_then_various'}; |
| 1583 | $lb_table[$lb_enums{'Break_Symbols'}][$lb_enums{'Break_Symbols'}] |
| 1584 | += $lb_actions{'LB_SY_or_IS_then_various'}; |
| 1585 | $lb_table[$lb_enums{'Break_Symbols'}][$lb_enums{'Infix_Numeric'}] |
| 1586 | += $lb_actions{'LB_SY_or_IS_then_various'}; |
| 1587 | $lb_table[$lb_enums{'Break_Symbols'}][$lb_enums{'Close_Punctuation'}] |
| 1588 | += $lb_actions{'LB_SY_or_IS_then_various'}; |
| 1589 | $lb_table[$lb_enums{'Break_Symbols'}][$lb_enums{'Close_Parenthesis'}] |
| 1590 | += $lb_actions{'LB_SY_or_IS_then_various'}; |
| 1591 | $lb_table[$lb_enums{'Infix_Numeric'}][$lb_enums{'Numeric'}] |
| 1592 | += $lb_actions{'LB_SY_or_IS_then_various'}; |
| 1593 | $lb_table[$lb_enums{'Infix_Numeric'}][$lb_enums{'Break_Symbols'}] |
| 1594 | += $lb_actions{'LB_SY_or_IS_then_various'}; |
| 1595 | $lb_table[$lb_enums{'Infix_Numeric'}][$lb_enums{'Infix_Numeric'}] |
| 1596 | += $lb_actions{'LB_SY_or_IS_then_various'}; |
| 1597 | $lb_table[$lb_enums{'Infix_Numeric'}][$lb_enums{'Close_Punctuation'}] |
| 1598 | += $lb_actions{'LB_SY_or_IS_then_various'}; |
| 1599 | $lb_table[$lb_enums{'Infix_Numeric'}][$lb_enums{'Close_Parenthesis'}] |
| 1600 | += $lb_actions{'LB_SY_or_IS_then_various'}; |
| 1601 | |
| 1602 | # NU (NU | SY | IS)* (CL | CP)? × (PO | PR) |
| 1603 | # which can be rewritten as: |
| 1604 | # NU (SY | IS)* (CL | CP)? × (PO | PR) |
| 1605 | $lb_table[$lb_enums{'Numeric'}][$lb_enums{'Postfix_Numeric'}] |
| 1606 | = $lb_actions{'LB_NOBREAK'}; |
| 1607 | $lb_table[$lb_enums{'Numeric'}][$lb_enums{'Prefix_Numeric'}] |
| 1608 | = $lb_actions{'LB_NOBREAK'}; |
| 1609 | |
| 1610 | $lb_table[$lb_enums{'Close_Parenthesis'}][$lb_enums{'Postfix_Numeric'}] |
| 1611 | += $lb_actions{'LB_various_then_PO_or_PR'}; |
| 1612 | $lb_table[$lb_enums{'Close_Punctuation'}][$lb_enums{'Postfix_Numeric'}] |
| 1613 | += $lb_actions{'LB_various_then_PO_or_PR'}; |
| 1614 | $lb_table[$lb_enums{'Infix_Numeric'}][$lb_enums{'Postfix_Numeric'}] |
| 1615 | += $lb_actions{'LB_various_then_PO_or_PR'}; |
| 1616 | $lb_table[$lb_enums{'Break_Symbols'}][$lb_enums{'Postfix_Numeric'}] |
| 1617 | += $lb_actions{'LB_various_then_PO_or_PR'}; |
| 1618 | |
| 1619 | $lb_table[$lb_enums{'Close_Parenthesis'}][$lb_enums{'Prefix_Numeric'}] |
| 1620 | += $lb_actions{'LB_various_then_PO_or_PR'}; |
| 1621 | $lb_table[$lb_enums{'Close_Punctuation'}][$lb_enums{'Prefix_Numeric'}] |
| 1622 | += $lb_actions{'LB_various_then_PO_or_PR'}; |
| 1623 | $lb_table[$lb_enums{'Infix_Numeric'}][$lb_enums{'Prefix_Numeric'}] |
| 1624 | += $lb_actions{'LB_various_then_PO_or_PR'}; |
| 1625 | $lb_table[$lb_enums{'Break_Symbols'}][$lb_enums{'Prefix_Numeric'}] |
| 1626 | += $lb_actions{'LB_various_then_PO_or_PR'}; |
| 1627 | |
| 1628 | # LB24 Do not break between numeric prefix/postfix and letters, or between |
| 1629 | # letters and prefix/postfix. |
| 1630 | # (PR | PO) × (AL | HL) |
| 1631 | $lb_table[$lb_enums{'Prefix_Numeric'}][$lb_enums{'Alphabetic'}] |
| 1632 | = $lb_actions{'LB_NOBREAK'}; |
| 1633 | $lb_table[$lb_enums{'Prefix_Numeric'}][$lb_enums{'Hebrew_Letter'}] |
| 1634 | = $lb_actions{'LB_NOBREAK'}; |
| 1635 | $lb_table[$lb_enums{'Postfix_Numeric'}][$lb_enums{'Alphabetic'}] |
| 1636 | = $lb_actions{'LB_NOBREAK'}; |
| 1637 | $lb_table[$lb_enums{'Postfix_Numeric'}][$lb_enums{'Hebrew_Letter'}] |
| 1638 | = $lb_actions{'LB_NOBREAK'}; |
| 1639 | |
| 1640 | # (AL | HL) × (PR | PO) |
| 1641 | $lb_table[$lb_enums{'Alphabetic'}][$lb_enums{'Prefix_Numeric'}] |
| 1642 | = $lb_actions{'LB_NOBREAK'}; |
| 1643 | $lb_table[$lb_enums{'Hebrew_Letter'}][$lb_enums{'Prefix_Numeric'}] |
| 1644 | = $lb_actions{'LB_NOBREAK'}; |
| 1645 | $lb_table[$lb_enums{'Alphabetic'}][$lb_enums{'Postfix_Numeric'}] |
| 1646 | = $lb_actions{'LB_NOBREAK'}; |
| 1647 | $lb_table[$lb_enums{'Hebrew_Letter'}][$lb_enums{'Postfix_Numeric'}] |
| 1648 | = $lb_actions{'LB_NOBREAK'}; |
| 1649 | |
| 1650 | # LB23a Do not break between numeric prefixes and ideographs, or between |
| 1651 | # ideographs and numeric postfixes. |
| 1652 | # PR × (ID | EB | EM) |
| 1653 | $lb_table[$lb_enums{'Prefix_Numeric'}][$lb_enums{'Ideographic'}] |
| 1654 | = $lb_actions{'LB_NOBREAK'}; |
| 1655 | $lb_table[$lb_enums{'Prefix_Numeric'}][$lb_enums{'E_Base'}] |
| 1656 | = $lb_actions{'LB_NOBREAK'}; |
| 1657 | $lb_table[$lb_enums{'Prefix_Numeric'}][$lb_enums{'E_Modifier'}] |
| 1658 | = $lb_actions{'LB_NOBREAK'}; |
| 1659 | |
| 1660 | # (ID | EB | EM) × PO |
| 1661 | $lb_table[$lb_enums{'Ideographic'}][$lb_enums{'Postfix_Numeric'}] |
| 1662 | = $lb_actions{'LB_NOBREAK'}; |
| 1663 | $lb_table[$lb_enums{'E_Base'}][$lb_enums{'Postfix_Numeric'}] |
| 1664 | = $lb_actions{'LB_NOBREAK'}; |
| 1665 | $lb_table[$lb_enums{'E_Modifier'}][$lb_enums{'Postfix_Numeric'}] |
| 1666 | = $lb_actions{'LB_NOBREAK'}; |
| 1667 | |
| 1668 | # LB23 Do not break between digits and letters |
| 1669 | # (AL | HL) × NU |
| 1670 | $lb_table[$lb_enums{'Alphabetic'}][$lb_enums{'Numeric'}] |
| 1671 | = $lb_actions{'LB_NOBREAK'}; |
| 1672 | $lb_table[$lb_enums{'Hebrew_Letter'}][$lb_enums{'Numeric'}] |
| 1673 | = $lb_actions{'LB_NOBREAK'}; |
| 1674 | |
| 1675 | # NU × (AL | HL) |
| 1676 | $lb_table[$lb_enums{'Numeric'}][$lb_enums{'Alphabetic'}] |
| 1677 | = $lb_actions{'LB_NOBREAK'}; |
| 1678 | $lb_table[$lb_enums{'Numeric'}][$lb_enums{'Hebrew_Letter'}] |
| 1679 | = $lb_actions{'LB_NOBREAK'}; |
| 1680 | |
| 1681 | # LB22 Do not break between two ellipses, or between letters, numbers or |
| 1682 | # exclamations and ellipsis. |
| 1683 | # (AL | HL) × IN |
| 1684 | $lb_table[$lb_enums{'Alphabetic'}][$lb_enums{'Inseparable'}] |
| 1685 | = $lb_actions{'LB_NOBREAK'}; |
| 1686 | $lb_table[$lb_enums{'Hebrew_Letter'}][$lb_enums{'Inseparable'}] |
| 1687 | = $lb_actions{'LB_NOBREAK'}; |
| 1688 | |
| 1689 | # Exclamation × IN |
| 1690 | $lb_table[$lb_enums{'Exclamation'}][$lb_enums{'Inseparable'}] |
| 1691 | = $lb_actions{'LB_NOBREAK'}; |
| 1692 | |
| 1693 | # (ID | EB | EM) × IN |
| 1694 | $lb_table[$lb_enums{'Ideographic'}][$lb_enums{'Inseparable'}] |
| 1695 | = $lb_actions{'LB_NOBREAK'}; |
| 1696 | $lb_table[$lb_enums{'E_Base'}][$lb_enums{'Inseparable'}] |
| 1697 | = $lb_actions{'LB_NOBREAK'}; |
| 1698 | $lb_table[$lb_enums{'E_Modifier'}][$lb_enums{'Inseparable'}] |
| 1699 | = $lb_actions{'LB_NOBREAK'}; |
| 1700 | |
| 1701 | # IN × IN |
| 1702 | $lb_table[$lb_enums{'Inseparable'}][$lb_enums{'Inseparable'}] |
| 1703 | = $lb_actions{'LB_NOBREAK'}; |
| 1704 | |
| 1705 | # NU × IN |
| 1706 | $lb_table[$lb_enums{'Numeric'}][$lb_enums{'Inseparable'}] |
| 1707 | = $lb_actions{'LB_NOBREAK'}; |
| 1708 | |
| 1709 | # LB21b Don’t break between Solidus and Hebrew letters. |
| 1710 | # SY × HL |
| 1711 | $lb_table[$lb_enums{'Break_Symbols'}][$lb_enums{'Hebrew_Letter'}] |
| 1712 | = $lb_actions{'LB_NOBREAK'}; |
| 1713 | |
| 1714 | # LB21a Don't break after Hebrew + Hyphen. |
| 1715 | # HL (HY | BA) × |
| 1716 | for my $i (0 .. @lb_table - 1) { |
| 1717 | $lb_table[$lb_enums{'Hyphen'}][$i] |
| 1718 | += $lb_actions{'LB_HY_or_BA_then_foo'}; |
| 1719 | $lb_table[$lb_enums{'Break_After'}][$i] |
| 1720 | += $lb_actions{'LB_HY_or_BA_then_foo'}; |
| 1721 | } |
| 1722 | |
| 1723 | # LB21 Do not break before hyphen-minus, other hyphens, fixed-width |
| 1724 | # spaces, small kana, and other non-starters, or after acute accents. |
| 1725 | # × BA |
| 1726 | # × HY |
| 1727 | # × NS |
| 1728 | # BB × |
| 1729 | for my $i (0 .. @lb_table - 1) { |
| 1730 | $lb_table[$i][$lb_enums{'Break_After'}] = $lb_actions{'LB_NOBREAK'}; |
| 1731 | $lb_table[$i][$lb_enums{'Hyphen'}] = $lb_actions{'LB_NOBREAK'}; |
| 1732 | $lb_table[$i][$lb_enums{'Nonstarter'}] = $lb_actions{'LB_NOBREAK'}; |
| 1733 | $lb_table[$lb_enums{'Break_Before'}][$i] = $lb_actions{'LB_NOBREAK'}; |
| 1734 | } |
| 1735 | |
| 1736 | # LB20 Break before and after unresolved CB. |
| 1737 | # ÷ CB |
| 1738 | # CB ÷ |
| 1739 | # Conditional breaks should be resolved external to the line breaking |
| 1740 | # rules. However, the default action is to treat unresolved CB as breaking |
| 1741 | # before and after. |
| 1742 | for my $i (0 .. @lb_table - 1) { |
| 1743 | $lb_table[$i][$lb_enums{'Contingent_Break'}] |
| 1744 | = $lb_actions{'LB_BREAKABLE'}; |
| 1745 | $lb_table[$lb_enums{'Contingent_Break'}][$i] |
| 1746 | = $lb_actions{'LB_BREAKABLE'}; |
| 1747 | } |
| 1748 | |
| 1749 | # LB19 Do not break before or after quotation marks, such as ‘ ” ’. |
| 1750 | # × QU |
| 1751 | # QU × |
| 1752 | for my $i (0 .. @lb_table - 1) { |
| 1753 | $lb_table[$i][$lb_enums{'Quotation'}] = $lb_actions{'LB_NOBREAK'}; |
| 1754 | $lb_table[$lb_enums{'Quotation'}][$i] = $lb_actions{'LB_NOBREAK'}; |
| 1755 | } |
| 1756 | |
| 1757 | # LB18 Break after spaces |
| 1758 | # SP ÷ |
| 1759 | for my $i (0 .. @lb_table - 1) { |
| 1760 | $lb_table[$lb_enums{'Space'}][$i] = $lb_actions{'LB_BREAKABLE'}; |
| 1761 | } |
| 1762 | |
| 1763 | # LB17 Do not break within ‘——’, even with intervening spaces. |
| 1764 | # B2 SP* × B2 |
| 1765 | $lb_table[$lb_enums{'Break_Both'}][$lb_enums{'Break_Both'}] |
| 1766 | = $lb_actions{'LB_NOBREAK_EVEN_WITH_SP_BETWEEN'}; |
| 1767 | |
| 1768 | # LB16 Do not break between closing punctuation and a nonstarter even with |
| 1769 | # intervening spaces. |
| 1770 | # (CL | CP) SP* × NS |
| 1771 | $lb_table[$lb_enums{'Close_Punctuation'}][$lb_enums{'Nonstarter'}] |
| 1772 | = $lb_actions{'LB_NOBREAK_EVEN_WITH_SP_BETWEEN'}; |
| 1773 | $lb_table[$lb_enums{'Close_Parenthesis'}][$lb_enums{'Nonstarter'}] |
| 1774 | = $lb_actions{'LB_NOBREAK_EVEN_WITH_SP_BETWEEN'}; |
| 1775 | |
| 1776 | |
| 1777 | # LB15 Do not break within ‘”[’, even with intervening spaces. |
| 1778 | # QU SP* × OP |
| 1779 | $lb_table[$lb_enums{'Quotation'}][$lb_enums{'Open_Punctuation'}] |
| 1780 | = $lb_actions{'LB_NOBREAK_EVEN_WITH_SP_BETWEEN'}; |
| 1781 | |
| 1782 | # LB14 Do not break after ‘[’, even after spaces. |
| 1783 | # OP SP* × |
| 1784 | for my $i (0 .. @lb_table - 1) { |
| 1785 | $lb_table[$lb_enums{'Open_Punctuation'}][$i] |
| 1786 | = $lb_actions{'LB_NOBREAK_EVEN_WITH_SP_BETWEEN'}; |
| 1787 | } |
| 1788 | |
| 1789 | # LB13 Do not break before ‘]’ or ‘!’ or ‘;’ or ‘/’, even after spaces, as |
| 1790 | # tailored by example 7 in http://www.unicode.org/reports/tr14/#Examples |
| 1791 | # [^NU] × CL |
| 1792 | # [^NU] × CP |
| 1793 | # × EX |
| 1794 | # [^NU] × IS |
| 1795 | # [^NU] × SY |
| 1796 | for my $i (0 .. @lb_table - 1) { |
| 1797 | $lb_table[$i][$lb_enums{'Exclamation'}] |
| 1798 | = $lb_actions{'LB_NOBREAK_EVEN_WITH_SP_BETWEEN'}; |
| 1799 | |
| 1800 | next if $i == $lb_enums{'Numeric'}; |
| 1801 | |
| 1802 | $lb_table[$i][$lb_enums{'Close_Punctuation'}] |
| 1803 | = $lb_actions{'LB_NOBREAK_EVEN_WITH_SP_BETWEEN'}; |
| 1804 | $lb_table[$i][$lb_enums{'Close_Parenthesis'}] |
| 1805 | = $lb_actions{'LB_NOBREAK_EVEN_WITH_SP_BETWEEN'}; |
| 1806 | $lb_table[$i][$lb_enums{'Infix_Numeric'}] |
| 1807 | = $lb_actions{'LB_NOBREAK_EVEN_WITH_SP_BETWEEN'}; |
| 1808 | $lb_table[$i][$lb_enums{'Break_Symbols'}] |
| 1809 | = $lb_actions{'LB_NOBREAK_EVEN_WITH_SP_BETWEEN'}; |
| 1810 | } |
| 1811 | |
| 1812 | # LB12a Do not break before NBSP and related characters, except after |
| 1813 | # spaces and hyphens. |
| 1814 | # [^SP BA HY] × GL |
| 1815 | for my $i (0 .. @lb_table - 1) { |
| 1816 | next if $i == $lb_enums{'Space'} |
| 1817 | || $i == $lb_enums{'Break_After'} |
| 1818 | || $i == $lb_enums{'Hyphen'}; |
| 1819 | |
| 1820 | # We don't break, but if a property above has said don't break even |
| 1821 | # with space between, don't override that (also in the next few rules) |
| 1822 | next if $lb_table[$i][$lb_enums{'Glue'}] |
| 1823 | == $lb_actions{'LB_NOBREAK_EVEN_WITH_SP_BETWEEN'}; |
| 1824 | $lb_table[$i][$lb_enums{'Glue'}] = $lb_actions{'LB_NOBREAK'}; |
| 1825 | } |
| 1826 | |
| 1827 | # LB12 Do not break after NBSP and related characters. |
| 1828 | # GL × |
| 1829 | for my $i (0 .. @lb_table - 1) { |
| 1830 | next if $lb_table[$lb_enums{'Glue'}][$i] |
| 1831 | == $lb_actions{'LB_NOBREAK_EVEN_WITH_SP_BETWEEN'}; |
| 1832 | $lb_table[$lb_enums{'Glue'}][$i] = $lb_actions{'LB_NOBREAK'}; |
| 1833 | } |
| 1834 | |
| 1835 | # LB11 Do not break before or after Word joiner and related characters. |
| 1836 | # × WJ |
| 1837 | # WJ × |
| 1838 | for my $i (0 .. @lb_table - 1) { |
| 1839 | if ($lb_table[$i][$lb_enums{'Word_Joiner'}] |
| 1840 | != $lb_actions{'LB_NOBREAK_EVEN_WITH_SP_BETWEEN'}) |
| 1841 | { |
| 1842 | $lb_table[$i][$lb_enums{'Word_Joiner'}] = $lb_actions{'LB_NOBREAK'}; |
| 1843 | } |
| 1844 | if ($lb_table[$lb_enums{'Word_Joiner'}][$i] |
| 1845 | != $lb_actions{'LB_NOBREAK_EVEN_WITH_SP_BETWEEN'}) |
| 1846 | { |
| 1847 | $lb_table[$lb_enums{'Word_Joiner'}][$i] = $lb_actions{'LB_NOBREAK'}; |
| 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | # Special case this here to avoid having to do a special case in the code, |
| 1852 | # by making this the same as other things with a SP in front of them that |
| 1853 | # don't break, we avoid an extra test |
| 1854 | $lb_table[$lb_enums{'Space'}][$lb_enums{'Word_Joiner'}] |
| 1855 | = $lb_actions{'LB_NOBREAK_EVEN_WITH_SP_BETWEEN'}; |
| 1856 | |
| 1857 | # LB9 and LB10 are done in the same loop |
| 1858 | # |
| 1859 | # LB9 Do not break a combining character sequence; treat it as if it has |
| 1860 | # the line breaking class of the base character in all of the |
| 1861 | # higher-numbered rules. Treat ZWJ as if it were CM |
| 1862 | # Treat X (CM|ZWJ)* as if it were X. |
| 1863 | # where X is any line break class except BK, CR, LF, NL, SP, or ZW. |
| 1864 | |
| 1865 | # LB10 Treat any remaining combining mark or ZWJ as AL. This catches the |
| 1866 | # case where a CM or ZWJ is the first character on the line or follows SP, |
| 1867 | # BK, CR, LF, NL, or ZW. |
| 1868 | for my $i (0 .. @lb_table - 1) { |
| 1869 | |
| 1870 | # When the CM or ZWJ is the first in the pair, we don't know without |
| 1871 | # looking behind whether the CM or ZWJ is going to attach to an |
| 1872 | # earlier character, or not. So have to figure this out at runtime in |
| 1873 | # the code |
| 1874 | $lb_table[$lb_enums{'Combining_Mark'}][$i] |
| 1875 | = $lb_actions{'LB_CM_ZWJ_foo'}; |
| 1876 | $lb_table[$lb_enums{'ZWJ'}][$i] = $lb_actions{'LB_CM_ZWJ_foo'}; |
| 1877 | |
| 1878 | if ( $i == $lb_enums{'Mandatory_Break'} |
| 1879 | || $i == $lb_enums{'EDGE'} |
| 1880 | || $i == $lb_enums{'Carriage_Return'} |
| 1881 | || $i == $lb_enums{'Line_Feed'} |
| 1882 | || $i == $lb_enums{'Next_Line'} |
| 1883 | || $i == $lb_enums{'Space'} |
| 1884 | || $i == $lb_enums{'ZWSpace'}) |
| 1885 | { |
| 1886 | # For these classes, a following CM doesn't combine, and should do |
| 1887 | # whatever 'Alphabetic' would do. |
| 1888 | $lb_table[$i][$lb_enums{'Combining_Mark'}] |
| 1889 | = $lb_table[$i][$lb_enums{'Alphabetic'}]; |
| 1890 | $lb_table[$i][$lb_enums{'ZWJ'}] |
| 1891 | = $lb_table[$i][$lb_enums{'Alphabetic'}]; |
| 1892 | } |
| 1893 | else { |
| 1894 | # For these classes, the CM or ZWJ combines, so doesn't break, |
| 1895 | # inheriting the type of nobreak from the master character. |
| 1896 | if ($lb_table[$i][$lb_enums{'Combining_Mark'}] |
| 1897 | != $lb_actions{'LB_NOBREAK_EVEN_WITH_SP_BETWEEN'}) |
| 1898 | { |
| 1899 | $lb_table[$i][$lb_enums{'Combining_Mark'}] |
| 1900 | = $lb_actions{'LB_NOBREAK'}; |
| 1901 | } |
| 1902 | if ($lb_table[$i][$lb_enums{'ZWJ'}] |
| 1903 | != $lb_actions{'LB_NOBREAK_EVEN_WITH_SP_BETWEEN'}) |
| 1904 | { |
| 1905 | $lb_table[$i][$lb_enums{'ZWJ'}] |
| 1906 | = $lb_actions{'LB_NOBREAK'}; |
| 1907 | } |
| 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | # LB8a Do not break after a zero width joiner |
| 1912 | # ZWJ × |
| 1913 | for my $i (0 .. @lb_table - 1) { |
| 1914 | $lb_table[$lb_enums{'ZWJ'}][$i] = $lb_actions{'LB_NOBREAK'}; |
| 1915 | } |
| 1916 | |
| 1917 | # LB8 Break before any character following a zero-width space, even if one |
| 1918 | # or more spaces intervene. |
| 1919 | # ZW SP* ÷ |
| 1920 | for my $i (0 .. @lb_table - 1) { |
| 1921 | $lb_table[$lb_enums{'ZWSpace'}][$i] = $lb_actions{'LB_BREAKABLE'}; |
| 1922 | } |
| 1923 | |
| 1924 | # Because of LB8-10, we need to look at context for "SP x", and this must |
| 1925 | # be done in the code. So override the existing rules for that, by adding |
| 1926 | # a constant to get new rules that tell the code it needs to look at |
| 1927 | # context. By adding this action instead of replacing the existing one, |
| 1928 | # we can get back to the original rule if necessary. |
| 1929 | for my $i (0 .. @lb_table - 1) { |
| 1930 | $lb_table[$lb_enums{'Space'}][$i] += $lb_actions{'LB_SP_foo'}; |
| 1931 | } |
| 1932 | |
| 1933 | # LB7 Do not break before spaces or zero width space. |
| 1934 | # × SP |
| 1935 | # × ZW |
| 1936 | for my $i (0 .. @lb_table - 1) { |
| 1937 | $lb_table[$i][$lb_enums{'Space'}] = $lb_actions{'LB_NOBREAK'}; |
| 1938 | $lb_table[$i][$lb_enums{'ZWSpace'}] = $lb_actions{'LB_NOBREAK'}; |
| 1939 | } |
| 1940 | |
| 1941 | # LB6 Do not break before hard line breaks. |
| 1942 | # × ( BK | CR | LF | NL ) |
| 1943 | for my $i (0 .. @lb_table - 1) { |
| 1944 | $lb_table[$i][$lb_enums{'Mandatory_Break'}] = $lb_actions{'LB_NOBREAK'}; |
| 1945 | $lb_table[$i][$lb_enums{'Carriage_Return'}] = $lb_actions{'LB_NOBREAK'}; |
| 1946 | $lb_table[$i][$lb_enums{'Line_Feed'}] = $lb_actions{'LB_NOBREAK'}; |
| 1947 | $lb_table[$i][$lb_enums{'Next_Line'}] = $lb_actions{'LB_NOBREAK'}; |
| 1948 | } |
| 1949 | |
| 1950 | # LB5 Treat CR followed by LF, as well as CR, LF, and NL as hard line breaks. |
| 1951 | # CR × LF |
| 1952 | # CR ! |
| 1953 | # LF ! |
| 1954 | # NL ! |
| 1955 | for my $i (0 .. @lb_table - 1) { |
| 1956 | $lb_table[$lb_enums{'Carriage_Return'}][$i] |
| 1957 | = $lb_actions{'LB_BREAKABLE'}; |
| 1958 | $lb_table[$lb_enums{'Line_Feed'}][$i] = $lb_actions{'LB_BREAKABLE'}; |
| 1959 | $lb_table[$lb_enums{'Next_Line'}][$i] = $lb_actions{'LB_BREAKABLE'}; |
| 1960 | } |
| 1961 | $lb_table[$lb_enums{'Carriage_Return'}][$lb_enums{'Line_Feed'}] |
| 1962 | = $lb_actions{'LB_NOBREAK'}; |
| 1963 | |
| 1964 | # LB4 Always break after hard line breaks. |
| 1965 | # BK ! |
| 1966 | for my $i (0 .. @lb_table - 1) { |
| 1967 | $lb_table[$lb_enums{'Mandatory_Break'}][$i] |
| 1968 | = $lb_actions{'LB_BREAKABLE'}; |
| 1969 | } |
| 1970 | |
| 1971 | # LB3 Always break at the end of text. |
| 1972 | # ! eot |
| 1973 | # LB2 Never break at the start of text. |
| 1974 | # sot × |
| 1975 | for my $i (0 .. @lb_table - 1) { |
| 1976 | $lb_table[$i][$lb_enums{'EDGE'}] = $lb_actions{'LB_BREAKABLE'}; |
| 1977 | $lb_table[$lb_enums{'EDGE'}][$i] = $lb_actions{'LB_NOBREAK'}; |
| 1978 | } |
| 1979 | |
| 1980 | # LB1 Assign a line breaking class to each code point of the input. |
| 1981 | # Resolve AI, CB, CJ, SA, SG, and XX into other line breaking classes |
| 1982 | # depending on criteria outside the scope of this algorithm. |
| 1983 | # |
| 1984 | # In the absence of such criteria all characters with a specific |
| 1985 | # combination of original class and General_Category property value are |
| 1986 | # resolved as follows: |
| 1987 | # Original Resolved General_Category |
| 1988 | # AI, SG, XX AL Any |
| 1989 | # SA CM Only Mn or Mc |
| 1990 | # SA AL Any except Mn and Mc |
| 1991 | # CJ NS Any |
| 1992 | # |
| 1993 | # This is done in mktables, so we never see any of the remapped-from |
| 1994 | # classes. |
| 1995 | |
| 1996 | output_table_common('LB', \%lb_actions, |
| 1997 | \@lb_table, \@lb_short_enums, \%lb_abbreviations); |
| 1998 | } |
| 1999 | |
| 2000 | sub output_WB_table() { |
| 2001 | |
| 2002 | # Create and output the enums, #defines, and pair table for use in |
| 2003 | # determining Word Breaks, given in http://www.unicode.org/reports/tr29/. |
| 2004 | |
| 2005 | # This uses the same mechanism in the other bounds tables generated by |
| 2006 | # this file. The actions that could override a 0 or 1 are added to those |
| 2007 | # numbers; the actions that clearly don't depend on the underlying rule |
| 2008 | # simply overwrite |
| 2009 | my %wb_actions = ( |
| 2010 | WB_NOBREAK => 0, |
| 2011 | WB_BREAKABLE => 1, |
| 2012 | WB_hs_then_hs => 2, |
| 2013 | WB_Ex_or_FO_or_ZWJ_then_foo => 3, |
| 2014 | WB_DQ_then_HL => 4, |
| 2015 | WB_HL_then_DQ => 6, |
| 2016 | WB_LE_or_HL_then_MB_or_ML_or_SQ => 8, |
| 2017 | WB_MB_or_ML_or_SQ_then_LE_or_HL => 10, |
| 2018 | WB_MB_or_MN_or_SQ_then_NU => 12, |
| 2019 | WB_NU_then_MB_or_MN_or_SQ => 14, |
| 2020 | WB_RI_then_RI => 16, |
| 2021 | ); |
| 2022 | |
| 2023 | # Construct the WB pair table. |
| 2024 | # The table is constructed in reverse order of the rules, to make the |
| 2025 | # lower-numbered, higher priority ones override the later ones, as the |
| 2026 | # algorithm stops at the earliest matching rule |
| 2027 | |
| 2028 | my @wb_table; |
| 2029 | my $table_size = @wb_short_enums; |
| 2030 | |
| 2031 | # Otherwise, break everywhere (including around ideographs). |
| 2032 | # WB99 Any ÷ Any |
| 2033 | for my $i (0 .. $table_size - 1) { |
| 2034 | for my $j (0 .. $table_size - 1) { |
| 2035 | $wb_table[$i][$j] = $wb_actions{'WB_BREAKABLE'}; |
| 2036 | } |
| 2037 | } |
| 2038 | |
| 2039 | # Do not break within emoji flag sequences. That is, do not break between |
| 2040 | # regional indicator (RI) symbols if there is an odd number of RI |
| 2041 | # characters before the break point. |
| 2042 | # WB16 [^RI] (RI RI)* RI × RI |
| 2043 | # WB15 sot (RI RI)* RI × RI |
| 2044 | $wb_table[$wb_enums{'Regional_Indicator'}] |
| 2045 | [$wb_enums{'Regional_Indicator'}] = $wb_actions{'WB_RI_then_RI'}; |
| 2046 | |
| 2047 | # Do not break within emoji modifier sequences. |
| 2048 | # WB14 ( E_Base | EBG ) × E_Modifier |
| 2049 | $wb_table[$wb_enums{'E_Base'}][$wb_enums{'E_Modifier'}] |
| 2050 | = $wb_actions{'WB_NOBREAK'}; |
| 2051 | $wb_table[$wb_enums{'E_Base_GAZ'}][$wb_enums{'E_Modifier'}] |
| 2052 | = $wb_actions{'WB_NOBREAK'}; |
| 2053 | |
| 2054 | # Do not break from extenders. |
| 2055 | # WB13b ExtendNumLet × (ALetter | Hebrew_Letter | Numeric | Katakana) |
| 2056 | $wb_table[$wb_enums{'ExtendNumLet'}][$wb_enums{'ALetter'}] |
| 2057 | = $wb_actions{'WB_NOBREAK'}; |
| 2058 | $wb_table[$wb_enums{'ExtendNumLet'}][$wb_enums{'XPG_LE'}] |
| 2059 | = $wb_actions{'WB_NOBREAK'}; |
| 2060 | $wb_table[$wb_enums{'ExtendNumLet'}][$wb_enums{'Hebrew_Letter'}] |
| 2061 | = $wb_actions{'WB_NOBREAK'}; |
| 2062 | $wb_table[$wb_enums{'ExtendNumLet'}][$wb_enums{'Numeric'}] |
| 2063 | = $wb_actions{'WB_NOBREAK'}; |
| 2064 | $wb_table[$wb_enums{'ExtendNumLet'}][$wb_enums{'Katakana'}] |
| 2065 | = $wb_actions{'WB_NOBREAK'}; |
| 2066 | |
| 2067 | # WB13a (ALetter | Hebrew_Letter | Numeric | Katakana | ExtendNumLet) |
| 2068 | # × ExtendNumLet |
| 2069 | $wb_table[$wb_enums{'ALetter'}][$wb_enums{'ExtendNumLet'}] |
| 2070 | = $wb_actions{'WB_NOBREAK'}; |
| 2071 | $wb_table[$wb_enums{'XPG_LE'}][$wb_enums{'ExtendNumLet'}] |
| 2072 | = $wb_actions{'WB_NOBREAK'}; |
| 2073 | $wb_table[$wb_enums{'Hebrew_Letter'}][$wb_enums{'ExtendNumLet'}] |
| 2074 | = $wb_actions{'WB_NOBREAK'}; |
| 2075 | $wb_table[$wb_enums{'Numeric'}][$wb_enums{'ExtendNumLet'}] |
| 2076 | = $wb_actions{'WB_NOBREAK'}; |
| 2077 | $wb_table[$wb_enums{'Katakana'}][$wb_enums{'ExtendNumLet'}] |
| 2078 | = $wb_actions{'WB_NOBREAK'}; |
| 2079 | $wb_table[$wb_enums{'ExtendNumLet'}][$wb_enums{'ExtendNumLet'}] |
| 2080 | = $wb_actions{'WB_NOBREAK'}; |
| 2081 | |
| 2082 | # Do not break between Katakana. |
| 2083 | # WB13 Katakana × Katakana |
| 2084 | $wb_table[$wb_enums{'Katakana'}][$wb_enums{'Katakana'}] |
| 2085 | = $wb_actions{'WB_NOBREAK'}; |
| 2086 | |
| 2087 | # Do not break within sequences, such as “3.2” or “3,456.789”. |
| 2088 | # WB12 Numeric × (MidNum | MidNumLet | Single_Quote) Numeric |
| 2089 | $wb_table[$wb_enums{'Numeric'}][$wb_enums{'MidNumLet'}] |
| 2090 | += $wb_actions{'WB_NU_then_MB_or_MN_or_SQ'}; |
| 2091 | $wb_table[$wb_enums{'Numeric'}][$wb_enums{'MidNum'}] |
| 2092 | += $wb_actions{'WB_NU_then_MB_or_MN_or_SQ'}; |
| 2093 | $wb_table[$wb_enums{'Numeric'}][$wb_enums{'Single_Quote'}] |
| 2094 | += $wb_actions{'WB_NU_then_MB_or_MN_or_SQ'}; |
| 2095 | |
| 2096 | # WB11 Numeric (MidNum | (MidNumLet | Single_Quote)) × Numeric |
| 2097 | $wb_table[$wb_enums{'MidNumLet'}][$wb_enums{'Numeric'}] |
| 2098 | += $wb_actions{'WB_MB_or_MN_or_SQ_then_NU'}; |
| 2099 | $wb_table[$wb_enums{'MidNum'}][$wb_enums{'Numeric'}] |
| 2100 | += $wb_actions{'WB_MB_or_MN_or_SQ_then_NU'}; |
| 2101 | $wb_table[$wb_enums{'Single_Quote'}][$wb_enums{'Numeric'}] |
| 2102 | += $wb_actions{'WB_MB_or_MN_or_SQ_then_NU'}; |
| 2103 | |
| 2104 | # Do not break within sequences of digits, or digits adjacent to letters |
| 2105 | # (“3a”, or “A3”). |
| 2106 | # WB10 Numeric × (ALetter | Hebrew_Letter) |
| 2107 | $wb_table[$wb_enums{'Numeric'}][$wb_enums{'ALetter'}] |
| 2108 | = $wb_actions{'WB_NOBREAK'}; |
| 2109 | $wb_table[$wb_enums{'Numeric'}][$wb_enums{'XPG_LE'}] |
| 2110 | = $wb_actions{'WB_NOBREAK'}; |
| 2111 | $wb_table[$wb_enums{'Numeric'}][$wb_enums{'Hebrew_Letter'}] |
| 2112 | = $wb_actions{'WB_NOBREAK'}; |
| 2113 | |
| 2114 | # WB9 (ALetter | Hebrew_Letter) × Numeric |
| 2115 | $wb_table[$wb_enums{'ALetter'}][$wb_enums{'Numeric'}] |
| 2116 | = $wb_actions{'WB_NOBREAK'}; |
| 2117 | $wb_table[$wb_enums{'XPG_LE'}][$wb_enums{'Numeric'}] |
| 2118 | = $wb_actions{'WB_NOBREAK'}; |
| 2119 | $wb_table[$wb_enums{'Hebrew_Letter'}][$wb_enums{'Numeric'}] |
| 2120 | = $wb_actions{'WB_NOBREAK'}; |
| 2121 | |
| 2122 | # WB8 Numeric × Numeric |
| 2123 | $wb_table[$wb_enums{'Numeric'}][$wb_enums{'Numeric'}] |
| 2124 | = $wb_actions{'WB_NOBREAK'}; |
| 2125 | |
| 2126 | # Do not break letters across certain punctuation. |
| 2127 | # WB7c Hebrew_Letter Double_Quote × Hebrew_Letter |
| 2128 | $wb_table[$wb_enums{'Double_Quote'}][$wb_enums{'Hebrew_Letter'}] |
| 2129 | += $wb_actions{'WB_DQ_then_HL'}; |
| 2130 | |
| 2131 | # WB7b Hebrew_Letter × Double_Quote Hebrew_Letter |
| 2132 | $wb_table[$wb_enums{'Hebrew_Letter'}][$wb_enums{'Double_Quote'}] |
| 2133 | += $wb_actions{'WB_HL_then_DQ'}; |
| 2134 | |
| 2135 | # WB7a Hebrew_Letter × Single_Quote |
| 2136 | $wb_table[$wb_enums{'Hebrew_Letter'}][$wb_enums{'Single_Quote'}] |
| 2137 | = $wb_actions{'WB_NOBREAK'}; |
| 2138 | |
| 2139 | # WB7 (ALetter | Hebrew_Letter) (MidLetter | MidNumLet | Single_Quote) |
| 2140 | # × (ALetter | Hebrew_Letter) |
| 2141 | $wb_table[$wb_enums{'MidNumLet'}][$wb_enums{'ALetter'}] |
| 2142 | += $wb_actions{'WB_MB_or_ML_or_SQ_then_LE_or_HL'}; |
| 2143 | $wb_table[$wb_enums{'MidNumLet'}][$wb_enums{'XPG_LE'}] |
| 2144 | += $wb_actions{'WB_MB_or_ML_or_SQ_then_LE_or_HL'}; |
| 2145 | $wb_table[$wb_enums{'MidNumLet'}][$wb_enums{'Hebrew_Letter'}] |
| 2146 | += $wb_actions{'WB_MB_or_ML_or_SQ_then_LE_or_HL'}; |
| 2147 | $wb_table[$wb_enums{'MidLetter'}][$wb_enums{'ALetter'}] |
| 2148 | += $wb_actions{'WB_MB_or_ML_or_SQ_then_LE_or_HL'}; |
| 2149 | $wb_table[$wb_enums{'MidLetter'}][$wb_enums{'XPG_LE'}] |
| 2150 | += $wb_actions{'WB_MB_or_ML_or_SQ_then_LE_or_HL'}; |
| 2151 | $wb_table[$wb_enums{'MidLetter'}][$wb_enums{'Hebrew_Letter'}] |
| 2152 | += $wb_actions{'WB_MB_or_ML_or_SQ_then_LE_or_HL'}; |
| 2153 | $wb_table[$wb_enums{'Single_Quote'}][$wb_enums{'ALetter'}] |
| 2154 | += $wb_actions{'WB_MB_or_ML_or_SQ_then_LE_or_HL'}; |
| 2155 | $wb_table[$wb_enums{'Single_Quote'}][$wb_enums{'XPG_LE'}] |
| 2156 | += $wb_actions{'WB_MB_or_ML_or_SQ_then_LE_or_HL'}; |
| 2157 | $wb_table[$wb_enums{'Single_Quote'}][$wb_enums{'Hebrew_Letter'}] |
| 2158 | += $wb_actions{'WB_MB_or_ML_or_SQ_then_LE_or_HL'}; |
| 2159 | |
| 2160 | # WB6 (ALetter | Hebrew_Letter) × (MidLetter | MidNumLet |
| 2161 | # | Single_Quote) (ALetter | Hebrew_Letter) |
| 2162 | $wb_table[$wb_enums{'ALetter'}][$wb_enums{'MidNumLet'}] |
| 2163 | += $wb_actions{'WB_LE_or_HL_then_MB_or_ML_or_SQ'}; |
| 2164 | $wb_table[$wb_enums{'XPG_LE'}][$wb_enums{'MidNumLet'}] |
| 2165 | += $wb_actions{'WB_LE_or_HL_then_MB_or_ML_or_SQ'}; |
| 2166 | $wb_table[$wb_enums{'Hebrew_Letter'}][$wb_enums{'MidNumLet'}] |
| 2167 | += $wb_actions{'WB_LE_or_HL_then_MB_or_ML_or_SQ'}; |
| 2168 | $wb_table[$wb_enums{'ALetter'}][$wb_enums{'MidLetter'}] |
| 2169 | += $wb_actions{'WB_LE_or_HL_then_MB_or_ML_or_SQ'}; |
| 2170 | $wb_table[$wb_enums{'XPG_LE'}][$wb_enums{'MidLetter'}] |
| 2171 | += $wb_actions{'WB_LE_or_HL_then_MB_or_ML_or_SQ'}; |
| 2172 | $wb_table[$wb_enums{'Hebrew_Letter'}][$wb_enums{'MidLetter'}] |
| 2173 | += $wb_actions{'WB_LE_or_HL_then_MB_or_ML_or_SQ'}; |
| 2174 | $wb_table[$wb_enums{'ALetter'}][$wb_enums{'Single_Quote'}] |
| 2175 | += $wb_actions{'WB_LE_or_HL_then_MB_or_ML_or_SQ'}; |
| 2176 | $wb_table[$wb_enums{'XPG_LE'}][$wb_enums{'Single_Quote'}] |
| 2177 | += $wb_actions{'WB_LE_or_HL_then_MB_or_ML_or_SQ'}; |
| 2178 | $wb_table[$wb_enums{'Hebrew_Letter'}][$wb_enums{'Single_Quote'}] |
| 2179 | += $wb_actions{'WB_LE_or_HL_then_MB_or_ML_or_SQ'}; |
| 2180 | |
| 2181 | # Do not break between most letters. |
| 2182 | # WB5 (ALetter | Hebrew_Letter) × (ALetter | Hebrew_Letter) |
| 2183 | $wb_table[$wb_enums{'ALetter'}][$wb_enums{'ALetter'}] |
| 2184 | = $wb_actions{'WB_NOBREAK'}; |
| 2185 | $wb_table[$wb_enums{'XPG_LE'}][$wb_enums{'ALetter'}] |
| 2186 | = $wb_actions{'WB_NOBREAK'}; |
| 2187 | $wb_table[$wb_enums{'ALetter'}][$wb_enums{'Hebrew_Letter'}] |
| 2188 | = $wb_actions{'WB_NOBREAK'}; |
| 2189 | $wb_table[$wb_enums{'XPG_LE'}][$wb_enums{'Hebrew_Letter'}] |
| 2190 | = $wb_actions{'WB_NOBREAK'}; |
| 2191 | $wb_table[$wb_enums{'Hebrew_Letter'}][$wb_enums{'ALetter'}] |
| 2192 | = $wb_actions{'WB_NOBREAK'}; |
| 2193 | $wb_table[$wb_enums{'Hebrew_Letter'}][$wb_enums{'XPG_LE'}] |
| 2194 | = $wb_actions{'WB_NOBREAK'}; |
| 2195 | $wb_table[$wb_enums{'Hebrew_Letter'}][$wb_enums{'Hebrew_Letter'}] |
| 2196 | = $wb_actions{'WB_NOBREAK'}; |
| 2197 | $wb_table[$wb_enums{'XPG_LE'}][$wb_enums{'XPG_LE'}] |
| 2198 | = $wb_actions{'WB_NOBREAK'}; |
| 2199 | |
| 2200 | # Ignore Format and Extend characters, except after sot, CR, LF, and |
| 2201 | # Newline. This also has the effect of: Any × (Format | Extend | ZWJ) |
| 2202 | # WB4 X (Extend | Format | ZWJ)* → X |
| 2203 | for my $i (0 .. @wb_table - 1) { |
| 2204 | $wb_table[$wb_enums{'Extend'}][$i] |
| 2205 | = $wb_actions{'WB_Ex_or_FO_or_ZWJ_then_foo'}; |
| 2206 | $wb_table[$wb_enums{'Format'}][$i] |
| 2207 | = $wb_actions{'WB_Ex_or_FO_or_ZWJ_then_foo'}; |
| 2208 | $wb_table[$wb_enums{'ZWJ'}][$i] |
| 2209 | = $wb_actions{'WB_Ex_or_FO_or_ZWJ_then_foo'}; |
| 2210 | } |
| 2211 | for my $i (0 .. @wb_table - 1) { |
| 2212 | $wb_table[$i][$wb_enums{'Extend'}] = $wb_actions{'WB_NOBREAK'}; |
| 2213 | $wb_table[$i][$wb_enums{'Format'}] = $wb_actions{'WB_NOBREAK'}; |
| 2214 | $wb_table[$i][$wb_enums{'ZWJ'}] = $wb_actions{'WB_NOBREAK'}; |
| 2215 | } |
| 2216 | |
| 2217 | # Implied is that these attach to the character before them, except for |
| 2218 | # the characters that mark the end of a region of text. The rules below |
| 2219 | # override the ones set up here, for all the characters that need |
| 2220 | # overriding. |
| 2221 | for my $i (0 .. @wb_table - 1) { |
| 2222 | $wb_table[$i][$wb_enums{'Extend'}] = $wb_actions{'WB_NOBREAK'}; |
| 2223 | $wb_table[$i][$wb_enums{'Format'}] = $wb_actions{'WB_NOBREAK'}; |
| 2224 | } |
| 2225 | |
| 2226 | # Keep horizontal whitespace together |
| 2227 | # Use perl's tailoring instead |
| 2228 | # WB3d WSegSpace × WSegSpace |
| 2229 | #$wb_table[$wb_enums{'WSegSpace'}][$wb_enums{'WSegSpace'}] |
| 2230 | # = $wb_actions{'WB_NOBREAK'}; |
| 2231 | |
| 2232 | # Do not break within emoji zwj sequences. |
| 2233 | # WB3c ZWJ × ( Glue_After_Zwj | EBG ) |
| 2234 | $wb_table[$wb_enums{'ZWJ'}][$wb_enums{'Glue_After_Zwj'}] |
| 2235 | = $wb_actions{'WB_NOBREAK'}; |
| 2236 | $wb_table[$wb_enums{'ZWJ'}][$wb_enums{'E_Base_GAZ'}] |
| 2237 | = $wb_actions{'WB_NOBREAK'}; |
| 2238 | $wb_table[$wb_enums{'ZWJ'}][$wb_enums{'XPG_XX'}] |
| 2239 | = $wb_actions{'WB_NOBREAK'}; |
| 2240 | $wb_table[$wb_enums{'ZWJ'}][$wb_enums{'XPG_LE'}] |
| 2241 | = $wb_actions{'WB_NOBREAK'}; |
| 2242 | |
| 2243 | # Break before and after newlines |
| 2244 | # WB3b ÷ (Newline | CR | LF) |
| 2245 | # WB3a (Newline | CR | LF) ÷ |
| 2246 | # et. al. |
| 2247 | for my $i ('CR', 'LF', 'Newline', 'Perl_Tailored_HSpace') { |
| 2248 | for my $j (0 .. @wb_table - 1) { |
| 2249 | $wb_table[$j][$wb_enums{$i}] = $wb_actions{'WB_BREAKABLE'}; |
| 2250 | $wb_table[$wb_enums{$i}][$j] = $wb_actions{'WB_BREAKABLE'}; |
| 2251 | } |
| 2252 | } |
| 2253 | |
| 2254 | # But do not break within white space. |
| 2255 | # WB3 CR × LF |
| 2256 | # et.al. |
| 2257 | for my $i ('CR', 'LF', 'Newline', 'Perl_Tailored_HSpace') { |
| 2258 | for my $j ('CR', 'LF', 'Newline', 'Perl_Tailored_HSpace') { |
| 2259 | $wb_table[$wb_enums{$i}][$wb_enums{$j}] = $wb_actions{'WB_NOBREAK'}; |
| 2260 | } |
| 2261 | } |
| 2262 | |
| 2263 | # And do not break horizontal space followed by Extend or Format or ZWJ |
| 2264 | $wb_table[$wb_enums{'Perl_Tailored_HSpace'}][$wb_enums{'Extend'}] |
| 2265 | = $wb_actions{'WB_NOBREAK'}; |
| 2266 | $wb_table[$wb_enums{'Perl_Tailored_HSpace'}][$wb_enums{'Format'}] |
| 2267 | = $wb_actions{'WB_NOBREAK'}; |
| 2268 | $wb_table[$wb_enums{'Perl_Tailored_HSpace'}][$wb_enums{'ZWJ'}] |
| 2269 | = $wb_actions{'WB_NOBREAK'}; |
| 2270 | $wb_table[$wb_enums{'Perl_Tailored_HSpace'}] |
| 2271 | [$wb_enums{'Perl_Tailored_HSpace'}] |
| 2272 | = $wb_actions{'WB_hs_then_hs'}; |
| 2273 | |
| 2274 | # Break at the start and end of text, unless the text is empty |
| 2275 | # WB2 Any ÷ eot |
| 2276 | # WB1 sot ÷ Any |
| 2277 | for my $i (0 .. @wb_table - 1) { |
| 2278 | $wb_table[$i][$wb_enums{'EDGE'}] = $wb_actions{'WB_BREAKABLE'}; |
| 2279 | $wb_table[$wb_enums{'EDGE'}][$i] = $wb_actions{'WB_BREAKABLE'}; |
| 2280 | } |
| 2281 | $wb_table[$wb_enums{'EDGE'}][$wb_enums{'EDGE'}] = 0; |
| 2282 | |
| 2283 | output_table_common('WB', \%wb_actions, |
| 2284 | \@wb_table, \@wb_short_enums, \%wb_abbreviations); |
| 2285 | } |
| 2286 | |
| 2287 | sub sanitize_name ($) { |
| 2288 | # Change the non-word characters in the input string to standardized word |
| 2289 | # equivalents |
| 2290 | # |
| 2291 | my $sanitized = shift; |
| 2292 | $sanitized =~ s/=/__/; |
| 2293 | $sanitized =~ s/&/_AMP_/; |
| 2294 | $sanitized =~ s/\./_DOT_/; |
| 2295 | $sanitized =~ s/-/_MINUS_/; |
| 2296 | $sanitized =~ s!/!_SLASH_!; |
| 2297 | |
| 2298 | return $sanitized; |
| 2299 | } |
| 2300 | |
| 2301 | switch_pound_if ('ALL', 'PERL_IN_REGCOMP_C'); |
| 2302 | |
| 2303 | output_invlist("Latin1", [ 0, 256 ]); |
| 2304 | output_invlist("AboveLatin1", [ 256 ]); |
| 2305 | |
| 2306 | end_file_pound_if; |
| 2307 | |
| 2308 | # We construct lists for all the POSIX and backslash sequence character |
| 2309 | # classes in two forms: |
| 2310 | # 1) ones which match only in the ASCII range |
| 2311 | # 2) ones which match either in the Latin1 range, or the entire Unicode range |
| 2312 | # |
| 2313 | # These get compiled in, and hence affect the memory footprint of every Perl |
| 2314 | # program, even those not using Unicode. To minimize the size, currently |
| 2315 | # the Latin1 version is generated for the beyond ASCII range except for those |
| 2316 | # lists that are quite small for the entire range, such as for \s, which is 22 |
| 2317 | # UVs long plus 4 UVs (currently) for the header. |
| 2318 | # |
| 2319 | # To save even more memory, the ASCII versions could be derived from the |
| 2320 | # larger ones at runtime, saving some memory (minus the expense of the machine |
| 2321 | # instructions to do so), but these are all small anyway, so their total is |
| 2322 | # about 100 UVs. |
| 2323 | # |
| 2324 | # In the list of properties below that get generated, the L1 prefix is a fake |
| 2325 | # property that means just the Latin1 range of the full property (whose name |
| 2326 | # has an X prefix instead of L1). |
| 2327 | # |
| 2328 | # An initial & means to use the subroutine from this file instead of an |
| 2329 | # official inversion list. |
| 2330 | |
| 2331 | # Below is the list of property names to generate. '&' means to use the |
| 2332 | # subroutine to generate the inversion list instead of the generic code |
| 2333 | # below. Some properties have a comma-separated list after the name, |
| 2334 | # These are extra enums to add to those found in the Unicode tables. |
| 2335 | no warnings 'qw'; |
| 2336 | # Ignore non-alpha in sort |
| 2337 | my @props; |
| 2338 | push @props, sort { prop_name_for_cmp($a) cmp prop_name_for_cmp($b) } qw( |
| 2339 | &UpperLatin1 |
| 2340 | _Perl_GCB,EDGE,E_Base,E_Base_GAZ,E_Modifier,Glue_After_Zwj,LV,Prepend,Regional_Indicator,SpacingMark,ZWJ,XPG_XX |
| 2341 | _Perl_LB,EDGE,Close_Parenthesis,Hebrew_Letter,Next_Line,Regional_Indicator,ZWJ,Contingent_Break,E_Base,E_Modifier,H2,H3,JL,JT,JV,Word_Joiner |
| 2342 | _Perl_SB,EDGE,SContinue,CR,Extend,LF |
| 2343 | _Perl_WB,Perl_Tailored_HSpace,EDGE,UNKNOWN,CR,Double_Quote,E_Base,E_Base_GAZ,E_Modifier,Extend,Glue_After_Zwj,Hebrew_Letter,LF,MidNumLet,Newline,Regional_Indicator,Single_Quote,ZWJ,XPG_XX,XPG_LE |
| 2344 | _Perl_SCX,Latin,Inherited,Unknown,Kore,Jpan,Hanb,INVALID |
| 2345 | Lowercase_Mapping |
| 2346 | Titlecase_Mapping |
| 2347 | Uppercase_Mapping |
| 2348 | Simple_Case_Folding |
| 2349 | Case_Folding |
| 2350 | &_Perl_IVCF |
| 2351 | &_Perl_CCC_non0_non230 |
| 2352 | ); |
| 2353 | # NOTE that the convention is that extra enum values come |
| 2354 | # after the property name, separated by commas, with the enums |
| 2355 | # that aren't ever defined by Unicode coming last, at least 4 |
| 2356 | # all-uppercase characters. The others are enum names that |
| 2357 | # are needed by perl, but aren't in all Unicode releases. |
| 2358 | |
| 2359 | my @bin_props; |
| 2360 | my @perl_prop_synonyms; |
| 2361 | my %enums; |
| 2362 | my @deprecated_messages = ""; # Element [0] is a placeholder |
| 2363 | my %deprecated_tags; |
| 2364 | |
| 2365 | my $float_e_format = qr/ ^ -? \d \. \d+ e [-+] \d+ $ /x; |
| 2366 | |
| 2367 | # Create another hash that maps floating point x.yyEzz representation to what |
| 2368 | # %stricter_to_file_of does for the equivalent rational. A typical entry in |
| 2369 | # the latter hash is |
| 2370 | # |
| 2371 | # 'nv=1/2' => 'Nv/1_2', |
| 2372 | # |
| 2373 | # From that, this loop creates an entry |
| 2374 | # |
| 2375 | # 'nv=5.00e-01' => 'Nv/1_2', |
| 2376 | # |
| 2377 | # %stricter_to_file_of contains far more than just the rationals. Instead we |
| 2378 | # use %utf8::nv_floating_to_rational which should have an entry for each |
| 2379 | # nv in the former hash. |
| 2380 | my %floating_to_file_of; |
| 2381 | foreach my $key (keys %utf8::nv_floating_to_rational) { |
| 2382 | my $value = $utf8::nv_floating_to_rational{$key}; |
| 2383 | $floating_to_file_of{$key} = $utf8::stricter_to_file_of{"nv=$value"}; |
| 2384 | } |
| 2385 | |
| 2386 | # Collect all the binary properties from data in lib/unicore |
| 2387 | # Sort so that complements come after the main table, and the shortest |
| 2388 | # names first, finally alphabetically. Also, sort together the tables we want |
| 2389 | # to be kept together, and prefer those with 'posix' in their names, which is |
| 2390 | # what the C code is expecting their names to be. |
| 2391 | foreach my $property (sort |
| 2392 | { exists $keep_together{lc $b} <=> exists $keep_together{lc $a} |
| 2393 | or $b =~ /posix/i <=> $a =~ /posix/i |
| 2394 | or $b =~ /perl/i <=> $a =~ /perl/i |
| 2395 | or $a =~ $float_e_format <=> $b =~ $float_e_format |
| 2396 | or $a =~ /!/ <=> $b =~ /!/ |
| 2397 | or length $a <=> length $b |
| 2398 | or $a cmp $b |
| 2399 | } keys %utf8::loose_to_file_of, |
| 2400 | keys %utf8::stricter_to_file_of, |
| 2401 | keys %floating_to_file_of |
| 2402 | ) { |
| 2403 | |
| 2404 | # These two hashes map properties to values that can be considered to |
| 2405 | # be checksums. If two properties have the same checksum, they have |
| 2406 | # identical entries. Otherwise they differ in some way. |
| 2407 | my $tag = $utf8::loose_to_file_of{$property}; |
| 2408 | $tag = $utf8::stricter_to_file_of{$property} unless defined $tag; |
| 2409 | $tag = $floating_to_file_of{$property} unless defined $tag; |
| 2410 | |
| 2411 | # The tag may contain an '!' meaning it is identical to the one formed |
| 2412 | # by removing the !, except that it is inverted. |
| 2413 | my $inverted = $tag =~ s/!//; |
| 2414 | |
| 2415 | # This hash is lacking the property name |
| 2416 | $property = "nv=$property" if $property =~ $float_e_format; |
| 2417 | |
| 2418 | # The list of 'prop=value' entries that this single entry expands to |
| 2419 | my @this_entries; |
| 2420 | |
| 2421 | # Split 'property=value' on the equals sign, with $lhs being the whole |
| 2422 | # thing if there is no '=' |
| 2423 | my ($lhs, $rhs) = $property =~ / ( [^=]* ) ( =? .*) /x; |
| 2424 | |
| 2425 | # $lhs then becomes the property name. See if there are any synonyms |
| 2426 | # for this property. |
| 2427 | if (exists $prop_name_aliases{$lhs}) { |
| 2428 | |
| 2429 | # If so, do the combinatorics so that a new entry is added for |
| 2430 | # each legal property combined with the property value (which is |
| 2431 | # $rhs) |
| 2432 | foreach my $alias (@{$prop_name_aliases{$lhs}}) { |
| 2433 | |
| 2434 | # But, there are some ambiguities, like 'script' is a synonym |
| 2435 | # for 'sc', and 'sc' can stand alone, meaning something |
| 2436 | # entirely different than 'script'. 'script' cannot stand |
| 2437 | # alone. Don't add if the potential new lhs is in the hash of |
| 2438 | # stand-alone properties. |
| 2439 | no warnings 'once'; |
| 2440 | next if $rhs eq "" && grep { $alias eq $_ } |
| 2441 | keys %utf8::loose_property_to_file_of; |
| 2442 | |
| 2443 | my $new_entry = $alias . $rhs; |
| 2444 | push @this_entries, $new_entry; |
| 2445 | } |
| 2446 | } |
| 2447 | |
| 2448 | # Above, we added the synonyms for the base entry we're now |
| 2449 | # processing. But we haven't dealt with it yet. If we already have a |
| 2450 | # property with the identical characteristics, this becomes just a |
| 2451 | # synonym for it. |
| 2452 | if (exists $enums{$tag}) { |
| 2453 | push @this_entries, $property; |
| 2454 | } |
| 2455 | else { # Otherwise, create a new entry. |
| 2456 | |
| 2457 | # Add to the list of properties to generate inversion lists for. |
| 2458 | push @bin_props, uc $property; |
| 2459 | |
| 2460 | # Create a rule for the parser |
| 2461 | if (! exists $keywords{$property}) { |
| 2462 | $keywords{$property} = token_name($property); |
| 2463 | } |
| 2464 | |
| 2465 | # And create an enum for it. |
| 2466 | $enums{$tag} = $table_name_prefix . uc sanitize_name($property); |
| 2467 | |
| 2468 | $perl_tags{$tag} = 1 if exists $keep_together{lc $property}; |
| 2469 | |
| 2470 | # Some properties are deprecated. This hash tells us so, and the |
| 2471 | # warning message to raise if they are used. |
| 2472 | if (exists $utf8::why_deprecated{$tag}) { |
| 2473 | $deprecated_tags{$enums{$tag}} = scalar @deprecated_messages; |
| 2474 | push @deprecated_messages, $utf8::why_deprecated{$tag}; |
| 2475 | } |
| 2476 | |
| 2477 | # Our sort above should have made sure that we see the |
| 2478 | # non-inverted version first, but this makes sure. |
| 2479 | warn "$property is inverted!!!" if $inverted; |
| 2480 | } |
| 2481 | |
| 2482 | # Everything else is #defined to be the base enum, inversion is |
| 2483 | # indicated by negating the value. |
| 2484 | my $defined_to = ""; |
| 2485 | $defined_to .= "-" if $inverted; |
| 2486 | $defined_to .= $enums{$tag}; |
| 2487 | |
| 2488 | # Go through the entries that evaluate to this. |
| 2489 | @this_entries = uniques @this_entries; |
| 2490 | foreach my $define (@this_entries) { |
| 2491 | |
| 2492 | # There is a rule for the parser for each. |
| 2493 | $keywords{$define} = $defined_to; |
| 2494 | |
| 2495 | # And a #define for all simple names equivalent to a perl property, |
| 2496 | # except those that begin with 'is' or 'in'; |
| 2497 | if (exists $perl_tags{$tag} && $property !~ / ^ i[ns] | = /x) { |
| 2498 | push @perl_prop_synonyms, "#define " |
| 2499 | . $table_name_prefix |
| 2500 | . uc(sanitize_name($define)) |
| 2501 | . " $defined_to"; |
| 2502 | } |
| 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | @bin_props = sort { exists $keep_together{lc $b} <=> exists $keep_together{lc $a} |
| 2507 | or $a cmp $b |
| 2508 | } @bin_props; |
| 2509 | @perl_prop_synonyms = sort(uniques(@perl_prop_synonyms)); |
| 2510 | push @props, @bin_props; |
| 2511 | |
| 2512 | foreach my $prop (@props) { |
| 2513 | |
| 2514 | # For the Latin1 properties, we change to use the eXtended version of the |
| 2515 | # base property, then go through the result and get rid of everything not |
| 2516 | # in Latin1 (above 255). Actually, we retain the element for the range |
| 2517 | # that crosses the 255/256 boundary if it is one that matches the |
| 2518 | # property. For example, in the Word property, there is a range of code |
| 2519 | # points that start at U+00F8 and goes through U+02C1. Instead of |
| 2520 | # artificially cutting that off at 256 because 256 is the first code point |
| 2521 | # above Latin1, we let the range go to its natural ending. That gives us |
| 2522 | # extra information with no added space taken. But if the range that |
| 2523 | # crosses the boundary is one that doesn't match the property, we don't |
| 2524 | # start a new range above 255, as that could be construed as going to |
| 2525 | # infinity. For example, the Upper property doesn't include the character |
| 2526 | # at 255, but does include the one at 256. We don't include the 256 one. |
| 2527 | my $prop_name = $prop; |
| 2528 | my $is_local_sub = $prop_name =~ s/^&//; |
| 2529 | my $extra_enums = ""; |
| 2530 | $extra_enums = $1 if $prop_name =~ s/, ( .* ) //x; |
| 2531 | my $lookup_prop = $prop_name; |
| 2532 | $prop_name = sanitize_name($prop_name); |
| 2533 | $prop_name = $table_name_prefix . $prop_name if grep { lc $lookup_prop eq lc $_ } @bin_props; |
| 2534 | my $l1_only = ($lookup_prop =~ s/^L1Posix/XPosix/ |
| 2535 | or $lookup_prop =~ s/^L1//); |
| 2536 | my $nonl1_only = 0; |
| 2537 | $nonl1_only = $lookup_prop =~ s/^NonL1// unless $l1_only; |
| 2538 | ($lookup_prop, my $has_suffixes) = $lookup_prop =~ / (.*) ( , .* )? /x; |
| 2539 | |
| 2540 | for my $charset (get_supported_code_pages()) { |
| 2541 | @a2n = @{get_a2n($charset)}; |
| 2542 | |
| 2543 | my @invlist; |
| 2544 | my @invmap; |
| 2545 | my $map_format; |
| 2546 | my $map_default; |
| 2547 | my $maps_to_code_point; |
| 2548 | my $to_adjust; |
| 2549 | my $same_in_all_code_pages; |
| 2550 | if ($is_local_sub) { |
| 2551 | my @return = eval $lookup_prop; |
| 2552 | die $@ if $@; |
| 2553 | my $invlist_ref = shift @return; |
| 2554 | @invlist = @$invlist_ref; |
| 2555 | if (@return) { # If has other values returned , must be an |
| 2556 | # inversion map |
| 2557 | my $invmap_ref = shift @return; |
| 2558 | @invmap = @$invmap_ref; |
| 2559 | $map_format = shift @return; |
| 2560 | $map_default = shift @return; |
| 2561 | } |
| 2562 | } |
| 2563 | else { |
| 2564 | @invlist = prop_invlist($lookup_prop, '_perl_core_internal_ok'); |
| 2565 | if (! @invlist) { |
| 2566 | |
| 2567 | # If couldn't find a non-empty inversion list, see if it is |
| 2568 | # instead an inversion map |
| 2569 | my ($list_ref, $map_ref, $format, $default) |
| 2570 | = prop_invmap($lookup_prop, '_perl_core_internal_ok'); |
| 2571 | if (! $list_ref) { |
| 2572 | # An empty return here could mean an unknown property, or |
| 2573 | # merely that the original inversion list is empty. Call |
| 2574 | # in scalar context to differentiate |
| 2575 | my $count = prop_invlist($lookup_prop, |
| 2576 | '_perl_core_internal_ok'); |
| 2577 | if (defined $count) { |
| 2578 | # Short-circuit an empty inversion list. |
| 2579 | output_invlist($prop_name, \@invlist, $charset); |
| 2580 | last; |
| 2581 | } |
| 2582 | die "Could not find inversion list for '$lookup_prop'" |
| 2583 | } |
| 2584 | else { |
| 2585 | @invlist = @$list_ref; |
| 2586 | @invmap = @$map_ref; |
| 2587 | $map_format = $format; |
| 2588 | $map_default = $default; |
| 2589 | $maps_to_code_point = $map_format =~ / a ($ | [^r] ) /x; |
| 2590 | $to_adjust = $map_format =~ /a/; |
| 2591 | } |
| 2592 | } |
| 2593 | } |
| 2594 | |
| 2595 | # Re-order the Unicode code points to native ones for this platform. |
| 2596 | # This is only needed for code points below 256, because native code |
| 2597 | # points are only in that range. For inversion maps of properties |
| 2598 | # where the mappings are adjusted (format =~ /a/), this reordering |
| 2599 | # could mess up the adjustment pattern that was in the input, so that |
| 2600 | # has to be dealt with. |
| 2601 | # |
| 2602 | # And inversion maps that map to code points need to eventually have |
| 2603 | # all those code points remapped to native, and it's better to do that |
| 2604 | # here, going through the whole list not just those below 256. This |
| 2605 | # is because some inversion maps have adjustments (format =~ /a/) |
| 2606 | # which may be affected by the reordering. This code needs to be done |
| 2607 | # both for when we are translating the inversion lists for < 256, and |
| 2608 | # for the inversion maps for everything. By doing both in this loop, |
| 2609 | # we can share that code. |
| 2610 | # |
| 2611 | # So, we go through everything for an inversion map to code points; |
| 2612 | # otherwise, we can skip any remapping at all if we are going to |
| 2613 | # output only the above-Latin1 values, or if the range spans the whole |
| 2614 | # of 0..256, as the remap will also include all of 0..256 (256 not |
| 2615 | # 255 because a re-ordering could cause 256 to need to be in the same |
| 2616 | # range as 255.) |
| 2617 | if ( (@invmap && $maps_to_code_point) |
| 2618 | || ( @invlist |
| 2619 | && $invlist[0] < 256 |
| 2620 | && ( $invlist[0] != 0 |
| 2621 | || (scalar @invlist != 1 && $invlist[1] < 256)))) |
| 2622 | { |
| 2623 | $same_in_all_code_pages = 0; |
| 2624 | if (! @invmap) { # Straight inversion list |
| 2625 | # Look at all the ranges that start before 257. |
| 2626 | my @latin1_list; |
| 2627 | while (@invlist) { |
| 2628 | last if $invlist[0] > 256; |
| 2629 | my $upper = @invlist > 1 |
| 2630 | ? $invlist[1] - 1 # In range |
| 2631 | |
| 2632 | # To infinity. You may want to stop much much |
| 2633 | # earlier; going this high may expose perl |
| 2634 | # deficiencies with very large numbers. |
| 2635 | : 256; |
| 2636 | for my $j ($invlist[0] .. $upper) { |
| 2637 | push @latin1_list, a2n($j); |
| 2638 | } |
| 2639 | |
| 2640 | shift @invlist; # Shift off the range that's in the list |
| 2641 | shift @invlist; # Shift off the range not in the list |
| 2642 | } |
| 2643 | |
| 2644 | # Here @invlist contains all the ranges in the original that |
| 2645 | # start at code points above 256, and @latin1_list contains |
| 2646 | # all the native code points for ranges that start with a |
| 2647 | # Unicode code point below 257. We sort the latter and |
| 2648 | # convert it to inversion list format. Then simply prepend it |
| 2649 | # to the list of the higher code points. |
| 2650 | @latin1_list = sort { $a <=> $b } @latin1_list; |
| 2651 | @latin1_list = mk_invlist_from_sorted_cp_list(\@latin1_list); |
| 2652 | unshift @invlist, @latin1_list; |
| 2653 | } |
| 2654 | else { # Is an inversion map |
| 2655 | |
| 2656 | # This is a similar procedure as plain inversion list, but has |
| 2657 | # multiple buckets. A plain inversion list just has two |
| 2658 | # buckets, 1) 'in' the list; and 2) 'not' in the list, and we |
| 2659 | # pretty much can ignore the 2nd bucket, as it is completely |
| 2660 | # defined by the 1st. But here, what we do is create buckets |
| 2661 | # which contain the code points that map to each, translated |
| 2662 | # to native and turned into an inversion list. Thus each |
| 2663 | # bucket is an inversion list of native code points that map |
| 2664 | # to it or don't map to it. We use these to create an |
| 2665 | # inversion map for the whole property. |
| 2666 | |
| 2667 | # As mentioned earlier, we use this procedure to not just |
| 2668 | # remap the inversion list to native values, but also the maps |
| 2669 | # of code points to native ones. In the latter case we have |
| 2670 | # to look at the whole of the inversion map (or at least to |
| 2671 | # above Unicode; as the maps of code points above that should |
| 2672 | # all be to the default). |
| 2673 | my $upper_limit = (! $maps_to_code_point) |
| 2674 | ? 256 |
| 2675 | : (Unicode::UCD::UnicodeVersion() eq '1.1.5') |
| 2676 | ? 0xFFFF |
| 2677 | : 0x10FFFF; |
| 2678 | |
| 2679 | my %mapped_lists; # A hash whose keys are the buckets. |
| 2680 | while (@invlist) { |
| 2681 | last if $invlist[0] > $upper_limit; |
| 2682 | |
| 2683 | # This shouldn't actually happen, as prop_invmap() returns |
| 2684 | # an extra element at the end that is beyond $upper_limit |
| 2685 | die "inversion map (for $prop_name) that extends to infinity is unimplemented" unless @invlist > 1; |
| 2686 | |
| 2687 | my $bucket; |
| 2688 | |
| 2689 | # A hash key can't be a ref (we are only expecting arrays |
| 2690 | # of scalars here), so convert any such to a string that |
| 2691 | # will be converted back later (using a vertical tab as |
| 2692 | # the separator). |
| 2693 | if (ref $invmap[0]) { |
| 2694 | $bucket = join "\cK", map { a2n($_) } @{$invmap[0]}; |
| 2695 | } |
| 2696 | elsif ($maps_to_code_point && $invmap[0] =~ $numeric_re) { |
| 2697 | |
| 2698 | # Do convert to native for maps to single code points. |
| 2699 | # There are some properties that have a few outlier |
| 2700 | # maps that aren't code points, so the above test |
| 2701 | # skips those. |
| 2702 | $bucket = a2n($invmap[0]); |
| 2703 | } else { |
| 2704 | $bucket = $invmap[0]; |
| 2705 | } |
| 2706 | |
| 2707 | # We now have the bucket that all code points in the range |
| 2708 | # map to, though possibly they need to be adjusted. Go |
| 2709 | # through the range and put each translated code point in |
| 2710 | # it into its bucket. |
| 2711 | my $base_map = $invmap[0]; |
| 2712 | for my $j ($invlist[0] .. $invlist[1] - 1) { |
| 2713 | if ($to_adjust |
| 2714 | # The 1st code point doesn't need adjusting |
| 2715 | && $j > $invlist[0] |
| 2716 | |
| 2717 | # Skip any non-numeric maps: these are outliers |
| 2718 | # that aren't code points. |
| 2719 | && $base_map =~ $numeric_re |
| 2720 | |
| 2721 | # 'ne' because the default can be a string |
| 2722 | && $base_map ne $map_default) |
| 2723 | { |
| 2724 | # We adjust, by incrementing each the bucket and |
| 2725 | # the map. For code point maps, translate to |
| 2726 | # native |
| 2727 | $base_map++; |
| 2728 | $bucket = ($maps_to_code_point) |
| 2729 | ? a2n($base_map) |
| 2730 | : $base_map; |
| 2731 | } |
| 2732 | |
| 2733 | # Add the native code point to the bucket for the |
| 2734 | # current map |
| 2735 | push @{$mapped_lists{$bucket}}, a2n($j); |
| 2736 | } # End of loop through all code points in the range |
| 2737 | |
| 2738 | # Get ready for the next range |
| 2739 | shift @invlist; |
| 2740 | shift @invmap; |
| 2741 | } # End of loop through all ranges in the map. |
| 2742 | |
| 2743 | # Here, @invlist and @invmap retain all the ranges from the |
| 2744 | # originals that start with code points above $upper_limit. |
| 2745 | # Each bucket in %mapped_lists contains all the code points |
| 2746 | # that map to that bucket. If the bucket is for a map to a |
| 2747 | # single code point, the bucket has been converted to native. |
| 2748 | # If something else (including multiple code points), no |
| 2749 | # conversion is done. |
| 2750 | # |
| 2751 | # Now we recreate the inversion map into %xlated, but this |
| 2752 | # time for the native character set. |
| 2753 | my %xlated; |
| 2754 | foreach my $bucket (keys %mapped_lists) { |
| 2755 | |
| 2756 | # Sort and convert this bucket to an inversion list. The |
| 2757 | # result will be that ranges that start with even-numbered |
| 2758 | # indexes will be for code points that map to this bucket; |
| 2759 | # odd ones map to some other bucket, and are discarded |
| 2760 | # below. |
| 2761 | @{$mapped_lists{$bucket}} |
| 2762 | = sort{ $a <=> $b} @{$mapped_lists{$bucket}}; |
| 2763 | @{$mapped_lists{$bucket}} |
| 2764 | = mk_invlist_from_sorted_cp_list(\@{$mapped_lists{$bucket}}); |
| 2765 | |
| 2766 | # Add each even-numbered range in the bucket to %xlated; |
| 2767 | # so that the keys of %xlated become the range start code |
| 2768 | # points, and the values are their corresponding maps. |
| 2769 | while (@{$mapped_lists{$bucket}}) { |
| 2770 | my $range_start = $mapped_lists{$bucket}->[0]; |
| 2771 | if ($bucket =~ /\cK/) { |
| 2772 | @{$xlated{$range_start}} = split /\cK/, $bucket; |
| 2773 | } |
| 2774 | else { |
| 2775 | # If adjusting, and there is more than one thing |
| 2776 | # that maps to the same thing, they must be split |
| 2777 | # so that later the adjusting doesn't think the |
| 2778 | # subsequent items can go away because of the |
| 2779 | # adjusting. |
| 2780 | my $range_end = ($to_adjust && $bucket != $map_default) |
| 2781 | ? $mapped_lists{$bucket}->[1] - 1 |
| 2782 | : $range_start; |
| 2783 | for my $i ($range_start .. $range_end) { |
| 2784 | $xlated{$i} = $bucket; |
| 2785 | } |
| 2786 | } |
| 2787 | shift @{$mapped_lists{$bucket}}; # Discard odd ranges |
| 2788 | shift @{$mapped_lists{$bucket}}; # Get ready for next |
| 2789 | # iteration |
| 2790 | } |
| 2791 | } # End of loop through all the buckets. |
| 2792 | |
| 2793 | # Here %xlated's keys are the range starts of all the code |
| 2794 | # points in the inversion map. Construct an inversion list |
| 2795 | # from them. |
| 2796 | my @new_invlist = sort { $a <=> $b } keys %xlated; |
| 2797 | |
| 2798 | # If the list is adjusted, we want to munge this list so that |
| 2799 | # we only have one entry for where consecutive code points map |
| 2800 | # to consecutive values. We just skip the subsequent entries |
| 2801 | # where this is the case. |
| 2802 | if ($to_adjust) { |
| 2803 | my @temp; |
| 2804 | for my $i (0 .. @new_invlist - 1) { |
| 2805 | next if $i > 0 |
| 2806 | && $new_invlist[$i-1] + 1 == $new_invlist[$i] |
| 2807 | && $xlated{$new_invlist[$i-1]} =~ $numeric_re |
| 2808 | && $xlated{$new_invlist[$i]} =~ $numeric_re |
| 2809 | && $xlated{$new_invlist[$i-1]} + 1 == $xlated{$new_invlist[$i]}; |
| 2810 | push @temp, $new_invlist[$i]; |
| 2811 | } |
| 2812 | @new_invlist = @temp; |
| 2813 | } |
| 2814 | |
| 2815 | # The inversion map comes from %xlated's values. We can |
| 2816 | # unshift each onto the front of the untouched portion, in |
| 2817 | # reverse order of the portion we did process. |
| 2818 | foreach my $start (reverse @new_invlist) { |
| 2819 | unshift @invmap, $xlated{$start}; |
| 2820 | } |
| 2821 | |
| 2822 | # Finally prepend the inversion list we have just constructed to the |
| 2823 | # one that contains anything we didn't process. |
| 2824 | unshift @invlist, @new_invlist; |
| 2825 | } |
| 2826 | } |
| 2827 | elsif (@invmap) { # inversion maps can't cope with this variable |
| 2828 | # being true, even if it could be true |
| 2829 | $same_in_all_code_pages = 0; |
| 2830 | } |
| 2831 | else { |
| 2832 | $same_in_all_code_pages = 1; |
| 2833 | } |
| 2834 | |
| 2835 | # prop_invmap() returns an extra final entry, which we can now |
| 2836 | # discard. |
| 2837 | if (@invmap) { |
| 2838 | pop @invlist; |
| 2839 | pop @invmap; |
| 2840 | } |
| 2841 | |
| 2842 | if ($l1_only) { |
| 2843 | die "Unimplemented to do a Latin-1 only inversion map" if @invmap; |
| 2844 | for my $i (0 .. @invlist - 1 - 1) { |
| 2845 | if ($invlist[$i] > 255) { |
| 2846 | |
| 2847 | # In an inversion list, even-numbered elements give the code |
| 2848 | # points that begin ranges that match the property; |
| 2849 | # odd-numbered give ones that begin ranges that don't match. |
| 2850 | # If $i is odd, we are at the first code point above 255 that |
| 2851 | # doesn't match, which means the range it is ending does |
| 2852 | # match, and crosses the 255/256 boundary. We want to include |
| 2853 | # this ending point, so increment $i, so the splice below |
| 2854 | # includes it. Conversely, if $i is even, it is the first |
| 2855 | # code point above 255 that matches, which means there was no |
| 2856 | # matching range that crossed the boundary, and we don't want |
| 2857 | # to include this code point, so splice before it. |
| 2858 | $i++ if $i % 2 != 0; |
| 2859 | |
| 2860 | # Remove everything past this. |
| 2861 | splice @invlist, $i; |
| 2862 | splice @invmap, $i if @invmap; |
| 2863 | last; |
| 2864 | } |
| 2865 | } |
| 2866 | } |
| 2867 | elsif ($nonl1_only) { |
| 2868 | my $found_nonl1 = 0; |
| 2869 | for my $i (0 .. @invlist - 1 - 1) { |
| 2870 | next if $invlist[$i] < 256; |
| 2871 | |
| 2872 | # Here, we have the first element in the array that indicates an |
| 2873 | # element above Latin1. Get rid of all previous ones. |
| 2874 | splice @invlist, 0, $i; |
| 2875 | splice @invmap, 0, $i if @invmap; |
| 2876 | |
| 2877 | # If this one's index is not divisible by 2, it means that this |
| 2878 | # element is inverting away from being in the list, which means |
| 2879 | # all code points from 256 to this one are in this list (or |
| 2880 | # map to the default for inversion maps) |
| 2881 | if ($i % 2 != 0) { |
| 2882 | unshift @invlist, 256; |
| 2883 | unshift @invmap, $map_default if @invmap; |
| 2884 | } |
| 2885 | $found_nonl1 = 1; |
| 2886 | last; |
| 2887 | } |
| 2888 | if (! $found_nonl1) { |
| 2889 | warn "No non-Latin1 code points in $prop_name"; |
| 2890 | output_invlist($prop_name, []); |
| 2891 | last; |
| 2892 | } |
| 2893 | } |
| 2894 | |
| 2895 | switch_pound_if ($prop_name, 'PERL_IN_REGCOMP_C'); |
| 2896 | start_charset_pound_if($charset, 1) unless $same_in_all_code_pages; |
| 2897 | |
| 2898 | output_invlist($prop_name, \@invlist, ($same_in_all_code_pages) |
| 2899 | ? $applies_to_all_charsets_text |
| 2900 | : $charset); |
| 2901 | |
| 2902 | if (@invmap) { |
| 2903 | output_invmap($prop_name, \@invmap, $lookup_prop, $map_format, |
| 2904 | $map_default, $extra_enums, $charset); |
| 2905 | } |
| 2906 | |
| 2907 | last if $same_in_all_code_pages; |
| 2908 | end_charset_pound_if; |
| 2909 | } |
| 2910 | } |
| 2911 | |
| 2912 | switch_pound_if ('binary_property_tables', 'PERL_IN_REGCOMP_C'); |
| 2913 | |
| 2914 | print $out_fh "\nconst char * deprecated_property_msgs[] = {\n\t"; |
| 2915 | print $out_fh join ",\n\t", map { "\"$_\"" } @deprecated_messages; |
| 2916 | print $out_fh "\n};\n"; |
| 2917 | |
| 2918 | my @enums = sort values %enums; |
| 2919 | |
| 2920 | # Save a copy of these before modification |
| 2921 | my @invlist_names = map { "${_}_invlist" } @enums; |
| 2922 | |
| 2923 | # Post-process the enums for deprecated properties. |
| 2924 | if (scalar keys %deprecated_tags) { |
| 2925 | my $seen_deprecated = 0; |
| 2926 | foreach my $enum (@enums) { |
| 2927 | if (grep { $_ eq $enum } keys %deprecated_tags) { |
| 2928 | |
| 2929 | # Change the enum name for this deprecated property to a |
| 2930 | # munged one to act as a placeholder in the typedef. Then |
| 2931 | # make the real name be a #define whose value is such that |
| 2932 | # its modulus with the number of enums yields the index into |
| 2933 | # the table occupied by the placeholder. And so that dividing |
| 2934 | # the #define value by the table length gives an index into |
| 2935 | # the table of deprecation messages for the corresponding |
| 2936 | # warning. |
| 2937 | my $revised_enum = "${enum}_perl_aux"; |
| 2938 | if (! $seen_deprecated) { |
| 2939 | $seen_deprecated = 1; |
| 2940 | print $out_fh "\n"; |
| 2941 | } |
| 2942 | print $out_fh "#define $enum ($revised_enum + (MAX_UNI_KEYWORD_INDEX * $deprecated_tags{$enum}))\n"; |
| 2943 | $enum = $revised_enum; |
| 2944 | } |
| 2945 | } |
| 2946 | } |
| 2947 | |
| 2948 | print $out_fh "\ntypedef enum {\n\tPERL_BIN_PLACEHOLDER = 0,\n\t"; |
| 2949 | print $out_fh join ",\n\t", @enums; |
| 2950 | print $out_fh "\n"; |
| 2951 | print $out_fh "} binary_invlist_enum;\n"; |
| 2952 | print $out_fh "\n#define MAX_UNI_KEYWORD_INDEX $enums[-1]\n"; |
| 2953 | |
| 2954 | output_table_header($out_fh, "UV *", "uni_prop_ptrs"); |
| 2955 | print $out_fh "\tNULL,\t/* Placeholder */\n"; |
| 2956 | print $out_fh "\t"; |
| 2957 | print $out_fh join ",\n\t", @invlist_names; |
| 2958 | print $out_fh "\n"; |
| 2959 | |
| 2960 | output_table_trailer(); |
| 2961 | |
| 2962 | print $out_fh join "\n", "\n", |
| 2963 | #'# ifdef DOINIT', |
| 2964 | #"\n", |
| 2965 | "/* Synonyms for perl properties */", |
| 2966 | @perl_prop_synonyms, |
| 2967 | #"\n", |
| 2968 | #"# endif /* DOINIT */", |
| 2969 | "\n"; |
| 2970 | |
| 2971 | switch_pound_if('Boundary_pair_tables', 'PERL_IN_REGEXEC_C'); |
| 2972 | |
| 2973 | output_GCB_table(); |
| 2974 | output_LB_table(); |
| 2975 | output_WB_table(); |
| 2976 | |
| 2977 | end_file_pound_if; |
| 2978 | |
| 2979 | print $out_fh <<"EOF"; |
| 2980 | |
| 2981 | /* More than one code point may have the same code point as their fold. This |
| 2982 | * gives the maximum number in the current Unicode release. (The folded-to |
| 2983 | * code point is not included in this count.) For example, both 'S' and |
| 2984 | * \\x{17F} fold to 's', so the number for that fold is 2. Another way to |
| 2985 | * look at it is the maximum length of all the IVCF_AUX_TABLE's */ |
| 2986 | #define MAX_FOLD_FROMS $max_fold_froms |
| 2987 | EOF |
| 2988 | |
| 2989 | my $sources_list = "lib/unicore/mktables.lst"; |
| 2990 | my @sources = qw(regen/mk_invlists.pl |
| 2991 | lib/unicore/mktables |
| 2992 | lib/Unicode/UCD.pm |
| 2993 | regen/charset_translations.pl |
| 2994 | regen/mk_PL_charclass.pl |
| 2995 | ); |
| 2996 | { |
| 2997 | # Depend on mktables’ own sources. It’s a shorter list of files than |
| 2998 | # those that Unicode::UCD uses. |
| 2999 | if (! open my $mktables_list, '<', $sources_list) { |
| 3000 | |
| 3001 | # This should force a rebuild once $sources_list exists |
| 3002 | push @sources, $sources_list; |
| 3003 | } |
| 3004 | else { |
| 3005 | while(<$mktables_list>) { |
| 3006 | last if /===/; |
| 3007 | chomp; |
| 3008 | push @sources, "lib/unicore/$_" if /^[^#]/; |
| 3009 | } |
| 3010 | } |
| 3011 | } |
| 3012 | |
| 3013 | read_only_bottom_close_and_rename($out_fh, \@sources); |
| 3014 | |
| 3015 | require './regen/mph.pl'; |
| 3016 | |
| 3017 | sub token_name |
| 3018 | { |
| 3019 | my $name = sanitize_name(shift); |
| 3020 | warn "$name contains non-word" if $name =~ /\W/; |
| 3021 | |
| 3022 | return "$table_name_prefix\U$name" |
| 3023 | } |
| 3024 | |
| 3025 | my $keywords_fh = open_new('uni_keywords.h', '>', |
| 3026 | {style => '*', by => 'regen/mk_invlists.pl', |
| 3027 | from => "mph.pl"}); |
| 3028 | |
| 3029 | no warnings 'once'; |
| 3030 | print $keywords_fh <<"EOF"; |
| 3031 | /* The precision to use in "%.*e" formats */ |
| 3032 | #define PL_E_FORMAT_PRECISION $utf8::e_precision |
| 3033 | |
| 3034 | EOF |
| 3035 | |
| 3036 | my ($second_level, $seed1, $length_all_keys, $smart_blob, $rows) = MinimalPerfectHash::make_mph_from_hash(\%keywords); |
| 3037 | print $keywords_fh MinimalPerfectHash::make_algo($second_level, $seed1, $length_all_keys, $smart_blob, $rows, undef, undef, undef, 'match_uniprop' ); |
| 3038 | |
| 3039 | push @sources, 'regen/mph.pl'; |
| 3040 | read_only_bottom_close_and_rename($keywords_fh, \@sources); |