Split the ebcdic details to perlebcdic.
[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",
4# "unfcmp.h", and "unfexc.h"
5# from CombiningClass.pl, Decomposition.pl, CompExcl.txt
6# in lib/unicore or unicode directory
7# for Unicode::Normalize.xs. (cf. Makefile.PL)
8#
9use 5.006;
10use strict;
11use warnings;
12use Carp;
13
14our $PACKAGE = 'Unicode::Normalize, mkheader';
15
16our $Combin = do "unicore/CombiningClass.pl"
17 || do "unicode/CombiningClass.pl"
18 || croak "$PACKAGE: CombiningClass.pl not found";
19
20our $Decomp = do "unicore/Decomposition.pl"
21 || do "unicode/Decomposition.pl"
22 || croak "$PACKAGE: Decomposition.pl not found";
23
24our %Combin; # $codepoint => $number : combination class
25our %Canon; # $codepoint => $hexstring : canonical decomp.
26our %Compat; # $codepoint => $hexstring : compat. decomp.
27our %Compos; # $string => $codepoint : composite
28
29our %Exclus; # $codepoint => 1 : composition exclusions
30
31{
32 my($f, $fh);
33 foreach my $d (@INC) {
34 use File::Spec;
551b6b6f 35 $f = File::Spec->catfile($d, "unicore", "CompositionExclusions.txt");
ac5ea531 36 last if open($fh, $f);
37 $f = File::Spec->catfile($d, "unicode", "CompExcl.txt");
38 last if open($fh, $f);
39 $f = undef;
40 }
41 croak "$PACKAGE: CompExcl.txt not found in @INC" unless defined $f;
42 while(<$fh>) {
43 next if /^#/ or /^$/;
44 s/#.*//;
45 $Exclus{ hex($1) } =1 if /([0-9A-Fa-f]+)/;
46 }
47 close $fh;
48}
49
50while($Combin =~ /(.+)/g) {
51 my @tab = split /\t/, $1;
52 my $ini = hex $tab[0];
53 if($tab[1] eq '') {
54 $Combin{ $ini } = $tab[2];
55 } else {
56 $Combin{ $_ } = $tab[2] foreach $ini .. hex($tab[1]);
57 }
58}
59
60while($Decomp =~ /(.+)/g) {
61 my @tab = split /\t/, $1;
62 my $compat = $tab[2] =~ s/<[^>]+>//;
63 my $dec = [ _getHexArray($tab[2]) ]; # decomposition
64 my $com = pack('U*', @$dec); # composable sequence
65 my $ini = hex($tab[0]);
66 if($tab[1] eq '') {
67 $Compat{ $ini } = $dec;
68 if(! $compat) {
69 $Canon{ $ini } = $dec;
70 $Compos{ $com } = $ini if @$dec > 1;
71 }
72 } else {
73 foreach my $u ($ini .. hex($tab[1])){
74 $Compat{ $u } = $dec;
75 if(! $compat){
76 $Canon{ $u } = $dec;
77 $Compos{ $com } = $ini if @$dec > 1;
78 }
79 }
80 }
81}
82
83# exhaustive decomposition
84foreach my $key (keys %Canon) {
85 $Canon{$key} = [ getCanonList($key) ];
86}
87
88# exhaustive decomposition
89foreach my $key (keys %Compat) {
90 $Compat{$key} = [ getCompatList($key) ];
91}
92
93sub getCanonList {
94 my @src = @_;
95 my @dec = map $Canon{$_} ? @{ $Canon{$_} } : $_, @src;
96 join(" ",@src) eq join(" ",@dec) ? @dec : getCanonList(@dec);
97 # condition @src == @dec is not ok.
98}
99
100sub getCompatList {
101 my @src = @_;
102 my @dec = map $Compat{$_} ? @{ $Compat{$_} } : $_, @src;
103 join(" ",@src) eq join(" ",@dec) ? @dec : getCompatList(@dec);
104 # condition @src == @dec is not ok.
105}
106
107sub _getHexArray {
108 my $str = shift;
109 map hex(), $str =~ /([0-9A-Fa-f]+)/g;
110}
111
112sub _U_stringify {
113 sprintf '"%s"', join '',
d85850a7 114 map sprintf("\\x%02x", $_), unpack 'C*', pack 'U*', @_;
ac5ea531 115}
116
117foreach my $hash (\%Canon, \%Compat) {
118 foreach my $key (keys %$hash) {
119 $hash->{$key} = _U_stringify( @{ $hash->{$key} } );
120 }
121}
122
ac5ea531 123my $prefix = "UNF_";
124
125my $structname = "${prefix}complist";
126
127our (%Comp1st, %CompList);
128
129foreach(sort keys %Compos) {
130 my @a = unpack('U*', $_);
131 my $val = $Compos{$_};
132 my $name = sprintf "${structname}_%06x", $a[0];
133 $Comp1st{ $a[0] } = $name;
134 $CompList{ $name }{ $a[1] } = $val;
ac5ea531 135}
136
137my $compinit =
138 "typedef struct { UV nextchar; UV composite; } $structname;\n\n";
139
140foreach my $i (sort keys %CompList) {
141 $compinit .= "$structname $i [] = {\n";
142 $compinit .= join ",\n",
143 map sprintf("\t{ %d, %d }", $_, $CompList{$i}{$_}),
144 sort {$a <=> $b } keys %{ $CompList{$i} };
145 $compinit .= ",\n{0,0}\n};\n\n"; # with sentinel
146}
147
148####################################
149
150my @Exclus = sort {$a <=> $b} keys %Exclus;
151
152my $file = "unfexc.h";
153open FH, ">$file" or croak "$PACKAGE: $file can't be made";
154binmode FH; select FH;
155
2a204b45 156print "bool isExclusion (UV uv) \n{\nreturn\n\t";
ac5ea531 157
158while(@Exclus) {
159 my $cur = shift @Exclus;
160 if(@Exclus && $cur + 1 == $Exclus[0]) {
20d72259 161 print "($cur <= uv && uv <= ";
ac5ea531 162 while(@Exclus && $cur + 1 == $Exclus[0]) {
163 $cur = shift @Exclus;
164 }
20d72259 165 print "$cur)";
ac5ea531 166 print "\n\t|| " if @Exclus;
167 } else {
168 print "uv == $cur";
169 print "\n\t|| " if @Exclus;
170 }
171}
172
173print "\n\t? TRUE : FALSE;\n}\n\n";
174close FH;
175
176####################################
177
178my @tripletable = (
179 {
180 file => "unfcmb",
181 name => "combin",
3164a1ca 182 type => "STDCHAR",
ac5ea531 183 hash => \%Combin,
184 null => 0,
185 },
186 {
187 file => "unfcan",
188 name => "canon",
189 type => "char*",
190 hash => \%Canon,
191 null => "NULL",
192 },
193 {
194 file => "unfcpt",
195 name => "compat",
196 type => "char*",
197 hash => \%Compat,
198 null => "NULL",
199 },
200 {
201 file => "unfcmp",
202 name => "compos",
203 type => "$structname *",
204 hash => \%Comp1st,
205 null => "NULL",
206 init => $compinit,
207 },
208);
209
210foreach my $tbl (@tripletable) {
211 my $file = "$tbl->{file}.h";
212 my $head = "${prefix}$tbl->{name}";
213 my $type = $tbl->{type};
214 my $hash = $tbl->{hash};
215 my $null = $tbl->{null};
216 my $init = $tbl->{init};
217
218 open FH, ">$file" or croak "$PACKAGE: $file can't be made";
219 binmode FH; select FH;
220 my %val;
221
222 print FH << 'EOF';
223/*
224 * This file is auto-generated by mkheader.
225 * Any changes here will be lost!
226 */
227EOF
228
229 print $init if defined $init;
230
231 foreach my $uv (keys %$hash) {
232 my @c = unpack 'CCCC', pack 'N', $uv;
233 $val{ $c[1] }{ $c[2] }{ $c[3] } = $hash->{$uv};
234 }
235
236 foreach my $p (sort { $a <=> $b } keys %val) {
237 next if ! $val{ $p };
238 for(my $r = 0; $r < 256; $r++){
239 next if ! $val{ $p }{ $r };
240 printf "$type ${head}_%02x_%02x [256] = {\n", $p, $r;
241 for(my $c = 0; $c < 256; $c++){
e898cac4 242 print "\t", defined $val{$p}{$r}{$c}
243 ? "($type)".$val{$p}{$r}{$c} : $null;
ac5ea531 244 print ',' if $c != 255;
245 print "\n" if $c % 8 == 7;
246 }
247 print "};\n\n";
248 }
249 }
250 foreach my $p (sort { $a <=> $b } keys %val) {
251 next if ! $val{ $p };
252 printf "$type* ${head}_%02x [256] = {\n", $p;
253 for(my $r = 0; $r < 256; $r++){
254 print $val{ $p }{ $r } ? sprintf("${head}_%02x_%02x", $p, $r) : "NULL";
255 print ',' if $r != 255;
256 print "\n" if $val{ $p }{ $r } || ($r+1) % 8 == 0;
257 }
258 print "};\n\n";
259 }
260 print "$type** $head [] = {\n";
261 for(my $p = 0; $p <= 0x10; $p++){
262 print $val{ $p } ? sprintf("${head}_%02x", $p) : "NULL";
263 print ',' if $p != 0x10;
264 print "\n";
265 }
266 print "};\n\n";
267 close FH;
268}
269
270__END__