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];
37 if ($table->[0]->[0]) {
38 $last = hex($table->[0]->[0]);
39 push @$inverse, [ "0000",
40 sprintf("%04X", $last - 1) ];
42 for my $i (0..$#$table-1) {
43 $first = defined $table->[$i ]->[1] ?
44 hex($table->[$i ]->[1]) : 0;
45 $last = defined $table->[$i + 1]->[0] ?
46 hex($table->[$i + 1]->[0]) : $first;
47 push @$inverse, [ sprintf("%04X", $first + 1),
48 sprintf("%04X", $last - 1) ]
49 unless $first + 1 == $last;
58 # !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
59 # This file is built by $0 from e.g. Unicode.txt.
60 # Any changes made here will be lost!
81 my ($table, $file) = @_;
83 if (open(my $fh, ">$file")) {
87 print $fh $i->[0], "\t",
88 $i->[1] ne $i->[0] ? $i->[1] : "", "\t",
89 defined $i->[2] ? $i->[2] : "", "\n";
94 die "$0: $file: $!\n";
99 # The %In contains the mapping of the script/block name into a number.
110 # Read in the Unicode.txt, the main Unicode database.
118 my ($Name, $GeneralH, $GeneralA, $Cat,
119 $name, $cat, $code, $op) = @_;
121 $op->($Name, $code, $name);
122 $op->($GeneralA, $code, $cat);
124 $op->($GeneralH->{$name} ||= [], $code, $name);
126 $op->($Cat->{$cat} ||= [], $code);
127 $op->($Cat->{substr($cat, 0, 1)}
129 # 005F: SPACING UNDERSCORE
130 $op->($Cat->{Word} ||= [], $code)
131 if $cat =~ /^[LMN]/ or $code eq "005F";
132 $op->($Cat->{Alnum} ||= [], $code)
134 $op->($Cat->{Alpha} ||= [], $code)
136 # 0009: HORIZONTAL TABULATION
138 # 000B: VERTICAL TABULATION
140 # 000D: CARRIAGE RETURN
142 $op->($Cat->{Space} ||= [], $code)
144 $code =~ /^(0009|000A|000B|000C|000D)$/;
145 $op->($Cat->{SpacePerl} ||= [], $code)
147 $code =~ /^(0009|000A|000C|000D)$/;
148 $op->($Cat->{Blank} ||= [], $code)
149 if $code =~ /^(0020|0009)$/ ||
151 $op->($Cat->{Digit} ||= [], $code) if $cat eq "Nd";
152 $op->($Cat->{Upper} ||= [], $code) if $cat eq "Lu";
153 $op->($Cat->{Lower} ||= [], $code) if $cat eq "Ll";
154 $op->($Cat->{Title} ||= [], $code) if $cat eq "Lt";
155 $op->($Cat->{ASCII} ||= [], $code) if $code le "007F";
156 $op->($Cat->{Cntrl} ||= [], $code) if $cat =~ /^C/;
157 $op->($Cat->{Graph} ||= [], $code) if $cat =~ /^([LMNPS]|Co)/;
158 $op->($Cat->{Print} ||= [], $code) if $cat =~ /^([LMNPS]|Co|Zs)/;
159 $op->($Cat->{Punct} ||= [], $code) if $cat =~ /^P/;
160 # 003[0-9]: DIGIT ZERO..NINE, 00[46][1-6]: A..F, a..f
161 $op->($Cat->{XDigit} ||= [], $code)
162 if $code =~ /^00(3[0-9]|[46][1-6])$/;
166 if (open(my $Unicode, "Unicode.txt")) {
178 next unless /^[0-9A-Fa-f]+;/;
181 my ($code, $name, $cat, $comb, $bidi, $deco,
182 $decimal, $digit, $number,
183 $mirrored, $unicode10, $comment,
184 $upper, $lower, $title) = split(/\s*;\s*/);
186 if ($name =~ /^<(.+), (First|Last)>$/) {
188 gencat(\@Name, \%General, \@General, \%Cat,
190 $2 eq 'First' ? \&append : \&extend);
191 unless (defined $In{$name}) {
192 $In{$name} = $InId++;
193 $InIn{$name} = $General{$name};
197 gencat(\@Name, \%General, \@General, \%Cat,
198 $name, $cat, $code, \&append);
200 append($To{Upper} ||= [], $code, $upper) if $upper;
201 append($To{Lower} ||= [], $code, $lower) if $lower;
202 append($To{Title} ||= [], $code, $title) if $title;
203 append($To{Digit} ||= [], $code, $decimal) if $decimal;
205 append(\@Bidi, $code, $bidi);
206 append($Bidi{$bidi} ||= [], $code);
208 append(\@Comb, $code, $comb) if $comb;
211 append(\@Deco, $code, $deco);
212 if ($deco =~/^<(\w+)>/) {
213 append($Deco{Compat} ||= [], $code);
214 append($DC{$1} ||= [], $code);
216 append($Deco{Canon} ||= [], $code);
220 append(\@Number, $code, $number) if $number;
222 append(\@Mirrored, $code) if $mirrored eq "Y";
226 flush(\@Name, "Name.pl");
228 foreach my $cat (sort keys %Cat) {
229 flush($Cat{$cat}, "Is/$cat.pl");
232 foreach my $to (sort keys %To) {
233 flush($To{$to}, "To/$to.pl");
236 flush(\@Bidi, "Bidirectional.pl");
237 foreach my $bidi (sort keys %Bidi) {
238 flush($Bidi{$bidi}, "Is/Bidi$bidi.pl");
241 flush(\@Comb, "CombiningClass.pl");
243 flush(\@Deco, "Decomposition.pl");
244 foreach my $deco (sort keys %Deco) {
245 flush($Deco{$deco}, "Is/Deco$deco.pl");
247 foreach my $dc (sort keys %DC) {
248 flush($DC{$dc}, "Is/DC$dc.pl");
251 flush(\@Number, "Number.pl");
253 flush(\@Mirrored, "Is/Mirrored.pl");
255 die "$0: Unicode.txt: $!\n";
258 # The general cateory can be written out already now.
260 flush(\@General, "Category.pl");
263 # Read in the LineBrk.txt.
266 if (open(my $LineBrk, "LineBrk.txt")) {
271 next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(\w+)/;
273 my ($first, $last, $lbrk) = ($1, $2, $3);
275 append(\@Lbrk, $first, $lbrk);
276 append($Lbrk{$lbrk} ||= [], $first);
278 extend(\@Lbrk, $last);
279 extend($Lbrk{$lbrk}, $last);
283 flush(\@Lbrk, "Lbrk.pl");
284 foreach my $lbrk (sort keys %Lbrk) {
285 flush($Lbrk{$lbrk}, "Is/Lbrk$lbrk.pl");
288 die "$0: LineBrk.txt: $!\n";
292 # Read in the ArabShap.txt.
295 if (open(my $ArabShap, "ArabShap.txt")) {
299 while (<$ArabShap>) {
300 next unless /^[0-9A-Fa-f]+;/;
303 my ($code, $name, $link, $linkgroup) = split(/\s*;\s*/);
305 append(\@ArabLink, $code, $link);
306 append(\@ArabLinkGroup, $code, $linkgroup);
309 flush(\@ArabLink, "ArabLink.pl");
310 flush(\@ArabLinkGroup, "ArabLnkGrp.pl");
312 die "$0: ArabShap.txt: $!\n";
316 # Read in the Jamo.txt.
319 if (open(my $Jamo, "Jamo.txt")) {
323 next unless /^([0-9A-Fa-f]+)\s*;\s*(\w*)/;
325 my ($code, $short) = ($1, $2);
327 append(\@Short, $code, $short);
330 flush(\@Short, "JamoShort.pl");
332 die "$0: Jamo.txt: $!\n";
336 # Read in the Scripts.txt.
341 if (open(my $Scripts, "Scripts.txt")) {
343 next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(.+?)\s*\#/;
345 # Wait until all the scripts have been read since
346 # they are not listed in numeric order.
347 push @Scripts, [ hex($1), $1, $2, $3 ];
350 die "$0: Scripts.txt: $!\n";
353 # Now append the scripts properties in their code point order.
358 for my $script (sort { $a->[0] <=> $b->[0] } @Scripts) {
359 my ($code, $first, $last, $name) = @$script;
360 append($Scripts, $first, $name);
361 append($Script{$name} ||= [], $first, $name);
363 extend($Scripts, $last);
364 extend($Script{$name}, $last);
366 unless (defined $In{$name}) {
367 $InScript{$InId} = $name;
368 $In{$name} = $InId++;
369 $InIn{$name} = $Script{$name};
373 # Scripts.pl can be written out already now.
375 flush(\@Scripts, "Scripts.pl");
377 # Common is everything not explicitly assigned to a Script
379 $In{Common} = $InId++;
380 my $Common = inverse($Scripts);
381 $InIn{Common} = $Common;
384 # Read in the Blocks.txt.
390 if (open(my $Blocks, "Blocks.txt")) {
392 next unless /^([0-9A-Fa-f]+)\.\.([0-9A-Fa-f]+)\s*;\s*(.+?)\s*$/;
394 my ($first, $last, $name) = ($1, $2, $3);
395 my $origname = $name;
397 # If there's a naming conflict (the script names are
398 # in uppercase), the name of the block has " Block"
401 $pat =~ s/([- _])/(?:[-_]|\\s+)?/g;
402 for my $i (values %InScript) {
403 if ($i =~ /^$pat$/i) {
409 append(\@Blocks, $first, $name);
410 append($Blocks{$name} ||= [], $first, $name);
412 extend(\@Blocks, $last);
413 extend($Blocks{$name}, $last);
415 unless (defined $In{$name}) {
416 $InBlock{$InId} = $origname;
417 $In{$name} = $InId++;
418 $InIn{$name} = $Blocks{$name};
422 die "$0: Blocks.txt: $!\n";
425 # Blocks.pl can be written out already now.
427 flush(\@Blocks, "Blocks.pl");
430 # Read in the PropList.txt. It contains extended properties not
431 # listed in the Unicode.txt, such as 'Other_Alphabetic':
432 # alphabetic but not of the general category L; many modifiers
433 # belong to this extended property category: while they are not
434 # alphabets, they are alphabetic in nature.
439 if (open(my $Props, "PropList.txt")) {
441 next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(.+?)\s*\#/;
443 # Wait until all the extended properties have been read since
444 # they are not listed in numeric order.
445 push @Props, [ hex($1), $1, $2, $3 ];
448 die "$0: PropList.txt: $!\n";
451 # Now append the extended properties in their code point order.
456 for my $prop (sort { $a->[0] <=> $b->[0] } @Props) {
457 my ($code, $first, $last, $name) = @$prop;
458 append($Props, $first, $name);
459 append($Prop{$name} ||= [], $first, $name);
461 extend($Props, $last);
462 extend($Prop{$name}, $last);
464 unless (defined $In{$name}) {
465 $In{$name} = $InId++;
466 $InIn{$name} = $Prop{$name};
470 # Assigned is everything not Cn aka Noncharacter_Code_Point
472 $In{Assigned} = $InId++;
473 my $Assigned = inverse($Prop{Noncharacter_Code_Point});
474 $InIn{Assigned} = $Assigned;
476 sub merge_general_and_extended {
477 my ($name, $general, $extended) = @_;
481 map { pop @{$_}; $_ }
482 sort { $a->[2] <=> $b->[2] }
483 map { [ $_->[0], $_->[1], hex($_->[0]) ] }
485 map { ref $_ ? @$_ : $_ }
486 @Cat {ref $general ? @$general : $general } :
489 map { ref $_ ? @$_ : $_ }
490 @Prop{ref $extended ? @$extended : $extended} :
493 $In{$name} = $InId++;
494 $InIn{$name} = $merged;
499 # Alphabetic is L and Other_Alphabetic.
502 merge_general_and_extended('Alphabetic', 'L', 'Other_Alphabetic');
504 # Lowercase is Ll and Other_Lowercase.
507 merge_general_and_extended('Lowercase', 'Ll', 'Other_Lowercase');
509 # Uppercase is Lu and Other_Uppercase.
512 merge_general_and_extended('Uppercase', 'Lu', 'Other_Uppercase');
514 # Math is Sm and Other_Math.
517 merge_general_and_extended('Math', 'Sm', 'Other_Math');
519 # Lampersand is Ll, Lu, and Lt.
522 merge_general_and_extended('Lampersand', [ qw(Ll Lu Lt) ]);
524 # ID_Start is Ll, Lu, Lt, Lm, Lo, and Nl.
527 merge_general_and_extended('ID_Start', [ qw(Ll Lu Lt Lm Lo Nl) ]);
529 # ID_Continue is ID_Start, Mn, Mc, Nd, and Pc.
532 merge_general_and_extended('ID_Continue', [ qw(Ll Lu Lt Lm Lo Nl
540 my $Any = [ [ 0, sprintf("%04X", $LastUnicodeCodepoint) ] ];
544 # mapping() will be used to write out the In and Is virtual mappings.
548 my ($map, $name) = @_;
550 if (open(my $fh, ">$name.pl")) {
554 # The %pat will hold a hash that maps the first two
555 # lowercased letters of a class to a 'fuzzified' regular
556 # expression that points to the real mapping.
560 # But first write out the offical name to real name
561 # (the filename) mapping.
567 for my $i (sort { lc $a cmp lc $b } keys %$map) {
569 # Here is the 'fuzzification': accept any space,
570 # dash, or underbar where in the official name
571 # there is space or a dash (or underbar, but
573 $pat =~ s/([- _])/(?:[-_]|\\s+)?/g;
574 # The prefix length of 2 is enough spread,
575 # and besides, we have 'Yi' as an In category.
576 push @{$pat{lc(substr($i, 0, 2))}}, [ $i, $pat ];
577 printf $fh "%-45s => '$map->{$i}',\n", "'$i'";
583 # Now write out the %pat mapping.
589 foreach my $prefix (sort keys %pat) {
590 print $fh "'$prefix' => {\n";
591 foreach my $ipat (@{$pat{$prefix}}) {
592 my ($i, $pat) = @$ipat;
593 print $fh "\t'$pat' => '$map->{$i}',\n";
603 die "$0: $name.pl: $!\n";
608 # Write out the virtual In mappings.
614 # Append the InScript and InBlock mappings.
615 # These are needed only if Script= and Block= syntaxes are used.
618 if (open(my $In, ">>In.pl")) {
624 for my $i (sort { $a <=> $b } keys %InScript) {
625 printf $In "%4d => '$InScript{$i}',\n", $i;
636 for my $i (sort { $a <=> $b } keys %InBlock) {
637 printf $In "%4d => '$InBlock{$i}',\n", $i;
643 die "$0: In.pl: $!\n";
646 # Easy low-calorie cheat.
648 copy("In/$In{Noncharacter_Code_Point}.pl", "Is/Cn.pl");
651 # Write out the real In mappings
652 # (the In.pl written out just above has the virtual In mappings)
655 foreach my $in (sort { $In{$a} <=> $In{$b} } keys %In) {
656 flush($InIn{$in}, "In/$In{$in}.pl");
660 # The mapping from General Category long forms to short forms is
661 # currently hardwired here since no simple data file in the UCD
662 # seems to do that. Unicode 3.2 will assumedly correct this.
667 'Uppercase_Letter' => 'Lu',
668 'Lowercase_Letter' => 'Ll',
669 'Titlecase_Letter' => 'Lt',
670 'Modifier_Letter' => 'Lm',
671 'Other_Letter' => 'Lo',
674 'Non_Spacing_Mark' => 'Mn',
675 'Spacing_Mark' => 'Mc',
676 'Enclosing_Mark' => 'Me',
679 'Space_Separator' => 'Zs',
680 'Line_Separator' => 'Zl',
681 'Paragraph_Separator' => 'Zp',
684 'Decimal_Number' => 'Nd',
685 'Letter_Number' => 'Nl',
686 'Other_Number' => 'No',
688 'Punctuation' => 'P',
689 'Connector_Punctuation' => 'Pc',
690 'Dash_Punctuation' => 'Pd',
691 'Open_Punctuation' => 'Ps',
692 'Close_Punctuation' => 'Pe',
693 'Initial_Punctuation' => 'Pi',
694 'Final_Punctuation' => 'Pf',
695 'Other_Punctuation' => 'Po',
698 'Math_Symbol' => 'Sm',
699 'Currency_Symbol' => 'Sc',
700 'Modifier_Symbol' => 'Sk',
701 'Other_Symbol' => 'So',
707 'Private Use' => 'Co',
708 'Unassigned' => 'Cn',
712 # Write out the virtual Is mappings.
718 # Read in the special cases.
723 if (open(my $SpecCase, "SpecCase.txt")) {
724 while (<$SpecCase>) {
725 next unless /^[0-9A-Fa-f]+;/;
729 my ($code, $lower, $title, $upper, $condition) = split(/\s*;\s*/);
731 if ($condition) { # not implemented yet
732 print "# SKIPPING $_\n";
736 # Wait until all the special cases have been read since
737 # they are not listed in numeric order.
739 push @{$Case{Lower}}, [ $ix, $code, $lower ];
740 push @{$Case{Title}}, [ $ix, $code, $title ];
741 push @{$Case{Upper}}, [ $ix, $code, $upper ];
744 die "$0: SpecCase.txt: $!\n";
747 # Now write out the special cases properties in their code point order.
748 # Prepend them to the To/{Upper,Lower,Title}.pl.
750 for my $case (qw(Lower Title Upper)) {
751 my $NormalCase = do "To/$case.pl" || die "$0: To/$case.pl: $!\n";
752 if (open(my $Case, ">To/$case.pl")) {
756 %utf8::ToSpec$case = (
758 for my $prop (sort { $a->[0] <=> $b->[0] } @{$Case{$case}}) {
759 my ($ix, $code, $to) = @$prop;
761 join "", map { sprintf "\\x{%s}", $_ } split ' ', $to;
762 printf $Case qq['%04X' => "$tostr",\n], $ix;
769 print $Case $NormalCase;
772 die "$0: To/$case.txt: $!\n";
777 # Read in the case foldings.
779 # We will do full case folding, C + F + I (see CaseFold.txt).
782 if (open(my $CaseFold, "CaseFold.txt")) {
786 while (<$CaseFold>) {
787 next unless /^([0-9A-Fa-f]+)\s*;\s*([CFI])\s*;\s*([0-9A-Fa-f]+(?: [0-9A-Fa-f]+)*)\s*;/;
789 my ($code, $status, $fold) = ($1, $2, $3);
791 if ($status eq 'C') { # Common: one-to-one folding
792 append(\@Fold, $code, $fold);
793 } else { # F: full, or I: dotted uppercase I -> dotless lowercase I
794 $Fold{hex($code)} = $fold;
798 flush(\@Fold, "To/Fold.pl");
801 # Prepend the special foldings to the common foldings.
804 my $CommonFold = do "To/Fold.pl" || die "$0: To/Fold.pl: $!\n";
805 if (open(my $Fold, ">To/Fold.pl")) {
809 %utf8::ToSpecFold = (
811 for my $code (sort { $a <=> $b } keys %Fold) {
813 join "", map { sprintf "\\x{%s}", $_ } split ' ', $Fold{$code};
814 printf $Fold qq['%04X' => "$foldstr",\n], $code;
821 print $Fold $CommonFold;
824 die "$0: To/Fold.pl: $!\n";
827 die "$0: CaseFold.txt: $!\n";