chiark / gitweb /
list uncovered files in lib/
[disorder] / scripts / make-unidata
CommitLineData
61507e3c
RK
1#! /usr/bin/perl -w
2#
e5a5a138
RK
3# This file is part of DisOrder.
4# Copyright (C) 2007 Richard Kettlewell
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful, but
12# WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14# General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19# USA
20#
21#
22# Generate Unicode support tables
23#
24# This script will download data from unicode.org if the required files
25# aren't in the current directory.
26#
27# After modifying this script you should run:
28# make -C lib rebuild-unicode check
29#
30# Things not supported yet:
31# - SpecialCasing.txt data for case mapping
32# - Title case offsets
33# - Some kind of hinting for composition
e5a5a138
RK
34# - ...
35#
36# NB the generated files DO NOT offer a stable ABI and so are not immediately
37# suitable for use in a general-purpose library. Things that would need to
38# be done:
39# - Hide unidata.h from applications; it will never be ABI- or even API-stable.
40# - Stablized General_Category values
41# - Extend the unicode.h API to general utility rather than just what
42# DisOrder needs.
43# - ...
44#
61507e3c 45use strict;
35b651f0 46use File::Basename;
61507e3c
RK
47
48sub out {
49 print @_ or die "$!\n";
50}
51
52sub key {
53 my $d = shift;
54 local $_;
55
56 return join("-", map($d->{$_}, sort keys %$d));
57}
58
e5a5a138
RK
59# Size of a subtable
60#
61# This can be varied to trade off the number of subtables against their size.
c2e01e0a
RK
62# 16 gave the smallest results last time I checked (on a Mac with a 32-bit
63# build).
64our $modulus = 16;
65
66if(@ARGV) {
67 $modulus = shift;
68}
e5a5a138 69
1a05e381
RK
70# Where to break the table. There is a huge empty section of the Unicode
71# code space and we deal with this by simply leaving it out of the table.
72# This complicates the lookup function a little but should not affect
73# performance in the cases we care about.
74our $break_start = 0x30000;
75our $break_end = 0xE0000;
76
77# Similarly we simply omit the very top of the table and sort it out in the
78# lookup function.
79our $break_top = 0xE0200;
80
61507e3c
RK
81my %cats = (); # known general categories
82my %data = (); # mapping of codepoints to information
61507e3c 83my $max = 0; # maximum codepoint
e5a5a138
RK
84my $maxccc = 0; # maximum combining class
85my $maxud = 0;
86my $minud = 0; # max/min upper case offset
87my $maxld = 0;
88my $minld = 0; # max/min lower case offset
89
90# Make sure we have our desired input files. We explicitly specify a
91# Unicode standard version to make sure that a given version of DisOrder
92# supports a given version of Unicode.
0b7052da 93sub input {
35b651f0
RK
94 my $path = shift;
95 my $lpath = basename($path);
96 if(!-e $lpath) {
97 system("wget http://www.unicode.org/Public/5.0.0/ucd/$path");
98 chmod(0444, $lpath) or die "$lpath: $!\n";
e5a5a138 99 }
0b7052da 100 open(STDIN, "<$lpath") or die "$lpath: $!\n";
bcf9ed7f 101 print STDERR "Reading $lpath...\n";
e5a5a138 102}
61507e3c 103
e5a5a138
RK
104
105# Read the main data file
0b7052da 106input("UnicodeData.txt");
1a05e381 107my ($start, $end);
16506c9d
RK
108my $maxcompat = 0;
109my $maxcanon = 0;
110my $hangul_syllable_decomps = 0;
111my $hangul_choseong_decomps = 0;
61507e3c
RK
112while(<>) {
113 my @f = split(/;/, $_);
114 my $c = hex($f[0]); # codepoint
61507e3c 115 my $name = $f[1];
1a05e381
RK
116 die "$f[0] $name is in the break\n"
117 if $c >= $break_start && $c < $break_end;
e5a5a138 118 my $gc = $f[2]; # General_Category
bcf9ed7f 119 # Variuos GCs we don't expect to see in UnicodeData.txt
0a4af692 120 $cats{$gc} = 1; # always record all GCs
1a05e381
RK
121 if($name =~ /first>/i) {
122 $start = $c;
123 next;
124 } elsif($name =~ /last>/i) {
125 $end = $c;
126 } else {
127 $start = $end = $c;
128 }
bcf9ed7f 129 die "unexpected Cn" if $gc eq 'Cn';
e5a5a138
RK
130 my $ccc = $f[3]; # Canonical_Combining_Class
131 my $dm = $f[5]; # Decomposition_Type + Decomposition_Mapping
132 my $sum = hex($f[12]) || $c; # Simple_Uppercase_Mapping
133 my $slm = hex($f[13]) || $c; # Simple_Lowercase_Mapping
61507e3c
RK
134 # recalculate the upper/lower case mappings as offsets
135 my $ud = $sum - $c;
136 my $ld = $slm - $c;
e5a5a138
RK
137 # update bounds on various values
138 $maxccc = $ccc if $ccc > $maxccc; # assumed never to be -ve
139 $minud = $ud if $ud < $minud;
140 $maxud = $ud if $ud > $maxud;
141 $minld = $ld if $ld < $minld;
142 $maxld = $ld if $ld > $maxld;
1a05e381 143 if($start != $end) {
0e843521 144 printf STDERR "> range %04X-%04X is %s\n", $start, $end, $gc;
1a05e381
RK
145 }
146 for($c = $start; $c <= $end; ++$c) {
0e843521
RK
147 my $d = {
148 "gc" => $gc,
149 "ccc" => $ccc,
150 "ud" => $ud,
151 "ld" => $ld,
152 };
153 if($dm ne '') {
16506c9d 154 my $maxref;
f98fcddb
RK
155 if($dm =~ /</) {
156 # This is a compatibility decomposition
0e843521 157 $dm =~ s/^<.*>\s*//;
f98fcddb 158 $d->{compat} = 1;
16506c9d
RK
159 $maxref = \$maxcompat;
160 } else {
161 $maxref = \$maxcanon;
0e843521 162 }
99695df9 163 $d->{decomp} = [map(hex($_), split(/\s+/, $dm))];
16506c9d
RK
164 my $len = scalar @{$d->{decomp}};
165 $$maxref = $len if $len > $$maxref;
166 if(!$d->{compat}) {
167 if(${$d->{decomp}}[0] >= 0xAC00 && ${$d->{decomp}}[0] <= 0xD7A3) {
168 ++$hangul_syllable_decomps;
169 }
170 if(${$d->{decomp}}[0] >= 0x1100 && ${$d->{decomp}}[0] <= 0x115F) {
171 ++$hangul_choseong_decomps;
172 }
173 }
0e843521 174 }
1a05e381
RK
175 $data{$c} = $d;
176 }
61507e3c 177 $cats{$gc} = 1;
1a05e381 178 $max = $end if $end > $max;
61507e3c
RK
179}
180
0b7052da
RK
181sub read_prop_with_ranges {
182 my $path = shift;
183 my $propkey = shift;
184 input($path);
185 while(<>) {
186 chomp;
187 s/\s*\#.*//;
188 next if $_ eq '';
189 my ($range, $propval) = split(/\s*;\s*/, $_);
190 if($range =~ /(.*)\.\.(.*)/) {
191 for my $c (hex($1) .. hex($2)) {
0e843521 192 $data{$c}->{$propkey} = $propval;
0b7052da
RK
193 }
194 } else {
195 my $c = hex($range);
0e843521 196 $data{$c}->{$propkey} = $propval;
35b651f0 197 }
0b7052da
RK
198 }
199}
200
349b7b74 201# Grapheme_Break etc
0b7052da 202read_prop_with_ranges("auxiliary/GraphemeBreakProperty.txt", "gbreak");
0b7052da 203read_prop_with_ranges("auxiliary/WordBreakProperty.txt", "wbreak");
349b7b74 204read_prop_with_ranges("auxiliary/SentenceBreakProperty.txt", "sbreak");
0b7052da 205
349b7b74
RK
206# Compute the full list and fill in the Extend category properly
207my %gbreak = ();
208my %wbreak = ();
209my %sbreak = ();
0b7052da 210for my $c (keys %data) {
349b7b74
RK
211 if(!exists $data{$c}->{gbreak}) {
212 $data{$c}->{gbreak} = 'Other';
213 }
214 $gbreak{$data{$c}->{gbreak}} = 1;
215
0b7052da 216 if(!exists $data{$c}->{wbreak}) {
349b7b74 217 if($data{$c}->{gbreak} eq 'Extend') {
0b7052da
RK
218 $data{$c}->{wbreak} = 'Extend';
219 } else {
220 $data{$c}->{wbreak} = 'Other';
35b651f0
RK
221 }
222 }
349b7b74
RK
223 $wbreak{$data{$c}->{wbreak}} = 1;
224
225 if(!exists $data{$c}->{sbreak}) {
226 if($data{$c}->{gbreak} eq 'Extend') {
227 $data{$c}->{sbreak} = 'Extend';
228 } else {
229 $data{$c}->{sbreak} = 'Other';
230 }
231 }
232 $sbreak{$data{$c}->{sbreak}} = 1;
35b651f0
RK
233}
234
99695df9
RK
235# Various derived properties
236input("DerivedNormalizationProps.txt");
237while(<>) {
238 chomp;
239 s/\s*\#.*//;
240 next if $_ eq '';
241 my @f = split(/\s*;\s*/, $_);
242 if(@f == 2) {
243 push(@f, 1);
244 }
245 my ($range, $propkey, $propval) = @f;
246 if($range =~ /(.*)\.\.(.*)/) {
247 for my $c (hex($1) .. hex($2)) {
248 $data{$c}->{$propkey} = $propval
249 }
250 } else {
251 my $c = hex($range);
252 $data{$c}->{$propkey} = $propval
253 }
254}
255
e5a5a138
RK
256# Round up the maximum value to a whole number of subtables
257$max += ($modulus - 1) - ($max % $modulus);
61507e3c 258
bcf9ed7f
RK
259# Private use characters
260# We only fill in values below $max, utf32__unidata()
261my $Co = {
262 "gc" => "Co",
263 "ccc" => 0,
264 "ud" => 0,
265 "ld" => 0
266};
267for(my $c = 0xE000; $c <= 0xF8FF && $c <= $max; ++$c) {
268 $data{$c} = $Co;
269}
270for(my $c = 0xF0000; $c <= 0xFFFFD && $c <= $max; ++$c) {
271 $data{$c} = $Co;
272}
273for(my $c = 0x100000; $c <= 0x10FFFD && $c <= $max; ++$c) {
274 $data{$c} = $Co;
275}
276
277# Anything left is not assigned
278my $Cn = {
279 "gc" => "Cn", # not assigned
280 "ccc" => 0,
281 "ud" => 0,
282 "ld" => 0
283};
61507e3c
RK
284for(my $c = 0; $c <= $max; ++$c) {
285 if(!exists $data{$c}) {
bcf9ed7f
RK
286 $data{$c} = $Cn;
287 }
288 if(!exists $data{$c}->{wbreak}) {
289 $data{$c}->{wbreak} = 'Other';
290 }
291 if(!exists $data{$c}->{gbreak}) {
292 $data{$c}->{gbreak} = 'Other';
293 }
294 if(!exists $data{$c}->{sbreak}) {
295 $data{$c}->{sbreak} = 'Other';
61507e3c
RK
296 }
297}
298$cats{'Cn'} = 1;
299
e5a5a138 300# Read the casefolding data too
0b7052da 301input("CaseFolding.txt");
e5a5a138
RK
302while(<>) {
303 chomp;
304 next if /^\#/ or $_ eq '';
305 my @f = split(/\s*;\s*/, $_);
306 # Full case folding means use status C and F.
307 # We discard status T, Turkish users may wish to change this.
308 if($f[1] eq 'C' or $f[1] eq 'F') {
309 my $c = hex($f[0]);
310 $data{$c}->{casefold} = $f[2];
311 # We are particularly interest in combining characters that
312 # case-fold to non-combining characters, or characters that
313 # case-fold to sequences with combining characters in non-initial
314 # positions, as these required decomposiiton before case-folding
315 my @d = map(hex($_), split(/\s+/, $data{$c}->{casefold}));
316 if($data{$c}->{ccc} != 0) {
317 # This is a combining character
318 if($data{$d[0]}->{ccc} == 0) {
319 # The first character of its case-folded form is NOT
320 # a combining character. The field name is the example
321 # explicitly mentioned in the spec.
322 $data{$c}->{ypogegrammeni} = 1;
323 }
324 } else {
325 # This is a non-combining character; inspect the non-initial
326 # code points of the case-folded sequence
327 shift(@d);
328 if(grep($data{$_}->{ccc} != 0, @d)) {
329 # Some non-initial code point in the case-folded for is NOT a
330 # a combining character.
331 $data{$c}->{ypogegrammeni} = 1;
332 }
333 }
334 }
335}
336
337# Generate the header file
bcf9ed7f 338print STDERR "Generating unidata.h...\n";
61507e3c
RK
339open(STDOUT, ">unidata.h") or die "unidata.h: $!\n";
340
e5a5a138
RK
341out("/* Automatically generated file, see scripts/make-unidata */\n",
342 "#ifndef UNIDATA_H\n",
61507e3c
RK
343 "#define UNIDATA_H\n");
344
e5a5a138 345# TODO choose stable values for General_Category
14523635 346out("enum unicode_General_Category {\n",
61507e3c 347 join(",\n",
14523635 348 map(" unicode_General_Category_$_", sort keys %cats)), "\n};\n");
e5a5a138 349
349b7b74
RK
350out("enum unicode_Grapheme_Break {\n",
351 join(",\n",
352 map(" unicode_Grapheme_Break_$_", sort keys %gbreak)),
353 "\n};\n");
354out("extern const char *const unicode_Grapheme_Break_names[];\n");
355
0b7052da
RK
356out("enum unicode_Word_Break {\n",
357 join(",\n",
349b7b74 358 map(" unicode_Word_Break_$_", sort keys %wbreak)),
0b7052da 359 "\n};\n");
bb48024f 360out("extern const char *const unicode_Word_Break_names[];\n");
0b7052da 361
349b7b74
RK
362out("enum unicode_Sentence_Break {\n",
363 join(",\n",
364 map(" unicode_Sentence_Break_$_", sort keys %sbreak)),
365 "\n};\n");
366out("extern const char *const unicode_Sentence_Break_names[];\n");
367
e5a5a138 368out("enum unicode_flags {\n",
f98fcddb
RK
369 " unicode_normalize_before_casefold = 1,\n",
370 " unicode_compatibility_decomposition = 2\n",
e5a5a138
RK
371 "};\n",
372 "\n");
373
374# Choose the narrowest type that will fit the required values
375sub choosetype {
376 my ($min, $max) = @_;
377 if($min >= 0) {
378 return "char" if $max <= 127;
379 return "unsigned char" if $max <= 255;
380 return "int16_t" if $max < 32767;
381 return "uint16_t" if $max < 65535;
382 return "int32_t";
383 } else {
384 return "char" if $min >= -127 && $max <= 127;
385 return "int16_t" if $min >= -32767 && $max <= 32767;
386 return "int32_t";
387 }
388}
389
61507e3c 390out("struct unidata {\n",
99695df9
RK
391 # decomposition (canonical or compatibility;
392 # unicode_compatibility_decomposition distinguishes) or NULL
f98fcddb 393 " const uint32_t *decomp;\n",
99695df9
RK
394
395 # case-folded string or NULL
e5a5a138 396 " const uint32_t *casefold;\n",
99695df9
RK
397
398 # composed characters that start with this code point. This only
399 # includes primary composites, i.e. the decomposition mapping is
400 # canonical and this code point is not in the exclusion table.
401 " const uint32_t *composed;\n",
402
1a05e381
RK
403# " ".choosetype($minud, $maxud)." upper_offset;\n",
404# " ".choosetype($minld, $maxld)." lower_offset;\n",
99695df9
RK
405
406 # canonical combining class
e5a5a138 407 " ".choosetype(0, $maxccc)." ccc;\n",
14523635 408 " char general_category;\n",
99695df9
RK
409
410 # see unicode_flags enum
e5a5a138 411 " uint8_t flags;\n",
349b7b74 412 " char grapheme_break;\n",
0b7052da 413 " char word_break;\n",
349b7b74 414 " char sentence_break;\n",
61507e3c 415 "};\n");
f98fcddb 416# decomp and casefold do have have non-BMP characters, so we
e5a5a138
RK
417# can't use a simple 16-bit table. We could use UTF-8 or UTF-16
418# though, saving a bit of space (probably not that much...) at the
419# cost of marginally reduced performance and additional complexity
61507e3c
RK
420
421out("extern const struct unidata *const unidata[];\n");
422
18cda350
RK
423out("extern const struct unicode_utf8_row {\n",
424 " uint8_t count;\n",
425 " uint8_t min2, max2;\n",
426 "} unicode_utf8_valid[];\n");
427
61507e3c 428out("#define UNICODE_NCHARS ", ($max + 1), "\n");
e5a5a138 429out("#define UNICODE_MODULUS $modulus\n");
1a05e381
RK
430out("#define UNICODE_BREAK_START $break_start\n");
431out("#define UNICODE_BREAK_END $break_end\n");
432out("#define UNICODE_BREAK_TOP $break_top\n");
61507e3c
RK
433
434out("#endif\n");
435
436close STDOUT or die "unidata.h: $!\n";
437
bcf9ed7f 438print STDERR "Generating unidata.c...\n";
61507e3c
RK
439open(STDOUT, ">unidata.c") or die "unidata.c: $!\n";
440
e5a5a138
RK
441out("/* Automatically generated file, see scripts/make-unidata */\n",
442 "#include <config.h>\n",
443 "#include \"types.h\"\n",
444 "#include \"unidata.h\"\n");
445
349b7b74 446# Short aliases to keep .c file small
e5a5a138 447
14523635
RK
448out(map(sprintf("#define %s unicode_General_Category_%s\n", $_, $_),
449 sort keys %cats));
450out(map(sprintf("#define GB%s unicode_Grapheme_Break_%s\n", $_, $_),
451 sort keys %gbreak));
452out(map(sprintf("#define WB%s unicode_Word_Break_%s\n", $_, $_),
453 sort keys %wbreak));
454out(map(sprintf("#define SB%s unicode_Sentence_Break_%s\n", $_, $_),
455 sort keys %sbreak));
99695df9
RK
456out("#define NBC unicode_normalize_before_casefold\n");
457out("#define CD unicode_compatibility_decomposition\n");
e5a5a138 458
349b7b74
RK
459# Names for *_Break properties
460out("const char *const unicode_Grapheme_Break_names[] = {\n",
461 join(",\n",
462 map(" \"$_\"", sort keys %gbreak)),
463 "\n};\n");
bb48024f
RK
464out("const char *const unicode_Word_Break_names[] = {\n",
465 join(",\n",
349b7b74
RK
466 map(" \"$_\"", sort keys %wbreak)),
467 "\n};\n");
468out("const char *const unicode_Sentence_Break_names[] = {\n",
469 join(",\n",
470 map(" \"$_\"", sort keys %sbreak)),
bb48024f
RK
471 "\n};\n");
472
99695df9
RK
473our $ddnum = 0;
474our $ddsaved = 0;
475our %ddnums = ();
476my $ddfirst = 1;
477out("static const uint32_t ");
478sub dedupe {
479 my $s = join(",", @_);
480 if(!exists $ddnums{$s}) {
481 if($ddfirst) {
482 $ddfirst = 0;
483 } else {
484 out(",\n");
485 }
486 out("dd$ddnum\[]={$s}");
487 $ddnums{$s} = $ddnum++;
488 } else {
489 ++$ddsaved;
490 }
491 return "dd$ddnums{$s}";
492}
493
16506c9d 494# Generate the decomposition mapping tables.
99695df9 495print STDERR "> decomposition mappings\n";
e5a5a138 496for(my $c = 0; $c <= $max; ++$c) {
f98fcddb 497 if(exists $data{$c} && exists $data{$c}->{decomp}) {
99695df9
RK
498 $data{$c}->{decompsym} = dedupe(@{$data{$c}->{decomp}}, 0);
499 }
500}
501
502print STDERR "> composition mappings\n";
503# First we must generate the mapping of each code point to possible
16506c9d 504# compositions.
99695df9
RK
505for(my $c = 0; $c <= $max; ++$c) {
506 if(exists $data{$c}
507 && exists $data{$c}->{decomp}
508 && !exists $data{$c}->{compat}
509 && !$data{$c}->{Full_Composition_Exclusion}) {
510 # $c has a non-excluded canonical decomposition, i.e. it is
511 # a primary composite. Find the first code point of the decomposition
512 my $first = ${$data{$c}->{decomp}}[0];
513 if(!exists $data{$first}->{compose}) {
16506c9d 514 $data{$first}->{compose} = [$c];
e5a5a138 515 } else {
16506c9d 516 push(@{$data{$first}->{compose}}, $c);
e5a5a138 517 }
e5a5a138
RK
518 }
519}
16506c9d 520# Then we can generate the tables.
99695df9
RK
521for(my $c = 0; $c <= $max; ++$c) {
522 if(exists $data{$c} && exists $data{$c}->{compose}) {
523 $data{$c}->{compsym} = dedupe(@{$data{$c}->{compose}}, 0);
524 }
525}
e5a5a138 526
16506c9d 527# The case folding table.
99695df9 528print STDERR "> case-fold mappings\n";
e5a5a138 529for(my $c = 0; $c <= $max; ++$c) {
bcf9ed7f 530 if(exists $data{$c} && exists $data{$c}->{casefold}) {
99695df9
RK
531 $data{$c}->{cfsym} = dedupe(map(hex($_), split(/\s+/,
532 $data{$c}->{casefold})),
533 0);
e5a5a138
RK
534 }
535}
99695df9
RK
536
537# End of de-dupable arrays
e5a5a138
RK
538out(";\n");
539
540# Visit all the $modulus-character blocks in turn and generate the
541# required subtables. As above we spot duplicates to save space. In
542# Unicode 5.0.0 with $modulus=128 and current table data this saves
543# 1372 subtables or at least three and a half megabytes on 32-bit
544# platforms.
99695df9 545print STDERR "> subtables\n";
61507e3c
RK
546my %subtable = (); # base->subtable number
547my %subtableno = (); # subtable number -> content
548my $subtablecounter = 0; # counter for subtable numbers
e5a5a138
RK
549my $subtablessaved = 0; # number of tables saved
550for(my $base = 0; $base <= $max; $base += $modulus) {
1a05e381
RK
551 next if $base >= $break_start && $base < $break_end;
552 next if $base >= $break_top;
61507e3c 553 my @t;
e5a5a138 554 for(my $c = $base; $c < $base + $modulus; ++$c) {
61507e3c 555 my $d = $data{$c};
f98fcddb 556 my $decompsym = ($data{$c}->{decompsym} or "0");
e5a5a138 557 my $cfsym = ($data{$c}->{cfsym} or "0");
99695df9 558 my $compsym = ($data{$c}->{compsym} or "0");
35b651f0
RK
559 my @flags = ();
560 if($data{$c}->{ypogegrammeni}) {
99695df9 561 push(@flags, "NBC");
35b651f0 562 }
f98fcddb 563 if($data{$c}->{compat}) {
99695df9 564 push(@flags, "CD");
f98fcddb 565 }
35b651f0 566 my $flags = @flags ? join("|", @flags) : 0;
e5a5a138
RK
567 push(@t, "{".
568 join(",",
f98fcddb 569 $decompsym,
e5a5a138 570 $cfsym,
99695df9 571 $compsym,
1a05e381
RK
572# $d->{ud},
573# $d->{ld},
0b7052da
RK
574 $d->{ccc},
575 $d->{gc},
e5a5a138 576 $flags,
349b7b74
RK
577 "GB$d->{gbreak}",
578 "WB$d->{wbreak}",
579 "SB$d->{sbreak}",
e5a5a138 580 )."}");
61507e3c
RK
581 }
582 my $t = join(",\n", @t);
583 if(!exists $subtable{$t}) {
0e843521 584 out(sprintf("/* %04X-%04X */\n", $base, $base + $modulus - 1));
e5a5a138 585 out("static const struct unidata st$subtablecounter\[] = {\n",
61507e3c
RK
586 "$t\n",
587 "};\n");
588 $subtable{$t} = $subtablecounter++;
e5a5a138
RK
589 } else {
590 ++$subtablessaved;
61507e3c
RK
591 }
592 $subtableno{$base} = $subtable{$t};
593}
594
99695df9 595print STDERR "> main table\n";
bcf9ed7f 596out("const struct unidata *const unidata[]={\n");
e5a5a138 597for(my $base = 0; $base <= $max; $base += $modulus) {
1a05e381
RK
598 next if $base >= $break_start && $base < $break_end;
599 next if $base >= $break_top;
0a4af692 600 #out("st$subtableno{$base} /* ".sprintf("%04x", $base)." */,\n");
e5a5a138 601 out("st$subtableno{$base},\n");
61507e3c
RK
602}
603out("};\n");
604
99695df9 605print STDERR "> UTF-8 table\n";
18cda350
RK
606out("const struct unicode_utf8_row unicode_utf8_valid[] = {\n");
607for(my $c = 0; $c <= 0x7F; ++$c) {
608 out(" { 1, 0, 0 }, /* $c */\n");
609}
610for(my $c = 0x80; $c < 0xC2; ++$c) {
611 out(" { 0, 0, 0 }, /* $c */\n");
612}
613for(my $c = 0xC2; $c <= 0xDF; ++$c) {
614 out(" { 2, 0x80, 0xBF }, /* $c */\n");
615}
616for(my $c = 0xE0; $c <= 0xE0; ++$c) {
617 out(" { 3, 0xA0, 0xBF }, /* $c */\n");
618}
619for(my $c = 0xE1; $c <= 0xEC; ++$c) {
620 out(" { 3, 0x80, 0xBF }, /* $c */\n");
621}
622for(my $c = 0xED; $c <= 0xED; ++$c) {
623 out(" { 3, 0x80, 0x9F }, /* $c */\n");
624}
625for(my $c = 0xEE; $c <= 0xEF; ++$c) {
626 out(" { 3, 0x80, 0xBF }, /* $c */\n");
627}
628for(my $c = 0xF0; $c <= 0xF0; ++$c) {
629 out(" { 4, 0x90, 0xBF }, /* $c */\n");
630}
631for(my $c = 0xF1; $c <= 0xF3; ++$c) {
632 out(" { 4, 0x80, 0xBF }, /* $c */\n");
633}
634for(my $c = 0xF4; $c <= 0xF4; ++$c) {
635 out(" { 4, 0x80, 0x8F }, /* $c */\n");
636}
637for(my $c = 0xF5; $c <= 0xFF; ++$c) {
638 out(" { 0, 0, 0 }, /* $c */\n");
639}
640out("};\n");
641
61507e3c
RK
642close STDOUT or die "unidata.c: $!\n";
643
99695df9 644print STDERR "Done.\n\n";
c2e01e0a 645printf STDERR "modulus=%d\n", $modulus;
bcf9ed7f
RK
646printf STDERR "max=%04X\n", $max;
647print STDERR "subtables=$subtablecounter, subtablessaved=$subtablessaved\n";
99695df9 648print STDERR "ddsaved=$ddsaved\n";
16506c9d
RK
649print STDERR "maxcompat=$maxcompat maxcanon=$maxcanon\n";
650print STDERR "$hangul_syllable_decomps canonical decompositions to Hangul syllables\n";
651print STDERR "$hangul_choseong_decomps canonical decompositions to Hangul Choseong\n";
652
653die "We assumed that canonical decompositions were never more than 2 long!\n"
654 if $maxcanon > 2;
655
656die "We assumed no canonical decompositions to Hangul syllables/Choseong!\n"
657 if $hangul_syllable_decomps || $hangul_choseong_decomps;