3 # This script generates "unfcan.h", "unfcpt.h", "unfcmb.h",
4 # "unfcmp.h", and "unfexc.h"
5 # from CombiningClass.pl, Decomposition.pl, CompositionExclusions.txt
6 # in lib/unicore or unicode directory
7 # for Unicode::Normalize.xs. (cf. Makefile.PL)
9 # Usage: <perl mkheader> in command line
10 # or <do 'mkheader'> in perl
17 our $PACKAGE = 'Unicode::Normalize, mkheader';
19 our $Combin = do "unicore/CombiningClass.pl"
20 || do "unicode/CombiningClass.pl"
21 || croak "$PACKAGE: CombiningClass.pl not found";
23 our $Decomp = do "unicore/Decomposition.pl"
24 || do "unicode/Decomposition.pl"
25 || croak "$PACKAGE: Decomposition.pl not found";
27 our %Combin; # $codepoint => $number : combination class
28 our %Canon; # $codepoint => $hexstring : canonical decomp.
29 our %Compat; # $codepoint => $hexstring : compat. decomp.
30 our %Exclus; # $codepoint => 1 : composition exclusions
31 our %Single; # $codepoint => 1 : singletons
32 our %NonStD; # $codepoint => 1 : non-starter decompositions
34 our %Comp1st; # $codepoint => $listname : may be composed with a next char.
35 our %Comp2nd; # $codepoint => 1 : may be composed with a prev char.
36 our %CompList; # $listname,$2nd => $codepoint : composite
39 our $structname = "${prefix}complist";
43 foreach my $d (@INC) {
45 $f = File::Spec->catfile($d, "unicore", "CompositionExclusions.txt");
46 last if open($fh, $f);
47 $f = File::Spec->catfile($d, "unicode", "CompExcl.txt");
48 last if open($fh, $f);
51 croak "$PACKAGE: neither unicore/CompositionExclusions.txt "
52 . "nor unicode/CompExcl.txt is found in @INC" unless defined $f;
57 $Exclus{ hex($1) } = 1 if /([0-9A-Fa-f]+)/;
63 ## converts string "hhhh hhhh hhhh" to a numeric list
65 sub _getHexArray { map hex, $_[0] =~ /([0-9A-Fa-f]+)/g }
67 while ($Combin =~ /(.+)/g) {
68 my @tab = split /\t/, $1;
69 my $ini = hex $tab[0];
71 $Combin{ $ini } = $tab[2];
73 $Combin{ $_ } = $tab[2] foreach $ini .. hex($tab[1]);
77 while ($Decomp =~ /(.+)/g) {
78 my @tab = split /\t/, $1;
79 my $compat = $tab[2] =~ s/<[^>]+>//;
80 my $dec = [ _getHexArray($tab[2]) ]; # decomposition
81 my $ini = hex($tab[0]); # initial decomposable character
84 @$dec == 2 ? sprintf("${structname}_%06x", $dec->[0]) : 'USELESS';
85 # %04x is bad since it'd place _3046 after _1d157.
88 $Compat{ $ini } = $dec;
91 $Canon{ $ini } = $dec;
94 if ($Combin{ $dec->[0] }) {
97 $CompList{ $listname }{ $dec->[1] } = $ini;
98 $Comp1st{ $dec->[0] } = $listname;
99 $Comp2nd{ $dec->[1] } = 1 if ! $Exclus{$ini};
101 } elsif (@$dec == 1) {
104 croak("Weird Canonical Decomposition of U+$tab[0]");
108 foreach my $u ($ini .. hex($tab[1])) {
109 $Compat{ $u } = $dec;
115 if ($Combin{ $dec->[0] }) {
118 $CompList{ $listname }{ $dec->[1] } = $u;
119 $Comp1st{ $dec->[0] } = $listname;
120 $Comp2nd{ $dec->[1] } = 1 if ! $Exclus{$u};
122 } elsif (@$dec == 1) {
125 croak("Weird Canonical Decomposition of U+$tab[0]");
132 # modern HANGUL JUNGSEONG and HANGUL JONGSEONG jamo
133 foreach my $j (0x1161..0x1175, 0x11A8..0x11C2) {
139 my @dec = map $Canon{$_} ? @{ $Canon{$_} } : $_, @src;
140 return join(" ",@src) eq join(" ",@dec) ? @dec : getCanonList(@dec);
141 # condition @src == @dec is not ok.
146 my @dec = map $Compat{$_} ? @{ $Compat{$_} } : $_, @src;
147 return join(" ",@src) eq join(" ",@dec) ? @dec : getCompatList(@dec);
148 # condition @src == @dec is not ok.
151 # exhaustive decomposition
152 foreach my $key (keys %Canon) {
153 $Canon{$key} = [ getCanonList($key) ];
156 # exhaustive decomposition
157 foreach my $key (keys %Compat) {
158 $Compat{$key} = [ getCompatList($key) ];
162 sprintf '"%s"', join '',
163 map sprintf("\\x%02x", $_), unpack 'C*', pack 'U*', @_;
165 # Do we need say <pack 'U*', map utf8::unicode_to_native($_),>
166 # instead of <pack 'U*',> for EBCDIC?
168 foreach my $hash (\%Canon, \%Compat) {
169 foreach my $key (keys %$hash) {
170 $hash->{$key} = _U_stringify( @{ $hash->{$key} } );
174 ####################################
188 name => "NonStDecomp",
199 my $file = "unfexc.h";
200 open FH, ">$file" or croak "$PACKAGE: $file can't be made";
201 binmode FH; select FH;
205 * This file is auto-generated by mkheader.
206 * Any changes here will be lost!
210 foreach my $tbl (@boolfunc) {
211 my @temp = sort {$a <=> $b} keys %{$tbl->{hash}};
212 my $type = $tbl->{type};
213 my $name = $tbl->{name};
214 print "$type is$name (UV uv)\n{\nreturn\n\t";
217 my $cur = shift @temp;
218 if (@temp && $cur + 1 == $temp[0]) {
219 print "($cur <= uv && uv <= ";
220 while (@temp && $cur + 1 == $temp[0]) {
224 print "\n\t|| " if @temp;
227 print "\n\t|| " if @temp;
230 print "\n\t? TRUE : FALSE;\n}\n\n";
235 ####################################
238 "typedef struct { UV nextchar; UV composite; } $structname;\n\n";
240 foreach my $i (sort keys %CompList) {
241 $compinit .= "$structname $i [] = {\n";
242 $compinit .= join ",\n",
243 map sprintf("\t{ %d, %d }", $_, $CompList{$i}{$_}),
244 sort {$a <=> $b } keys %{ $CompList{$i} };
245 $compinit .= ",\n{0,0}\n};\n\n"; # with sentinel
273 type => "$structname *",
280 foreach my $tbl (@tripletable) {
281 my $file = "$tbl->{file}.h";
282 my $head = "${prefix}$tbl->{name}";
283 my $type = $tbl->{type};
284 my $hash = $tbl->{hash};
285 my $null = $tbl->{null};
286 my $init = $tbl->{init};
288 open FH, ">$file" or croak "$PACKAGE: $file can't be made";
289 binmode FH; select FH;
294 * This file is auto-generated by mkheader.
295 * Any changes here will be lost!
299 print $init if defined $init;
301 foreach my $uv (keys %$hash) {
302 croak sprintf("a Unicode code point 0x%04X over 0x10FFFF.", $uv)
303 unless $uv <= 0x10FFFF;
304 my @c = unpack 'CCCC', pack 'N', $uv;
305 $val{ $c[1] }{ $c[2] }{ $c[3] } = $hash->{$uv};
308 foreach my $p (sort { $a <=> $b } keys %val) {
309 next if ! $val{ $p };
310 for (my $r = 0; $r < 256; $r++) {
311 next if ! $val{ $p }{ $r };
312 printf "$type ${head}_%02x_%02x [256] = {\n", $p, $r;
313 for (my $c = 0; $c < 256; $c++) {
314 print "\t", defined $val{$p}{$r}{$c}
315 ? "($type)".$val{$p}{$r}{$c}
317 print ',' if $c != 255;
318 print "\n" if $c % 8 == 7;
323 foreach my $p (sort { $a <=> $b } keys %val) {
324 next if ! $val{ $p };
325 printf "$type* ${head}_%02x [256] = {\n", $p;
326 for (my $r = 0; $r < 256; $r++) {
327 print $val{ $p }{ $r }
328 ? sprintf("${head}_%02x_%02x", $p, $r)
330 print ',' if $r != 255;
331 print "\n" if $val{ $p }{ $r } || ($r+1) % 8 == 0;
335 print "$type** $head [] = {\n";
336 for (my $p = 0; $p <= 0x10; $p++) {
337 print $val{ $p } ? sprintf("${head}_%02x", $p) : "NULL";
338 print ',' if $p != 0x10;