compact.awk 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # compact.awk -- Make charset map compact.
  2. # Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
  3. # National Institute of Advanced Industrial Science and Technology (AIST)
  4. # Registration Number H13PRO009
  5. # This file is part of GNU Emacs.
  6. # GNU Emacs is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. # GNU Emacs is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. # You should have received a copy of the GNU General Public License
  15. # along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. # Commentary:
  17. # Make a charset map compact by changing this kind of line sequence:
  18. # 0x00 0x0000
  19. # 0x01 0x0001
  20. # ...
  21. # 0x7F 0x007F
  22. # to one line of this format:
  23. # 0x00-0x7F 0x0000
  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. function decode_hex(str, idx) {
  53. n = 0;
  54. len = length(str);
  55. for (i = idx; i <= len; i++)
  56. {
  57. c = tohex[substr (str, i, 1)];
  58. if (c == 0)
  59. break;
  60. n = n * 16 + c - 1;
  61. }
  62. return n;
  63. }
  64. /^\#/ {
  65. print;
  66. next;
  67. }
  68. {
  69. code = decode_hex($1, 3);
  70. unicode = decode_hex($2, 3);
  71. if ((code == to_code + 1) && (unicode == to_unicode + 1))
  72. {
  73. to_code++;
  74. to_unicode++;
  75. }
  76. else
  77. {
  78. if (to_code < 256)
  79. {
  80. if (from_code == to_code)
  81. printf "0x%02X 0x%04X\n", from_code, from_unicode;
  82. else if (from_code < to_code)
  83. printf "0x%02X-0x%02X 0x%04X\n", from_code, to_code, from_unicode;
  84. }
  85. else
  86. {
  87. if (from_code == to_code)
  88. printf "0x%04X 0x%04X\n", from_code, from_unicode;
  89. else if (from_code < to_code)
  90. printf "0x%04X-0x%04X 0x%04X\n", from_code, to_code, from_unicode;
  91. }
  92. from_code = to_code = code;
  93. from_unicode = to_unicode = unicode;
  94. }
  95. }
  96. END {
  97. if (to_code < 256)
  98. {
  99. if (from_code == to_code)
  100. printf "0x%02X 0x%04X\n", from_code, from_unicode;
  101. else
  102. printf "0x%02X-0x%02X 0x%04X\n", from_code, to_code, from_unicode;
  103. }
  104. else
  105. {
  106. if (from_code == to_code)
  107. printf "0x%04X 0x%04X\n", from_code, from_unicode;
  108. else
  109. printf "0x%04X-0x%04X 0x%04X\n", from_code, to_code, from_unicode;
  110. }
  111. }