Merge from emacs--devo--0
[bpt/emacs.git] / admin / charsets / gb180302.awk
1 BEGIN {
2 tohex["A"] = 10;
3 tohex["B"] = 11;
4 tohex["C"] = 12;
5 tohex["D"] = 13;
6 tohex["E"] = 14;
7 tohex["F"] = 15;
8 tohex["a"] = 10;
9 tohex["b"] = 11;
10 tohex["c"] = 12;
11 tohex["d"] = 13;
12 tohex["e"] = 14;
13 tohex["f"] = 15;
14 from_gb = 0;
15 to_gb = -1;
16 to_unicode = 0;
17 from_unicode = 0;
18 }
19
20 function decode_hex(str) {
21 n = 0;
22 len = length(str);
23 for (i = 1; i <= len; i++)
24 {
25 c = substr (str, i, 1);
26 if (c >= "0" && c <= "9")
27 n = n * 16 + (c - "0");
28 else
29 n = n * 16 + tohex[c];
30 }
31 return n;
32 }
33
34 function gb_to_index(gb) {
35 b0 = int(gb / 256);
36 b1 = gb % 256;
37 idx = (((b0 - 129)) * 191 + b1 - 64);
38 # if (b1 >= 128)
39 # idx--;
40 return idx
41 }
42
43 function index_to_gb(idx) {
44 b0 = int(idx / 191) + 129;
45 b1 = (idx % 191) + 64;
46 # if (b1 >= 127)
47 # b1++;
48 return (b0 * 256 + b1);
49 }
50
51 /^\#/ {
52 print;
53 next;
54 }
55
56 {
57 gb = gb_to_index(decode_hex(substr($1, 3, 4)));
58 unicode = decode_hex(substr($2, 3, 4));
59 if ((gb == to_gb + 1) && (unicode == to_unicode + 1))
60 {
61 to_gb++;
62 to_unicode++;
63 }
64 else
65 {
66 if (from_gb == to_gb)
67 printf "0x%04X 0x%04X\n", index_to_gb(from_gb), from_unicode;
68 else if (from_gb < to_gb)
69 printf "0x%04X-0x%04X 0x%04X\n",
70 index_to_gb(from_gb), index_to_gb(to_gb), from_unicode;
71 from_gb = to_gb = gb;
72 from_unicode = to_unicode = unicode;
73 }
74 }
75
76 END {
77 if (from_gb <= to_gb)
78 printf "0x%04X-0x%04X 0x%04X\n",
79 index_to_gb(from_gb), index_to_gb(to_gb), from_unicode;
80 }
81
82 # arch-tag: d7dbad89-a512-41a4-8ee0-ba1a4505b8c1