4 # mktables -- create the runtime Perl Unicode files (lib/unicore/**/*.pl)
5 # from the Unicode database files (lib/unicore/*.txt).
10 my $LastUnicodeCodepoint = 0x10FFFF; # As of Unicode 3.1.1.
17 my ($table, $last) = @_;
19 $table->[-1]->[1] = $last;
23 my ($table, $code, $name) = @_;
25 hex($table->[-1]->[1]) == hex($code) - 1 &&
26 (!defined $name || $table->[-1]->[2] eq $name)) {
27 extend($table, $code);
29 push @$table, [$code, $code, $name];
34 my ($table, $code_ini, $code_fin, $name) = @_;
35 append($table, $code_ini, $name);
36 extend($table, $code_fin);
43 if ($table->[0]->[0]) {
44 $last = hex($table->[0]->[0]);
45 push @$inverse, [ "0000",
46 sprintf("%04X", $last - 1) ];
48 for my $i (0..$#$table-1) {
49 $first = defined $table->[$i ]->[1] ?
50 hex($table->[$i ]->[1]) : 0;
51 $last = defined $table->[$i + 1]->[0] ?
52 hex($table->[$i + 1]->[0]) : $first;
53 push @$inverse, [ sprintf("%04X", $first + 1),
54 sprintf("%04X", $last - 1) ]
55 unless $first + 1 == $last;
64 # !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
65 # This file is built by $0 from e.g. Unicode.txt.
66 # Any changes made here will be lost!
87 my ($table, $file) = @_;
89 if (open(my $fh, ">$file")) {
93 print $fh $i->[0], "\t",
94 $i->[1] ne $i->[0] ? $i->[1] : "", "\t",
95 defined $i->[2] ? $i->[2] : "", "\n";
100 die "$0: $file: $!\n";
105 # The %In contains the mapping of the script/block name into a number.
116 # Read in the Unicode.txt, the main Unicode database.
124 my ($Name, $GeneralH, $GeneralA, $Cat,
125 $name, $cat, $code, $op) = @_;
127 $op->($Name, $code, $name);
128 $op->($GeneralA, $code, $cat);
130 $op->($GeneralH->{$name} ||= [], $code, $name);
132 $op->($Cat->{$cat} ||= [], $code);
133 $op->($Cat->{substr($cat, 0, 1)}
135 # 005F: SPACING UNDERSCORE
136 $op->($Cat->{Word} ||= [], $code)
137 if $cat =~ /^[LMN]/ or $code eq "005F";
138 $op->($Cat->{Alnum} ||= [], $code)
140 $op->($Cat->{Alpha} ||= [], $code)
142 # 0009: HORIZONTAL TABULATION
144 # 000B: VERTICAL TABULATION
146 # 000D: CARRIAGE RETURN
148 $op->($Cat->{Space} ||= [], $code)
150 $code =~ /^(0009|000A|000B|000C|000D)$/;
151 $op->($Cat->{SpacePerl} ||= [], $code)
153 $code =~ /^(0009|000A|000C|000D)$/;
154 $op->($Cat->{Blank} ||= [], $code)
155 if $code =~ /^(0020|0009)$/ ||
157 $op->($Cat->{Digit} ||= [], $code) if $cat eq "Nd";
158 $op->($Cat->{Upper} ||= [], $code) if $cat eq "Lu";
159 $op->($Cat->{Lower} ||= [], $code) if $cat eq "Ll";
160 $op->($Cat->{Title} ||= [], $code) if $cat eq "Lt";
161 $op->($Cat->{ASCII} ||= [], $code) if $code le "007F";
162 $op->($Cat->{Cntrl} ||= [], $code) if $cat =~ /^C/;
163 $op->($Cat->{Graph} ||= [], $code) if $cat =~ /^([LMNPS]|Co)/;
164 $op->($Cat->{Print} ||= [], $code) if $cat =~ /^([LMNPS]|Co|Zs)/;
165 $op->($Cat->{Punct} ||= [], $code) if $cat =~ /^P/;
166 # 003[0-9]: DIGIT ZERO..NINE, 00[46][1-6]: A..F, a..f
167 $op->($Cat->{XDigit} ||= [], $code)
168 if $code =~ /^00(3[0-9]|[46][1-6])$/;
172 if (open(my $Unicode, "Unicode.txt")) {
185 my $LastCodeInt = -1; # a numeric, not a hexadecimal string.
187 # UnicodeData-3.1.0.html says
188 # no characters in the file have the property, Cn, Not Assigned.
190 sub check_no_characters { # in the scope of my $LastCodeInt;
192 my $diff_from_last = hex($code) - $LastCodeInt;
193 my $code_ini = sprintf("%04X", $LastCodeInt + 1);
194 $LastCodeInt = hex($code);
195 if ($diff_from_last == 1) {
197 } elsif ($diff_from_last == 2) {
198 append($Cat{Cn} ||= [], $code_ini);
199 append($Cat{C} ||= [], $code_ini);
201 my $code_fin = sprintf("%04X", hex($code) - 1);
202 append_range($Cat{Cn} ||= [], $code_ini, $code_fin);
203 append_range($Cat{C} ||= [], $code_ini, $code_fin);
208 next unless /^[0-9A-Fa-f]+;/;
211 my ($code, $name, $cat, $comb, $bidi, $deco,
212 $decimal, $digit, $number,
213 $mirrored, $unicode10, $comment,
214 $upper, $lower, $title) = split(/\s*;\s*/);
216 if ($name =~ /^<(.+), (First|Last)>$/) {
218 check_no_characters($code);
220 $LastCodeInt = hex($code);
224 gencat(\@Name, \%General, \@General, \%Cat,
226 $2 eq 'First' ? \&append : \&extend);
227 unless (defined $In{$name}) {
228 $In{$name} = $InId++;
229 $InIn{$name} = $General{$name};
232 check_no_characters($code);
234 gencat(\@Name, \%General, \@General, \%Cat,
235 $name, $cat, $code, \&append);
237 append($To{Upper} ||= [], $code, $upper) if $upper;
238 append($To{Lower} ||= [], $code, $lower) if $lower;
239 append($To{Title} ||= [], $code, $title) if $title;
240 append($To{Digit} ||= [], $code, $decimal) if $decimal;
242 append(\@Bidi, $code, $bidi);
243 append($Bidi{$bidi} ||= [], $code);
245 append(\@Comb, $code, $comb) if $comb;
248 append(\@Deco, $code, $deco);
249 if ($deco =~/^<(\w+)>/) {
250 append($Deco{Compat} ||= [], $code);
251 append($DC{$1} ||= [], $code);
253 append($Deco{Canon} ||= [], $code);
257 append(\@Number, $code, $number) if $number;
259 append(\@Mirrored, $code) if $mirrored eq "Y";
263 check_no_characters(sprintf("%X", $LastUnicodeCodepoint + 1));
265 flush(\@Name, "Name.pl");
267 foreach my $cat (sort keys %Cat) {
268 flush($Cat{$cat}, "Is/$cat.pl");
271 foreach my $to (sort keys %To) {
272 flush($To{$to}, "To/$to.pl");
275 flush(\@Bidi, "Bidirectional.pl");
276 foreach my $bidi (sort keys %Bidi) {
277 flush($Bidi{$bidi}, "Is/Bidi$bidi.pl");
280 flush(\@Comb, "CombiningClass.pl");
282 flush(\@Deco, "Decomposition.pl");
283 foreach my $deco (sort keys %Deco) {
284 flush($Deco{$deco}, "Is/Deco$deco.pl");
286 foreach my $dc (sort keys %DC) {
287 flush($DC{$dc}, "Is/DC$dc.pl");
290 flush(\@Number, "Number.pl");
292 flush(\@Mirrored, "Is/Mirrored.pl");
294 die "$0: Unicode.txt: $!\n";
297 # The general cateory can be written out already now.
299 flush(\@General, "Category.pl");
302 # Read in the LineBrk.txt.
305 if (open(my $LineBrk, "LineBrk.txt")) {
310 next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(\w+)/;
312 my ($first, $last, $lbrk) = ($1, $2, $3);
314 append(\@Lbrk, $first, $lbrk);
315 append($Lbrk{$lbrk} ||= [], $first);
317 extend(\@Lbrk, $last);
318 extend($Lbrk{$lbrk}, $last);
322 flush(\@Lbrk, "Lbrk.pl");
323 foreach my $lbrk (sort keys %Lbrk) {
324 flush($Lbrk{$lbrk}, "Is/Lbrk$lbrk.pl");
327 die "$0: LineBrk.txt: $!\n";
331 # Read in the ArabShap.txt.
334 if (open(my $ArabShap, "ArabShap.txt")) {
338 while (<$ArabShap>) {
339 next unless /^[0-9A-Fa-f]+;/;
342 my ($code, $name, $link, $linkgroup) = split(/\s*;\s*/);
344 append(\@ArabLink, $code, $link);
345 append(\@ArabLinkGroup, $code, $linkgroup);
348 flush(\@ArabLink, "ArabLink.pl");
349 flush(\@ArabLinkGroup, "ArabLnkGrp.pl");
351 die "$0: ArabShap.txt: $!\n";
355 # Read in the Jamo.txt.
358 if (open(my $Jamo, "Jamo.txt")) {
362 next unless /^([0-9A-Fa-f]+)\s*;\s*(\w*)/;
364 my ($code, $short) = ($1, $2);
366 append(\@Short, $code, $short);
369 flush(\@Short, "JamoShort.pl");
371 die "$0: Jamo.txt: $!\n";
375 # Read in the Scripts.txt.
380 if (open(my $Scripts, "Scripts.txt")) {
382 next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(.+?)\s*\#/;
384 # Wait until all the scripts have been read since
385 # they are not listed in numeric order.
386 push @Scripts, [ hex($1), $1, $2, $3 ];
389 die "$0: Scripts.txt: $!\n";
392 # Now append the scripts properties in their code point order.
397 for my $script (sort { $a->[0] <=> $b->[0] } @Scripts) {
398 my ($code, $first, $last, $name) = @$script;
399 append($Scripts, $first, $name);
400 append($Script{$name} ||= [], $first, $name);
402 extend($Scripts, $last);
403 extend($Script{$name}, $last);
405 unless (defined $In{$name}) {
406 $InScript{$InId} = $name;
407 $In{$name} = $InId++;
408 $InIn{$name} = $Script{$name};
412 # Scripts.pl can be written out already now.
414 flush(\@Scripts, "Scripts.pl");
416 # Common is everything not explicitly assigned to a Script
418 $In{Common} = $InId++;
419 my $Common = inverse($Scripts);
420 $InIn{Common} = $Common;
423 # Read in the Blocks.txt.
429 if (open(my $Blocks, "Blocks.txt")) {
431 next unless /^([0-9A-Fa-f]+)\.\.([0-9A-Fa-f]+)\s*;\s*(.+?)\s*$/;
433 my ($first, $last, $name) = ($1, $2, $3);
434 my $origname = $name;
436 # If there's a naming conflict (the script names are
437 # in uppercase), the name of the block has " Block"
440 $pat =~ s/([- _])/(?:[-_]|\\s+)?/g;
441 for my $i (values %InScript) {
442 if ($i =~ /^$pat$/i) {
448 append(\@Blocks, $first, $name);
449 append($Blocks{$name} ||= [], $first, $name);
451 extend(\@Blocks, $last);
452 extend($Blocks{$name}, $last);
454 unless (defined $In{$name}) {
455 $InBlock{$InId} = $origname;
456 $In{$name} = $InId++;
457 $InIn{$name} = $Blocks{$name};
461 die "$0: Blocks.txt: $!\n";
464 # Blocks.pl can be written out already now.
466 flush(\@Blocks, "Blocks.pl");
469 # Read in the PropList.txt. It contains extended properties not
470 # listed in the Unicode.txt, such as 'Other_Alphabetic':
471 # alphabetic but not of the general category L; many modifiers
472 # belong to this extended property category: while they are not
473 # alphabets, they are alphabetic in nature.
478 if (open(my $Props, "PropList.txt")) {
480 next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(.+?)\s*\#/;
482 # Wait until all the extended properties have been read since
483 # they are not listed in numeric order.
484 push @Props, [ hex($1), $1, $2, $3 ];
487 die "$0: PropList.txt: $!\n";
490 # Now append the extended properties in their code point order.
495 for my $prop (sort { $a->[0] <=> $b->[0] } @Props) {
496 my ($code, $first, $last, $name) = @$prop;
497 append($Props, $first, $name);
498 append($Prop{$name} ||= [], $first, $name);
500 extend($Props, $last);
501 extend($Prop{$name}, $last);
503 unless (defined $In{$name}) {
504 $In{$name} = $InId++;
505 $InIn{$name} = $Prop{$name};
509 # Assigned is everything not Cn
511 $In{Assigned} = $InId++;
512 my $Assigned = inverse($Cat{Cn});
513 $InIn{Assigned} = $Assigned;
515 # Unassigned is everything not Assigned
517 $In{Unassigned} = $InId++;
518 my $Unassigned = $Cat{Cn};
519 $InIn{Unassigned} = $Unassigned;
521 # Unassigned is everything not Assigned
522 sub merge_general_and_extended {
523 my ($name, $general, $extended) = @_;
527 map { pop @{$_}; $_ }
528 sort { $a->[2] <=> $b->[2] }
529 map { [ $_->[0], $_->[1], hex($_->[0]) ] }
531 map { ref $_ ? @$_ : $_ }
532 @Cat {ref $general ? @$general : $general } :
535 map { ref $_ ? @$_ : $_ }
536 @Prop{ref $extended ? @$extended : $extended} :
539 $In{$name} = $InId++;
540 $InIn{$name} = $merged;
545 # Alphabetic is L and Other_Alphabetic.
548 merge_general_and_extended('Alphabetic', 'L', 'Other_Alphabetic');
550 # Lowercase is Ll and Other_Lowercase.
553 merge_general_and_extended('Lowercase', 'Ll', 'Other_Lowercase');
555 # Uppercase is Lu and Other_Uppercase.
558 merge_general_and_extended('Uppercase', 'Lu', 'Other_Uppercase');
560 # Math is Sm and Other_Math.
563 merge_general_and_extended('Math', 'Sm', 'Other_Math');
565 # Lampersand is Ll, Lu, and Lt.
568 merge_general_and_extended('Lampersand', [ qw(Ll Lu Lt) ]);
570 # ID_Start is Ll, Lu, Lt, Lm, Lo, and Nl.
573 merge_general_and_extended('ID_Start', [ qw(Ll Lu Lt Lm Lo Nl) ]);
575 # ID_Continue is ID_Start, Mn, Mc, Nd, and Pc.
578 merge_general_and_extended('ID_Continue', [ qw(Ll Lu Lt Lm Lo Nl
586 my $Any = [ [ 0, sprintf("%04X", $LastUnicodeCodepoint) ] ];
597 # mapping() will be used to write out the In and Is virtual mappings.
601 my ($map, $name) = @_;
603 if (open(my $fh, ">$name.pl")) {
607 # The %pat will hold a hash that maps the first two
608 # lowercased letters of a class to a 'fuzzified' regular
609 # expression that points to the real mapping.
613 # But first write out the offical name to real name
614 # (the filename) mapping.
620 for my $i (sort { lc $a cmp lc $b } keys %$map) {
622 # Here is the 'fuzzification': accept any space,
623 # dash, or underbar where in the official name
624 # there is space or a dash (or underbar, but
626 $pat =~ s/([- _])/(?:[-_]|\\s+)?/g;
627 # The prefix length of 2 is enough spread,
628 # and besides, we have 'Yi' as an In category.
629 push @{$pat{lc(substr($i, 0, 2))}}, [ $i, $pat ];
630 printf $fh "%-45s => '$map->{$i}',\n", "'$i'";
636 # Now write out the %pat mapping.
642 foreach my $prefix (sort keys %pat) {
643 print $fh "'$prefix' => {\n";
644 foreach my $ipat (@{$pat{$prefix}}) {
645 my ($i, $pat) = @$ipat;
646 print $fh "\t'$pat' => '$map->{$i}',\n";
656 die "$0: $name.pl: $!\n";
661 # Write out the virtual In mappings.
667 # Append the InScript and InBlock mappings.
668 # These are needed only if Script= and Block= syntaxes are used.
671 if (open(my $In, ">>In.pl")) {
677 for my $i (sort { $a <=> $b } keys %InScript) {
678 printf $In "%4d => '$InScript{$i}',\n", $i;
689 for my $i (sort { $a <=> $b } keys %InBlock) {
690 printf $In "%4d => '$InBlock{$i}',\n", $i;
696 die "$0: In.pl: $!\n";
700 # Write out the real In mappings
701 # (the In.pl written out just above has the virtual In mappings)
704 foreach my $in (sort { $In{$a} <=> $In{$b} } keys %In) {
705 flush($InIn{$in}, "In/$In{$in}.pl");
709 # The mapping from General Category long forms to short forms is
710 # currently hardwired here since no simple data file in the UCD
711 # seems to do that. Unicode 3.2 will assumedly correct this.
716 'Uppercase_Letter' => 'Lu',
717 'Lowercase_Letter' => 'Ll',
718 'Titlecase_Letter' => 'Lt',
719 'Modifier_Letter' => 'Lm',
720 'Other_Letter' => 'Lo',
723 'Non_Spacing_Mark' => 'Mn',
724 'Spacing_Mark' => 'Mc',
725 'Enclosing_Mark' => 'Me',
728 'Space_Separator' => 'Zs',
729 'Line_Separator' => 'Zl',
730 'Paragraph_Separator' => 'Zp',
733 'Decimal_Number' => 'Nd',
734 'Letter_Number' => 'Nl',
735 'Other_Number' => 'No',
737 'Punctuation' => 'P',
738 'Connector_Punctuation' => 'Pc',
739 'Dash_Punctuation' => 'Pd',
740 'Open_Punctuation' => 'Ps',
741 'Close_Punctuation' => 'Pe',
742 'Initial_Punctuation' => 'Pi',
743 'Final_Punctuation' => 'Pf',
744 'Other_Punctuation' => 'Po',
747 'Math_Symbol' => 'Sm',
748 'Currency_Symbol' => 'Sc',
749 'Modifier_Symbol' => 'Sk',
750 'Other_Symbol' => 'So',
756 'Private Use' => 'Co',
757 'Unassigned' => 'Cn',
761 # Write out the virtual Is mappings.
767 # Read in the special cases.
772 if (open(my $SpecCase, "SpecCase.txt")) {
773 while (<$SpecCase>) {
774 next unless /^[0-9A-Fa-f]+;/;
778 my ($code, $lower, $title, $upper, $condition) = split(/\s*;\s*/);
780 if ($condition) { # not implemented yet
781 print "# SKIPPING $_\n";
785 # Wait until all the special cases have been read since
786 # they are not listed in numeric order.
788 push @{$Case{Lower}}, [ $ix, $code, $lower ];
789 push @{$Case{Title}}, [ $ix, $code, $title ];
790 push @{$Case{Upper}}, [ $ix, $code, $upper ];
793 die "$0: SpecCase.txt: $!\n";
796 # Now write out the special cases properties in their code point order.
797 # Prepend them to the To/{Upper,Lower,Title}.pl.
799 for my $case (qw(Lower Title Upper)) {
800 my $NormalCase = do "To/$case.pl" || die "$0: To/$case.pl: $!\n";
801 if (open(my $Case, ">To/$case.pl")) {
805 %utf8::ToSpec$case = (
807 for my $prop (sort { $a->[0] <=> $b->[0] } @{$Case{$case}}) {
808 my ($ix, $code, $to) = @$prop;
810 join "", map { sprintf "\\x{%s}", $_ } split ' ', $to;
811 printf $Case qq['%04X' => "$tostr",\n], $ix;
818 print $Case $NormalCase;
821 die "$0: To/$case.txt: $!\n";
826 # Read in the case foldings.
828 # We will do full case folding, C + F + I (see CaseFold.txt).
831 if (open(my $CaseFold, "CaseFold.txt")) {
835 while (<$CaseFold>) {
836 next unless /^([0-9A-Fa-f]+)\s*;\s*([CFI])\s*;\s*([0-9A-Fa-f]+(?: [0-9A-Fa-f]+)*)\s*;/;
838 my ($code, $status, $fold) = ($1, $2, $3);
840 if ($status eq 'C') { # Common: one-to-one folding
841 append(\@Fold, $code, $fold);
842 } else { # F: full, or I: dotted uppercase I -> dotless lowercase I
843 $Fold{hex($code)} = $fold;
847 flush(\@Fold, "To/Fold.pl");
850 # Prepend the special foldings to the common foldings.
853 my $CommonFold = do "To/Fold.pl" || die "$0: To/Fold.pl: $!\n";
854 if (open(my $Fold, ">To/Fold.pl")) {
858 %utf8::ToSpecFold = (
860 for my $code (sort { $a <=> $b } keys %Fold) {
862 join "", map { sprintf "\\x{%s}", $_ } split ' ', $Fold{$code};
863 printf $Fold qq['%04X' => "$foldstr",\n], $code;
870 print $Fold $CommonFold;
873 die "$0: To/Fold.pl: $!\n";
876 die "$0: CaseFold.txt: $!\n";