chiark / gitweb /
start on ut32__unidata() which will provide a uniform interface
[disorder] / scripts / make-unidata
index 58de22ff81a446beaac3a9da061326f317fc3dd0..b78d289fb593f0143a9e7550aa61c19e3ee964fc 100755 (executable)
@@ -82,6 +82,7 @@ sub input {
        chmod(0444, $lpath) or die "$lpath: $!\n";
     }
     open(STDIN, "<$lpath") or die "$lpath: $!\n";
+    print STDERR "Reading $lpath...\n";
 }
 
 
@@ -94,6 +95,12 @@ while(<>) {
     # TODO justify this exclusion!
     my $name = $f[1];
     my $gc = $f[2];            # General_Category
+    # Variuos GCs we don't expect to see in UnicodeData.txt
+    $cats{$gc} = 1;            # always record all GCs
+    next if $name =~ /(first|last)>/i; # ignore placeholders
+    die "unexpected Cn" if $gc eq 'Cn';
+    die "unexpected Co" if $gc eq 'Cn';
+    die "unexpected Cs" if $gc eq 'Cs';
     my $ccc = $f[3];           # Canonical_Combining_Class
     my $dm = $f[5];            # Decomposition_Type + Decomposition_Mapping
     my $sum = hex($f[12]) || $c; # Simple_Uppercase_Mapping
@@ -152,42 +159,93 @@ sub read_prop_with_ranges {
     }
 }
 
-# Grapheme_Break
+# Grapheme_Break etc
 # NB we do this BEFORE filling in blanks so that the Hangul characters
 # don't get filled in; we can compute their properties mechanically.
 read_prop_with_ranges("auxiliary/GraphemeBreakProperty.txt", "gbreak");
-
-# Word_Break
-# Same remarks about Hangul as above.  This one currently seems just too
-# complicated to do programmatically so we'll take a byte to store it.
 read_prop_with_ranges("auxiliary/WordBreakProperty.txt", "wbreak");
+read_prop_with_ranges("auxiliary/SentenceBreakProperty.txt", "sbreak");
 
-# Make the list of Word_Break values
-my %wbpropvals = ();
+# Compute the full list and fill in the Extend category properly
+my %gbreak = ();
+my %wbreak = ();
+my %sbreak = ();
 for my $c (keys %data) {
+    if(!exists $data{$c}->{gbreak}) {
+       $data{$c}->{gbreak} = 'Other';
+    }
+    $gbreak{$data{$c}->{gbreak}} = 1;
+
     if(!exists $data{$c}->{wbreak}) {
-       if(exists $data{$c}->{gbreak} && $data{$c}->{gbreak} eq 'Extend') {
+       if($data{$c}->{gbreak} eq 'Extend') {
            $data{$c}->{wbreak} = 'Extend';
        } else {
            $data{$c}->{wbreak} = 'Other';
        }
     }
-    $wbpropvals{$data{$c}->{wbreak}} = 1;
+    $wbreak{$data{$c}->{wbreak}} = 1;
+
+    if(!exists $data{$c}->{sbreak}) {
+       if($data{$c}->{gbreak} eq 'Extend') {
+           $data{$c}->{sbreak} = 'Extend';
+       } else {
+           $data{$c}->{sbreak} = 'Other';
+       }
+    }
+    $sbreak{$data{$c}->{sbreak}} = 1;
 }
 
 # Round up the maximum value to a whole number of subtables
 $max += ($modulus - 1) - ($max % $modulus);
 
-# Make sure there are no gaps
+# Surrogates
+my $Cs = {
+    "gc" => "Cs",              # UTF-16 surrogate
+    "ccc" => 0,
+    "ud" => 0,
+    "ld" => 0
+};
+for(my $c = 0xD800; $c <= 0xDFFF; ++$c) {
+    $data{$c} = $Cs;
+}
+
+# Private use characters
+# We only fill in values below $max, utf32__unidata() 
+my $Co = {
+    "gc" => "Co",
+    "ccc" => 0,
+    "ud" => 0,
+    "ld" => 0
+};
+for(my $c = 0xE000; $c <= 0xF8FF && $c <= $max; ++$c) {
+    $data{$c} = $Co;
+}
+for(my $c = 0xF0000; $c <= 0xFFFFD && $c <= $max; ++$c) {
+    $data{$c} = $Co;
+}
+for(my $c = 0x100000; $c <= 0x10FFFD && $c <= $max; ++$c) {
+    $data{$c} = $Co;
+}
+
+# Anything left is not assigned
+my $Cn = {
+    "gc" => "Cn",              # not assigned
+    "ccc" => 0,
+    "ud" => 0,
+    "ld" => 0
+};
 for(my $c = 0; $c <= $max; ++$c) {
     if(!exists $data{$c}) {
-       $data{$c} = {
-           "gc" => "Cn",       # not assigned
-           "ccc" => 0,
-           "ud" => 0,
-           "ld" => 0,
-           "wbreak" => 'Other',
-           };
+       $data{$c} = $Cn;
+    }
+    if(!exists $data{$c}->{wbreak}) {
+       $data{$c}->{wbreak} = 'Other';
+    }
+    if(!exists $data{$c}->{gbreak}) {
+       $data{$c}->{gbreak} = 'Other';
+    }
+    if(!exists $data{$c}->{sbreak}) {
+       $data{$c}->{sbreak} = 'Other';
     }
 }
 $cats{'Cn'} = 1;
@@ -230,6 +288,7 @@ while(<>) {
 }
 
 # Generate the header file
+print STDERR "Generating unidata.h...\n";
 open(STDOUT, ">unidata.h") or die "unidata.h: $!\n";
 
 out("/* Automatically generated file, see scripts/make-unidata */\n",
@@ -237,16 +296,28 @@ out("/* Automatically generated file, see scripts/make-unidata */\n",
     "#define UNIDATA_H\n");
 
 # TODO choose stable values for General_Category
-out("enum unicode_gc_cat {\n",
+out("enum unicode_General_Category {\n",
     join(",\n",
-        map("  unicode_gc_$_", sort keys %cats)), "\n};\n");
+        map("  unicode_General_Category_$_", sort keys %cats)), "\n};\n");
+
+out("enum unicode_Grapheme_Break {\n",
+    join(",\n",
+        map("  unicode_Grapheme_Break_$_", sort keys %gbreak)),
+    "\n};\n");
+out("extern const char *const unicode_Grapheme_Break_names[];\n");
 
 out("enum unicode_Word_Break {\n",
     join(",\n",
-        map("  unicode_Word_Break_$_", sort keys %wbpropvals)),
+        map("  unicode_Word_Break_$_", sort keys %wbreak)),
     "\n};\n");
 out("extern const char *const unicode_Word_Break_names[];\n");
 
+out("enum unicode_Sentence_Break {\n",
+    join(",\n",
+        map("  unicode_Sentence_Break_$_", sort keys %sbreak)),
+    "\n};\n");
+out("extern const char *const unicode_Sentence_Break_names[];\n");
+
 out("enum unicode_flags {\n",
     "  unicode_normalize_before_casefold = 1\n",
     "};\n",
@@ -275,9 +346,11 @@ out("struct unidata {\n",
     "  ".choosetype($minud, $maxud)." upper_offset;\n",
     "  ".choosetype($minld, $maxld)." lower_offset;\n",
     "  ".choosetype(0, $maxccc)." ccc;\n",
-    "  char gc;\n",
+    "  char general_category;\n",
     "  uint8_t flags;\n",
+    "  char grapheme_break;\n",
     "  char word_break;\n",
+    "  char sentence_break;\n",
     "};\n");
 # compat, canon and casefold do have have non-BMP characters, so we
 # can't use a simple 16-bit table.  We could use UTF-8 or UTF-16
@@ -293,6 +366,7 @@ out("#endif\n");
 
 close STDOUT or die "unidata.h: $!\n";
 
+print STDERR "Generating unidata.c...\n";
 open(STDOUT, ">unidata.c") or die "unidata.c: $!\n";
 
 out("/* Automatically generated file, see scripts/make-unidata */\n",
@@ -300,15 +374,29 @@ out("/* Automatically generated file, see scripts/make-unidata */\n",
     "#include \"types.h\"\n",
     "#include \"unidata.h\"\n");
 
-# Short aliases for general category codes
+# Short aliases to keep .c file small
 
-out(map(sprintf("#define %s unicode_gc_%s\n", $_, $_), sort keys %cats));
-
-# Names for Word_Break property
+out(map(sprintf("#define %s unicode_General_Category_%s\n", $_, $_),
+       sort keys %cats));
+out(map(sprintf("#define GB%s unicode_Grapheme_Break_%s\n", $_, $_),
+       sort keys %gbreak));
+out(map(sprintf("#define WB%s unicode_Word_Break_%s\n", $_, $_),
+       sort keys %wbreak));
+out(map(sprintf("#define SB%s unicode_Sentence_Break_%s\n", $_, $_),
+       sort keys %sbreak));
 
+# Names for *_Break properties
+out("const char *const unicode_Grapheme_Break_names[] = {\n",
+    join(",\n",
+        map("  \"$_\"", sort keys %gbreak)),
+    "\n};\n");
 out("const char *const unicode_Word_Break_names[] = {\n",
     join(",\n",
-        map("  \"$_\"", sort keys %wbpropvals)),
+        map("  \"$_\"", sort keys %wbreak)),
+    "\n};\n");
+out("const char *const unicode_Sentence_Break_names[] = {\n",
+    join(",\n",
+        map("  \"$_\"", sort keys %sbreak)),
     "\n};\n");
 
 # Generate the decomposition mapping tables.  We look out for duplicates
@@ -322,7 +410,7 @@ for(my $c = 0; $c <= $max; ++$c) {
     # If canon is set then compat will be too and will be identical.
     # If compat is set the canon might be clear.  So we use the
     # compat version and fix up the symbols after.
-    if(exists $data{$c}->{compat}) {
+    if(exists $data{$c} && exists $data{$c}->{compat}) {
        my $s = join(",",
                     (map(hex($_), split(/\s+/, $data{$c}->{compat})), 0));
        if(!exists $decompnums{$s}) {
@@ -348,7 +436,7 @@ my %cfnums = ();
 my $cfsaved = 0;
 out("static const uint32_t ");
 for(my $c = 0; $c <= $max; ++$c) {
-    if(exists $data{$c}->{casefold}) {
+    if(exists $data{$c} && exists $data{$c}->{casefold}) {
        my $s = join(",",
                     (map(hex($_), split(/\s+/, $data{$c}->{casefold})), 0));
        if(!exists $cfnums{$s}) {
@@ -395,7 +483,9 @@ for(my $base = 0; $base <= $max; $base += $modulus) {
                  $d->{ccc},
                  $d->{gc},
                  $flags,
-                 "unicode_Word_Break_$d->{wbreak}",
+                 "GB$d->{gbreak}",
+                 "WB$d->{wbreak}",
+                 "SB$d->{sbreak}",
             )."}");
     }
     my $t = join(",\n", @t);
@@ -410,13 +500,15 @@ for(my $base = 0; $base <= $max; $base += $modulus) {
     $subtableno{$base} = $subtable{$t};
 }
 
-out("const struct unidata*const unidata[]={\n");
+out("const struct unidata *const unidata[]={\n");
 for(my $base = 0; $base <= $max; $base += $modulus) {
+    #out("st$subtableno{$base} /* ".sprintf("%04x", $base)." */,\n");
     out("st$subtableno{$base},\n");
 }
 out("};\n");
 
 close STDOUT or die "unidata.c: $!\n";
 
-print STDERR "max=$max, subtables=$subtablecounter, subtablessaved=$subtablessaved\n";
+printf STDERR "max=%04X\n", $max;
+print STDERR "subtables=$subtablecounter, subtablessaved=$subtablessaved\n";
 print STDERR "decompsaved=$decompsaved cfsaved=$cfsaved\n";