5 require 'regen/regen_lib.pl';
6 require 'regen/charset_translations.pl';
8 # This program outputs l1_charclass_tab.h, which defines the guts of the
9 # PL_charclass table. 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 # This program need be run only when adding new properties to it, or upon a
23 # new Unicode release, to make sure things haven't been changed by it.
49 BACKSLASH_FOO_LBRACE_IS_META
52 # Read in the case fold mappings.
54 my @hex_non_final_folds;
58 BEGIN { # Have to do this at compile time because using user-defined \p{property}
60 # Use the Unicode data file if we are on an ASCII platform (which its data
61 # is for), and it is in the modern format (starting in Unicode 3.1.0) and
62 # it is available. This avoids being affected by potential bugs
63 # introduced by other layers of Perl
64 my $file="lib/unicore/CaseFolding.txt";
67 && pack("C*", split /\./, Unicode::UCD::UnicodeVersion()) ge v3.1.0
68 && open my $fh, "<", $file)
73 my ($invlist_ref, $invmap_ref, undef, $default)
74 = Unicode::UCD::prop_invmap('Case_Folding');
75 for my $i (0 .. @$invlist_ref - 1 - 1) {
76 next if $invmap_ref->[$i] == $default;
78 for my $j ($invlist_ref->[$i] .. $invlist_ref->[$i+1] -1) {
81 # Single-code point maps go to a 'C' type
82 if (! ref $invmap_ref->[$i]) {
83 push @folds, sprintf("%04X; C; %04X\n",
85 $invmap_ref->[$i] + $adjust);
87 else { # Multi-code point maps go to 'F'. prop_invmap()
88 # guarantees that no adjustment is needed for these,
89 # as the range will contain just one element
90 push @folds, sprintf("%04X; F; %s\n",
92 join " ", map { sprintf "%04X", $_ }
93 @{$invmap_ref->[$i]});
102 # Lines look like (without the initial '#'
103 #0130; F; 0069 0307; # LATIN CAPITAL LETTER I WITH DOT ABOVE
104 # Get rid of comments, ignore blank or comment-only lines
105 my $line = $_ =~ s/ (?: \s* \# .* )? $ //rx;
106 next unless length $line;
107 my ($hex_from, $fold_type, @folded) = split /[\s;]+/, $line;
109 my $from = hex $hex_from;
111 # Perl only deals with C and F folds
112 next if $fold_type ne 'C' and $fold_type ne 'F';
114 # Get each code point in the range that participates in this line's fold.
115 # The hash has keys of each code point in the range, and values of what it
116 # folds to and what folds to it
117 for my $i (0 .. @folded - 1) {
118 my $hex_fold = $folded[$i];
119 my $fold = hex $hex_fold;
120 push @{$folded_closure{$fold}}, $from if $fold < 256;
121 push @{$folded_closure{$from}}, $fold if $from < 256;
125 && ! grep { $_ eq $hex_fold } @hex_non_final_folds)
127 push @hex_non_final_folds, $hex_fold;
129 # Also add the upper case, which in the latin1 range folds to
131 push @hex_non_final_folds, sprintf "%04X", ord uc chr $fold;
136 # Now having read all the lines, combine them into the full closure of each
137 # code point in the range by adding lists together that share a common
139 foreach my $folded (keys %folded_closure) {
140 foreach my $from (grep { $_ < 256 } @{$folded_closure{$folded}}) {
141 push @{$folded_closure{$from}}, @{$folded_closure{$folded}};
146 sub Is_Non_Latin1_Fold {
149 foreach my $folded (keys %folded_closure) {
150 push @return, sprintf("%X", $folded), if grep { $_ > 255 }
151 @{$folded_closure{$folded}};
153 return join("\n", @return) . "\n";
156 sub Is_Non_Final_Fold {
157 return join("\n", @hex_non_final_folds) . "\n";
160 my @bits; # Bit map for each code point
162 # For each character, calculate which properties it matches.
163 for my $ord (0..255) {
164 my $char = chr($ord);
165 utf8::upgrade($char); # Important to use Unicode rules!
167 # Look at all the properties we care about here.
168 for my $property (@properties) {
169 my $name = $property;
171 # Remove the suffix to get the actual property name.
172 # Currently the suffixes are '_L1', '_A', and none.
173 # If is a latin1 version, no further checking is needed.
174 if (! ($name =~ s/_L1$//)) {
176 # Here, isn't an _L1. If its _A, it's automatically false for
177 # non-ascii. The only current ones (besides ASCII) without a
178 # suffix are valid over the whole range.
179 next if $name =~ s/_A$// && $char !~ /\p{ASCII}/;
182 if ($name eq 'PUNCT') {;
184 # Sadly, this is inconsistent: \pP and \pS for the ascii range,
185 # just \pP outside it.
186 $re = qr/\p{Punct}|[^\P{Symbol}\P{ASCII}]/;
187 } elsif ($name eq 'CHARNAME_CONT') {;
188 $re = qr/\p{_Perl_Charname_Continue}/,
189 } elsif ($name eq 'SPACE') {;
190 $re = qr/\p{XPerlSpace}/;
191 } elsif ($name eq 'IDFIRST') {
192 $re = qr/[_\p{Alpha}]/;
193 } elsif ($name eq 'PSXSPC') {
194 $re = qr/[\v\p{Space}]/;
195 } elsif ($name eq 'WORDCHAR') {
196 $re = qr/\p{XPosixWord}/;
197 } elsif ($name eq 'ALPHANUMERIC') {
198 # Like \w, but no underscore
200 } elsif ($name eq 'QUOTEMETA') {
201 $re = qr/\p{_Perl_Quotemeta}/;
202 } elsif ($name eq 'NONLATIN1_FOLD') {
203 $re = qr/\p{Is_Non_Latin1_Fold}/;
204 } elsif ($name eq 'NON_FINAL_FOLD') {
205 $re = qr/\p{Is_Non_Final_Fold}/;
206 } elsif ($name eq 'IS_IN_SOME_FOLD') {
207 $re = qr/\p{_Perl_Any_Folds}/;
208 } elsif ($name eq 'BACKSLASH_FOO_LBRACE_IS_META') {
210 # This is true for FOO where FOO is the varying character in:
212 # and the sequence has non-literal meaning to Perl; so it is true
213 # for 'x' because \x{ is special, but not 'a' because \a{ isn't.
215 } else { # The remainder have the same name and values as Unicode
216 $re = eval "qr/\\p{$name}/";
218 carp $@ if ! defined $re;
220 #print "$ord, $name $property, $re\n";
221 if ($char =~ $re) { # Add this property if matches
222 $bits[$ord] .= '|' if $bits[$ord];
223 $bits[$ord] .= "(1U<<_CC_$property)";
226 #print __LINE__, " $ord $char $bits[$ord]\n";
229 my $out_fh = open_new('l1_char_class_tab.h', '>',
230 {style => '*', by => $0,
231 from => "property definitions"});
234 /* For code points whose position is not the same as Unicode, both are shown
238 # Output the table using fairly short names for each char.
239 foreach my $charset (get_supported_code_pages()) {
240 my @a2n = get_a2n($charset);
243 print $out_fh "\n" . get_conditional_compile_line_start($charset);
244 for my $ord (0..255) {
247 if ($char =~ /\p{PosixGraph}/) {
248 my $quote = $char eq "'" ? '"' : "'";
249 $name = $quote . chr($ord) . $quote;
251 elsif ($char =~ /\p{XPosixGraph}/) {
253 $name = charnames::viacode($ord);
254 $name =~ s/LATIN CAPITAL LETTER //
255 or $name =~ s/LATIN SMALL LETTER (.*)/\L$1/
256 or $name =~ s/ SIGN\b//
257 or $name =~ s/EXCLAMATION MARK/'!'/
258 or $name =~ s/QUESTION MARK/'?'/
259 or $name =~ s/QUOTATION MARK/QUOTE/
260 or $name =~ s/ INDICATOR//;
261 $name =~ s/\bWITH\b/\L$&/;
262 $name =~ s/\bONE\b/1/;
263 $name =~ s/\b(TWO|HALF)\b/2/;
264 $name =~ s/\bTHREE\b/3/;
265 $name =~ s/\b QUARTER S? \b/4/x;
266 $name =~ s/VULGAR FRACTION (.) (.)/$1\/$2/;
267 $name =~ s/\bTILDE\b/'~'/i
268 or $name =~ s/\bCIRCUMFLEX\b/'^'/i
269 or $name =~ s/\bSTROKE\b/'\/'/i
270 or $name =~ s/ ABOVE\b//i;
273 use Unicode::UCD qw(prop_invmap);
274 my ($list_ref, $map_ref, $format) = prop_invmap("Name_Alias");
275 if ($format !~ /^s/) {
277 carp "Unexpected format '$format' for 'Name_Alias";
280 my $which = Unicode::UCD::search_invlist($list_ref, $ord);
281 if (! defined $which) {
283 carp "No name found for code pont $ord";
286 my $map = $map_ref->[$which];
291 # Just pick the first abbreviation if more than one
292 my @names = grep { $_ =~ /abbreviation/ } @$map;
298 my $index = $a2n[$ord];
299 $out[$index] = ($ord == $index)
300 ? sprintf "/* U+%02X %s */ %s,\n", $ord, $name, $bits[$ord]
301 : sprintf "/* 0x%02X U+%02X %s */ %s,\n", $index, $ord, $name, $bits[$ord];
303 print $out_fh join "", @out;
304 print $out_fh "\n" . get_conditional_compile_line_end();
307 read_only_bottom_close_and_rename($out_fh)