5 ## mktables -- create the runtime Perl Unicode files (lib/unicore/**/*.pl)
6 ## from the Unicode database files (lib/unicore/*.txt).
20 my $arg = shift @ARGV;
23 } elsif ($arg eq '-q') {
26 die "usage: $0 [-v|-q]";
30 my $LastUnicodeCodepoint = 0x10FFFF; # As of Unicode 3.1.1.
33 # !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
34 # This file is built by $0 from e.g. Unicode.txt.
35 # Any changes made here will be lost!
40 ## The main datastructure (a "Table") represents a set of code points that
41 ## are part of a particular quality (that are part of \pL, \p{InGreek},
42 ## etc.). They are kept as ranges of code points (starting and ending of
45 ## For example, a range ASCII LETTERS would be represented as:
46 ## [ [ 0x41 => 0x5A, 'UPPER' ],
47 ## [ 0x61 => 0x7A, 'LOWER, ] ]
49 sub RANGE_START() { 0 } ## index into range element
50 sub RANGE_END() { 1 } ## index into range element
51 sub RANGE_NAME() { 2 } ## index into range element
53 ## Conceptually, these should really be folded into the 'Table' objects
60 ## Turn something like
68 $name =~ s/(?<![a-z])(\w)/\u$1/g;
74 ## Turn something like
79 sub CanonicalNameForPattern($)
82 $name =~ s/(?<![a-z])(\w)/\u$1/g;
89 ## Associates a property ("Greek", "Lu", "Assigned",...) with a Table.
92 ## New_Prop(In => 'Greek', $Table, Desc => 'Greek Block', Fuzzy => 1);
94 ## Normally, these parameters are set when the Table is created (when the
95 ## Table->New constructor is called), but there are times when it needs to
96 ## be done after-the-fact...)
100 my $Type = shift; ## "Is" or "In";
104 ## remaining args are optional key/val
107 my $Fuzzy = delete $Args{Fuzzy};
108 my $Desc = delete $Args{Desc}; # description
110 $Name = CanonicalNameForPattern($Name) if $Fuzzy;
112 ## sanity check a few args
113 if (%Args or ($Type ne 'Is' and $Type ne 'In') or not ref $Table) {
114 confess "$0: bad args to New_Prop"
117 if (not $TableInfo{$Type}->{$Name})
119 $TableInfo{$Type}->{$Name} = $Table;
120 $TableDesc{$Type}->{$Name} = $Desc;
122 $FuzzyNames{$Type}->{$Name} = $Name;
129 ## Creates a new Table object.
131 ## Args are key/value pairs:
132 ## In => Name -- Name of "In" property to be associated with
133 ## Is => Name -- Name of "Is" property to be associated with
134 ## Fuzzy => Boolean -- True if name can be accessed "fuzzily"
135 ## Desc => String -- Description of the property
137 ## No args are required.
144 my $Table = bless [], $class;
146 my $Fuzzy = delete $Args{Fuzzy};
147 my $Desc = delete $Args{Desc};
149 for my $Type ('Is', 'In')
151 if (my $Name = delete $Args{$Type}) {
152 New_Prop($Type => $Name, $Table, Desc => $Desc, Fuzzy => $Fuzzy);
156 ## shouldn't have any left over
158 confess "$0: bad args to Table->New"
165 ## Returns true if the Table has no code points
169 my $Table = shift; #self
174 ## Returns true if the Table has code points
178 my $Table = shift; #self
183 ## Returns the maximum code point currently in the table.
187 my $Table = shift; #self
188 confess "oops" if $Table->IsEmpty; ## must have code points to have a max
189 return $Table->[-1]->[RANGE_END];
193 ## Replaces the codepoints in the Table with those in the Table given
194 ## as an arg. (NOTE: this is not a "deep copy").
196 sub Table::Replace($$)
198 my $Table = shift; #self
205 ## Given a new code point, make the last range of the Table extend to
206 ## include the new (and all intervening) code points.
210 my $Table = shift; #self
211 my $codepoint = shift;
213 my $PrevMax = $Table->Max;
215 confess "oops ($codepoint <= $PrevMax)" if $codepoint <= $PrevMax;
217 $Table->[-1]->[RANGE_END] = $codepoint;
221 ## Given a code point range start and end (and optional name), blindly
222 ## append them to the list of ranges for the Table.
224 ## NOTE: Code points must be added in strictly ascending numeric order.
226 sub Table::RawAppendRange
228 my $Table = shift; #self
232 $name = "" if not defined $name; ## warning: $name can be "0"
234 push @$Table, [ $start, # RANGE_START
236 $name ]; # RANGE_NAME
240 ## Given a code point (and optional name), add it to the Table.
242 ## NOTE: Code points must be added in strictly ascending numeric order.
246 my $Table = shift; #self
247 my $codepoint = shift;
249 $name = "" if not defined $name; ## warning: $name can be "0"
252 ## If we've already got a range working, and this code point is the next
253 ## one in line, and if the name is the same, just extend the current range.
257 $Table->Max == $codepoint - 1
259 $Table->[-1]->[RANGE_NAME] eq $name)
261 $Table->Extend($codepoint);
265 $Table->RawAppendRange($codepoint, $codepoint, $name);
270 ## Given a code point range starting value and ending value (and name),
271 ## Add the range to teh Table.
273 ## NOTE: Code points must be added in strictly ascending numeric order.
275 sub Table::AppendRange
277 my $Table = shift; #self
281 $name = "" if not defined $name; ## warning: $name can be "0"
283 $Table->Append($start, $name);
284 $Table->Extend($end) if $end > $start;
288 ## Return a new Table that represents all code points not in the Table.
292 my $Table = shift; #self
294 my $New = Table->New();
296 for my $range (@$Table)
298 my $start = $range->[RANGE_START];
299 my $end = $range->[RANGE_END];
300 if ($start-1 >= $max+1) {
301 $New->AppendRange($max+1, $start-1, "");
305 if ($max+1 < $LastUnicodeCodepoint) {
306 $New->AppendRange($max+1, $LastUnicodeCodepoint);
312 ## Merges any number of other tables with $self, returning the new table.
313 ## (existing tables are not modified)
316 ## Args may be Tables, or individual code points (as integers).
318 ## Can be called as either a constructor or a method.
322 shift(@_) if not ref $_[0]; ## if called as a constructor, lose the class
325 ## Accumulate all records from all tables
327 for my $Arg (@Tables)
330 ## arg is a table -- get its ranges
331 push @Records, @$Arg;
333 ## arg is a codepoint, make a range
334 push @Records, [ $Arg, $Arg ]
338 ## sort by range start, with longer ranges coming first.
339 my ($first, @Rest) = sort {
340 ($a->[RANGE_START] <=> $b->[RANGE_START])
342 ($b->[RANGE_END] <=> $b->[RANGE_END])
345 my $New = Table->New();
347 ## Ensuring the first range is there makes the subsequent loop easier
348 $New->AppendRange($first->[RANGE_START],
349 $first->[RANGE_END]);
351 ## Fold in records so long as they add new information.
354 my $start = $set->[RANGE_START];
355 my $end = $set->[RANGE_END];
356 if ($start > $New->Max) {
357 $New->AppendRange($start, $end);
358 } elsif ($end > $New->Max) {
367 ## Given a filename, write a representation of the Table to a file.
368 ## May have an optional comment as a 2nd arg.
372 my $Table = shift; #self
373 my $filename = shift;
376 print "$filename\n" if $Verbose;
378 if (not open(OUT, ">$filename")) {
379 die "$0: can't write $filename: $!\n";
383 if (defined $comment) {
384 $comment =~ s/\s+\Z//;
385 $comment =~ s/^/# /gm;
386 print OUT "#\n$comment\n#\n";
388 print OUT "return <<'END';\n";
390 for my $set (@$Table)
392 my $start = $set->[RANGE_START];
393 my $end = $set->[RANGE_END];
394 my $name = $set->[RANGE_NAME];
396 if ($start == $end) {
397 printf OUT "%04X\t\t%s\n", $start, $name;
399 printf OUT "%04X\t%04X\t%s\n", $start, $end, $name;
407 ###########################################################################
408 ###########################################################################
409 ###########################################################################
414 ## New_Alias(Is => 'All', SameAs => 'Any', Fuzzy => 1);
416 ## The args must be in that order, although the Fuzzy pair may be omitted.
418 ## This creates 'IsAll' as an alias for 'IsAny'
422 my $Type = shift; ## "Is" or "In"
424 my $SameAs = shift; # expecting "SameAs" -- just ignored
427 ## remaining args are optional key/val
430 my $Fuzzy = delete $Args{Fuzzy};
432 ## sanity check a few args
433 if (%Args or ($Type ne 'Is' and $Type ne 'In') or $SameAs ne 'SameAs') {
434 confess "$0: bad args to New_Alias"
437 if (not $TableInfo{$Type}->{$Name}) {
438 confess "$0: don't have orignial $Type => $Name to make alias"
440 if ($TableInfo{$Alias}) {
441 confess "$0: already have original $Type => $Alias; can't make alias";
443 $AliasInfo{$Type}->{$Name} = $Alias;
445 $FuzzyNames{$Type}->{$Alias} = $Name;
451 ## All assigned code points
452 my $Assigned = Table->New(Is => 'Assigned',
453 Desc => "All assigned code points",
456 my $Name = Table->New(); ## all characters, individually by name
457 my $General = Table->New(); ## all characters, grouped by category
462 ## Process Unicode.txt (Categories, etc.)
466 my $Bidi = Table->New();
467 my $Deco = Table->New();
468 my $Comb = Table->New();
469 my $Number = Table->New();
470 my $Mirrored = Table->New(Is => 'Mirrored',
471 Desc => "Mirrored in bidirectional text",
477 $Deco{Canon} = Table->New(Is => 'Canon',
478 Desc => 'Decomposes to multiple characters',
480 $Deco{Compat} = Table->New(Is => 'Compat',
481 Desc => 'Compatible with a more-basic character',
484 ## Initialize Perl-generated categories
485 ## (Categories from Unicode.txt are auto-initialized in gencat)
486 $Cat{Alnum} = Table->New(Is => 'Alnum', Desc => "[[:Alnum:]]", Fuzzy => 0);
487 $Cat{Alpha} = Table->New(Is => 'Alpha', Desc => "[[:Alpha:]]", Fuzzy => 0);
488 $Cat{ASCII} = Table->New(Is => 'ASCII', Desc => "[[:ASCII:]]", Fuzzy => 0);
489 $Cat{Blank} = Table->New(Is => 'Blank', Desc => "[[:Blank:]]", Fuzzy => 0);
490 $Cat{Cntrl} = Table->New(Is => 'Cntrl', Desc => "[[:Cntrl:]]", Fuzzy => 0);
491 $Cat{Digit} = Table->New(Is => 'Digit', Desc => "[[:Digit:]]", Fuzzy => 0);
492 $Cat{Graph} = Table->New(Is => 'Graph', Desc => "[[:Graph:]]", Fuzzy => 0);
493 $Cat{Lower} = Table->New(Is => 'Lower', Desc => "[[:Lower:]]", Fuzzy => 0);
494 $Cat{Print} = Table->New(Is => 'Print', Desc => "[[:Print:]]", Fuzzy => 0);
495 $Cat{Punct} = Table->New(Is => 'Punct', Desc => "[[:Punct:]]", Fuzzy => 0);
496 $Cat{Space} = Table->New(Is => 'Space', Desc => "[[:Space:]]", Fuzzy => 0);
497 $Cat{Title} = Table->New(Is => 'Title', Desc => "[[:Title:]]", Fuzzy => 0);
498 $Cat{Upper} = Table->New(Is => 'Upper', Desc => "[[:Upper:]]", Fuzzy => 0);
499 $Cat{XDigit} = Table->New(Is => 'XDigit', Desc => "[[:XDigit:]]", Fuzzy => 0);
500 $Cat{Word} = Table->New(Is => 'Word', Desc => "[[:Word:]]", Fuzzy => 0);
501 $Cat{SpacePerl} = Table->New(Is => 'SpacePerl', Desc => '\s', Fuzzy => 0);
504 $To{Upper} = Table->New();
505 $To{Lower} = Table->New();
506 $To{Title} = Table->New();
507 $To{Digit} = Table->New();
511 my ($name, ## Name ("LATIN CAPITAL LETTER A")
512 $cat, ## Category ("Lu", "Zp", "Nd", etc.)
513 $code, ## Code point (as an integer)
516 my $MajorCat = substr($cat, 0, 1); ## L, M, Z, S, etc
518 $Assigned->$op($code);
519 $Name->$op($code, $name);
520 $General->$op($code, $cat);
522 ## add to the sub category (e.g. "Lu", "Nd", "Cf", ..)
523 $Cat{$cat} ||= Table->New(Is => $cat,
524 Desc => "General Category '$cat'",
526 $Cat{$cat}->$op($code);
528 ## add to the major category (e.g. "L", "N", "C", ...)
529 $Cat{$MajorCat} ||= Table->New(Is => $MajorCat,
530 Desc => "Major Category '$MajorCat'",
532 $Cat{$MajorCat}->$op($code);
534 ($General{$name} ||= Table->New)->$op($code, $name);
536 # 005F: SPACING UNDERSCORE
537 $Cat{Word}->$op($code) if $cat =~ /^[LMN]/ || $code == 0x005F;
538 $Cat{Alnum}->$op($code) if $cat =~ /^[LMN]/;
539 $Cat{Alpha}->$op($code) if $cat =~ /^[LM]/;
543 $Cat{Space}->$op($code) if $cat =~ /^Z/
544 || $code == 0x0009 # 0009: HORIZONTAL TAB
545 || $code == 0x000A # 000A: LINE FEED
546 || $code == 0x000B # 000B: VERTICAL TAB
547 || $code == 0x000C # 000C: FORM FEED
548 || $code == 0x000D; # 000D: CARRIAGE RETURN
551 $Cat{SpacePerl}->$op($code) if $cat =~ /^Z/
552 || $code == 0x0009 # 0009: HORIZONTAL TAB
553 || $code == 0x000A # 000A: LINE FEED
554 || $code == 0x000C # 000C: FORM FEED
555 || $code == 0x000D # 000D: CARRIAGE RETURN
556 || $code == 0x0085 # 0085: <NEXT LINE>
557 || $code == 0x2028 # 2028: LINE SEPARATOR
558 || $code == 0x2029;# 2029: PARAGRAPH SEP.
560 $Cat{Blank}->$op($code) if $cat =~ /^Z[^lp]$/
561 || $code == 0x0009 # 0009: HORIZONTAL TAB
562 || $code == 0x0020; # 0020: SPACE
564 $Cat{Digit}->$op($code) if $cat eq "Nd";
565 $Cat{Upper}->$op($code) if $cat eq "Lu";
566 $Cat{Lower}->$op($code) if $cat eq "Ll";
567 $Cat{Title}->$op($code) if $cat eq "Lt";
568 $Cat{ASCII}->$op($code) if $code <= 0x007F;
569 $Cat{Cntrl}->$op($code) if $cat =~ /^C/;
570 $Cat{Graph}->$op($code) if $cat =~ /^([LMNPS]|Co)/;
571 $Cat{Print}->$op($code) if $cat =~ /^([LMNPS]|Co|Zs)/;
572 $Cat{Punct}->$op($code) if $cat =~ /^P/;
574 $Cat{XDigit}->$op($code) if ($code >= 0x30 && $code <= 0x39) ## 0..9
575 || ($code >= 0x41 && $code <= 0x46) ## A..F
576 || ($code >= 0x61 && $code <= 0x66); ## a..f
579 ## open ane read file.....
580 if (not open IN, "Unicode.txt") {
581 die "$0: Unicode.txt: $!\n";
585 ## For building \p{_CombAbove} and \p{_CanonDCIJ}
587 my %_Above_HexCodes; ## Hexcodes for chars with $comb == 230 ("ABOVE")
589 my %CodeToDeco; ## Maps code to decomp. list for chars with first
590 ## decomp. char an "i" or "j" (for \p{_CanonDCIJ})
592 ## This is filled in as we go....
593 my $CombAbove = Table->New(Is => '_CombAbove',
594 Desc => '(for internal casefolding use)',
599 next unless /^[0-9A-Fa-f]+;/;
602 my ($hexcode, ## code point in hex (e.g. "0041")
603 $name, ## character name (e.g. "LATIN CAPITAL LETTER A")
604 $cat, ## category (e.g. "Lu")
605 $comb, ## Canonical combining class (e.t. "230")
606 $bidi, ## directional category (e.g. "L")
607 $deco, ## decomposition mapping
608 $decimal, ## decimal digit value
609 $digit, ## digit value
610 $number, ## numeric value
611 $mirrored, ## mirrored
612 $unicode10, ## name in Unicode 1.0
613 $comment, ## comment field
614 $upper, ## uppercase mapping
615 $lower, ## lowercase mapping
616 $title, ## titlecase mapping
617 ) = split(/\s*;\s*/);
619 my $code = hex($hexcode);
621 if ($comb and $comb == 230) {
622 $CombAbove->Append($code);
623 $_Above_HexCodes{$hexcode} = 1;
626 ## Used in building \p{_CanonDCIJ}
627 if ($deco and $deco =~ m/^006[9A]\b/) {
628 $CodeToDeco{$code} = $deco;
632 ## There are a few pairs of lines like:
633 ## AC00;<Hangul Syllable, First>;Lo;0;L;;;;;N;;;;;
634 ## D7A3;<Hangul Syllable, Last>;Lo;0;L;;;;;N;;;;;
635 ## that define ranges.
637 if ($name =~ /^<(.+), (First|Last)>$/)
640 gencat($name, $cat, $code, $2 eq 'First' ? 'Append' : 'Extend');
641 #New_Prop(In => $name, $General{$name}, Fuzzy => 1);
645 ## normal (single-character) lines
646 gencat($name, $cat, $code, 'Append');
648 # No Append() here since since several codes may map into one.
649 $To{Upper}->RawAppendRange($code, $code, $upper) if $upper;
650 $To{Lower}->RawAppendRange($code, $code, $lower) if $lower;
651 $To{Title}->RawAppendRange($code, $code, $title) if $title;
652 $To{Digit}->Append($code, $decimal) if length $decimal;
654 $Bidi->Append($code, $bidi);
655 $Comb->Append($code, $comb) if $comb;
656 $Number->Append($code, $number) if length $number;
658 $Mirrored->Append($code) if $mirrored eq "Y";
660 $Bidi{$bidi} ||= Table->New(Is => "Bidi$bidi",
661 Desc => "Bi-directional category '$bidi'",
663 $Bidi{$bidi}->Append($code);
667 $Deco->Append($code, $deco);
668 if ($deco =~/^<(\w+)>/)
670 $Deco{Compat}->Append($code);
672 $DC{$1} ||= Table->New(Is => "DC$1",
673 Desc => "Compatible with '$1'",
675 $DC{$1}->Append($code);
679 $Deco{Canon}->Append($code);
687 ## Tidy up a few special cases....
690 $Cat{Cn} = $Assigned->Invert; ## Cn is everything that doesn't exist
693 Desc => "General Category 'Cn' [not functional in Perl]",
696 ## Unassigned is the same as 'Cn'
697 New_Alias(Is => 'Unassigned', SameAs => 'Cn', Fuzzy => 1);
699 $Cat{C}->Replace($Cat{C}->Merge($Cat{Cn})); ## Now merge in Cn into C
702 # L& is Ll, Lu, and Lt.
704 Table->Merge(@Cat{qw[Ll Lu Lt]}),
705 Desc => '[\p{Ll}\p{Lu}\p{Lt}]',
708 ## Any and All are all code points.
709 my $Any = Table->New(Is => 'Any',
710 Desc => sprintf("[\\x{0000}-\\x{%X}]",
711 $LastUnicodeCodepoint),
713 $Any->RawAppendRange(0, $LastUnicodeCodepoint);
715 New_Alias(Is => 'All', SameAs => 'Any', Fuzzy => 1);
718 ## Build special properties for Perl's internal case-folding needs:
719 ## \p{_CaseIgnorable}
722 ## _CombAbove was built above. Others are built here....
725 ## \p{_CaseIgnorable} is [\p{Mn}\0x00AD\x2010]
726 New_Prop(Is => '_CaseIgnorable',
727 Table->Merge($Cat{Mn},
730 Desc => '(for internal casefolding use)',
734 ## \p{_CanonDCIJ} is fairly complex...
735 my $CanonCDIJ = Table->New(Is => '_CanonDCIJ',
736 Desc => '(for internal casefolding use)',
738 ## It contains the ASCII 'i' and 'j'....
739 $CanonCDIJ->Append(0x0069); # ASCII ord("i")
740 $CanonCDIJ->Append(0x006A); # ASCII ord("j")
741 ## ...and any character with a decomposition that starts with either of
742 ## those code points, but only if the decomposition does not have any
743 ## combining character with the "ABOVE" canonical combining class.
744 for my $code (sort { $a <=> $b} keys %CodeToDeco)
746 ## Need to ensure that all decomposition characters do not have
747 ## a %HexCodeToComb in %AboveCombClasses.
749 for my $deco_hexcode (split / /, $CodeToDeco{$code})
751 if (exists $_Above_HexCodes{$deco_hexcode}) {
752 ## one of the decmposition chars has an ABOVE combination
753 ## class, so we're not interested in this one
759 $CanonCDIJ->Append($code);
766 ## Now dump the files.
768 $Name->Write("Name.pl");
769 $Bidi->Write("Bidirectional.pl");
770 $Comb->Write("CombiningClass.pl");
771 $Deco->Write("Decomposition.pl");
772 $Number->Write("Number.pl");
773 $General->Write("Category.pl");
775 for my $to (sort keys %To) {
776 $To{$to}->Write("To/$to.pl");
781 ## Process LineBrk.txt
785 if (not open IN, "LineBrk.txt") {
786 die "$0: LineBrk.txt: $!\n";
789 my $Lbrk = Table->New();
794 next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(\w+)/;
796 my ($first, $last, $lbrk) = (hex($1), hex($2||""), $3);
798 $Lbrk->Append($first, $lbrk);
800 $Lbrk{$lbrk} ||= Table->New(Is => "Lbrk$lbrk",
801 Desc => "Linebreak category '$lbrk'",
803 $Lbrk{$lbrk}->Append($first);
806 $Lbrk->Extend($last);
807 $Lbrk{$lbrk}->Extend($last);
812 $Lbrk->Write("Lbrk.pl");
816 ## Process ArabShap.txt.
820 if (not open IN, "ArabShap.txt") {
821 die "$0: ArabShap.txt: $!\n";
824 my $ArabLink = Table->New();
825 my $ArabLinkGroup = Table->New();
829 next unless /^[0-9A-Fa-f]+;/;
832 my ($hexcode, $name, $link, $linkgroup) = split(/\s*;\s*/);
833 my $code = hex($hexcode);
834 $ArabLink->Append($code, $link);
835 $ArabLinkGroup->Append($code, $linkgroup);
839 $ArabLink->Write("ArabLink.pl");
840 $ArabLinkGroup->Write("ArabLnkGrp.pl");
848 if (not open IN, "Jamo.txt") {
849 die "$0: Jamo.txt: $!\n";
851 my $Short = Table->New();
855 next unless /^([0-9A-Fa-f]+)\s*;\s*(\w*)/;
856 my ($code, $short) = (hex($1), $2);
858 $Short->Append($code, $short);
861 $Short->Write("JamoShort.pl");
865 ## Process Scripts.txt.
871 if (not open(IN, "Scripts.txt")) {
872 die "$0: Scripts.txt: $!\n";
875 next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(.+?)\s*\#/;
877 # Wait until all the scripts have been read since
878 # they are not listed in numeric order.
879 push @ScriptInfo, [ hex($1), hex($2||""), $3 ];
883 # Now append the scripts properties in their code point order.
886 my $Scripts = Table->New();
888 for my $script (sort { $a->[0] <=> $b->[0] } @ScriptInfo)
890 my ($first, $last, $name) = @$script;
891 $Scripts->Append($first, $name);
893 $Script{$name} ||= Table->New(Is => $name,
894 Desc => "Script '$name'",
896 $Script{$name}->Append($first, $name);
899 $Scripts->Extend($last);
900 $Script{$name}->Extend($last);
904 $Scripts->Write("Scripts.pl");
906 ## Common is everything not explicitly assigned to a Script
908 ## ***shouldn't this be intersected with \p{Assigned}? ******
910 New_Prop(Is => 'Common',
912 Desc => 'Pseudo-Script of codepoints not in other Unicode scripts',
917 ## Given a name like "Close Punctuation", return a regex (that when applied
918 ## with /i) matches any valid form of that name (e.g. "ClosePunctuation",
919 ## "Close-Punctuation", etc.)
921 ## Accept any space, dash, or underbar where in the official name there is
922 ## space or a dash (or underbar, but there never is).
928 $Name =~ s/[- _]/(?:[-_]|\\s+)?/g;
933 ## Process Blocks.txt.
937 my $Blocks = Table->New();
940 if (not open IN, "Blocks.txt") {
941 die "$0: Blocks.txt: $!\n";
946 #next if not /Private Use$/;
947 next if not /^([0-9A-Fa-f]+)\.\.([0-9A-Fa-f]+)\s*;\s*(.+?)\s*$/;
949 my ($first, $last, $name) = (hex($1), hex($2), $3);
951 $Blocks->Append($first, $name);
953 $Blocks{$name} ||= Table->New(In => $name,
954 Desc => "Block '$name'",
956 $Blocks{$name}->Append($first, $name);
958 if ($last and $last != $first) {
959 $Blocks->Extend($last);
960 $Blocks{$name}->Extend($last);
965 $Blocks->Write("Blocks.pl");
969 ## Read in the PropList.txt. It contains extended properties not
970 ## listed in the Unicode.txt, such as 'Other_Alphabetic':
971 ## alphabetic but not of the general category L; many modifiers
972 ## belong to this extended property category: while they are not
973 ## alphabets, they are alphabetic in nature.
979 if (not open IN, "PropList.txt") {
980 die "$0: PropList.txt: $!\n";
985 next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(.+?)\s*\#/;
987 # Wait until all the extended properties have been read since
988 # they are not listed in numeric order.
989 push @PropInfo, [ hex($1), hex($2||""), $3 ];
993 # Now append the extended properties in their code point order.
994 my $Props = Table->New();
997 for my $prop (sort { $a->[0] <=> $b->[0] } @PropInfo)
999 my ($first, $last, $name) = @$prop;
1000 $Props->Append($first, $name);
1002 $Prop{$name} ||= Table->New(Is => $name,
1003 Desc => "Extended property '$name'",
1005 $Prop{$name}->Append($first, $name);
1008 $Props->Extend($last);
1009 $Prop{$name}->Extend($last);
1013 # Alphabetic is L and Other_Alphabetic.
1014 New_Prop(Is => 'Alphabetic',
1015 Table->Merge($Cat{L}, $Prop{Other_Alphabetic}),
1016 Desc => '[\p{L}\p{OtherAlphabetic}]', # use canonical names here
1019 # Lowercase is Ll and Other_Lowercase.
1020 New_Prop(Is => 'Lowercase',
1021 Table->Merge($Cat{Ll}, $Prop{Other_Lowercase}),
1022 Desc => '[\p{Ll}\p{OtherLowercase}]', # use canonical names here
1025 # Uppercase is Lu and Other_Uppercase.
1026 New_Prop(Is => 'Uppercase',
1027 Table->Merge($Cat{Lu}, $Prop{Other_Uppercase}),
1028 Desc => '[\p{Lu}\p{Other_Uppercase}]', # use canonical names here
1031 # Math is Sm and Other_Math.
1032 New_Prop(Is => 'Math',
1033 Table->Merge($Cat{Sm}, $Prop{Other_Math}),
1034 Desc => '[\p{Sm}\p{OtherMath}]', # use canonical names here
1037 # ID_Start is Ll, Lu, Lt, Lm, Lo, and Nl.
1038 New_Prop(Is => 'ID_Start',
1039 Table->Merge(@Cat{qw[Ll Lu Lt Lm Lo Nl]}),
1040 Desc => '[\p{Ll}\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{Nl}]',
1043 # ID_Continue is ID_Start, Mn, Mc, Nd, and Pc.
1044 New_Prop(Is => 'ID_Continue',
1045 Table->Merge(@Cat{qw[Ll Lu Lt Lm Lo Nl Mn Mc Nd Pc ]}),
1046 Desc => '[\p{ID_Start}\p{Mn}\p{Mc}\p{Nd}\p{Pc}]',
1050 sub Make_GC_Aliases()
1053 ## The mapping from General Category long forms to short forms is
1054 ## currently hardwired here since no simple data file in the UCD
1055 ## seems to do that. Unicode 3.2 will assumedly correct this.
1059 'Uppercase_Letter' => 'Lu',
1060 'Lowercase_Letter' => 'Ll',
1061 'Titlecase_Letter' => 'Lt',
1062 'Modifier_Letter' => 'Lm',
1063 'Other_Letter' => 'Lo',
1066 'Non_Spacing_Mark' => 'Mn',
1067 'Spacing_Mark' => 'Mc',
1068 'Enclosing_Mark' => 'Me',
1071 'Space_Separator' => 'Zs',
1072 'Line_Separator' => 'Zl',
1073 'Paragraph_Separator' => 'Zp',
1076 'Decimal_Number' => 'Nd',
1077 'Letter_Number' => 'Nl',
1078 'Other_Number' => 'No',
1080 'Punctuation' => 'P',
1081 'Connector_Punctuation' => 'Pc',
1082 'Dash_Punctuation' => 'Pd',
1083 'Open_Punctuation' => 'Ps',
1084 'Close_Punctuation' => 'Pe',
1085 'Initial_Punctuation' => 'Pi',
1086 'Final_Punctuation' => 'Pf',
1087 'Other_Punctuation' => 'Po',
1090 'Math_Symbol' => 'Sm',
1091 'Currency_Symbol' => 'Sc',
1092 'Modifier_Symbol' => 'Sk',
1093 'Other_Symbol' => 'So',
1098 'Surrogate' => 'Cs',
1099 'Private Use' => 'Co',
1100 'Unassigned' => 'Cn',
1103 ## make the aliases....
1104 while (my ($Alias, $Name) = each %Is) {
1105 New_Alias(Is => $Alias, SameAs => $Name, Fuzzy => 1);
1110 ## Writes the info accumulated in
1117 sub WriteAllMappings()
1121 for my $Type ('In', 'Is')
1126 my %Exact; ## will become %utf8::Is or %utf8::In
1127 my %Pat; ## will become %utf8::IsPat or %utf8::InPat
1130 ## First write all the files to the $Type/ directory
1132 for my $Name (sort { length $a <=> length $b } keys %{$TableInfo{$Type}})
1134 my $Table = $TableInfo{$Type}->{$Name};
1136 ## Need an 8.3 safe filename (which means "an 8 safe" $filename)
1137 my $filename = $FuzzyNames{$Type}->{$Name} ? CanonicalName($Name): $Name;
1138 $filename =~ s/[^\w_]+/_/g; # "L&" -> "L_"
1139 substr($filename, 8) = '' if length($filename) > 8;
1142 ## Make sure the filename doesn't conflict with something we
1143 ## might have already written. If we have, say,
1150 while (my $num = $Filenames{lc $filename}++)
1152 $num++; ## so filenames with numbers start with '2', which
1153 ## just looks more natural.
1154 ## Want to append $num, but if it'll make the filename longer
1155 ## than 8 characters, pre-truncate $filename so that the result
1157 my $delta = length($filename) + length($num) - 8;
1159 substr($filename, -$delta) = $num;
1165 $Exact{$Name} = $filename;
1168 ## Construct a nice comment to add to the file, and build data
1169 ## for the "./Properties" file along the way.
1173 my $Desc = $TableDesc{$Type}->{$Name} || "";
1174 ## get list of names this table is reference by
1175 my @Supported = $Name;
1176 while (my ($Orig, $Alias) = each %{ $AliasInfo{$Type} })
1178 if ($Orig eq $Name) {
1179 push @Supported, $Alias;
1183 my $TypeToShow = $Type eq 'Is' ? "" : $Type;
1186 $Comment = "This file supports:\n";
1187 for my $N (@Supported)
1189 my $IsFuzzy = $FuzzyNames{$Type}->{$N};
1190 my $CName = $IsFuzzy ? CanonicalName($N): $N;
1191 my $Prop = "\\p{$TypeToShow$CName}";
1192 $OrigProp = $Prop if not $OrigProp; #cache for aliases
1194 $Comment .= "\t$Prop (and fuzzy permutations)\n";
1196 $Comment .= "\t$Prop\n";
1198 my $MyDesc = ($N eq $Name) ? $Desc : "Alias for $OrigProp ($Desc)";
1200 push @MAP, sprintf("%s %-42s %s\n",
1201 $IsFuzzy ? '*' : ' ', $Prop, $MyDesc);
1204 $Comment .= "\nMeaning: $Desc\n";
1209 ## Okay, write the file...
1211 $Table->Write("$Type/$filename.pl", $Comment);
1215 ## Write out the map
1217 if (not open MAP, ">Properties") {
1218 die "$0: can't write Properties: $!\n";
1221 print MAP "## This file created by $0\n";
1222 print MAP "## List of built-in \\p{...}/\\P{...} properties.\n";
1224 print MAP "## '*' means name may be 'fuzzy'\n";
1227 print MAP sort { substr($a,2) cmp substr($b, 2) } @MAP;
1233 while (my ($Fuzzy, $Real) = each %{$FuzzyNames{$Type}})
1235 my $File = $Exact{$Real};
1238 die "$0: oops [$Real]";
1241 ## The prefix length of 2 is enough spread,
1242 ## and besides, we have 'Yi' as an In category.
1243 my $Prefix = lc(substr($Fuzzy, 0, 2));
1244 my $Regex = NameToRegex($Fuzzy);
1246 if ($Pat{$Prefix}->{$Regex}) {
1247 warn "WHOA, conflict with /$Regex/: $Pat{$Prefix}->{$Regex} vs $File\n";
1250 $Pat{$Prefix}->{$Regex} = $File;
1254 ## Since the fuzzy method will provide for a way to match $Fuzzy,
1255 ## there's no need for $Fuzzy to be in %Exact as well.
1256 ## This can't be done in the loop above because there could be
1257 ## multiple $Fuzzys pointing at the same $Real, and we don't want
1258 ## the first to delete the exact mapping out from under the second.
1260 for my $Fuzzy (keys %{$FuzzyNames{$Type}})
1262 delete $Exact{$Fuzzy};
1268 ## Now write In.pl / Is.pl
1270 if (not open OUT, ">$Type.pl") {
1271 die "$0: $Type.pl: $!\n";
1275 print OUT "## Data in this file used by ../utf8_heavy.pl\n";
1278 print OUT "## Mapping from name to filename in ./$Type\n";
1279 print OUT "%utf8::$Type = (\n";
1280 for my $Name (sort keys %Exact)
1282 my $File = $Exact{$Name};
1283 printf OUT " %-41s => %s,\n", "'$Name'", "'$File'";
1287 print OUT "## Mappings from regex to filename in ./$Type/\n";
1288 print OUT "%utf8::${Type}Pat = (\n";
1289 for my $Prefix (sort keys %Pat)
1291 print OUT " '$Prefix' => {\n";
1292 while (my ($Regex, $File) = each %{ $Pat{$Prefix} }) {
1293 print OUT "\t'$Regex' => '$File',\n";
1306 # Read in the special cases.
1311 if (not open IN, "SpecCase.txt") {
1312 die "$0: SpecCase.txt: $!\n";
1315 next unless /^[0-9A-Fa-f]+;/;
1319 my ($code, $lower, $title, $upper, $condition) = split(/\s*;\s*/);
1321 if ($condition) { # not implemented yet
1322 print "# SKIPPING $_\n" if $Verbose;
1326 # Wait until all the special cases have been read since
1327 # they are not listed in numeric order.
1328 my $ix = hex($code);
1329 push @{$CaseInfo{Lower}}, [ $ix, $code, $lower ];
1330 push @{$CaseInfo{Title}}, [ $ix, $code, $title ];
1331 push @{$CaseInfo{Upper}}, [ $ix, $code, $upper ];
1335 # Now write out the special cases properties in their code point order.
1336 # Prepend them to the To/{Upper,Lower,Title}.pl.
1338 for my $case (qw(Lower Title Upper))
1340 my $NormalCase = do "To/$case.pl" || die "$0: $@\n";
1341 if (not open OUT, ">To/$case.pl") {
1342 die "$0: To/$case.txt: $!";
1345 print OUT $HEADER, "\n";
1346 print OUT "%utf8::ToSpec$case =\n(\n";
1348 for my $prop (sort { $a->[0] <=> $b->[0] } @{$CaseInfo{$case}}) {
1349 my ($ix, $code, $to) = @$prop;
1351 join "", map { sprintf "\\x{%s}", $_ } split ' ', $to;
1352 printf OUT qq['%04X' => "$tostr",\n], $ix;
1355 print OUT "return <<'END';\n";
1356 print OUT $NormalCase;
1363 # Read in the case foldings.
1365 # We will do full case folding, C + F + I (see CaseFold.txt).
1369 if (not open IN, "CaseFold.txt") {
1370 die "$0: To/Fold.pl: $!\n";
1373 my $Fold = Table->New();
1377 # Skip status 'S', simple case folding
1378 next unless /^([0-9A-Fa-f]+)\s*;\s*([CFI])\s*;\s*([0-9A-Fa-f]+(?: [0-9A-Fa-f]+)*)\s*;/;
1380 my ($code, $status, $fold) = (hex($1), $2, $3);
1382 if ($status eq 'C') { # Common: one-to-one folding
1383 # No append() since several codes may fold into one.
1384 $Fold->RawAppendRange($code, $code, $fold);
1385 } else { # F: full, or I: dotted uppercase I -> dotless lowercase I
1386 $Fold{$code} = $fold;
1391 $Fold->Write("To/Fold.pl");
1394 # Prepend the special foldings to the common foldings.
1397 my $CommonFold = do "To/Fold.pl" || die "$0: To/Fold.pl: $!\n";
1398 if (not open OUT, ">To/Fold.pl") {
1399 die "$0: To/Fold.pl: $!\n";
1401 print OUT $HEADER, "\n";
1402 print OUT "%utf8::ToSpecFold =\n(\n";
1403 for my $code (sort { $a <=> $b } keys %Fold) {
1405 join "", map { sprintf "\\x{%s}", $_ } split ' ', $Fold{$code};
1406 printf OUT qq['%04X' => "$foldstr",\n], $code;
1409 print OUT "return <<'END';\n";
1410 print OUT $CommonFold;
1433 # That's all, folks!