5 require 'regen/regen_lib.pl';
7 # This program outputs the 256 lines that form the guts of the PL_charclass
8 # table. The output should be used to manually replace the table contents in
9 # l1_charclass_tab.h. Each line is a bit map of properties that the Unicode
10 # code point at the corresponding position in the table array has. The first
11 # line corresponds to code point U+0000, NULL, the last line to U=00FF. For
12 # an application to see if the code point "i" has a particular property, it
14 # 'PL_charclass[i] & BIT'
15 # The bit names are of the form '_CC_property_suffix', where 'CC' stands for
16 # character class, and 'property' is the corresponding property, and 'suffix'
17 # is one of '_A' to mean the property is true only if the corresponding code
18 # point is ASCII, and '_L1' means that the range includes any Latin1
19 # character (ISO-8859-1 including the C0 and C1 controls). A property without
20 # these suffixes does not have different forms for both ranges.
22 # The data in the table is pretty well set in stone, so that this program need
23 # be run only when adding new properties to it.
58 # Read in the case fold mappings.
60 my $file="lib/unicore/CaseFolding.txt";
61 open my $fh, "<", $file or die "Failed to read '$file': $!";
65 # Lines look like (without the initial '#'
66 #0130; F; 0069 0307; # LATIN CAPITAL LETTER I WITH DOT ABOVE
67 my ($line, $comment) = split / \s+ \# \s+ /x, $_;
68 next if $line eq "" || substr($line, 0, 1) eq '#';
69 my ($hex_from, $fold_type, @folded) = split /[\s;]+/, $line;
71 my $from = hex $hex_from;
73 # Perl only deals with C and F folds
74 next if $fold_type ne 'C' and $fold_type ne 'F';
76 # Get each code point in the range that participates in this line's fold.
77 # The hash has keys of each code point in the range, and values of what it
78 # folds to and what folds to it
79 foreach my $hex_fold (@folded) {
80 my $fold = hex $hex_fold;
81 push @{$folded_closure{$fold}}, $from if $fold < 256;
82 push @{$folded_closure{$from}}, $fold if $from < 256;
86 # Now having read all the lines, combine them into the full closure of each
87 # code point in the range by adding lists together that share a common element
88 foreach my $folded (keys %folded_closure) {
89 foreach my $from (grep { $_ < 256 } @{$folded_closure{$folded}}) {
90 push @{$folded_closure{$from}}, @{$folded_closure{$folded}};
94 my @bits; # Bit map for each code point
96 foreach my $folded (keys %folded_closure) {
97 $bits[$folded] = "_CC_NONLATIN1_FOLD" if grep { $_ > 255 }
98 @{$folded_closure{$folded}};
101 for my $ord (0..255) {
102 my $char = chr($ord);
103 utf8::upgrade($char); # Important to use Unicode semantics!
104 for my $property (@properties) {
105 my $name = $property;
107 # The property name that corresponds to this doesn't have a suffix.
108 # If is a latin1 version, no further checking is needed.
109 if (! ($name =~ s/_L1$//)) {
111 # Here, isn't an L1. It's either a special one or the suffix ends
112 # in _A. In the latter case, it's automatically false for
113 # non-ascii. The one current special is valid over the whole range.
114 next if $name =~ s/_A$// && $ord >= 128;
118 if ($name eq 'PUNCT') {;
120 # Sadly, this is inconsistent: \pP and \pS for the ascii range,
121 # just \pP outside it.
122 $re = qr/\p{Punct}|[^\P{Symbol}\P{ASCII}]/;
123 } elsif ($name eq 'CHARNAME_CONT') {;
124 $re = qr/[-\w ():\xa0]/;
125 } elsif ($name eq 'SPACE') {;
127 } elsif ($name eq 'IDFIRST') {
128 $re = qr/[_\p{Alpha}]/;
129 } elsif ($name eq 'PSXSPC') {
130 $re = qr/[\v\p{Space}]/;
131 } elsif ($name eq 'WORDCHAR') {
133 } elsif ($name eq 'ALNUMC') {
134 # Like \w, but no underscore
136 } elsif ($name eq 'OCTAL') {
138 } else { # The remainder have the same name and values as Unicode
139 $re = eval "qr/\\p{$name}/";
141 carp $@ if ! defined $re;
143 #print "$ord, $name $property, $re\n";
144 if ($char =~ $re) { # Add this property if matches
145 $bits[$ord] .= '|' if $bits[$ord];
146 $bits[$ord] .= "_CC_$property";
149 #print __LINE__, " $ord $char $bits[$ord]\n";
152 # Names of C0 controls
188 # Names of C1 controls, plus the adjacent DEL
225 my $out_fh = safer_open('l1_char_class_tab.h-new', 'l1_char_class_tab.h');
226 print $out_fh read_only_top(lang => 'C', style => '*', by => $0, from => $file);
228 # Output the table using fairly short names for each char.
229 for my $ord (0..255) {
231 if ($ord < 32) { # A C0 control
233 } elsif ($ord > 32 && $ord < 127) { # Graphic
234 $name = "'" . chr($ord) . "'";
235 } elsif ($ord >= 127 && $ord <= 0x9f) {
236 $name = $C1[$ord - 127]; # A C1 control + DEL
237 } else { # SPACE, or, if Latin1, shorten the name */
239 $name = charnames::viacode($ord);
240 $name =~ s/LATIN CAPITAL LETTER //
241 || $name =~ s/LATIN SMALL LETTER (.*)/\L$1/;
243 printf $out_fh "/* U+%02X %s */ %s,\n", $ord, $name, $bits[$ord];
246 read_only_bottom_close_and_rename($out_fh)