(%.tab): Fix previous change.
[m17n/m17n-db.git] / compact.awk
1 # compact.awk -- awk script to produce a compact charset map
2 # Copyright (C) 2003, 2004
3 #   National Institute of Advanced Industrial Science and Technology (AIST)
4 #   Registration Number H15PRO112
5
6 # This file is part of the m17n database; a sub-part of the m17n
7 # library.
8
9 # The m17n library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1 of
12 # the License, or (at your option) any later version.
13
14 # The m17n library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with the m17n library; if not, write to the Free
21 # Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 # Boston, MA 02110-1301, USA.
23
24 BEGIN {
25   tohex["0"] = 1;
26   tohex["1"] = 2;
27   tohex["2"] = 3;
28   tohex["3"] = 4;
29   tohex["4"] = 5;
30   tohex["5"] = 6;
31   tohex["6"] = 7;
32   tohex["7"] = 8;
33   tohex["8"] = 9;
34   tohex["9"] = 10;
35   tohex["A"] = 11;
36   tohex["B"] = 12;
37   tohex["C"] = 13;
38   tohex["D"] = 14;
39   tohex["E"] = 15;
40   tohex["F"] = 16;
41   tohex["a"] = 11;
42   tohex["b"] = 12;
43   tohex["c"] = 13;
44   tohex["d"] = 14;
45   tohex["e"] = 15;
46   tohex["f"] = 16;
47   from_code = 0;
48   to_code = -1;
49   to_unicode = 0;
50   from_unicode = 0;
51 }
52
53 function decode_hex(str, idx) {
54   n = 0;
55   len = length(str);
56   for (i = idx; i <= len; i++)
57     {
58       c = tohex[substr (str, i, 1)];
59       if (c == 0)
60         break;
61       n = n * 16 + c - 1;
62     }
63   return n;
64 }
65
66 /^\#/ {
67   print;
68   next;
69 }
70
71 {
72   code = decode_hex($1, 3);
73   unicode = decode_hex($2, 3);
74   if ((code == to_code + 1) && (unicode == to_unicode + 1))
75     {
76       to_code++;
77       to_unicode++;
78     }
79   else
80     {
81       if (from_code == to_code)
82         printf "0x%04X 0x%04X\n", from_code, from_unicode;
83       else if (from_code < to_code)
84         printf "0x%04X-0x%04X 0x%04X\n", from_code, to_code, from_unicode;
85       from_code = to_code = code;
86       from_unicode = to_unicode = unicode;
87     }
88 }
89
90 END {
91   if (from_code == to_code)
92     printf "0x%04X 0x%04X\n", from_code, from_unicode;
93   else
94     printf "0x%04X-0x%04X 0x%04X\n", from_code, to_code, from_unicode;
95 }