Updates
[glib.git] / glib / gen-script-table.pl
blob12e0aa9af0b6bf461321e3d328b5a2505b3aafe9
1 #!/usr/bin/perl -w
3 # Script to convert http://www.unicode.org/Public/UNIDATA/Scripts.txt
4 # into a machine-readable table.
6 ######################################################################
8 if (@ARGV != 1) {
9 die "Usage: gen-script-table.pl Scripts.txt > gscripttable.h\n";
12 open IN, $ARGV[0] || die "Cannot open $ARGV[0]: $!\n";
14 my @ranges;
15 my $file;
16 my $easy_range;
17 my $i;
18 my $start;
19 my $end;
20 my $script;
23 while (<IN>) {
24 if (/^\#\s+(Scripts-.*.txt)/) {
25 $file = $1;
28 s/#.*//;
29 next if /^\s*$/;
30 if (!/^([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s*;\s*([A-Za-z_]+)\s*$/) {
31 die "Cannot parse line: '$_'\n";
34 if (defined $2) {
35 push @ranges, [ hex $1, hex $2, uc $3 ];
36 } else {
37 push @ranges, [ hex $1, hex $1, uc $3 ];
41 @ranges = sort { $a->[0] <=> $b->[0] } @ranges;
42 $date = gmtime;
44 print <<"EOT";
45 /* gscripttable.h: Generated by gen-script-table.pl
47 * Date: $date
48 * Source: $file
50 * Do not edit.
53 EOT
55 $easy_range = 0x2000;
57 print <<"EOT";
58 #define G_EASY_SCRIPTS_RANGE $easy_range
60 static const guchar g_script_easy_table[$easy_range] = {
61 EOT
63 $i = 0;
64 $end = -1;
66 for (my $c = 0; $c < $easy_range; $c++) {
68 if ($c % 3 == 0) {
69 printf "\n ";
72 if ($c > $end) {
73 $start = $ranges[$i]->[0];
74 $end = $ranges[$i]->[1];
75 $script = $ranges[$i]->[2];
76 $i++;
79 if ($c < $start) {
80 printf " G_SCRIPT_UNKNOWN,";
81 } else {
82 printf " G_SCRIPT_%s,", $script;
86 if ($end >= $easy_range) {
87 $i--;
88 $ranges[$i]->[0] = $easy_range;
92 print <<"EOT";
96 static const struct {
97 gunichar start;
98 guint16 chars;
99 guint16 script;
100 } g_script_table[] = {
103 for (; $i <= $#ranges; $i++) {
104 $start = $ranges[$i]->[0];
105 $end = $ranges[$i]->[1];
106 $script = $ranges[$i]->[2];
108 while ($i <= $#ranges - 1 &&
109 $ranges[$i + 1]->[0] == $end + 1 &&
110 $ranges[$i + 1]->[2] eq $script) {
111 $i++;
112 $end = $ranges[$i]->[1];
115 printf " { %#06x, %5d, G_SCRIPT_%s },\n", $start, $end - $start + 1, $script;
118 printf "};\n";