gconvert: mention that the g_convert len should be in bytes
[platform/upstream/glib.git] / glib / gen-script-table.pl
1 #!/usr/bin/perl -w 
2 #
3 # Script to convert http://www.unicode.org/Public/UNIDATA/Scripts.txt
4 # into a machine-readable table.
5 #
6 ######################################################################
7
8 if (@ARGV != 1) {
9     die "Usage: gen-script-table.pl Scripts.txt > gscripttable.h\n";
10 }
11
12 open IN, $ARGV[0] || die "Cannot open $ARGV[0]: $!\n";
13
14 my @ranges;
15 my $file;
16 my $easy_range;
17 my $i;
18 my $start;
19 my $end;
20 my $script;
21
22
23 while (<IN>) {
24     if (/^\#\s+(Scripts-.*.txt)/) {
25         $file = $1;
26     }
27     
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";
32     }
33
34     if (defined $2) {
35         push @ranges, [ hex $1, hex $2, uc $3 ];
36     } else {
37         push @ranges, [ hex $1, hex $1, uc $3 ];
38     }
39 }
40
41 @ranges = sort { $a->[0] <=> $b->[0] } @ranges;
42 $date = gmtime;
43
44 print <<"EOT";
45 /* gscripttable.h: Generated by gen-script-table.pl
46  *
47  *  Date: $date
48  *  Source: $file
49  *
50  * Do not edit.
51  */
52
53 EOT
54
55 $easy_range = 0x2000;
56
57 print <<"EOT";
58 #define G_EASY_SCRIPTS_RANGE $easy_range
59
60 static const guchar g_script_easy_table[$easy_range] = {
61 EOT
62
63 $i = 0;
64 $end = -1;
65
66 for (my $c = 0; $c < $easy_range; $c++) {
67
68     if ($c % 3 == 0) {
69       printf "\n ";
70     }
71
72     if ($c > $end) {
73         $start = $ranges[$i]->[0];
74         $end = $ranges[$i]->[1];
75         $script = $ranges[$i]->[2];
76         $i++;
77     }
78     
79     if ($c < $start) {
80         printf " G_UNICODE_SCRIPT_UNKNOWN,";
81     } else {
82         printf " G_UNICODE_SCRIPT_%s,", $script;
83     }
84 }
85
86 if ($end >= $easy_range) {
87     $i--;
88     $ranges[$i]->[0] = $easy_range;
89 }
90
91
92 print <<"EOT";
93
94 };
95
96 static const struct {
97     gunichar    start;
98     guint16     chars;
99     guint16     script;
100 } g_script_table[] = { 
101 EOT
102
103 for (; $i <= $#ranges; $i++) {
104     $start = $ranges[$i]->[0];
105     $end = $ranges[$i]->[1];
106     $script = $ranges[$i]->[2];
107
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];
113     }
114
115     printf " { %#06x, %5d, G_UNICODE_SCRIPT_%s },\n", $start, $end - $start + 1, $script;
116 }
117
118 printf "};\n";
119