]> arthur.barton.de Git - netatalk.git/blob - contrib/misc/make-casetable.pl
5227150d07d0c80f7cf06f5d4b90d3cdf99f814a
[netatalk.git] / contrib / misc / make-casetable.pl
1 #!/usr/bin/perl
2 #
3 # usage: make-casetable.pl <infile> <outfile1> <outfile2>
4 #        make-casetable.pl UnicodeData.txt utf16_casetable.h utf16_case.c
5 #
6 # (c) 2011 by HAT <hat@fa2.so-net.ne.jp>
7 #
8 #  This program is free software; you can redistribute it and/or modify
9 #  it under the terms of the GNU General Public License as published by
10 #  the Free Software Foundation; either version 2 of the License, or
11 #  (at your option) any later version.
12 #
13 #  This program is distributed in the hope that it will be useful,
14 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #  GNU General Public License for more details.
17 #
18
19 # See
20 # http://www.unicode.org/reports/tr44/
21 # http://www.unicode.org/Public/UNIDATA/UnicodeData.txt
22
23 # One block has 64 chars.
24 #
25 # BMP
26 # block    0 = dummy
27 # block    1 = U+0000 - U+003F
28 # block    2 = U+0040 - U+007F
29 # .....
30 # block 1024 = U+FFC0 - U+FFFF
31 # block 1025 = dummy
32 #
33 # Surrogate Pair
34 # block  1024 = dummy
35 # block  1025 = U+010000 - U+01003F
36 # block  1026 = U+010040 - U+01007F
37 # .....
38 # block 17408 = U+10FFC0 - U+10FFFF
39 # block 17409 = dummy
40 #
41 # Dummy block is for edge detection.
42 # If block include upper/lower chars, block_enable[]=1.
43
44 use strict;
45 use warnings;
46
47 our $code0;
48 our $Name1;
49 our $General_Category2;
50 our $Canonical_Combining_Class3;
51 our $Bidi_Class4;
52 our $Decomposition_Mapping5;
53 our $Numeric_Value6;
54 our $Numeric_Value7;
55 our $Numeric_Value8;
56 our $Bidi_Mirrored9;
57 our $Unicode_1_Name10;
58 our $ISO_Comment11;
59 our $Simple_Uppercase_Mapping12;
60 our $Simple_Lowercase_Mapping13;
61 our $Simple_Titlecase_Mapping14;
62
63 our $hex_code0;
64 our $Mapping;
65 our $hex_Mapping;
66
67 our $char;
68 our $sp;
69 our $block;
70
71 our @table;
72 our @table_sp;
73
74 our @block_enable;
75 our @block_enable_sp;
76
77 our $table_no;
78 our $block_start;
79 our $block_end;
80 our $char_start;
81 our $char_end;
82
83 open(CHEADER, ">$ARGV[1]");
84 open(CSOURCE, ">$ARGV[2]");
85
86 printf (CHEADER "\/\*\n");
87 printf (CHEADER "DO NOT EDIT BY HAND\!\!\!\n");
88 printf (CHEADER "\n");
89 printf (CHEADER "This file is generated by\n");
90 printf (CHEADER " contrib/misc/make-casetable.pl %s %s %s\n", $ARGV[0], $ARGV[1], $ARGV[2]);
91 printf (CHEADER "\n");
92 printf (CHEADER "%s is got from\n", $ARGV[0]);
93 printf (CHEADER "http\:\/\/www.unicode.org\/Public\/UNIDATA\/UnicodeData.txt\n");
94 printf (CHEADER "\*\/\n");
95 printf (CHEADER "\n");
96
97 printf (CSOURCE "\/\*\n");
98 printf (CSOURCE "DO NOT EDIT BY HAND\!\!\!\n");
99 printf (CSOURCE "\n");
100 printf (CSOURCE "This file is generated by\n");
101 printf (CSOURCE " contrib/misc/make-casetable.pl %s %s %s\n", $ARGV[0], $ARGV[1], $ARGV[2]);
102 printf (CSOURCE "\n");
103 printf (CSOURCE "%s is got from\n", $ARGV[0]);
104 printf (CSOURCE "http\:\/\/www.unicode.org\/Public\/UNIDATA\/UnicodeData.txt\n");
105 printf (CSOURCE "\*\/\n");
106 printf (CSOURCE "\n");
107 printf (CSOURCE "\#include \<netatalk\/endian.h\>\n");
108 printf (CSOURCE "\#include \<atalk\/unicode.h\>\n");
109 printf (CSOURCE "\#include \"%s\"\n", $ARGV[1]);
110 printf (CSOURCE "\n");
111
112 &make_array("upper");
113 &make_array("lower");
114
115 printf (CHEADER "\/\* EOF \*\/\n");
116 printf (CSOURCE "\/\* EOF \*\/\n");
117
118 close(CHEADER);
119 close(CSOURCE);
120
121
122 ###########################################################################
123 sub make_array{
124
125     # init table -----------------------------------------------------
126
127     for ($char = 0 ; $char <= 0xFFFF ; $char++) {
128         $table[$char][0] = $char;       # mapped char
129         $table[$char][1] = $char;       # orig char
130         $table[$char][2] = "";          # char name
131     }
132
133     for ($char = 0x10000 ; $char <= 0x10FFFF ; $char++) {
134         $sp = ((0xD800 - (0x10000 >> 10) + ($char >> 10)) << 16)
135             + (0xDC00 + ($char & 0x3FF));
136         $table_sp[$char][0] = $sp;      # mapped surrogate pair
137         $table_sp[$char][1] = $sp;      # orig surrogate pair
138         $table_sp[$char][2] = $char;    # mapped char
139         $table_sp[$char][3] = $char;    # orig char
140         $table_sp[$char][4] = "";       # char name
141     }
142
143     for ($block = 0 ; $block <= 1025 ; $block++) {
144         $block_enable[$block] = 0;
145     }
146
147     for ($block = 1024 ; $block <= 17409 ; $block++) {
148         $block_enable_sp[$block] = 0;
149     }
150
151     # write data to table --------------------------------------------
152
153     open(UNICODEDATA, "<$ARGV[0]");
154
155     while (<UNICODEDATA>) {
156         chop;
157         (
158          $code0,
159          $Name1,
160          $General_Category2,
161          $Canonical_Combining_Class3,
162          $Bidi_Class4,
163          $Decomposition_Mapping5,
164          $Numeric_Value6,
165          $Numeric_Value7,
166          $Numeric_Value8,
167          $Bidi_Mirrored9,
168          $Unicode_1_Name10,
169          $ISO_Comment11,
170          $Simple_Uppercase_Mapping12,
171          $Simple_Lowercase_Mapping13,
172          $Simple_Titlecase_Mapping14
173         ) = split(/\;/);
174
175         if ($_[0] eq "upper") {
176             $Mapping = $Simple_Uppercase_Mapping12;
177         } elsif ($_[0] eq "lower") {
178             $Mapping = $Simple_Lowercase_Mapping13;
179         } else {
180             exit(1);
181         }
182
183         next if ($Mapping eq "");
184
185         $hex_code0 = hex($code0);
186         $hex_Mapping = hex($Mapping);
187
188         if ($hex_code0 <= 0xFFFF) {
189             $table[$hex_code0][0] = $hex_Mapping;
190             #table[$hex_code0][1]   already set
191             $table[$hex_code0][2] = $Name1;
192             $block_enable[($hex_code0 / 64) +1] = 1;
193         } else {
194             $sp = ((0xD800 - (0x10000 >> 10) + ($hex_Mapping >> 10)) << 16)
195                 + (0xDC00 + ($hex_Mapping & 0x3FF));
196             $table_sp[$hex_code0][0] = $sp;
197             #table_sp[$hex_code0][1]   already set
198             $table_sp[$hex_code0][2] = $hex_Mapping;
199             #table_sp[$hex_code0][3]   already set
200             $table_sp[$hex_code0][4] = $Name1;
201             $block_enable_sp[($hex_code0 / 64) +1] = 1;
202         }
203     }
204
205     close(UNICODEDATA);
206
207     # array for BMP --------------------------------------------------
208
209     printf(CSOURCE "\/*******************************************************************\n");
210     printf(CSOURCE " Convert a wide character to %s case.\n", $_[0]);
211     printf(CSOURCE "*******************************************************************\/\n");
212     printf(CSOURCE "ucs2\_t to%s\_w\(ucs2\_t val\)\n", $_[0]);
213     printf(CSOURCE "{\n");
214
215     $table_no = 1;
216
217     for ($block = 1 ; $block <= 1024 ; $block++) {
218
219         # rising edge detection
220         if ($block_enable[$block - 1] == 0 && $block_enable[$block] == 1) {
221             $block_start = $block;
222         }
223
224         # falling edge detection
225         if ($block_enable[$block] == 1 && $block_enable[$block + 1] == 0) {
226             $block_end = $block;
227
228             $char_start = ($block_start -1)* 64;
229             $char_end = ($block_end * 64) -1;
230
231             printf(CHEADER "static const u\_int16\_t %s\_table\_%d\[%d\] \= \{\n",
232                    $_[0], $table_no, $char_end - $char_start +1);
233
234             for ($char = $char_start ; $char <= $char_end ; $char++) {
235                 printf(CHEADER "  0x%04X, /*U\+%04X*/ /*%s*/\n",
236                        $table[$char][0],
237                        $table[$char][1],
238                        $table[$char][2]
239                     );
240             }
241             printf(CHEADER "\}\;\n");
242             printf(CHEADER "\n");
243
244             printf(CSOURCE "    if \( val \>\= 0x%04X \&\& val \<\= 0x%04X)\n",
245                    $char_start, $char_end);
246             printf(CSOURCE "        return %s\_table\_%d\[val-0x%04X\]\;\n",
247                    $_[0], $table_no, $char_start);
248             printf(CSOURCE "\n");
249
250             $table_no++;
251         }
252     }
253
254     printf(CSOURCE "\treturn \(val\)\;\n");
255     printf(CSOURCE "\}\n");
256     printf(CSOURCE "\n");
257
258     # array for Surrogate Pair ---------------------------------------
259
260     printf(CSOURCE "\/*******************************************************************\n");
261     printf(CSOURCE " Convert a surrogate pair to %s case.\n", $_[0]);
262     printf(CSOURCE "*******************************************************************\/\n");
263     printf(CSOURCE "u\_int32\_t to%s\_sp\(u\_int32\_t val\)\n", $_[0]);
264     printf(CSOURCE "{\n");
265
266     $table_no = 1;
267
268     for ($block = 1025 ; $block <= 17408 ; $block++) {
269
270         # rising edge detection
271         if ((($block_enable_sp[$block - 1] == 0) || ((($block - 1) & 0xF) == 0))
272             && ($block_enable_sp[$block] == 1)) {
273             $block_start = $block;
274         }
275
276         # falling edge detection
277         if (($block_enable_sp[$block] == 1) &&
278             ((($block - 1) & 0xF == 0xF) || ($block_enable_sp[$block + 1] == 0))) {
279             $block_end = $block;
280
281             $char_start = ($block_start -1)* 64;
282             $char_end = ($block_end * 64) -1;
283
284             printf(CHEADER "static const u\_int32\_t %s\_table\_sp\_%d\[%d\] \= \{\n",
285                    $_[0], $table_no, $char_end - $char_start +1);
286
287             for ($char = $char_start ; $char <= $char_end ; $char++) {
288                 printf(CHEADER "  0x%08X, /*0x%08X*/ /*U\+%06X*/ /*U\+%06X*/ /*%s*/\n",
289                        $table_sp[$char][0],
290                        $table_sp[$char][1],
291                        $table_sp[$char][2],
292                        $table_sp[$char][3],
293                        $table_sp[$char][4]
294                     );
295             }
296             printf(CHEADER "\}\;\n");
297             printf(CHEADER "\n");
298
299             printf(CSOURCE "    if \( val \>\= 0x%08X \&\& val \<\= 0x%08X)\n",
300                    $table_sp[$char_start][1], $table_sp[$char_end][1]);
301             printf(CSOURCE "        return %s\_table\_sp\_%d\[val-0x%08X\]\;\n",
302                    $_[0], $table_no, $table_sp[$char_start][1]);
303             printf(CSOURCE "\n");
304
305             $table_no++;
306         }
307     }
308
309     printf(CSOURCE "\treturn \(val\)\;\n");
310     printf(CSOURCE "\}\n");
311     printf(CSOURCE "\n");
312 }
313
314 # EOF