Upgrade to Unicode::Normalize 0.21 and Unicode::Collate 0.24,
[p5sagit/p5-mst-13.2.git] / ext / Unicode / Normalize / mkheader
CommitLineData
ac5ea531 1#!perl
2#
3# This script generates "unfcan.h", "unfcpt.h", "unfcmb.h",
f027f502 4# "unfcmp.h", and "unfexc.h"
48287974 5# from CombiningClass.pl, Decomposition.pl, CompositionExclusions.txt
ac5ea531 6# in lib/unicore or unicode directory
7# for Unicode::Normalize.xs. (cf. Makefile.PL)
8#
f027f502 9# Usage: <perl mkheader> in command line
10# or <do 'mkheader'> in perl
11#
ac5ea531 12use 5.006;
13use strict;
14use warnings;
15use Carp;
6c941e0c 16use File::Spec;
17
9f1f04a1 18BEGIN {
19 unless ("A" eq pack('U', 0x41) || "A" eq pack('U', ord("A"))) {
20 die "Unicode::Normalize cannot stringify a Unicode code point\n";
21 }
22}
ac5ea531 23
24our $PACKAGE = 'Unicode::Normalize, mkheader';
25
26our $Combin = do "unicore/CombiningClass.pl"
8f118dcd 27 || do "unicode/CombiningClass.pl"
28 || croak "$PACKAGE: CombiningClass.pl not found";
ac5ea531 29
30our $Decomp = do "unicore/Decomposition.pl"
8f118dcd 31 || do "unicode/Decomposition.pl"
32 || croak "$PACKAGE: Decomposition.pl not found";
ac5ea531 33
48287974 34our %Combin; # $codepoint => $number : combination class
6c941e0c 35our %Canon; # $codepoint => \@codepoints : canonical decomp.
36our %Compat; # $codepoint => \@codepoints : compat. decomp.
37# after _U_stringify(), ($codepoint => $hexstring) for %Canon and %Compat
48287974 38our %Exclus; # $codepoint => 1 : composition exclusions
39our %Single; # $codepoint => 1 : singletons
40our %NonStD; # $codepoint => 1 : non-starter decompositions
41
42our %Comp1st; # $codepoint => $listname : may be composed with a next char.
43our %Comp2nd; # $codepoint => 1 : may be composed with a prev char.
44our %CompList; # $listname,$2nd => $codepoint : composite
45
46our $prefix = "UNF_";
47our $structname = "${prefix}complist";
ac5ea531 48
6c941e0c 49########## definition of Hangul constants ##########
50use constant SBase => 0xAC00;
51use constant SFinal => 0xD7A3; # SBase -1 + SCount
52use constant SCount => 11172; # LCount * NCount
53use constant NCount => 588; # VCount * TCount
54use constant LBase => 0x1100;
55use constant LFinal => 0x1112;
56use constant LCount => 19;
57use constant VBase => 0x1161;
58use constant VFinal => 0x1175;
59use constant VCount => 21;
60use constant TBase => 0x11A7;
61use constant TFinal => 0x11C2;
62use constant TCount => 28;
63
64sub decomposeHangul {
65 my $SIndex = $_[0] - SBase;
66 my $LIndex = int( $SIndex / NCount);
67 my $VIndex = int(($SIndex % NCount) / TCount);
68 my $TIndex = $SIndex % TCount;
69 my @ret = (
70 LBase + $LIndex,
71 VBase + $VIndex,
72 $TIndex ? (TBase + $TIndex) : (),
73 );
74 wantarray ? @ret : pack('U*', @ret);
75 # any element in @ret greater than 0xFF, so no need of u2n conversion.
76}
77
78########## getting full decomposion ##########
ac5ea531 79{
8f118dcd 80 my($f, $fh);
81 foreach my $d (@INC) {
8f118dcd 82 $f = File::Spec->catfile($d, "unicore", "CompositionExclusions.txt");
83 last if open($fh, $f);
84 $f = File::Spec->catfile($d, "unicode", "CompExcl.txt");
85 last if open($fh, $f);
86 $f = undef;
87 }
48287974 88 croak "$PACKAGE: neither unicore/CompositionExclusions.txt "
89 . "nor unicode/CompExcl.txt is found in @INC" unless defined $f;
90
91 while (<$fh>) {
92 next if /^#/ or /^$/;
93 s/#.*//;
94 $Exclus{ hex($1) } = 1 if /([0-9A-Fa-f]+)/;
95 }
8f118dcd 96 close $fh;
ac5ea531 97}
98
48287974 99##
100## converts string "hhhh hhhh hhhh" to a numeric list
101##
102sub _getHexArray { map hex, $_[0] =~ /([0-9A-Fa-f]+)/g }
103
8f118dcd 104while ($Combin =~ /(.+)/g) {
105 my @tab = split /\t/, $1;
106 my $ini = hex $tab[0];
107 if ($tab[1] eq '') {
108 $Combin{ $ini } = $tab[2];
109 } else {
110 $Combin{ $_ } = $tab[2] foreach $ini .. hex($tab[1]);
111 }
ac5ea531 112}
113
8f118dcd 114while ($Decomp =~ /(.+)/g) {
115 my @tab = split /\t/, $1;
116 my $compat = $tab[2] =~ s/<[^>]+>//;
117 my $dec = [ _getHexArray($tab[2]) ]; # decomposition
8f118dcd 118 my $ini = hex($tab[0]); # initial decomposable character
48287974 119
120 my $listname =
121 @$dec == 2 ? sprintf("${structname}_%06x", $dec->[0]) : 'USELESS';
122 # %04x is bad since it'd place _3046 after _1d157.
123
8f118dcd 124 if ($tab[1] eq '') {
125 $Compat{ $ini } = $dec;
126
127 if (! $compat) {
48287974 128 $Canon{ $ini } = $dec;
8f118dcd 129
f027f502 130 if (@$dec == 2) {
8f118dcd 131 if ($Combin{ $dec->[0] }) {
132 $NonStD{ $ini } = 1;
133 } else {
48287974 134 $CompList{ $listname }{ $dec->[1] } = $ini;
135 $Comp1st{ $dec->[0] } = $listname;
136 $Comp2nd{ $dec->[1] } = 1 if ! $Exclus{$ini};
8f118dcd 137 }
f027f502 138 } elsif (@$dec == 1) {
8f118dcd 139 $Single{ $ini } = 1;
f027f502 140 } else {
141 croak("Weird Canonical Decomposition of U+$tab[0]");
8f118dcd 142 }
143 }
144 } else {
48287974 145 foreach my $u ($ini .. hex($tab[1])) {
8f118dcd 146 $Compat{ $u } = $dec;
48287974 147
8f118dcd 148 if (! $compat) {
48287974 149 $Canon{ $u } = $dec;
8f118dcd 150
f027f502 151 if (@$dec == 2) {
8f118dcd 152 if ($Combin{ $dec->[0] }) {
153 $NonStD{ $u } = 1;
154 } else {
48287974 155 $CompList{ $listname }{ $dec->[1] } = $u;
156 $Comp1st{ $dec->[0] } = $listname;
157 $Comp2nd{ $dec->[1] } = 1 if ! $Exclus{$u};
8f118dcd 158 }
f027f502 159 } elsif (@$dec == 1) {
8f118dcd 160 $Single{ $u } = 1;
f027f502 161 } else {
162 croak("Weird Canonical Decomposition of U+$tab[0]");
8f118dcd 163 }
164 }
165 }
ac5ea531 166 }
ac5ea531 167}
168
48287974 169# modern HANGUL JUNGSEONG and HANGUL JONGSEONG jamo
170foreach my $j (0x1161..0x1175, 0x11A8..0x11C2) {
171 $Comp2nd{$j} = 1;
ac5ea531 172}
173
174sub getCanonList {
8f118dcd 175 my @src = @_;
6c941e0c 176 my @dec = map {
177 (SBase <= $_ && $_ <= SFinal) ? decomposeHangul($_)
178 : $Canon{$_} ? @{ $Canon{$_} } : $_
179 } @src;
8f118dcd 180 return join(" ",@src) eq join(" ",@dec) ? @dec : getCanonList(@dec);
181 # condition @src == @dec is not ok.
ac5ea531 182}
183
184sub getCompatList {
8f118dcd 185 my @src = @_;
6c941e0c 186 my @dec = map {
187 (SBase <= $_ && $_ <= SFinal) ? decomposeHangul($_)
188 : $Compat{$_} ? @{ $Compat{$_} } : $_
189 } @src;
8f118dcd 190 return join(" ",@src) eq join(" ",@dec) ? @dec : getCompatList(@dec);
191 # condition @src == @dec is not ok.
ac5ea531 192}
193
48287974 194# exhaustive decomposition
195foreach my $key (keys %Canon) {
196 $Canon{$key} = [ getCanonList($key) ];
197}
198
199# exhaustive decomposition
200foreach my $key (keys %Compat) {
201 $Compat{$key} = [ getCompatList($key) ];
202}
ac5ea531 203
9f1f04a1 204sub _pack_U {
205 return "A" eq pack('U', 0x41)
206 ? pack('U*', @_)
207 : "A" eq pack('U', ord("A"))
208 ? pack('U*', map utf8::unicode_to_native($_), @_)
209 : die "$PACKAGE, a Unicode code point cannot be stringified.\n";
210}
211
ac5ea531 212sub _U_stringify {
8f118dcd 213 sprintf '"%s"', join '',
9f1f04a1 214 map sprintf("\\x%02x", $_), unpack 'C*', _pack_U(@_);
ac5ea531 215}
216
217foreach my $hash (\%Canon, \%Compat) {
8f118dcd 218 foreach my $key (keys %$hash) {
219 $hash->{$key} = _U_stringify( @{ $hash->{$key} } );
220 }
ac5ea531 221}
222
6c941e0c 223########## writing header files ##########
ac5ea531 224
8f118dcd 225my @boolfunc = (
226 {
227 name => "Exclusion",
228 type => "bool",
229 hash => \%Exclus,
230 },
231 {
232 name => "Singleton",
233 type => "bool",
234 hash => \%Single,
235 },
236 {
237 name => "NonStDecomp",
238 type => "bool",
239 hash => \%NonStD,
240 },
241 {
242 name => "Comp2nd",
243 type => "bool",
244 hash => \%Comp2nd,
245 },
246);
ac5ea531 247
248my $file = "unfexc.h";
249open FH, ">$file" or croak "$PACKAGE: $file can't be made";
250binmode FH; select FH;
251
8f118dcd 252 print << 'EOF';
253/*
254 * This file is auto-generated by mkheader.
255 * Any changes here will be lost!
256 */
257EOF
ac5ea531 258
8f118dcd 259foreach my $tbl (@boolfunc) {
260 my @temp = sort {$a <=> $b} keys %{$tbl->{hash}};
261 my $type = $tbl->{type};
262 my $name = $tbl->{name};
263 print "$type is$name (UV uv)\n{\nreturn\n\t";
264
265 while (@temp) {
266 my $cur = shift @temp;
267 if (@temp && $cur + 1 == $temp[0]) {
268 print "($cur <= uv && uv <= ";
269 while (@temp && $cur + 1 == $temp[0]) {
270 $cur = shift @temp;
271 }
272 print "$cur)";
273 print "\n\t|| " if @temp;
274 } else {
275 print "uv == $cur";
276 print "\n\t|| " if @temp;
277 }
ac5ea531 278 }
8f118dcd 279 print "\n\t? TRUE : FALSE;\n}\n\n";
ac5ea531 280}
281
ac5ea531 282close FH;
283
284####################################
285
48287974 286my $compinit =
287 "typedef struct { UV nextchar; UV composite; } $structname;\n\n";
288
289foreach my $i (sort keys %CompList) {
290 $compinit .= "$structname $i [] = {\n";
291 $compinit .= join ",\n",
292 map sprintf("\t{ %d, %d }", $_, $CompList{$i}{$_}),
293 sort {$a <=> $b } keys %{ $CompList{$i} };
294 $compinit .= ",\n{0,0}\n};\n\n"; # with sentinel
295}
296
ac5ea531 297my @tripletable = (
8f118dcd 298 {
299 file => "unfcmb",
300 name => "combin",
301 type => "STDCHAR",
302 hash => \%Combin,
303 null => 0,
304 },
305 {
306 file => "unfcan",
307 name => "canon",
308 type => "char*",
309 hash => \%Canon,
310 null => "NULL",
311 },
312 {
313 file => "unfcpt",
314 name => "compat",
315 type => "char*",
316 hash => \%Compat,
317 null => "NULL",
318 },
319 {
320 file => "unfcmp",
321 name => "compos",
322 type => "$structname *",
323 hash => \%Comp1st,
324 null => "NULL",
325 init => $compinit,
326 },
ac5ea531 327);
328
329foreach my $tbl (@tripletable) {
8f118dcd 330 my $file = "$tbl->{file}.h";
331 my $head = "${prefix}$tbl->{name}";
332 my $type = $tbl->{type};
333 my $hash = $tbl->{hash};
334 my $null = $tbl->{null};
335 my $init = $tbl->{init};
336
337 open FH, ">$file" or croak "$PACKAGE: $file can't be made";
338 binmode FH; select FH;
339 my %val;
340
341 print FH << 'EOF';
ac5ea531 342/*
343 * This file is auto-generated by mkheader.
344 * Any changes here will be lost!
345 */
346EOF
347
8f118dcd 348 print $init if defined $init;
349
350 foreach my $uv (keys %$hash) {
f027f502 351 croak sprintf("a Unicode code point 0x%04X over 0x10FFFF.", $uv)
352 unless $uv <= 0x10FFFF;
8f118dcd 353 my @c = unpack 'CCCC', pack 'N', $uv;
354 $val{ $c[1] }{ $c[2] }{ $c[3] } = $hash->{$uv};
355 }
356
357 foreach my $p (sort { $a <=> $b } keys %val) {
358 next if ! $val{ $p };
359 for (my $r = 0; $r < 256; $r++) {
360 next if ! $val{ $p }{ $r };
361 printf "$type ${head}_%02x_%02x [256] = {\n", $p, $r;
362 for (my $c = 0; $c < 256; $c++) {
363 print "\t", defined $val{$p}{$r}{$c}
364 ? "($type)".$val{$p}{$r}{$c}
365 : $null;
366 print ',' if $c != 255;
367 print "\n" if $c % 8 == 7;
368 }
369 print "};\n\n";
370 }
371 }
372 foreach my $p (sort { $a <=> $b } keys %val) {
373 next if ! $val{ $p };
374 printf "$type* ${head}_%02x [256] = {\n", $p;
375 for (my $r = 0; $r < 256; $r++) {
376 print $val{ $p }{ $r }
377 ? sprintf("${head}_%02x_%02x", $p, $r)
378 : "NULL";
379 print ',' if $r != 255;
380 print "\n" if $val{ $p }{ $r } || ($r+1) % 8 == 0;
381 }
382 print "};\n\n";
ac5ea531 383 }
8f118dcd 384 print "$type** $head [] = {\n";
385 for (my $p = 0; $p <= 0x10; $p++) {
386 print $val{ $p } ? sprintf("${head}_%02x", $p) : "NULL";
387 print ',' if $p != 0x10;
388 print "\n";
ac5ea531 389 }
390 print "};\n\n";
8f118dcd 391 close FH;
ac5ea531 392}
393
394__END__