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]/ || $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)
152 if $cat =~ /^Z/ || $code =~ /^(?:0085|2028|2029)$/ ||
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 # No append() here since since several codes may map into one.
238 push @{$To{Upper}}, [ $code, $code, $upper ] if $upper;
239 push @{$To{Lower}}, [ $code, $code, $lower ] if $lower;
240 push @{$To{Title}}, [ $code, $code, $title ] if $title;
242 append($To{Digit} ||= [], $code, $decimal) if $decimal;
244 append(\@Bidi, $code, $bidi);
245 append($Bidi{$bidi} ||= [], $code);
247 append(\@Comb, $code, $comb) if $comb;
250 append(\@Deco, $code, $deco);
251 if ($deco =~/^<(\w+)>/) {
252 append($Deco{Compat} ||= [], $code);
253 append($DC{$1} ||= [], $code);
255 append($Deco{Canon} ||= [], $code);
259 append(\@Number, $code, $number) if $number;
261 append(\@Mirrored, $code) if $mirrored eq "Y";
265 check_no_characters(sprintf("%X", $LastUnicodeCodepoint + 1));
267 flush(\@Name, "Name.pl");
269 foreach my $cat (sort keys %Cat) {
270 flush($Cat{$cat}, "Is/$cat.pl");
273 foreach my $to (sort keys %To) {
274 flush($To{$to}, "To/$to.pl");
277 flush(\@Bidi, "Bidirectional.pl");
278 foreach my $bidi (sort keys %Bidi) {
279 flush($Bidi{$bidi}, "Is/Bidi$bidi.pl");
282 flush(\@Comb, "CombiningClass.pl");
284 flush(\@Deco, "Decomposition.pl");
285 foreach my $deco (sort keys %Deco) {
286 flush($Deco{$deco}, "Is/Deco$deco.pl");
288 foreach my $dc (sort keys %DC) {
289 flush($DC{$dc}, "Is/DC$dc.pl");
292 flush(\@Number, "Number.pl");
294 flush(\@Mirrored, "Is/Mirrored.pl");
296 die "$0: Unicode.txt: $!\n";
299 # The general cateory can be written out already now.
301 flush(\@General, "Category.pl");
304 # Read in the LineBrk.txt.
307 if (open(my $LineBrk, "LineBrk.txt")) {
312 next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(\w+)/;
314 my ($first, $last, $lbrk) = ($1, $2, $3);
316 append(\@Lbrk, $first, $lbrk);
317 append($Lbrk{$lbrk} ||= [], $first);
319 extend(\@Lbrk, $last);
320 extend($Lbrk{$lbrk}, $last);
324 flush(\@Lbrk, "Lbrk.pl");
325 foreach my $lbrk (sort keys %Lbrk) {
326 flush($Lbrk{$lbrk}, "Is/Lbrk$lbrk.pl");
329 die "$0: LineBrk.txt: $!\n";
333 # Read in the ArabShap.txt.
336 if (open(my $ArabShap, "ArabShap.txt")) {
340 while (<$ArabShap>) {
341 next unless /^[0-9A-Fa-f]+;/;
344 my ($code, $name, $link, $linkgroup) = split(/\s*;\s*/);
346 append(\@ArabLink, $code, $link);
347 append(\@ArabLinkGroup, $code, $linkgroup);
350 flush(\@ArabLink, "ArabLink.pl");
351 flush(\@ArabLinkGroup, "ArabLnkGrp.pl");
353 die "$0: ArabShap.txt: $!\n";
357 # Read in the Jamo.txt.
360 if (open(my $Jamo, "Jamo.txt")) {
364 next unless /^([0-9A-Fa-f]+)\s*;\s*(\w*)/;
366 my ($code, $short) = ($1, $2);
368 append(\@Short, $code, $short);
371 flush(\@Short, "JamoShort.pl");
373 die "$0: Jamo.txt: $!\n";
377 # Read in the Scripts.txt.
382 if (open(my $Scripts, "Scripts.txt")) {
384 next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(.+?)\s*\#/;
386 # Wait until all the scripts have been read since
387 # they are not listed in numeric order.
388 push @Scripts, [ hex($1), $1, $2, $3 ];
391 die "$0: Scripts.txt: $!\n";
394 # Now append the scripts properties in their code point order.
399 for my $script (sort { $a->[0] <=> $b->[0] } @Scripts) {
400 my ($code, $first, $last, $name) = @$script;
401 append($Scripts, $first, $name);
402 append($Script{$name} ||= [], $first, $name);
404 extend($Scripts, $last);
405 extend($Script{$name}, $last);
407 unless (defined $In{$name}) {
408 $InScript{$InId} = $name;
409 $In{$name} = $InId++;
410 $InIn{$name} = $Script{$name};
414 # Scripts.pl can be written out already now.
416 flush(\@Scripts, "Scripts.pl");
418 # Common is everything not explicitly assigned to a Script
420 $In{Common} = $InId++;
421 my $Common = inverse($Scripts);
422 $InIn{Common} = $Common;
425 # Read in the Blocks.txt.
431 if (open(my $Blocks, "Blocks.txt")) {
433 next unless /^([0-9A-Fa-f]+)\.\.([0-9A-Fa-f]+)\s*;\s*(.+?)\s*$/;
435 my ($first, $last, $name) = ($1, $2, $3);
436 my $origname = $name;
438 # If there's a naming conflict (the script names are
439 # in uppercase), the name of the block has " Block"
442 $pat =~ s/([- _])/(?:[-_]|\\s+)?/g;
443 for my $i (values %InScript) {
444 if ($i =~ /^$pat$/i) {
450 append(\@Blocks, $first, $name);
451 append($Blocks{$name} ||= [], $first, $name);
453 extend(\@Blocks, $last);
454 extend($Blocks{$name}, $last);
456 unless (defined $In{$name}) {
457 $InBlock{$InId} = $origname;
458 $In{$name} = $InId++;
459 $InIn{$name} = $Blocks{$name};
463 die "$0: Blocks.txt: $!\n";
466 # Blocks.pl can be written out already now.
468 flush(\@Blocks, "Blocks.pl");
471 # Read in the PropList.txt. It contains extended properties not
472 # listed in the Unicode.txt, such as 'Other_Alphabetic':
473 # alphabetic but not of the general category L; many modifiers
474 # belong to this extended property category: while they are not
475 # alphabets, they are alphabetic in nature.
480 if (open(my $Props, "PropList.txt")) {
482 next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(.+?)\s*\#/;
484 # Wait until all the extended properties have been read since
485 # they are not listed in numeric order.
486 push @Props, [ hex($1), $1, $2, $3 ];
489 die "$0: PropList.txt: $!\n";
492 # Now append the extended properties in their code point order.
497 for my $prop (sort { $a->[0] <=> $b->[0] } @Props) {
498 my ($code, $first, $last, $name) = @$prop;
499 append($Props, $first, $name);
500 append($Prop{$name} ||= [], $first, $name);
502 extend($Props, $last);
503 extend($Prop{$name}, $last);
505 unless (defined $In{$name}) {
506 $In{$name} = $InId++;
507 $InIn{$name} = $Prop{$name};
511 # Assigned is everything not Cn
513 $In{Assigned} = $InId++;
514 my $Assigned = inverse($Cat{Cn});
515 $InIn{Assigned} = $Assigned;
517 # Unassigned is everything not Assigned
519 $In{Unassigned} = $InId++;
520 my $Unassigned = $Cat{Cn};
521 $InIn{Unassigned} = $Unassigned;
523 # Unassigned is everything not Assigned
524 sub merge_general_and_extended {
525 my ($name, $general, $extended) = @_;
529 map { pop @{$_}; $_ }
530 sort { $a->[2] <=> $b->[2] }
531 map { [ $_->[0], $_->[1], hex($_->[0]) ] }
533 map { ref $_ ? @$_ : $_ }
534 @Cat {ref $general ? @$general : $general } :
537 map { ref $_ ? @$_ : $_ }
538 @Prop{ref $extended ? @$extended : $extended} :
541 $In{$name} = $InId++;
542 $InIn{$name} = $merged;
547 # Alphabetic is L and Other_Alphabetic.
550 merge_general_and_extended('Alphabetic', 'L', 'Other_Alphabetic');
552 # Lowercase is Ll and Other_Lowercase.
555 merge_general_and_extended('Lowercase', 'Ll', 'Other_Lowercase');
557 # Uppercase is Lu and Other_Uppercase.
560 merge_general_and_extended('Uppercase', 'Lu', 'Other_Uppercase');
562 # Math is Sm and Other_Math.
565 merge_general_and_extended('Math', 'Sm', 'Other_Math');
567 # Lampersand is Ll, Lu, and Lt.
570 merge_general_and_extended('Lampersand', [ qw(Ll Lu Lt) ]);
572 # ID_Start is Ll, Lu, Lt, Lm, Lo, and Nl.
575 merge_general_and_extended('ID_Start', [ qw(Ll Lu Lt Lm Lo Nl) ]);
577 # ID_Continue is ID_Start, Mn, Mc, Nd, and Pc.
580 merge_general_and_extended('ID_Continue', [ qw(Ll Lu Lt Lm Lo Nl
588 my $Any = [ [ 0, sprintf("%04X", $LastUnicodeCodepoint) ] ];
599 # mapping() will be used to write out the In and Is virtual mappings.
603 my ($map, $name) = @_;
605 if (open(my $fh, ">$name.pl")) {
609 # The %pat will hold a hash that maps the first two
610 # lowercased letters of a class to a 'fuzzified' regular
611 # expression that points to the real mapping.
615 # But first write out the offical name to real name
616 # (the filename) mapping.
622 for my $i (sort { lc $a cmp lc $b } keys %$map) {
624 # Here is the 'fuzzification': accept any space,
625 # dash, or underbar where in the official name
626 # there is space or a dash (or underbar, but
628 $pat =~ s/([- _])/(?:[-_]|\\s+)?/g;
629 # The prefix length of 2 is enough spread,
630 # and besides, we have 'Yi' as an In category.
631 push @{$pat{lc(substr($i, 0, 2))}}, [ $i, $pat ];
632 printf $fh "%-45s => '$map->{$i}',\n", "'$i'";
638 # Now write out the %pat mapping.
644 foreach my $prefix (sort keys %pat) {
645 print $fh "'$prefix' => {\n";
646 foreach my $ipat (@{$pat{$prefix}}) {
647 my ($i, $pat) = @$ipat;
648 print $fh "\t'$pat' => '$map->{$i}',\n";
658 die "$0: $name.pl: $!\n";
663 # Write out the virtual In mappings.
669 # Append the InScript and InBlock mappings.
670 # These are needed only if Script= and Block= syntaxes are used.
673 if (open(my $In, ">>In.pl")) {
679 for my $i (sort { $a <=> $b } keys %InScript) {
680 printf $In "%4d => '$InScript{$i}',\n", $i;
691 for my $i (sort { $a <=> $b } keys %InBlock) {
692 printf $In "%4d => '$InBlock{$i}',\n", $i;
698 die "$0: In.pl: $!\n";
702 # Write out the real In mappings
703 # (the In.pl written out just above has the virtual In mappings)
706 foreach my $in (sort { $In{$a} <=> $In{$b} } keys %In) {
707 flush($InIn{$in}, "In/$In{$in}.pl");
711 # The mapping from General Category long forms to short forms is
712 # currently hardwired here since no simple data file in the UCD
713 # seems to do that. Unicode 3.2 will assumedly correct this.
718 'Uppercase_Letter' => 'Lu',
719 'Lowercase_Letter' => 'Ll',
720 'Titlecase_Letter' => 'Lt',
721 'Modifier_Letter' => 'Lm',
722 'Other_Letter' => 'Lo',
725 'Non_Spacing_Mark' => 'Mn',
726 'Spacing_Mark' => 'Mc',
727 'Enclosing_Mark' => 'Me',
730 'Space_Separator' => 'Zs',
731 'Line_Separator' => 'Zl',
732 'Paragraph_Separator' => 'Zp',
735 'Decimal_Number' => 'Nd',
736 'Letter_Number' => 'Nl',
737 'Other_Number' => 'No',
739 'Punctuation' => 'P',
740 'Connector_Punctuation' => 'Pc',
741 'Dash_Punctuation' => 'Pd',
742 'Open_Punctuation' => 'Ps',
743 'Close_Punctuation' => 'Pe',
744 'Initial_Punctuation' => 'Pi',
745 'Final_Punctuation' => 'Pf',
746 'Other_Punctuation' => 'Po',
749 'Math_Symbol' => 'Sm',
750 'Currency_Symbol' => 'Sc',
751 'Modifier_Symbol' => 'Sk',
752 'Other_Symbol' => 'So',
758 'Private Use' => 'Co',
759 'Unassigned' => 'Cn',
763 # Write out the virtual Is mappings.
769 # Read in the special cases.
774 if (open(my $SpecCase, "SpecCase.txt")) {
775 while (<$SpecCase>) {
776 next unless /^[0-9A-Fa-f]+;/;
780 my ($code, $lower, $title, $upper, $condition) = split(/\s*;\s*/);
782 if ($condition) { # not implemented yet
783 print "# SKIPPING $_\n";
787 # Wait until all the special cases have been read since
788 # they are not listed in numeric order.
790 push @{$Case{Lower}}, [ $ix, $code, $lower ];
791 push @{$Case{Title}}, [ $ix, $code, $title ];
792 push @{$Case{Upper}}, [ $ix, $code, $upper ];
795 die "$0: SpecCase.txt: $!\n";
798 # Now write out the special cases properties in their code point order.
799 # Prepend them to the To/{Upper,Lower,Title}.pl.
801 for my $case (qw(Lower Title Upper)) {
802 my $NormalCase = do "To/$case.pl" || die "$0: To/$case.pl: $!\n";
803 if (open(my $Case, ">To/$case.pl")) {
807 %utf8::ToSpec$case = (
809 for my $prop (sort { $a->[0] <=> $b->[0] } @{$Case{$case}}) {
810 my ($ix, $code, $to) = @$prop;
812 join "", map { sprintf "\\x{%s}", $_ } split ' ', $to;
813 printf $Case qq['%04X' => "$tostr",\n], $ix;
820 print $Case $NormalCase;
823 die "$0: To/$case.txt: $!\n";
828 # Read in the case foldings.
830 # We will do full case folding, C + F + I (see CaseFold.txt).
833 if (open(my $CaseFold, "CaseFold.txt")) {
837 while (<$CaseFold>) {
838 # Skip status 'S', simple case folding
839 next unless /^([0-9A-Fa-f]+)\s*;\s*([CFI])\s*;\s*([0-9A-Fa-f]+(?: [0-9A-Fa-f]+)*)\s*;/;
841 my ($code, $status, $fold) = ($1, $2, $3);
843 if ($status eq 'C') { # Common: one-to-one folding
844 # No append() since several codes may fold into one.
845 push @Fold, [ $code, $code, $fold ];
846 } else { # F: full, or I: dotted uppercase I -> dotless lowercase I
847 $Fold{hex($code)} = $fold;
851 flush(\@Fold, "To/Fold.pl");
854 # Prepend the special foldings to the common foldings.
857 my $CommonFold = do "To/Fold.pl" || die "$0: To/Fold.pl: $!\n";
858 if (open(my $Fold, ">To/Fold.pl")) {
862 %utf8::ToSpecFold = (
864 for my $code (sort { $a <=> $b } keys %Fold) {
866 join "", map { sprintf "\\x{%s}", $_ } split ' ', $Fold{$code};
867 printf $Fold qq['%04X' => "$foldstr",\n], $code;
874 print $Fold $CommonFold;
877 die "$0: To/Fold.pl: $!\n";
880 die "$0: CaseFold.txt: $!\n";