1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
#! /usr/bin/perl
#
# Compile DerivedNormalizationProps.txt into C array declarations.
#
# See mkgraphemebreak.pl for the description of the arrays' structures.
use strict;
use warnings;
use mkcommon;
open(F, "<DerivedNormalizationProps.txt") || die;
my @full_composition_exclusion;
my $nfc = mkcommon->new(prefix => 'nfc_qc');
my $nfkc = mkcommon->new(prefix => 'nfkc_qc');
my @nfc_qc;
my @nfkc_qc;
while (defined($_=<F>))
{
chomp;
next unless /^
( [0-9A-F]+ )
(\.\.( [0-9A-F]+ ))?
\s*\;\s*
([^\s;]+)\s*
(;\s*([^\s;#]+))?
/xx;
my $f=$1;
my $l=$3;
my $t=$4;
my $v=$6;
$l=$f unless $l;
eval "\$f=0x$f";
eval "\$l=0x$l";
if ($t eq 'Full_Composition_Exclusion')
{
push @full_composition_exclusion, [$f, $l];
}
if ($t eq 'NFC_QC')
{
push @nfc_qc, [$f, $l, "UNICODE_NFC_QC_$v"];
}
if ($t eq 'NFKC_QC')
{
push @nfkc_qc, [$f, $l, "UNICODE_NFKC_QC_$v"];
}
}
foreach my $rec (
sort {
$a->[0] <=> $b->[0];
} @nfc_qc)
{
$nfc->range(@$rec);
}
foreach my $rec (
sort {
$a->[0] <=> $b->[0];
} @nfkc_qc)
{
$nfkc->range(@$rec);
}
my $obj=mkcommon->new(prefix => 'exclusion', noclass => 1);
foreach (sort { $$a[0] <=> $$b[0] } @full_composition_exclusion)
{
$obj->range($$_[0], $$_[1]);
}
print "/* Full_Composition_Exclusion table lookup */\n";
print "#ifdef exclusion_table\n";
$obj->output;
print "#endif\n";
print "\n/* NFC_QC table lookup */\n";
$nfc->output;
print "\n/* NFC_KQC table lookup */\n";
$nfkc->output;
$obj = mkcommon->new(prefix => 'ccc');
# Construct decomposition mappings.
#
# We use unicode_tab to look up ccc values.
#
# Along the way we collect all decomposition mappings into %decomps
open(F, "<UnicodeData.txt") || die;
my %decomps;
# All the decomposed values will be written into an array, so we'll record
# the starting of each mapping in the array.
my $decomp_index = 0;
# We will also number each decomposition mapping.
my $ordinal = 0;
print "\n/* Linear dump of decomposed strings */\n";
print "static const char32_t decompositions[]={\n";
my $comma = "\t";
while(defined($_=<F>))
{
s/\#.*//;
my @w=split(/\;/);
s/^\s+// for @w;
s/\s+$// for @w;
my $n;
eval "\$n=0x$w[0]";
die $@ if $@;
$obj->range($n, $n, $w[3]) if $w[3] ne "0";
if ($w[5] ne "")
{
my @v = split(/\s+/, $w[5]);
my $type = $v[0];
if ($type =~ /^\</)
{
shift @v;
$type =~ s/<//;
$type =~ s/>//;
$type = "UNICODE_CANONICAL_FMT_" . uc($type);
}
else
{
$type = "UNICODE_CANONICAL_FMT_NONE";
}
@v = map {
my $dec;
eval "\$dec = 0x$_";
die $@ if $@;
$dec;
} @v;
$decomps{$n} = {
char => $n,
index => $decomp_index,
type => $type,
ordinal => $ordinal++,
values => \@v,
};
foreach my $hex (@v)
{
print "${comma}0x" .sprintf("%04x", $hex);
$comma=", ";
if ( ((++$decomp_index) % 8) == 0)
{
$comma=",\n\t";
}
}
}
}
print "};\n\n";
# Figure out the optimal hash bucket size.
my $hash = scalar @{ [ keys %decomps ] };
my $maxbuckets = 3; # Got lucky
foreach (0..100)
{
my %buckets;
my $good = 1;
foreach my $ch (sort { $a <=> $b } keys %decomps)
{
my $bucket = ($buckets{$ch % $hash} //= []);
push @$bucket, $decomps{$ch};
if (scalar @$bucket > $maxbuckets)
{
$good = 0;
last;
}
}
unless ($good)
{
++$hash;
next;
}
print qq(
/*
** Hash table lookup of decompositions, hashed by unicode char % $hash with
** $maxbuckets slots in each bucket for unicode chars with the same hash value.
*/
struct decomposition_info {
char32_t ch; /* Unicode character, 0: unused slot */
uint16_t decomp_index; /* Index into the decompositions array */
uint8_t decomp_size; /* Number of chars in the decomposition */
uint8_t decomp_type; /* Type of decomposition */
};\n);
print "\nstatic const struct decomposition_info decomp_lookup[][$maxbuckets]={\n";
$comma="\t";
foreach my $bucket ( 0..($hash-1) )
{
print "${comma}{";
$comma = "";
my $info = $buckets{$bucket};
foreach my $bucket_index (1..$maxbuckets)
{
my $info1 = shift @$info;
print "${comma}{";
unless ($info1)
{
print "0, 0, 0, 0";
}
else
{
print join(", ",
"0x" . sprintf("%04x", $info1->{char}),
$info1->{index},
scalar @{ $info1->{values} },
$info1->{type});
$info1->{bucket} = [$bucket, $bucket_index-1];
}
print "}";
$comma=", ";
}
print "}";
$comma = ",\n\t";
}
print "\n};\n";
print "\n/* CCC table lookup */\n";
$obj->output;
my @all_decomps = map {
die "What's this?\n" if scalar @{$_->{values}} != 2;
$_;
} grep {
my $ch = $_->{char};
my $found = grep { $ch >= $_->[0] && $ch <= $_->[1] }
@full_composition_exclusion;
!$found && $_->{type} eq "UNICODE_CANONICAL_FMT_NONE";
} map { $decomps{$_} } sort keys %decomps;
$maxbuckets = 4;
foreach my $mult1 (41..71)
{
foreach my $mult2 (41..71)
{
$hash = scalar @all_decomps;
foreach (0..500)
{
my %buckets;
my $good;
%buckets = ();
$good = 1;
foreach my $ch (@all_decomps)
{
my ($ch1, $ch2) = @{$ch->{values}};
my $bucket = ($ch1 * $mult1 + $ch2 * $mult2 ) % $hash;
push @{$buckets{$bucket} //= []}, $ch;
if (scalar @{$buckets{$bucket}} > $maxbuckets)
{
$good = 0;
last;
}
}
if ($good)
{
print qq(
/*
** Hashed canonical compositions: first char, last char, composition.
*/
static const char32_t canonical_compositions[][3]={);
$comma="\n\t";
my $counter=0;
foreach (0..($hash-1))
{
foreach my $bucket (@{ $buckets{$_} // []})
{
print $comma . "{"
. join(",",
map { sprintf("0x%04x", $_); }
$bucket->{values}[0],
$bucket->{values}[1],
$bucket->{char}) . "}";
$comma=", ";
$comma=",\n\t" if (++$counter % 8) == 0;
}
}
print qq(
};
/*
** Canonical composition lookup: first*canonical_mult1+second*canonical_mult2
** modulo the size of the canonical_compositions_lookup table.
**
** The value in the table is the index into canonical_composition table.
*/
#define canonical_mult1 $mult1
#define canonical_mult2 $mult2
#ifndef exclusion_table
static uint16_t canonical_compositions_lookup[]={
);
$comma="\t";
$counter=0;
foreach my $index (0..($hash-1))
{
print $comma . $counter;
$comma=",\n\t";
$counter += scalar @{ $buckets{$index} // []};
}
print "\n};\n";
print "#endif\n";
exit(0);
}
++$hash;
}
}
}
die "Hashing of composition failed.\n";
}
die "Hashing of decompositions failed.\n";
|