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