3
# This auxiliary script makes five header files
4
# used for building XSUB of Unicode::Collate.
7
# <do 'mkheader'> in perl, or <perl mkheader> in command line
22
unless ("A" eq pack('U', 0x41)) {
23
die "Unicode::Collate cannot stringify a Unicode code point\n";
27
use constant TRUE => 1;
28
use constant FALSE => "";
29
use constant VCE_TEMPLATE => 'Cn4';
31
sub _getHexArray { map hex, $_[0] =~ /([0-9a-fA-F]+)/g }
33
our $PACKAGE = 'Unicode::Collate, mkheader';
36
our %SimpleEntries; # $codepoint => $keys
42
$f = File::Spec->catfile($d, "Collate", "allkeys.txt");
43
last if open($fh, $f);
46
croak "$PACKAGE: Collate/allkeys.txt is not found" if !defined $f;
48
while (my $line = <$fh>) {
49
next if $line =~ /^\s*#/;
50
if ($line =~ /^\s*\@/) {
55
next if $line !~ /^\s*[0-9A-Fa-f]/;
57
$line =~ s/[#%]\s*(.*)//; # removing comment (not getting the name)
60
my($e, $k) = split /;/, $line;
62
croak "Wrong Entry: <charList> must be separated by ';' ".
63
"from <collElement>" if ! $k;
65
my @uv = _getHexArray($e);
73
my $is_L3_ignorable = TRUE;
76
foreach my $arr ($k =~ /\[([^\[\]]+)\]/g) { # SPACEs allowed
77
my $var = $arr =~ /\*/; # exactly /^\*/ but be lenient.
78
my @wt = _getHexArray($arr);
79
push @key, pack(VCE_TEMPLATE, $var, @wt);
80
$is_L3_ignorable = FALSE
81
if $wt[0] || $wt[1] || $wt[2];
82
# Conformance Test for 3.1.1 and 4.0.0 shows Level 3 ignorable
83
# is completely ignorable.
84
# For expansion, an entry $is_L3_ignorable
85
# if and only if "all" CEs are [.0000.0000.0000].
87
my $mapping = $is_L3_ignorable ? [] : \@key;
89
my $str = chr($num).join('', @$mapping);
90
$SimpleEntries{$uv[0]} = stringify($str);
96
return sprintf '"%s"', join '',
97
map sprintf("\\x%02x", ord $_), split //, $str;
101
########## writing header files ##########
106
my $head = $prefix."rest";
108
$init .= "static $type $head [] = {\n";
109
for my $line (@Rest) {
112
$init .= "/*$line*/\n" if $line =~ /^[A-Za-z0-9_.:;@\ \[\]]+\z/;
113
$init .= "($type)".stringify($line).",\n";
115
$init .= "NULL\n"; # sentinel
124
hash => \%SimpleEntries,
130
foreach my $tbl (@tripletable) {
131
my $file = "$tbl->{file}.h";
132
my $head = "${prefix}$tbl->{name}";
133
my $type = $tbl->{type};
134
my $hash = $tbl->{hash};
135
my $null = $tbl->{null};
136
my $init = $tbl->{init};
138
open FH, ">$file" or croak "$PACKAGE: $file can't be made";
139
binmode FH; select FH;
144
* This file is auto-generated by mkheader.
145
* Any changes here will be lost!
149
print $init if defined $init;
151
foreach my $uv (keys %$hash) {
152
croak sprintf("a Unicode code point 0x%04X over 0x10FFFF.", $uv)
153
unless $uv <= 0x10FFFF;
154
my @c = unpack 'CCCC', pack 'N', $uv;
155
$val{ $c[1] }{ $c[2] }{ $c[3] } = $hash->{$uv};
158
foreach my $p (sort { $a <=> $b } keys %val) {
159
next if ! $val{ $p };
160
for (my $r = 0; $r < 256; $r++) {
161
next if ! $val{ $p }{ $r };
162
printf "static $type ${head}_%02x_%02x [256] = {\n", $p, $r;
163
for (my $c = 0; $c < 256; $c++) {
164
print "\t", defined $val{$p}{$r}{$c}
165
? "($type)".$val{$p}{$r}{$c}
167
print ',' if $c != 255;
168
print "\n" if $c % 8 == 7;
173
foreach my $p (sort { $a <=> $b } keys %val) {
174
next if ! $val{ $p };
175
printf "static $type* ${head}_%02x [256] = {\n", $p;
176
for (my $r = 0; $r < 256; $r++) {
177
print $val{ $p }{ $r }
178
? sprintf("${head}_%02x_%02x", $p, $r)
180
print ',' if $r != 255;
181
print "\n" if $val{ $p }{ $r } || ($r+1) % 8 == 0;
185
print "static $type** $head [] = {\n";
186
for (my $p = 0; $p <= 0x10; $p++) {
187
print $val{ $p } ? sprintf("${head}_%02x", $p) : "NULL";
188
print ',' if $p != 0x10;