5 require './regen/regen_lib.pl';
6 require './regen/charset_translations.pl';
7 use Unicode::UCD 'prop_invlist';
9 # This program outputs l1_charclass_tab.h, which defines the guts of the
10 # PL_charclass table. Each line is a bit map of properties that the Unicode
11 # code point at the corresponding position in the table array has. The first
12 # line corresponds to code point 0x0, NULL, the last line to 0xFF. For
13 # an application to see if the code point "i" has a particular property, it
15 # 'PL_charclass[i] & BIT'
16 # The bit names are of the form '_CC_property_suffix', where 'CC' stands for
17 # character class, and 'property' is the corresponding property, and 'suffix'
18 # is one of '_A' to mean the property is true only if the corresponding code
19 # point is ASCII, and '_L1' means that the range includes any Latin1
20 # character (ISO-8859-1 including the C0 and C1 controls). A property without
21 # these suffixes does not have different forms for both ranges.
23 # This program need be run only when adding new properties to it, or upon a
24 # new Unicode release, to make sure things haven't been changed by it.
26 # keys are the names of the bits; values are what generates the code points
27 # that have the bit set, or 0 if \p{key} is the generator
29 NONLATIN1_SIMPLE_FOLD => \&Non_Latin1_Simple_Folds,
30 NONLATIN1_FOLD => \&Non_Latin1_Folds,
31 ALPHANUMERIC => 'Alnum', # Like \w, but no underscore
32 ALPHA => 'XPosixAlpha',
36 CHARNAME_CONT => '_Perl_Charname_Continue',
40 IDFIRST => \&Id_First,
41 LOWER => 'XPosixLower',
42 NON_FINAL_FOLD => \&Non_Final_Folds,
44 PUNCT => \&Punct_and_Symbols,
45 QUOTEMETA => '_Perl_Quotemeta',
46 SPACE => 'XPerlSpace',
47 UPPER => 'XPosixUpper',
48 WORDCHAR => 'XPosixWord',
51 IS_IN_SOME_FOLD => '_Perl_Any_Folds',
52 BINDIGIT => [ ord '0', ord '1' ],
53 OCTDIGIT => [ ord '0', ord '1', ord '2', ord '3',
54 ord '4', ord '5', ord '6', ord '7' ],
56 # These are the control characters that there are mnemonics for
57 MNEMONIC_CNTRL => [ ord "\a", ord "\b", ord "\e", ord "\f",
58 ord "\n", ord "\r", ord "\t" ],
62 # Returns non-duplicated input values. From "Perl Best Practices:
63 # Encapsulated Cleverness". p. 455 in first edition.
66 return grep { ! $seen{$_}++ } @_;
70 # Return the code points that are in the inversion list given by the
73 my $invlist_ref = shift;
77 for (my $i = 0; $i < @$invlist_ref; $i += 2) {
78 my $upper = ($i + 1) < @$invlist_ref
79 ? $invlist_ref->[$i+1] - 1 # In range
80 : $Unicode::UCD::MAX_CP; # To infinity.
81 for my $j ($invlist_ref->[$i] .. $upper) {
89 # Read in the case fold mappings.
91 my %simple_folded_closure;
93 my @non_latin1_simple_folds;
97 # Use the Unicode data file if we are on an ASCII platform (which its data
98 # is for), and it is in the modern format (starting in Unicode 3.1.0) and
99 # it is available. This avoids being affected by potential bugs
100 # introduced by other layers of Perl
101 my $file="lib/unicore/CaseFolding.txt";
104 && pack("C*", split /\./, Unicode::UCD::UnicodeVersion()) ge v3.1.0
105 && open my $fh, "<", $file)
110 my ($invlist_ref, $invmap_ref, undef, $default)
111 = Unicode::UCD::prop_invmap('Case_Folding');
112 for my $i (0 .. @$invlist_ref - 1 - 1) {
113 next if $invmap_ref->[$i] == $default;
115 for my $j ($invlist_ref->[$i] .. $invlist_ref->[$i+1] -1) {
118 # Single-code point maps go to a 'C' type
119 if (! ref $invmap_ref->[$i]) {
120 push @folds, sprintf("%04X; C; %04X\n",
122 $invmap_ref->[$i] + $adjust);
124 else { # Multi-code point maps go to 'F'. prop_invmap()
125 # guarantees that no adjustment is needed for these,
126 # as the range will contain just one element
127 push @folds, sprintf("%04X; F; %s\n",
129 join " ", map { sprintf "%04X", $_ }
130 @{$invmap_ref->[$i]});
139 # Lines look like (without the initial '#'
140 #0130; F; 0069 0307; # LATIN CAPITAL LETTER I WITH DOT ABOVE
141 # Get rid of comments, ignore blank or comment-only lines
142 my $line = $_ =~ s/ (?: \s* \# .* )? $ //rx;
143 next unless length $line;
144 my ($hex_from, $fold_type, @folded) = split /[\s;]+/, $line;
146 my $from = hex $hex_from;
148 # Perl only deals with S, C, and F folds
149 next if $fold_type ne 'C' and $fold_type ne 'F' and $fold_type ne 'S';
151 # Get each code point in the range that participates in this line's fold.
152 # The hash has keys of each code point in the range, and values of what it
153 # folds to and what folds to it
154 for my $i (0 .. @folded - 1) {
155 my $fold = hex $folded[$i];
157 push @{$folded_closure{$fold}}, $from;
158 push @{$simple_folded_closure{$fold}}, $from if $fold_type ne 'F';
161 push @{$folded_closure{$from}}, $fold;
162 push @{$simple_folded_closure{$from}}, $fold if $fold_type ne 'F';
165 if (($fold_type eq 'C' || $fold_type eq 'S')
166 && ($fold < 256 != $from < 256))
168 # Fold is simple (hence can't be a non-final fold, so the 'if'
169 # above is mutualy exclusive from the 'if below) and crosses
170 # 255/256 boundary. We keep track of the Latin1 code points
172 push @non_latin1_simple_folds, ($fold < 256)
176 elsif ($i < @folded-1
178 && ! grep { $_ == $fold } @non_final_folds)
180 push @non_final_folds, $fold;
182 # Also add the upper case, which in the latin1 range folds to
184 push @non_final_folds, ord uc chr $fold;
189 # Now having read all the lines, combine them into the full closure of each
190 # code point in the range by adding lists together that share a common
192 foreach my $folded (keys %folded_closure) {
193 foreach my $from (grep { $_ < 256 } @{$folded_closure{$folded}}) {
194 push @{$folded_closure{$from}}, @{$folded_closure{$folded}};
197 foreach my $folded (keys %simple_folded_closure) {
198 foreach my $from (grep { $_ < 256 } @{$simple_folded_closure{$folded}}) {
199 push @{$simple_folded_closure{$from}}, @{$simple_folded_closure{$folded}};
203 # We have the single-character folds that cross the 255/256, like KELVIN
204 # SIGN => 'k', but we need the closure, so add like 'K' to it
205 foreach my $folded (@non_latin1_simple_folds) {
206 foreach my $fold (@{$simple_folded_closure{$folded}}) {
207 if ($fold < 256 && ! grep { $fold == $_ } @non_latin1_simple_folds) {
208 push @non_latin1_simple_folds, $fold;
214 my @alpha_invlist = prop_invlist("XPosixAlpha");
215 my @ids = expand_invlist(\@alpha_invlist);
217 return sort { $a <=> $b } uniques @ids;
220 sub Non_Latin1_Folds {
223 foreach my $folded (keys %folded_closure) {
224 push @return, $folded if grep { $_ > 255 } @{$folded_closure{$folded}};
229 sub Non_Latin1_Simple_Folds { # Latin1 code points that are folded to by
230 # non-Latin1 code points as single character
232 return @non_latin1_simple_folds;
235 sub Non_Final_Folds {
236 return @non_final_folds;
239 sub Punct_and_Symbols {
240 # Sadly, this is inconsistent: \pP and \pS for the ascii range;
241 # just \pP outside it.
243 my @punct_invlist = prop_invlist("Punct");
244 my @return = expand_invlist(\@punct_invlist);
246 my @symbols_invlist = prop_invlist("Symbol");
247 my @symbols = expand_invlist(\@symbols_invlist);
248 foreach my $cp (@symbols) {
253 return sort { $a <=> $b } uniques @return;
256 my @bits; # Each element is a bit map for a single code point
258 # For each bit type, calculate which code points should have it set
259 foreach my $bit_name (sort keys %bit_names) {
262 my $property = $bit_name; # The bit name is the same as its property,
264 $property = $bit_names{$bit_name} if $bit_names{$bit_name};
266 if (! ref $property) {
267 my @invlist = prop_invlist($property, '_perl_core_internal_ok');
268 @code_points = expand_invlist(\@invlist);
270 elsif (ref $property eq 'CODE') {
271 @code_points = &$property;
273 elsif (ref $property eq 'ARRAY') {
274 @code_points = @{$property};
277 foreach my $cp (@code_points) {
279 $bits[$cp] .= '|' if $bits[$cp];
280 $bits[$cp] .= "(1U<<_CC_$bit_name)";
284 my $out_fh = open_new('l1_char_class_tab.h', '>',
285 {style => '*', by => $0,
286 from => "Unicode::UCD"});
289 /* For code points whose position is not the same as Unicode, both are shown
293 # Output the table using fairly short names for each char.
294 my $is_for_ascii = 1; # get_supported_code_pages() returns the ASCII
295 # character set first
296 foreach my $charset (get_supported_code_pages()) {
297 my @a2n = @{get_a2n($charset)};
304 else { # EBCDIC. Calculate mapping from UTF-EBCDIC bytes to I8
305 my $i8_to_utf_ref = get_I8_2_utf($charset);
307 $utf_to_i8[$i8_to_utf_ref->[$i]] = $i;
311 print $out_fh "\n" . get_conditional_compile_line_start($charset);
312 for my $ord (0..255) {
315 if ($char =~ /\p{PosixGraph}/) {
316 my $quote = $char eq "'" ? '"' : "'";
317 $name = $quote . chr($ord) . $quote;
319 elsif ($char =~ /\p{XPosixGraph}/) {
321 $name = charnames::viacode($ord);
322 $name =~ s/LATIN CAPITAL LETTER //
323 or $name =~ s/LATIN SMALL LETTER (.*)/\L$1/
324 or $name =~ s/ SIGN\b//
325 or $name =~ s/EXCLAMATION MARK/'!'/
326 or $name =~ s/QUESTION MARK/'?'/
327 or $name =~ s/QUOTATION MARK/QUOTE/
328 or $name =~ s/ INDICATOR//;
329 $name =~ s/\bWITH\b/\L$&/;
330 $name =~ s/\bONE\b/1/;
331 $name =~ s/\b(TWO|HALF)\b/2/;
332 $name =~ s/\bTHREE\b/3/;
333 $name =~ s/\b QUARTER S? \b/4/x;
334 $name =~ s/VULGAR FRACTION (.) (.)/$1\/$2/;
335 $name =~ s/\bTILDE\b/'~'/i
336 or $name =~ s/\bCIRCUMFLEX\b/'^'/i
337 or $name =~ s/\bSTROKE\b/'\/'/i
338 or $name =~ s/ ABOVE\b//i;
341 use Unicode::UCD qw(prop_invmap);
342 my ($list_ref, $map_ref, $format)
343 = prop_invmap("_Perl_Name_Alias", '_perl_core_internal_ok');
344 if ($format !~ /^s/) {
346 carp "Unexpected format '$format' for '_Perl_Name_Alias";
349 my $which = Unicode::UCD::search_invlist($list_ref, $ord);
350 if (! defined $which) {
352 carp "No name found for code pont $ord";
355 my $map = $map_ref->[$which];
360 # Just pick the first abbreviation if more than one
361 my @names = grep { $_ =~ /abbreviation/ } @$map;
368 my $index = $a2n[$ord];
370 $i8 = $utf_to_i8[$index] if @utf_to_i8;
372 $out[$index] = "/* ";
373 $out[$index] .= sprintf "0x%02X ", $index if $ord != $index;
374 $out[$index] .= sprintf "U+%02X ", $ord;
375 $out[$index] .= sprintf "I8=%02X ", $i8 if defined $i8 && $i8 != $ord;
376 $out[$index] .= "$name */ ";
377 $out[$index] .= $bits[$ord];
379 # For EBCDIC character sets, we also add some data for when the bytes
380 # are in UTF-EBCDIC; these are based on the fundamental
381 # characteristics of UTF-EBCDIC.
385 '|(1U<<_CC_UTF8_START_BYTE_IS_FOR_AT_LEAST_SURROGATE)';
389 $out[$index] .= ",\n";
391 $out[-1] =~ s/,$//; # No trailing comma in the final entry
393 print $out_fh join "", @out;
394 print $out_fh "\n" . get_conditional_compile_line_end();
397 read_only_bottom_close_and_rename($out_fh)