1N/A#!/usr/bin/perl -w
1N/Ause strict;
1N/Ause Carp;
1N/A
1N/Adie "$0: Please run me as ./mktables to avoid unnecessary differences\n"
1N/A unless $0 eq "./mktables";
1N/A
1N/A##
1N/A## mktables -- create the runtime Perl Unicode files (lib/unicore/**/*.pl)
1N/A## from the Unicode database files (lib/unicore/*.txt).
1N/A##
1N/A
1N/Amkdir("lib", 0755);
1N/Amkdir("To", 0755);
1N/A
1N/A##
1N/A## Process any args.
1N/A##
1N/Amy $Verbose = 0;
1N/Amy $MakeTestScript = 0;
1N/A
1N/Awhile (@ARGV)
1N/A{
1N/A my $arg = shift @ARGV;
1N/A if ($arg eq '-v') {
1N/A $Verbose = 1;
1N/A } elsif ($arg eq '-q') {
1N/A $Verbose = 0;
1N/A } elsif ($arg eq '-maketest') {
1N/A $MakeTestScript = 1;
1N/A } else {
1N/A die "usage: $0 [-v|-q] [-maketest]";
1N/A }
1N/A}
1N/A
1N/Amy $LastUnicodeCodepoint = 0x10FFFF; # As of Unicode 3.1.1.
1N/A
1N/Amy $HEADER=<<"EOF";
1N/A# !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
1N/A# This file is built by $0 from e.g. UnicodeData.txt.
1N/A# Any changes made here will be lost!
1N/A
1N/AEOF
1N/A
1N/A
1N/A##
1N/A## Given a filename and a reference to an array of lines,
1N/A## write the lines to the file only if the contents have not changed.
1N/A##
1N/Asub WriteIfChanged($\@)
1N/A{
1N/A my $file = shift;
1N/A my $lines = shift;
1N/A
1N/A my $TextToWrite = join '', @$lines;
1N/A if (open IN, $file) {
1N/A local($/) = undef;
1N/A my $PreviousText = <IN>;
1N/A close IN;
1N/A if ($PreviousText eq $TextToWrite) {
1N/A print "$file unchanged.\n" if $Verbose;
1N/A return;
1N/A }
1N/A }
1N/A if (not open OUT, ">$file") {
1N/A die "$0: can't open $file for output: $!\n";
1N/A }
1N/A print "$file written.\n" if $Verbose;
1N/A
1N/A print OUT $TextToWrite;
1N/A close OUT;
1N/A}
1N/A
1N/A##
1N/A## The main datastructure (a "Table") represents a set of code points that
1N/A## are part of a particular quality (that are part of \pL, \p{InGreek},
1N/A## etc.). They are kept as ranges of code points (starting and ending of
1N/A## each range).
1N/A##
1N/A## For example, a range ASCII LETTERS would be represented as:
1N/A## [ [ 0x41 => 0x5A, 'UPPER' ],
1N/A## [ 0x61 => 0x7A, 'LOWER, ] ]
1N/A##
1N/Asub RANGE_START() { 0 } ## index into range element
1N/Asub RANGE_END() { 1 } ## index into range element
1N/Asub RANGE_NAME() { 2 } ## index into range element
1N/A
1N/A## Conceptually, these should really be folded into the 'Table' objects
1N/Amy %TableInfo;
1N/Amy %TableDesc;
1N/Amy %FuzzyNames;
1N/Amy %AliasInfo;
1N/Amy %CanonicalToOrig;
1N/A
1N/A##
1N/A## Turn something like
1N/A## OLD-ITALIC
1N/A## into
1N/A## OldItalic
1N/A##
1N/Asub CanonicalName($)
1N/A{
1N/A my $orig = shift;
1N/A my $name = lc $orig;
1N/A $name =~ s/(?<![a-z])(\w)/\u$1/g;
1N/A $name =~ s/[-_\s]+//g;
1N/A
1N/A $CanonicalToOrig{$name} = $orig if not $CanonicalToOrig{$name};
1N/A return $name;
1N/A}
1N/A
1N/A##
1N/A## Associates a property ("Greek", "Lu", "Assigned",...) with a Table.
1N/A##
1N/A## Called like:
1N/A## New_Prop(In => 'Greek', $Table, Desc => 'Greek Block', Fuzzy => 1);
1N/A##
1N/A## Normally, these parameters are set when the Table is created (when the
1N/A## Table->New constructor is called), but there are times when it needs to
1N/A## be done after-the-fact...)
1N/A##
1N/Asub New_Prop($$$@)
1N/A{
1N/A my $Type = shift; ## "Is" or "In";
1N/A my $Name = shift;
1N/A my $Table = shift;
1N/A
1N/A ## remaining args are optional key/val
1N/A my %Args = @_;
1N/A
1N/A my $Fuzzy = delete $Args{Fuzzy};
1N/A my $Desc = delete $Args{Desc}; # description
1N/A
1N/A $Name = CanonicalName($Name) if $Fuzzy;
1N/A
1N/A ## sanity check a few args
1N/A if (%Args or ($Type ne 'Is' and $Type ne 'In') or not ref $Table) {
1N/A confess "$0: bad args to New_Prop"
1N/A }
1N/A
1N/A if (not $TableInfo{$Type}->{$Name})
1N/A {
1N/A $TableInfo{$Type}->{$Name} = $Table;
1N/A $TableDesc{$Type}->{$Name} = $Desc;
1N/A if ($Fuzzy) {
1N/A $FuzzyNames{$Type}->{$Name} = $Name;
1N/A }
1N/A }
1N/A}
1N/A
1N/A
1N/A##
1N/A## Creates a new Table object.
1N/A##
1N/A## Args are key/value pairs:
1N/A## In => Name -- Name of "In" property to be associated with
1N/A## Is => Name -- Name of "Is" property to be associated with
1N/A## Fuzzy => Boolean -- True if name can be accessed "fuzzily"
1N/A## Desc => String -- Description of the property
1N/A##
1N/A## No args are required.
1N/A##
1N/Asub Table::New
1N/A{
1N/A my $class = shift;
1N/A my %Args = @_;
1N/A
1N/A my $Table = bless [], $class;
1N/A
1N/A my $Fuzzy = delete $Args{Fuzzy};
1N/A my $Desc = delete $Args{Desc};
1N/A
1N/A for my $Type ('Is', 'In')
1N/A {
1N/A if (my $Name = delete $Args{$Type}) {
1N/A New_Prop($Type => $Name, $Table, Desc => $Desc, Fuzzy => $Fuzzy);
1N/A }
1N/A }
1N/A
1N/A ## shouldn't have any left over
1N/A if (%Args) {
1N/A confess "$0: bad args to Table->New"
1N/A }
1N/A
1N/A return $Table;
1N/A}
1N/A
1N/A##
1N/A## Returns true if the Table has no code points
1N/A##
1N/Asub Table::IsEmpty
1N/A{
1N/A my $Table = shift; #self
1N/A return not @$Table;
1N/A}
1N/A
1N/A##
1N/A## Returns true if the Table has code points
1N/A##
1N/Asub Table::NotEmpty
1N/A{
1N/A my $Table = shift; #self
1N/A return @$Table;
1N/A}
1N/A
1N/A##
1N/A## Returns the maximum code point currently in the table.
1N/A##
1N/Asub Table::Max
1N/A{
1N/A my $Table = shift; #self
1N/A confess "oops" if $Table->IsEmpty; ## must have code points to have a max
1N/A return $Table->[-1]->[RANGE_END];
1N/A}
1N/A
1N/A##
1N/A## Replaces the codepoints in the Table with those in the Table given
1N/A## as an arg. (NOTE: this is not a "deep copy").
1N/A##
1N/Asub Table::Replace($$)
1N/A{
1N/A my $Table = shift; #self
1N/A my $New = shift;
1N/A
1N/A @$Table = @$New;
1N/A}
1N/A
1N/A##
1N/A## Given a new code point, make the last range of the Table extend to
1N/A## include the new (and all intervening) code points.
1N/A##
1N/Asub Table::Extend
1N/A{
1N/A my $Table = shift; #self
1N/A my $codepoint = shift;
1N/A
1N/A my $PrevMax = $Table->Max;
1N/A
1N/A confess "oops ($codepoint <= $PrevMax)" if $codepoint <= $PrevMax;
1N/A
1N/A $Table->[-1]->[RANGE_END] = $codepoint;
1N/A}
1N/A
1N/A##
1N/A## Given a code point range start and end (and optional name), blindly
1N/A## append them to the list of ranges for the Table.
1N/A##
1N/A## NOTE: Code points must be added in strictly ascending numeric order.
1N/A##
1N/Asub Table::RawAppendRange
1N/A{
1N/A my $Table = shift; #self
1N/A my $start = shift;
1N/A my $end = shift;
1N/A my $name = shift;
1N/A $name = "" if not defined $name; ## warning: $name can be "0"
1N/A
1N/A push @$Table, [ $start, # RANGE_START
1N/A $end, # RANGE_END
1N/A $name ]; # RANGE_NAME
1N/A}
1N/A
1N/A##
1N/A## Given a code point (and optional name), add it to the Table.
1N/A##
1N/A## NOTE: Code points must be added in strictly ascending numeric order.
1N/A##
1N/Asub Table::Append
1N/A{
1N/A my $Table = shift; #self
1N/A my $codepoint = shift;
1N/A my $name = shift;
1N/A $name = "" if not defined $name; ## warning: $name can be "0"
1N/A
1N/A ##
1N/A ## If we've already got a range working, and this code point is the next
1N/A ## one in line, and if the name is the same, just extend the current range.
1N/A ##
1N/A if ($Table->NotEmpty
1N/A and
1N/A $Table->Max == $codepoint - 1
1N/A and
1N/A $Table->[-1]->[RANGE_NAME] eq $name)
1N/A {
1N/A $Table->Extend($codepoint);
1N/A }
1N/A else
1N/A {
1N/A $Table->RawAppendRange($codepoint, $codepoint, $name);
1N/A }
1N/A}
1N/A
1N/A##
1N/A## Given a code point range starting value and ending value (and name),
1N/A## Add the range to teh Table.
1N/A##
1N/A## NOTE: Code points must be added in strictly ascending numeric order.
1N/A##
1N/Asub Table::AppendRange
1N/A{
1N/A my $Table = shift; #self
1N/A my $start = shift;
1N/A my $end = shift;
1N/A my $name = shift;
1N/A $name = "" if not defined $name; ## warning: $name can be "0"
1N/A
1N/A $Table->Append($start, $name);
1N/A $Table->Extend($end) if $end > $start;
1N/A}
1N/A
1N/A##
1N/A## Return a new Table that represents all code points not in the Table.
1N/A##
1N/Asub Table::Invert
1N/A{
1N/A my $Table = shift; #self
1N/A
1N/A my $New = Table->New();
1N/A my $max = -1;
1N/A for my $range (@$Table)
1N/A {
1N/A my $start = $range->[RANGE_START];
1N/A my $end = $range->[RANGE_END];
1N/A if ($start-1 >= $max+1) {
1N/A $New->AppendRange($max+1, $start-1, "");
1N/A }
1N/A $max = $end;
1N/A }
1N/A if ($max+1 < $LastUnicodeCodepoint) {
1N/A $New->AppendRange($max+1, $LastUnicodeCodepoint);
1N/A }
1N/A return $New;
1N/A}
1N/A
1N/A##
1N/A## Merges any number of other tables with $self, returning the new table.
1N/A## (existing tables are not modified)
1N/A##
1N/A##
1N/A## Args may be Tables, or individual code points (as integers).
1N/A##
1N/A## Can be called as either a constructor or a method.
1N/A##
1N/Asub Table::Merge
1N/A{
1N/A shift(@_) if not ref $_[0]; ## if called as a constructor, lose the class
1N/A my @Tables = @_;
1N/A
1N/A ## Accumulate all records from all tables
1N/A my @Records;
1N/A for my $Arg (@Tables)
1N/A {
1N/A if (ref $Arg) {
1N/A ## arg is a table -- get its ranges
1N/A push @Records, @$Arg;
1N/A } else {
1N/A ## arg is a codepoint, make a range
1N/A push @Records, [ $Arg, $Arg ]
1N/A }
1N/A }
1N/A
1N/A ## sort by range start, with longer ranges coming first.
1N/A my ($first, @Rest) = sort {
1N/A ($a->[RANGE_START] <=> $b->[RANGE_START])
1N/A or
1N/A ($b->[RANGE_END] <=> $b->[RANGE_END])
1N/A } @Records;
1N/A
1N/A my $New = Table->New();
1N/A
1N/A ## Ensuring the first range is there makes the subsequent loop easier
1N/A $New->AppendRange($first->[RANGE_START],
1N/A $first->[RANGE_END]);
1N/A
1N/A ## Fold in records so long as they add new information.
1N/A for my $set (@Rest)
1N/A {
1N/A my $start = $set->[RANGE_START];
1N/A my $end = $set->[RANGE_END];
1N/A if ($start > $New->Max) {
1N/A $New->AppendRange($start, $end);
1N/A } elsif ($end > $New->Max) {
1N/A $New->Extend($end);
1N/A }
1N/A }
1N/A
1N/A return $New;
1N/A}
1N/A
1N/A##
1N/A## Given a filename, write a representation of the Table to a file.
1N/A## May have an optional comment as a 2nd arg.
1N/A##
1N/Asub Table::Write
1N/A{
1N/A my $Table = shift; #self
1N/A my $filename = shift;
1N/A my $comment = shift;
1N/A
1N/A my @OUT = $HEADER;
1N/A if (defined $comment) {
1N/A $comment =~ s/\s+\Z//;
1N/A $comment =~ s/^/# /gm;
1N/A push @OUT, "#\n$comment\n#\n";
1N/A }
1N/A push @OUT, "return <<'END';\n";
1N/A
1N/A for my $set (@$Table)
1N/A {
1N/A my $start = $set->[RANGE_START];
1N/A my $end = $set->[RANGE_END];
1N/A my $name = $set->[RANGE_NAME];
1N/A
1N/A if ($start == $end) {
1N/A push @OUT, sprintf "%04X\t\t%s\n", $start, $name;
1N/A } else {
1N/A push @OUT, sprintf "%04X\t%04X\t%s\n", $start, $end, $name;
1N/A }
1N/A }
1N/A
1N/A push @OUT, "END\n";
1N/A
1N/A WriteIfChanged($filename, @OUT);
1N/A}
1N/A
1N/A## This used only for making the test script.
1N/A## helper function
1N/Asub IsUsable($)
1N/A{
1N/A my $code = shift;
1N/A return 0 if $code <= 0x0000; ## don't use null
1N/A return 0 if $code >= $LastUnicodeCodepoint; ## keep in range
1N/A return 0 if ($code >= 0xD800 and $code <= 0xDFFF); ## no surrogates
1N/A return 0 if ($code >= 0xFDD0 and $code <= 0xFDEF); ## utf8.c says no good
1N/A return 0 if (($code & 0xFFFF) == 0xFFFE); ## utf8.c says no good
1N/A return 0 if (($code & 0xFFFF) == 0xFFFF); ## utf8.c says no good
1N/A return 1;
1N/A}
1N/A
1N/A## Return a code point that's part of the table.
1N/A## Returns nothing if the table is empty (or covers only surrogates).
1N/A## This used only for making the test script.
1N/Asub Table::ValidCode
1N/A{
1N/A my $Table = shift; #self
1N/A for my $set (@$Table) {
1N/A return $set->[RANGE_END] if IsUsable($set->[RANGE_END]);
1N/A }
1N/A return ();
1N/A}
1N/A
1N/A## Return a code point that's not part of the table
1N/A## Returns nothing if the table covers all code points.
1N/A## This used only for making the test script.
1N/Asub Table::InvalidCode
1N/A{
1N/A my $Table = shift; #self
1N/A
1N/A return 0x1234 if $Table->IsEmpty();
1N/A
1N/A for my $set (@$Table)
1N/A {
1N/A if (IsUsable($set->[RANGE_END] + 1))
1N/A {
1N/A return $set->[RANGE_END] + 1;
1N/A }
1N/A
1N/A if (IsUsable($set->[RANGE_START] - 1))
1N/A {
1N/A return $set->[RANGE_START] - 1;
1N/A }
1N/A }
1N/A return ();
1N/A}
1N/A
1N/A###########################################################################
1N/A###########################################################################
1N/A###########################################################################
1N/A
1N/A
1N/A##
1N/A## Called like:
1N/A## New_Alias(Is => 'All', SameAs => 'Any', Fuzzy => 1);
1N/A##
1N/A## The args must be in that order, although the Fuzzy pair may be omitted.
1N/A##
1N/A## This creates 'IsAll' as an alias for 'IsAny'
1N/A##
1N/Asub New_Alias($$$@)
1N/A{
1N/A my $Type = shift; ## "Is" or "In"
1N/A my $Alias = shift;
1N/A my $SameAs = shift; # expecting "SameAs" -- just ignored
1N/A my $Name = shift;
1N/A
1N/A ## remaining args are optional key/val
1N/A my %Args = @_;
1N/A
1N/A my $Fuzzy = delete $Args{Fuzzy};
1N/A
1N/A ## sanity check a few args
1N/A if (%Args or ($Type ne 'Is' and $Type ne 'In') or $SameAs ne 'SameAs') {
1N/A confess "$0: bad args to New_Alias"
1N/A }
1N/A
1N/A $Alias = CanonicalName($Alias) if $Fuzzy;
1N/A
1N/A if (not $TableInfo{$Type}->{$Name})
1N/A {
1N/A my $CName = CanonicalName($Name);
1N/A if ($TableInfo{$Type}->{$CName}) {
1N/A confess "$0: Use canonical form '$CName' instead of '$Name' for alias.";
1N/A } else {
1N/A confess "$0: don't have orignial $Type => $Name to make alias";
1N/A }
1N/A }
1N/A if ($TableInfo{$Alias}) {
1N/A confess "$0: already have original $Type => $Alias; can't make alias";
1N/A }
1N/A $AliasInfo{$Type}->{$Name} = $Alias;
1N/A if ($Fuzzy) {
1N/A $FuzzyNames{$Type}->{$Alias} = $Name;
1N/A }
1N/A
1N/A}
1N/A
1N/A
1N/A## All assigned code points
1N/Amy $Assigned = Table->New(Is => 'Assigned',
1N/A Desc => "All assigned code points",
1N/A Fuzzy => 0);
1N/A
1N/Amy $Name = Table->New(); ## all characters, individually by name
1N/Amy $General = Table->New(); ## all characters, grouped by category
1N/Amy %General;
1N/Amy %Cat;
1N/A
1N/A##
1N/A## Process UnicodeData.txt (Categories, etc.)
1N/A##
1N/Asub UnicodeData_Txt()
1N/A{
1N/A my $Bidi = Table->New();
1N/A my $Deco = Table->New();
1N/A my $Comb = Table->New();
1N/A my $Number = Table->New();
1N/A my $Mirrored = Table->New(Is => 'Mirrored',
1N/A Desc => "Mirrored in bidirectional text",
1N/A Fuzzy => 0);
1N/A
1N/A my %DC;
1N/A my %Bidi;
1N/A my %Deco;
1N/A $Deco{Canon} = Table->New(Is => 'Canon',
1N/A Desc => 'Decomposes to multiple characters',
1N/A Fuzzy => 0);
1N/A $Deco{Compat} = Table->New(Is => 'Compat',
1N/A Desc => 'Compatible with a more-basic character',
1N/A Fuzzy => 0);
1N/A
1N/A ## Initialize Perl-generated categories
1N/A ## (Categories from UnicodeData.txt are auto-initialized in gencat)
1N/A $Cat{Alnum} =
1N/A Table->New(Is => 'Alnum', Desc => "[[:Alnum:]]", Fuzzy => 0);
1N/A $Cat{Alpha} =
1N/A Table->New(Is => 'Alpha', Desc => "[[:Alpha:]]", Fuzzy => 0);
1N/A $Cat{ASCII} =
1N/A Table->New(Is => 'ASCII', Desc => "[[:ASCII:]]", Fuzzy => 0);
1N/A $Cat{Blank} =
1N/A Table->New(Is => 'Blank', Desc => "[[:Blank:]]", Fuzzy => 0);
1N/A $Cat{Cntrl} =
1N/A Table->New(Is => 'Cntrl', Desc => "[[:Cntrl:]]", Fuzzy => 0);
1N/A $Cat{Digit} =
1N/A Table->New(Is => 'Digit', Desc => "[[:Digit:]]", Fuzzy => 0);
1N/A $Cat{Graph} =
1N/A Table->New(Is => 'Graph', Desc => "[[:Graph:]]", Fuzzy => 0);
1N/A $Cat{Lower} =
1N/A Table->New(Is => 'Lower', Desc => "[[:Lower:]]", Fuzzy => 0);
1N/A $Cat{Print} =
1N/A Table->New(Is => 'Print', Desc => "[[:Print:]]", Fuzzy => 0);
1N/A $Cat{Punct} =
1N/A Table->New(Is => 'Punct', Desc => "[[:Punct:]]", Fuzzy => 0);
1N/A $Cat{Space} =
1N/A Table->New(Is => 'Space', Desc => "[[:Space:]]", Fuzzy => 0);
1N/A $Cat{Title} =
1N/A Table->New(Is => 'Title', Desc => "[[:Title:]]", Fuzzy => 0);
1N/A $Cat{Upper} =
1N/A Table->New(Is => 'Upper', Desc => "[[:Upper:]]", Fuzzy => 0);
1N/A $Cat{XDigit} =
1N/A Table->New(Is => 'XDigit', Desc => "[[:XDigit:]]", Fuzzy => 0);
1N/A $Cat{Word} =
1N/A Table->New(Is => 'Word', Desc => "[[:Word:]]", Fuzzy => 0);
1N/A $Cat{SpacePerl} =
1N/A Table->New(Is => 'SpacePerl', Desc => '\s', Fuzzy => 0);
1N/A
1N/A my %To;
1N/A $To{Upper} = Table->New();
1N/A $To{Lower} = Table->New();
1N/A $To{Title} = Table->New();
1N/A $To{Digit} = Table->New();
1N/A
1N/A sub gencat($$$$)
1N/A {
1N/A my ($name, ## Name ("LATIN CAPITAL LETTER A")
1N/A $cat, ## Category ("Lu", "Zp", "Nd", etc.)
1N/A $code, ## Code point (as an integer)
1N/A $op) = @_;
1N/A
1N/A my $MajorCat = substr($cat, 0, 1); ## L, M, Z, S, etc
1N/A
1N/A $Assigned->$op($code);
1N/A $Name->$op($code, $name);
1N/A $General->$op($code, $cat);
1N/A
1N/A ## add to the sub category (e.g. "Lu", "Nd", "Cf", ..)
1N/A $Cat{$cat} ||= Table->New(Is => $cat,
1N/A Desc => "General Category '$cat'",
1N/A Fuzzy => 0);
1N/A $Cat{$cat}->$op($code);
1N/A
1N/A ## add to the major category (e.g. "L", "N", "C", ...)
1N/A $Cat{$MajorCat} ||= Table->New(Is => $MajorCat,
1N/A Desc => "Major Category '$MajorCat'",
1N/A Fuzzy => 0);
1N/A $Cat{$MajorCat}->$op($code);
1N/A
1N/A ($General{$name} ||= Table->New)->$op($code, $name);
1N/A
1N/A # 005F: SPACING UNDERSCORE
1N/A $Cat{Word}->$op($code) if $cat =~ /^[LMN]|Pc/;
1N/A $Cat{Alnum}->$op($code) if $cat =~ /^[LM]|Nd/;
1N/A $Cat{Alpha}->$op($code) if $cat =~ /^[LM]/;
1N/A
1N/A my $isspace =
1N/A ($cat =~ /Zs|Zl|Zp/ &&
1N/A $code != 0x200B) # 200B is ZWSP which is for line break control
1N/A # and therefore it is not part of "space" even while it is "Zs".
1N/A || $code == 0x0009 # 0009: HORIZONTAL TAB
1N/A || $code == 0x000A # 000A: LINE FEED
1N/A || $code == 0x000B # 000B: VERTICAL TAB
1N/A || $code == 0x000C # 000C: FORM FEED
1N/A || $code == 0x000D # 000D: CARRIAGE RETURN
1N/A || $code == 0x0085 # 0085: NEL
1N/A
1N/A ;
1N/A
1N/A $Cat{Space}->$op($code) if $isspace;
1N/A
1N/A $Cat{SpacePerl}->$op($code) if $isspace
1N/A && $code != 0x000B; # Backward compat.
1N/A
1N/A $Cat{Blank}->$op($code) if $isspace
1N/A && !($code == 0x000A ||
1N/A $code == 0x000B ||
1N/A $code == 0x000C ||
1N/A $code == 0x000D ||
1N/A $code == 0x0085 ||
1N/A $cat =~ /^Z[lp]/);
1N/A
1N/A $Cat{Digit}->$op($code) if $cat eq "Nd";
1N/A $Cat{Upper}->$op($code) if $cat eq "Lu";
1N/A $Cat{Lower}->$op($code) if $cat eq "Ll";
1N/A $Cat{Title}->$op($code) if $cat eq "Lt";
1N/A $Cat{ASCII}->$op($code) if $code <= 0x007F;
1N/A $Cat{Cntrl}->$op($code) if $cat =~ /^C/;
1N/A my $isgraph = !$isspace && $cat !~ /Cc|Cs|Cn/;
1N/A $Cat{Graph}->$op($code) if $isgraph;
1N/A $Cat{Print}->$op($code) if $isgraph || $isspace;
1N/A $Cat{Punct}->$op($code) if $cat =~ /^P/;
1N/A
1N/A $Cat{XDigit}->$op($code) if ($code >= 0x30 && $code <= 0x39) ## 0..9
1N/A || ($code >= 0x41 && $code <= 0x46) ## A..F
1N/A || ($code >= 0x61 && $code <= 0x66); ## a..f
1N/A }
1N/A
1N/A ## open ane read file.....
1N/A if (not open IN, "UnicodeData.txt") {
1N/A die "$0: UnicodeData.txt: $!\n";
1N/A }
1N/A
1N/A ##
1N/A ## For building \p{_CombAbove} and \p{_CanonDCIJ}
1N/A ##
1N/A my %_Above_HexCodes; ## Hexcodes for chars with $comb == 230 ("ABOVE")
1N/A
1N/A my %CodeToDeco; ## Maps code to decomp. list for chars with first
1N/A ## decomp. char an "i" or "j" (for \p{_CanonDCIJ})
1N/A
1N/A ## This is filled in as we go....
1N/A my $CombAbove = Table->New(Is => '_CombAbove',
1N/A Desc => '(for internal casefolding use)',
1N/A Fuzzy => 0);
1N/A
1N/A while (<IN>)
1N/A {
1N/A next unless /^[0-9A-Fa-f]+;/;
1N/A s/\s+$//;
1N/A
1N/A my ($hexcode, ## code point in hex (e.g. "0041")
1N/A $name, ## character name (e.g. "LATIN CAPITAL LETTER A")
1N/A $cat, ## category (e.g. "Lu")
1N/A $comb, ## Canonical combining class (e.t. "230")
1N/A $bidi, ## directional category (e.g. "L")
1N/A $deco, ## decomposition mapping
1N/A $decimal, ## decimal digit value
1N/A $digit, ## digit value
1N/A $number, ## numeric value
1N/A $mirrored, ## mirrored
1N/A $unicode10, ## name in Unicode 1.0
1N/A $comment, ## comment field
1N/A $upper, ## uppercase mapping
1N/A $lower, ## lowercase mapping
1N/A $title, ## titlecase mapping
1N/A ) = split(/\s*;\s*/);
1N/A
1N/A # Note that in Unicode 3.2 there will be names like
1N/A # LINE FEED (LF), which probably means that \N{} needs
1N/A # to cope also with LINE FEED and LF.
1N/A $name = $unicode10 if $name eq '<control>' && $unicode10 ne '';
1N/A
1N/A my $code = hex($hexcode);
1N/A
1N/A if ($comb and $comb == 230) {
1N/A $CombAbove->Append($code);
1N/A $_Above_HexCodes{$hexcode} = 1;
1N/A }
1N/A
1N/A ## Used in building \p{_CanonDCIJ}
1N/A if ($deco and $deco =~ m/^006[9A]\b/) {
1N/A $CodeToDeco{$code} = $deco;
1N/A }
1N/A
1N/A ##
1N/A ## There are a few pairs of lines like:
1N/A ## AC00;<Hangul Syllable, First>;Lo;0;L;;;;;N;;;;;
1N/A ## D7A3;<Hangul Syllable, Last>;Lo;0;L;;;;;N;;;;;
1N/A ## that define ranges.
1N/A ##
1N/A if ($name =~ /^<(.+), (First|Last)>$/)
1N/A {
1N/A $name = $1;
1N/A gencat($name, $cat, $code, $2 eq 'First' ? 'Append' : 'Extend');
1N/A #New_Prop(In => $name, $General{$name}, Fuzzy => 1);
1N/A }
1N/A else
1N/A {
1N/A ## normal (single-character) lines
1N/A gencat($name, $cat, $code, 'Append');
1N/A
1N/A # No Append() here since since several codes may map into one.
1N/A $To{Upper}->RawAppendRange($code, $code, $upper) if $upper;
1N/A $To{Lower}->RawAppendRange($code, $code, $lower) if $lower;
1N/A $To{Title}->RawAppendRange($code, $code, $title) if $title;
1N/A $To{Digit}->Append($code, $decimal) if length $decimal;
1N/A
1N/A $Bidi->Append($code, $bidi);
1N/A $Comb->Append($code, $comb) if $comb;
1N/A $Number->Append($code, $number) if length $number;
1N/A
1N/A $Mirrored->Append($code) if $mirrored eq "Y";
1N/A
1N/A $Bidi{$bidi} ||= Table->New(Is => "Bidi$bidi",
1N/A Desc => "Bi-directional category '$bidi'",
1N/A Fuzzy => 0);
1N/A $Bidi{$bidi}->Append($code);
1N/A
1N/A if ($deco)
1N/A {
1N/A $Deco->Append($code, $deco);
1N/A if ($deco =~/^<(\w+)>/)
1N/A {
1N/A $Deco{Compat}->Append($code);
1N/A
1N/A $DC{$1} ||= Table->New(Is => "DC$1",
1N/A Desc => "Compatible with '$1'",
1N/A Fuzzy => 0);
1N/A $DC{$1}->Append($code);
1N/A }
1N/A else
1N/A {
1N/A $Deco{Canon}->Append($code);
1N/A }
1N/A }
1N/A }
1N/A }
1N/A close IN;
1N/A
1N/A ##
1N/A ## Tidy up a few special cases....
1N/A ##
1N/A
1N/A $Cat{Cn} = $Assigned->Invert; ## Cn is everything that doesn't exist
1N/A New_Prop(Is => 'Cn',
1N/A $Cat{Cn},
1N/A Desc => "General Category 'Cn' [not functional in Perl]",
1N/A Fuzzy => 0);
1N/A
1N/A ## Unassigned is the same as 'Cn'
1N/A New_Alias(Is => 'Unassigned', SameAs => 'Cn', Fuzzy => 0);
1N/A
1N/A $Cat{C}->Replace($Cat{C}->Merge($Cat{Cn})); ## Now merge in Cn into C
1N/A
1N/A
1N/A # L& is Ll, Lu, and Lt.
1N/A New_Prop(Is => 'L&',
1N/A Table->Merge(@Cat{qw[Ll Lu Lt]}),
1N/A Desc => '[\p{Ll}\p{Lu}\p{Lt}]',
1N/A Fuzzy => 0);
1N/A
1N/A ## Any and All are all code points.
1N/A my $Any = Table->New(Is => 'Any',
1N/A Desc => sprintf("[\\x{0000}-\\x{%X}]",
1N/A $LastUnicodeCodepoint),
1N/A Fuzzy => 0);
1N/A $Any->RawAppendRange(0, $LastUnicodeCodepoint);
1N/A
1N/A New_Alias(Is => 'All', SameAs => 'Any', Fuzzy => 0);
1N/A
1N/A ##
1N/A ## Build special properties for Perl's internal case-folding needs:
1N/A ## \p{_CaseIgnorable}
1N/A ## \p{_CanonDCIJ}
1N/A ## \p{_CombAbove}
1N/A ## _CombAbove was built above. Others are built here....
1N/A ##
1N/A
1N/A ## \p{_CaseIgnorable} is [\p{Mn}\0x00AD\x2010]
1N/A New_Prop(Is => '_CaseIgnorable',
1N/A Table->Merge($Cat{Mn},
1N/A 0x00AD, #SOFT HYPHEN
1N/A 0x2010), #HYPHEN
1N/A Desc => '(for internal casefolding use)',
1N/A Fuzzy => 0);
1N/A
1N/A
1N/A ## \p{_CanonDCIJ} is fairly complex...
1N/A my $CanonCDIJ = Table->New(Is => '_CanonDCIJ',
1N/A Desc => '(for internal casefolding use)',
1N/A Fuzzy => 0);
1N/A ## It contains the ASCII 'i' and 'j'....
1N/A $CanonCDIJ->Append(0x0069); # ASCII ord("i")
1N/A $CanonCDIJ->Append(0x006A); # ASCII ord("j")
1N/A ## ...and any character with a decomposition that starts with either of
1N/A ## those code points, but only if the decomposition does not have any
1N/A ## combining character with the "ABOVE" canonical combining class.
1N/A for my $code (sort { $a <=> $b} keys %CodeToDeco)
1N/A {
1N/A ## Need to ensure that all decomposition characters do not have
1N/A ## a %HexCodeToComb in %AboveCombClasses.
1N/A my $want = 1;
1N/A for my $deco_hexcode (split / /, $CodeToDeco{$code})
1N/A {
1N/A if (exists $_Above_HexCodes{$deco_hexcode}) {
1N/A ## one of the decmposition chars has an ABOVE combination
1N/A ## class, so we're not interested in this one
1N/A $want = 0;
1N/A last;
1N/A }
1N/A }
1N/A if ($want) {
1N/A $CanonCDIJ->Append($code);
1N/A }
1N/A }
1N/A
1N/A
1N/A
1N/A ##
1N/A ## Now dump the files.
1N/A ##
1N/A $Name->Write("Name.pl");
1N/A $Bidi->Write("Bidirectional.pl");
1N/A $Comb->Write("CombiningClass.pl");
1N/A $Deco->Write("Decomposition.pl");
1N/A $Number->Write("Number.pl");
1N/A $General->Write("Category.pl");
1N/A
1N/A for my $to (sort keys %To) {
1N/A $To{$to}->Write("To/$to.pl");
1N/A }
1N/A}
1N/A
1N/A##
1N/A## Process LineBreak.txt
1N/A##
1N/Asub LineBreak_Txt()
1N/A{
1N/A if (not open IN, "LineBreak.txt") {
1N/A die "$0: LineBreak.txt: $!\n";
1N/A }
1N/A
1N/A my $Lbrk = Table->New();
1N/A my %Lbrk;
1N/A
1N/A while (<IN>)
1N/A {
1N/A next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(\w+)/;
1N/A
1N/A my ($first, $last, $lbrk) = (hex($1), hex($2||""), $3);
1N/A
1N/A $Lbrk->Append($first, $lbrk);
1N/A
1N/A $Lbrk{$lbrk} ||= Table->New(Is => "Lbrk$lbrk",
1N/A Desc => "Linebreak category '$lbrk'",
1N/A Fuzzy => 0);
1N/A $Lbrk{$lbrk}->Append($first);
1N/A
1N/A if ($last) {
1N/A $Lbrk->Extend($last);
1N/A $Lbrk{$lbrk}->Extend($last);
1N/A }
1N/A }
1N/A close IN;
1N/A
1N/A $Lbrk->Write("Lbrk.pl");
1N/A}
1N/A
1N/A##
1N/A## Process ArabicShaping.txt.
1N/A##
1N/Asub ArabicShaping_txt()
1N/A{
1N/A if (not open IN, "ArabicShaping.txt") {
1N/A die "$0: ArabicShaping.txt: $!\n";
1N/A }
1N/A
1N/A my $ArabLink = Table->New();
1N/A my $ArabLinkGroup = Table->New();
1N/A
1N/A while (<IN>)
1N/A {
1N/A next unless /^[0-9A-Fa-f]+;/;
1N/A s/\s+$//;
1N/A
1N/A my ($hexcode, $name, $link, $linkgroup) = split(/\s*;\s*/);
1N/A my $code = hex($hexcode);
1N/A $ArabLink->Append($code, $link);
1N/A $ArabLinkGroup->Append($code, $linkgroup);
1N/A }
1N/A close IN;
1N/A
1N/A $ArabLink->Write("ArabLink.pl");
1N/A $ArabLinkGroup->Write("ArabLnkGrp.pl");
1N/A}
1N/A
1N/A##
1N/A## Process Jamo.txt.
1N/A##
1N/Asub Jamo_txt()
1N/A{
1N/A if (not open IN, "Jamo.txt") {
1N/A die "$0: Jamo.txt: $!\n";
1N/A }
1N/A my $Short = Table->New();
1N/A
1N/A while (<IN>)
1N/A {
1N/A next unless /^([0-9A-Fa-f]+)\s*;\s*(\w*)/;
1N/A my ($code, $short) = (hex($1), $2);
1N/A
1N/A $Short->Append($code, $short);
1N/A }
1N/A close IN;
1N/A $Short->Write("JamoShort.pl");
1N/A}
1N/A
1N/A##
1N/A## Process Scripts.txt.
1N/A##
1N/Asub Scripts_txt()
1N/A{
1N/A my @ScriptInfo;
1N/A
1N/A if (not open(IN, "Scripts.txt")) {
1N/A die "$0: Scripts.txt: $!\n";
1N/A }
1N/A while (<IN>) {
1N/A next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(.+?)\s*\#/;
1N/A
1N/A # Wait until all the scripts have been read since
1N/A # they are not listed in numeric order.
1N/A push @ScriptInfo, [ hex($1), hex($2||""), $3 ];
1N/A }
1N/A close IN;
1N/A
1N/A # Now append the scripts properties in their code point order.
1N/A
1N/A my %Script;
1N/A my $Scripts = Table->New();
1N/A
1N/A for my $script (sort { $a->[0] <=> $b->[0] } @ScriptInfo)
1N/A {
1N/A my ($first, $last, $name) = @$script;
1N/A $Scripts->Append($first, $name);
1N/A
1N/A $Script{$name} ||= Table->New(Is => $name,
1N/A Desc => "Script '$name'",
1N/A Fuzzy => 1);
1N/A $Script{$name}->Append($first, $name);
1N/A
1N/A if ($last) {
1N/A $Scripts->Extend($last);
1N/A $Script{$name}->Extend($last);
1N/A }
1N/A }
1N/A
1N/A $Scripts->Write("Scripts.pl");
1N/A
1N/A ## Common is everything not explicitly assigned to a Script
1N/A ##
1N/A ## ***shouldn't this be intersected with \p{Assigned}? ******
1N/A ##
1N/A New_Prop(Is => 'Common',
1N/A $Scripts->Invert,
1N/A Desc => 'Pseudo-Script of codepoints not in other Unicode scripts',
1N/A Fuzzy => 1);
1N/A}
1N/A
1N/A##
1N/A## Given a name like "Close Punctuation", return a regex (that when applied
1N/A## with /i) matches any valid form of that name (e.g. "ClosePunctuation",
1N/A## "Close-Punctuation", etc.)
1N/A##
1N/A## Accept any space, dash, or underbar where in the official name there is
1N/A## space or a dash (or underbar, but there never is).
1N/A##
1N/A##
1N/Asub NameToRegex($)
1N/A{
1N/A my $Name = shift;
1N/A $Name =~ s/[- _]/(?:[-_]|\\s+)?/g;
1N/A return $Name;
1N/A}
1N/A
1N/A##
1N/A## Process Blocks.txt.
1N/A##
1N/Asub Blocks_txt()
1N/A{
1N/A my $Blocks = Table->New();
1N/A my %Blocks;
1N/A
1N/A if (not open IN, "Blocks.txt") {
1N/A die "$0: Blocks.txt: $!\n";
1N/A }
1N/A
1N/A while (<IN>)
1N/A {
1N/A #next if not /Private Use$/;
1N/A next if not /^([0-9A-Fa-f]+)\.\.([0-9A-Fa-f]+)\s*;\s*(.+?)\s*$/;
1N/A
1N/A my ($first, $last, $name) = (hex($1), hex($2), $3);
1N/A
1N/A $Blocks->Append($first, $name);
1N/A
1N/A $Blocks{$name} ||= Table->New(In => $name,
1N/A Desc => "Block '$name'",
1N/A Fuzzy => 1);
1N/A $Blocks{$name}->Append($first, $name);
1N/A
1N/A if ($last and $last != $first) {
1N/A $Blocks->Extend($last);
1N/A $Blocks{$name}->Extend($last);
1N/A }
1N/A }
1N/A close IN;
1N/A
1N/A $Blocks->Write("Blocks.pl");
1N/A}
1N/A
1N/A##
1N/A## Read in the PropList.txt. It contains extended properties not
1N/A## listed in the UnicodeData.txt, such as 'Other_Alphabetic':
1N/A## alphabetic but not of the general category L; many modifiers
1N/A## belong to this extended property category: while they are not
1N/A## alphabets, they are alphabetic in nature.
1N/A##
1N/Asub PropList_txt()
1N/A{
1N/A my @PropInfo;
1N/A
1N/A if (not open IN, "PropList.txt") {
1N/A die "$0: PropList.txt: $!\n";
1N/A }
1N/A
1N/A while (<IN>)
1N/A {
1N/A next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(.+?)\s*\#/;
1N/A
1N/A # Wait until all the extended properties have been read since
1N/A # they are not listed in numeric order.
1N/A push @PropInfo, [ hex($1), hex($2||""), $3 ];
1N/A }
1N/A close IN;
1N/A
1N/A # Now append the extended properties in their code point order.
1N/A my $Props = Table->New();
1N/A my %Prop;
1N/A
1N/A for my $prop (sort { $a->[0] <=> $b->[0] } @PropInfo)
1N/A {
1N/A my ($first, $last, $name) = @$prop;
1N/A $Props->Append($first, $name);
1N/A
1N/A $Prop{$name} ||= Table->New(Is => $name,
1N/A Desc => "Extended property '$name'",
1N/A Fuzzy => 1);
1N/A $Prop{$name}->Append($first, $name);
1N/A
1N/A if ($last) {
1N/A $Props->Extend($last);
1N/A $Prop{$name}->Extend($last);
1N/A }
1N/A }
1N/A
1N/A # Alphabetic is L and Other_Alphabetic.
1N/A New_Prop(Is => 'Alphabetic',
1N/A Table->Merge($Cat{L}, $Prop{Other_Alphabetic}),
1N/A Desc => '[\p{L}\p{OtherAlphabetic}]', # use canonical names here
1N/A Fuzzy => 1);
1N/A
1N/A # Lowercase is Ll and Other_Lowercase.
1N/A New_Prop(Is => 'Lowercase',
1N/A Table->Merge($Cat{Ll}, $Prop{Other_Lowercase}),
1N/A Desc => '[\p{Ll}\p{OtherLowercase}]', # use canonical names here
1N/A Fuzzy => 1);
1N/A
1N/A # Uppercase is Lu and Other_Uppercase.
1N/A New_Prop(Is => 'Uppercase',
1N/A Table->Merge($Cat{Lu}, $Prop{Other_Uppercase}),
1N/A Desc => '[\p{Lu}\p{Other_Uppercase}]', # use canonical names here
1N/A Fuzzy => 1);
1N/A
1N/A # Math is Sm and Other_Math.
1N/A New_Prop(Is => 'Math',
1N/A Table->Merge($Cat{Sm}, $Prop{Other_Math}),
1N/A Desc => '[\p{Sm}\p{OtherMath}]', # use canonical names here
1N/A Fuzzy => 1);
1N/A
1N/A # ID_Start is Ll, Lu, Lt, Lm, Lo, and Nl.
1N/A New_Prop(Is => 'ID_Start',
1N/A Table->Merge(@Cat{qw[Ll Lu Lt Lm Lo Nl]}),
1N/A Desc => '[\p{Ll}\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{Nl}]',
1N/A Fuzzy => 1);
1N/A
1N/A # ID_Continue is ID_Start, Mn, Mc, Nd, and Pc.
1N/A New_Prop(Is => 'ID_Continue',
1N/A Table->Merge(@Cat{qw[Ll Lu Lt Lm Lo Nl Mn Mc Nd Pc ]}),
1N/A Desc => '[\p{ID_Start}\p{Mn}\p{Mc}\p{Nd}\p{Pc}]',
1N/A Fuzzy => 1);
1N/A}
1N/A
1N/Asub Make_GC_Aliases()
1N/A{
1N/A ##
1N/A ## The mapping from General Category long forms to short forms is
1N/A ## currently hardwired here since no simple data file in the UCD
1N/A ## seems to do that. Unicode 3.2 will assumedly correct this.
1N/A ##
1N/A my %Is = (
1N/A 'Letter' => 'L',
1N/A 'Uppercase_Letter' => 'Lu',
1N/A 'Lowercase_Letter' => 'Ll',
1N/A 'Titlecase_Letter' => 'Lt',
1N/A 'Modifier_Letter' => 'Lm',
1N/A 'Other_Letter' => 'Lo',
1N/A
1N/A 'Mark' => 'M',
1N/A 'Non_Spacing_Mark' => 'Mn',
1N/A 'Spacing_Mark' => 'Mc',
1N/A 'Enclosing_Mark' => 'Me',
1N/A
1N/A 'Separator' => 'Z',
1N/A 'Space_Separator' => 'Zs',
1N/A 'Line_Separator' => 'Zl',
1N/A 'Paragraph_Separator' => 'Zp',
1N/A
1N/A 'Number' => 'N',
1N/A 'Decimal_Number' => 'Nd',
1N/A 'Letter_Number' => 'Nl',
1N/A 'Other_Number' => 'No',
1N/A
1N/A 'Punctuation' => 'P',
1N/A 'Connector_Punctuation' => 'Pc',
1N/A 'Dash_Punctuation' => 'Pd',
1N/A 'Open_Punctuation' => 'Ps',
1N/A 'Close_Punctuation' => 'Pe',
1N/A 'Initial_Punctuation' => 'Pi',
1N/A 'Final_Punctuation' => 'Pf',
1N/A 'Other_Punctuation' => 'Po',
1N/A
1N/A 'Symbol' => 'S',
1N/A 'Math_Symbol' => 'Sm',
1N/A 'Currency_Symbol' => 'Sc',
1N/A 'Modifier_Symbol' => 'Sk',
1N/A 'Other_Symbol' => 'So',
1N/A
1N/A 'Other' => 'C',
1N/A 'Control' => 'Cc',
1N/A 'Format' => 'Cf',
1N/A 'Surrogate' => 'Cs',
1N/A 'Private Use' => 'Co',
1N/A 'Unassigned' => 'Cn',
1N/A );
1N/A
1N/A ## make the aliases....
1N/A while (my ($Alias, $Name) = each %Is) {
1N/A New_Alias(Is => $Alias, SameAs => $Name, Fuzzy => 1);
1N/A }
1N/A}
1N/A
1N/A
1N/A##
1N/A## These are used in:
1N/A## MakePropTestScript()
1N/A## WriteAllMappings()
1N/A## for making the test script.
1N/A##
1N/Amy %FuzzyNameToTest;
1N/Amy %ExactNameToTest;
1N/A
1N/A
1N/A## This used only for making the test script
1N/Asub GenTests($$$$)
1N/A{
1N/A my $FH = shift;
1N/A my $Prop = shift;
1N/A my $MatchCode = shift;
1N/A my $FailCode = shift;
1N/A
1N/A if (defined $MatchCode) {
1N/A printf $FH qq/Expect(1, "\\x{%04X}", '\\p{$Prop}' );\n/, $MatchCode;
1N/A printf $FH qq/Expect(0, "\\x{%04X}", '\\p{^$Prop}');\n/, $MatchCode;
1N/A printf $FH qq/Expect(0, "\\x{%04X}", '\\P{$Prop}' );\n/, $MatchCode;
1N/A printf $FH qq/Expect(1, "\\x{%04X}", '\\P{^$Prop}');\n/, $MatchCode;
1N/A }
1N/A if (defined $FailCode) {
1N/A printf $FH qq/Expect(0, "\\x{%04X}", '\\p{$Prop}' );\n/, $FailCode;
1N/A printf $FH qq/Expect(1, "\\x{%04X}", '\\p{^$Prop}');\n/, $FailCode;
1N/A printf $FH qq/Expect(1, "\\x{%04X}", '\\P{$Prop}' );\n/, $FailCode;
1N/A printf $FH qq/Expect(0, "\\x{%04X}", '\\P{^$Prop}');\n/, $FailCode;
1N/A }
1N/A}
1N/A
1N/A## This used only for making the test script
1N/Asub ExpectError($$)
1N/A{
1N/A my $FH = shift;
1N/A my $prop = shift;
1N/A
1N/A print $FH qq/Error('\\p{$prop}');\n/;
1N/A print $FH qq/Error('\\P{$prop}');\n/;
1N/A}
1N/A
1N/A## This used only for making the test script
1N/Amy @GoodSeps = (
1N/A " ",
1N/A "-",
1N/A " \t ",
1N/A "",
1N/A "",
1N/A "_",
1N/A );
1N/Amy @BadSeps = (
1N/A "--",
1N/A "__",
1N/A " _",
1N/A "/"
1N/A );
1N/A
1N/A## This used only for making the test script
1N/Asub RandomlyFuzzifyName($;$)
1N/A{
1N/A my $Name = shift;
1N/A my $WantError = shift; ## if true, make an error
1N/A
1N/A my @parts;
1N/A for my $part (split /[-\s_]+/, $Name)
1N/A {
1N/A if (@parts) {
1N/A if ($WantError and rand() < 0.3) {
1N/A push @parts, $BadSeps[rand(@BadSeps)];
1N/A $WantError = 0;
1N/A } else {
1N/A push @parts, $GoodSeps[rand(@GoodSeps)];
1N/A }
1N/A }
1N/A my $switch = int rand(4);
1N/A if ($switch == 0) {
1N/A push @parts, uc $part;
1N/A } elsif ($switch == 1) {
1N/A push @parts, lc $part;
1N/A } elsif ($switch == 2) {
1N/A push @parts, ucfirst $part;
1N/A } else {
1N/A push @parts, $part;
1N/A }
1N/A }
1N/A my $new = join('', @parts);
1N/A
1N/A if ($WantError) {
1N/A if (rand() >= 0.5) {
1N/A $new .= $BadSeps[rand(@BadSeps)];
1N/A } else {
1N/A $new = $BadSeps[rand(@BadSeps)] . $new;
1N/A }
1N/A }
1N/A return $new;
1N/A}
1N/A
1N/A## This used only for making the test script
1N/Asub MakePropTestScript()
1N/A{
1N/A ## this written directly -- it's huge.
1N/A if (not open OUT, ">TestProp.pl") {
1N/A die "$0: TestProp.pl: $!\n";
1N/A }
1N/A print OUT <DATA>;
1N/A
1N/A while (my ($Name, $Table) = each %ExactNameToTest)
1N/A {
1N/A GenTests(*OUT, $Name, $Table->ValidCode, $Table->InvalidCode);
1N/A ExpectError(*OUT, uc $Name) if uc $Name ne $Name;
1N/A ExpectError(*OUT, lc $Name) if lc $Name ne $Name;
1N/A }
1N/A
1N/A
1N/A while (my ($Name, $Table) = each %FuzzyNameToTest)
1N/A {
1N/A my $Orig = $CanonicalToOrig{$Name};
1N/A my %Names = (
1N/A $Name => 1,
1N/A $Orig => 1,
1N/A RandomlyFuzzifyName($Orig) => 1
1N/A );
1N/A
1N/A for my $N (keys %Names) {
1N/A GenTests(*OUT, $N, $Table->ValidCode, $Table->InvalidCode);
1N/A }
1N/A
1N/A ExpectError(*OUT, RandomlyFuzzifyName($Orig, 'ERROR'));
1N/A }
1N/A
1N/A print OUT "Finished();\n";
1N/A close OUT;
1N/A}
1N/A
1N/A
1N/A##
1N/A## These are used only in:
1N/A## RegisterFileForName()
1N/A## WriteAllMappings()
1N/A##
1N/Amy %Exact; ## will become %utf8::Exact;
1N/Amy %Canonical; ## will become %utf8::Canonical;
1N/Amy %CaComment; ## Comment for %Canonical entry of same key
1N/A
1N/A##
1N/A## Given info about a name and a datafile that it should be associated with,
1N/A## register that assocation in %Exact and %Canonical.
1N/Asub RegisterFileForName($$$$)
1N/A{
1N/A my $Type = shift;
1N/A my $Name = shift;
1N/A my $IsFuzzy = shift;
1N/A my $filename = shift;
1N/A
1N/A ##
1N/A ## Now in details for the mapping. $Type eq 'Is' has the
1N/A ## Is removed, as it will be removed in utf8_heavy when this
1N/A ## data is being checked. In keeps its "In", but a second
1N/A ## sans-In record is written if it doesn't conflict with
1N/A ## anything already there.
1N/A ##
1N/A if (not $IsFuzzy)
1N/A {
1N/A if ($Type eq 'Is') {
1N/A die "oops[$Name]" if $Exact{$Name};
1N/A $Exact{$Name} = $filename;
1N/A } else {
1N/A die "oops[$Type$Name]" if $Exact{"$Type$Name"};
1N/A $Exact{"$Type$Name"} = $filename;
1N/A $Exact{$Name} = $filename if not $Exact{$Name};
1N/A }
1N/A }
1N/A else
1N/A {
1N/A my $CName = lc $Name;
1N/A if ($Type eq 'Is') {
1N/A die "oops[$CName]" if $Canonical{$CName};
1N/A $Canonical{$CName} = $filename;
1N/A $CaComment{$CName} = $Name if $Name =~ tr/A-Z// >= 2;
1N/A } else {
1N/A die "oops[$Type$CName]" if $Canonical{lc "$Type$CName"};
1N/A $Canonical{lc "$Type$CName"} = $filename;
1N/A $CaComment{lc "$Type$CName"} = "$Type$Name";
1N/A if (not $Canonical{$CName}) {
1N/A $Canonical{$CName} = $filename;
1N/A $CaComment{$CName} = "$Type$Name";
1N/A }
1N/A }
1N/A }
1N/A}
1N/A
1N/A##
1N/A## Writes the info accumulated in
1N/A##
1N/A## %TableInfo;
1N/A## %FuzzyNames;
1N/A## %AliasInfo;
1N/A##
1N/A##
1N/Asub WriteAllMappings()
1N/A{
1N/A my @MAP;
1N/A
1N/A my %BaseNames; ## Base names already used (for avoiding 8.3 conflicts)
1N/A
1N/A ## 'Is' *MUST* come first, so its names have precidence over 'In's
1N/A for my $Type ('Is', 'In')
1N/A {
1N/A my %RawNameToFile; ## a per-$Type cache
1N/A
1N/A for my $Name (sort {length $a <=> length $b} keys %{$TableInfo{$Type}})
1N/A {
1N/A ## Note: $Name is already canonical
1N/A my $Table = $TableInfo{$Type}->{$Name};
1N/A my $IsFuzzy = $FuzzyNames{$Type}->{$Name};
1N/A
1N/A ## Need an 8.3 safe filename (which means "an 8 safe" $filename)
1N/A my $filename;
1N/A {
1N/A ## 'Is' items lose 'Is' from the basename.
1N/A $filename = $Type eq 'Is' ? $Name : "$Type$Name";
1N/A
1N/A $filename =~ s/[^\w_]+/_/g; # "L&" -> "L_"
1N/A substr($filename, 8) = '' if length($filename) > 8;
1N/A
1N/A ##
1N/A ## Make sure the basename doesn't conflict with something we
1N/A ## might have already written. If we have, say,
1N/A ## InGreekExtended1
1N/A ## InGreekExtended2
1N/A ## they become
1N/A ## InGreekE
1N/A ## InGreek2
1N/A ##
1N/A while (my $num = $BaseNames{lc $filename}++)
1N/A {
1N/A $num++; ## so basenames with numbers start with '2', which
1N/A ## just looks more natural.
1N/A ## Want to append $num, but if it'll make the basename longer
1N/A ## than 8 characters, pre-truncate $filename so that the result
1N/A ## is acceptable.
1N/A my $delta = length($filename) + length($num) - 8;
1N/A if ($delta > 0) {
1N/A substr($filename, -$delta) = $num;
1N/A } else {
1N/A $filename .= $num;
1N/A }
1N/A }
1N/A };
1N/A
1N/A ##
1N/A ## Construct a nice comment to add to the file, and build data
1N/A ## for the "./Properties" file along the way.
1N/A ##
1N/A my $Comment;
1N/A {
1N/A my $Desc = $TableDesc{$Type}->{$Name} || "";
1N/A ## get list of names this table is reference by
1N/A my @Supported = $Name;
1N/A while (my ($Orig, $Alias) = each %{ $AliasInfo{$Type} })
1N/A {
1N/A if ($Orig eq $Name) {
1N/A push @Supported, $Alias;
1N/A }
1N/A }
1N/A
1N/A my $TypeToShow = $Type eq 'Is' ? "" : $Type;
1N/A my $OrigProp;
1N/A
1N/A $Comment = "This file supports:\n";
1N/A for my $N (@Supported)
1N/A {
1N/A my $IsFuzzy = $FuzzyNames{$Type}->{$N};
1N/A my $Prop = "\\p{$TypeToShow$Name}";
1N/A $OrigProp = $Prop if not $OrigProp; #cache for aliases
1N/A if ($IsFuzzy) {
1N/A $Comment .= "\t$Prop (and fuzzy permutations)\n";
1N/A } else {
1N/A $Comment .= "\t$Prop\n";
1N/A }
1N/A my $MyDesc = ($N eq $Name) ? $Desc : "Alias for $OrigProp ($Desc)";
1N/A
1N/A push @MAP, sprintf("%s %-42s %s\n",
1N/A $IsFuzzy ? '*' : ' ', $Prop, $MyDesc);
1N/A }
1N/A if ($Desc) {
1N/A $Comment .= "\nMeaning: $Desc\n";
1N/A }
1N/A
1N/A }
1N/A ##
1N/A ## Okay, write the file...
1N/A ##
1N/A $Table->Write("lib/$filename.pl", $Comment);
1N/A
1N/A ## and register it
1N/A $RawNameToFile{$Name} = $filename;
1N/A RegisterFileForName($Type => $Name, $IsFuzzy, $filename);
1N/A
1N/A if ($IsFuzzy)
1N/A {
1N/A my $CName = CanonicalName($Type . '_'. $Name);
1N/A $FuzzyNameToTest{$Name} = $Table if !$FuzzyNameToTest{$Name};
1N/A $FuzzyNameToTest{$CName} = $Table if !$FuzzyNameToTest{$CName};
1N/A } else {
1N/A $ExactNameToTest{$Name} = $Table;
1N/A }
1N/A
1N/A }
1N/A
1N/A ## Register aliase info
1N/A for my $Name (sort {length $a <=> length $b} keys %{$AliasInfo{$Type}})
1N/A {
1N/A my $Alias = $AliasInfo{$Type}->{$Name};
1N/A my $IsFuzzy = $FuzzyNames{$Type}->{$Alias};
1N/A my $filename = $RawNameToFile{$Name};
1N/A die "oops [$Alias]->[$Name]" if not $filename;
1N/A RegisterFileForName($Type => $Alias, $IsFuzzy, $filename);
1N/A
1N/A my $Table = $TableInfo{$Type}->{$Name};
1N/A die "oops" if not $Table;
1N/A if ($IsFuzzy)
1N/A {
1N/A my $CName = CanonicalName($Type .'_'. $Alias);
1N/A $FuzzyNameToTest{$Alias} = $Table if !$FuzzyNameToTest{$Alias};
1N/A $FuzzyNameToTest{$CName} = $Table if !$FuzzyNameToTest{$CName};
1N/A } else {
1N/A $ExactNameToTest{$Alias} = $Table;
1N/A }
1N/A }
1N/A }
1N/A
1N/A ##
1N/A ## Write out the property list
1N/A ##
1N/A {
1N/A my @OUT = (
1N/A "##\n",
1N/A "## This file created by $0\n",
1N/A "## List of built-in \\p{...}/\\P{...} properties.\n",
1N/A "##\n",
1N/A "## '*' means name may be 'fuzzy'\n",
1N/A "##\n\n",
1N/A sort { substr($a,2) cmp substr($b, 2) } @MAP,
1N/A );
1N/A WriteIfChanged('Properties', @OUT);
1N/A }
1N/A
1N/A use Text::Tabs (); ## using this makes the files about half the size
1N/A
1N/A ## Write Exact.pl
1N/A {
1N/A my @OUT = (
1N/A $HEADER,
1N/A "##\n",
1N/A "## Data in this file used by ../utf8_heavy.pl\n",
1N/A "##\n\n",
1N/A "## Mapping from name to filename in ./lib\n",
1N/A "%utf8::Exact = (\n",
1N/A );
1N/A
1N/A for my $Name (sort keys %Exact)
1N/A {
1N/A my $File = $Exact{$Name};
1N/A $Name = $Name =~ m/\W/ ? qq/'$Name'/ : " $Name ";
1N/A my $Text = sprintf("%-15s => %s,\n", $Name, qq/'$File'/);
1N/A push @OUT, Text::Tabs::unexpand($Text);
1N/A }
1N/A push @OUT, ");\n1;\n";
1N/A
1N/A WriteIfChanged('Exact.pl', @OUT);
1N/A }
1N/A
1N/A ## Write Canonical.pl
1N/A {
1N/A my @OUT = (
1N/A $HEADER,
1N/A "##\n",
1N/A "## Data in this file used by ../utf8_heavy.pl\n",
1N/A "##\n\n",
1N/A "## Mapping from lc(canonical name) to filename in ./lib\n",
1N/A "%utf8::Canonical = (\n",
1N/A );
1N/A my $Trail = ""; ## used just to keep the spacing pretty
1N/A for my $Name (sort keys %Canonical)
1N/A {
1N/A my $File = $Canonical{$Name};
1N/A if ($CaComment{$Name}) {
1N/A push @OUT, "\n" if not $Trail;
1N/A push @OUT, " # $CaComment{$Name}\n";
1N/A $Trail = "\n";
1N/A } else {
1N/A $Trail = "";
1N/A }
1N/A $Name = $Name =~ m/\W/ ? qq/'$Name'/ : " $Name ";
1N/A my $Text = sprintf(" %-41s => %s,\n$Trail", $Name, qq/'$File'/);
1N/A push @OUT, Text::Tabs::unexpand($Text);
1N/A }
1N/A push @OUT, ");\n1\n";
1N/A WriteIfChanged('Canonical.pl', @OUT);
1N/A }
1N/A
1N/A MakePropTestScript() if $MakeTestScript;
1N/A}
1N/A
1N/A
1N/Asub SpecialCasing_txt()
1N/A{
1N/A #
1N/A # Read in the special cases.
1N/A #
1N/A
1N/A my %CaseInfo;
1N/A
1N/A if (not open IN, "SpecialCasing.txt") {
1N/A die "$0: SpecialCasing.txt: $!\n";
1N/A }
1N/A while (<IN>) {
1N/A next unless /^[0-9A-Fa-f]+;/;
1N/A s/\#.*//;
1N/A s/\s+$//;
1N/A
1N/A my ($code, $lower, $title, $upper, $condition) = split(/\s*;\s*/);
1N/A
1N/A if ($condition) { # not implemented yet
1N/A print "# SKIPPING $_\n" if $Verbose;
1N/A next;
1N/A }
1N/A
1N/A # Wait until all the special cases have been read since
1N/A # they are not listed in numeric order.
1N/A my $ix = hex($code);
1N/A push @{$CaseInfo{Lower}}, [ $ix, $code, $lower ]
1N/A unless $code eq $lower;
1N/A push @{$CaseInfo{Title}}, [ $ix, $code, $title ]
1N/A unless $code eq $title;
1N/A push @{$CaseInfo{Upper}}, [ $ix, $code, $upper ]
1N/A unless $code eq $upper;
1N/A }
1N/A close IN;
1N/A
1N/A # Now write out the special cases properties in their code point order.
1N/A # Prepend them to the To/{Upper,Lower,Title}.pl.
1N/A
1N/A for my $case (qw(Lower Title Upper))
1N/A {
1N/A my $NormalCase = do "To/$case.pl" || die "$0: $@\n";
1N/A
1N/A my @OUT =
1N/A (
1N/A $HEADER, "\n",
1N/A "# The key UTF-8 _bytes_, the value UTF-8 (speed hack)\n",
1N/A "%utf8::ToSpec$case =\n(\n",
1N/A );
1N/A
1N/A for my $prop (sort { $a->[0] <=> $b->[0] } @{$CaseInfo{$case}}) {
1N/A my ($ix, $code, $to) = @$prop;
1N/A my $tostr =
1N/A join "", map { sprintf "\\x{%s}", $_ } split ' ', $to;
1N/A push @OUT, sprintf qq["%s" => "$tostr",\n], join("", map { sprintf "\\x%02X", $_ } unpack("U0C*", pack("U", $ix)));
1N/A # Remove any single-character mappings for
1N/A # the same character since we are going for
1N/A # the special casing rules.
1N/A $NormalCase =~ s/^$code\t\t\w+\n//m;
1N/A }
1N/A push @OUT, (
1N/A ");\n\n",
1N/A "return <<'END';\n",
1N/A $NormalCase,
1N/A "END\n"
1N/A );
1N/A WriteIfChanged("To/$case.pl", @OUT);
1N/A }
1N/A}
1N/A
1N/A#
1N/A# Read in the case foldings.
1N/A#
1N/A# We will do full case folding, C + F + I (see CaseFolding.txt).
1N/A#
1N/Asub CaseFolding_txt()
1N/A{
1N/A if (not open IN, "CaseFolding.txt") {
1N/A die "$0: CaseFolding.txt: $!\n";
1N/A }
1N/A
1N/A my $Fold = Table->New();
1N/A my %Fold;
1N/A
1N/A while (<IN>) {
1N/A # Skip status 'S', simple case folding
1N/A next unless /^([0-9A-Fa-f]+)\s*;\s*([CFI])\s*;\s*([0-9A-Fa-f]+(?: [0-9A-Fa-f]+)*)\s*;/;
1N/A
1N/A my ($code, $status, $fold) = (hex($1), $2, $3);
1N/A
1N/A if ($status eq 'C') { # Common: one-to-one folding
1N/A # No append() since several codes may fold into one.
1N/A $Fold->RawAppendRange($code, $code, $fold);
1N/A } else { # F: full, or I: dotted uppercase I -> dotless lowercase I
1N/A $Fold{$code} = $fold;
1N/A }
1N/A }
1N/A close IN;
1N/A
1N/A $Fold->Write("To/Fold.pl");
1N/A
1N/A #
1N/A # Prepend the special foldings to the common foldings.
1N/A #
1N/A my $CommonFold = do "To/Fold.pl" || die "$0: To/Fold.pl: $!\n";
1N/A
1N/A my @OUT =
1N/A (
1N/A $HEADER, "\n",
1N/A "# The ke UTF-8 _bytes_, the value UTF-8 (speed hack)\n",
1N/A "%utf8::ToSpecFold =\n(\n",
1N/A );
1N/A for my $code (sort { $a <=> $b } keys %Fold) {
1N/A my $foldstr =
1N/A join "", map { sprintf "\\x{%s}", $_ } split ' ', $Fold{$code};
1N/A push @OUT, sprintf qq["%s" => "$foldstr",\n], join("", map { sprintf "\\x%02X", $_ } unpack("U0C*", pack("U", $code)));
1N/A }
1N/A push @OUT, (
1N/A ");\n\n",
1N/A "return <<'END';\n",
1N/A $CommonFold,
1N/A "END\n",
1N/A );
1N/A
1N/A WriteIfChanged("To/Fold.pl", @OUT);
1N/A}
1N/A
1N/A## Do it....
1N/A
1N/AUnicodeData_Txt();
1N/AMake_GC_Aliases();
1N/APropList_txt();
1N/A
1N/AScripts_txt();
1N/ABlocks_txt();
1N/A
1N/AWriteAllMappings();
1N/A
1N/ALineBreak_Txt();
1N/AArabicShaping_txt();
1N/AJamo_txt();
1N/ASpecialCasing_txt();
1N/ACaseFolding_txt();
1N/A
1N/Aexit(0);
1N/A
1N/A## TRAILING CODE IS USED BY MakePropTestScript()
1N/A__DATA__
1N/Ause strict;
1N/Ause warnings;
1N/A
1N/Amy $Tests = 0;
1N/Amy $Fails = 0;
1N/A
1N/Asub Expect($$$)
1N/A{
1N/A my $Expect = shift;
1N/A my $String = shift;
1N/A my $Regex = shift;
1N/A my $Line = (caller)[2];
1N/A
1N/A $Tests++;
1N/A my $RegObj;
1N/A my $result = eval {
1N/A $RegObj = qr/$Regex/;
1N/A $String =~ $RegObj ? 1 : 0
1N/A };
1N/A
1N/A if (not defined $result) {
1N/A print "couldn't compile /$Regex/ on $0 line $Line: $@\n";
1N/A $Fails++;
1N/A } elsif ($result ^ $Expect) {
1N/A print "bad result (expected $Expect) on $0 line $Line: $@\n";
1N/A $Fails++;
1N/A }
1N/A}
1N/A
1N/Asub Error($)
1N/A{
1N/A my $Regex = shift;
1N/A $Tests++;
1N/A if (eval { 'x' =~ qr/$Regex/; 1 }) {
1N/A $Fails++;
1N/A my $Line = (caller)[2];
1N/A print "expected error for /$Regex/ on $0 line $Line: $@\n";
1N/A }
1N/A}
1N/A
1N/Asub Finished()
1N/A{
1N/A if ($Fails == 0) {
1N/A print "All $Tests tests passed.\n";
1N/A exit(0);
1N/A } else {
1N/A print "$Tests tests, $Fails failed!\n";
1N/A exit(-1);
1N/A }
1N/A}