unicode-case-fold.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python
  2. """
  3. Unicode case folding database conversion utility
  4. Parses the database and generates a C++ function which implements the case
  5. folding algorithm. The database entries are of the form:
  6. <code>; <status>; <mapping>; # <name>
  7. <status> can be one of four characters:
  8. C - Common mappings
  9. S - mappings for Simple case folding
  10. F - mappings for Full case folding
  11. T - special case for Turkish I characters
  12. Right now this generates a function which implements simple case folding (C+S
  13. entries).
  14. """
  15. from __future__ import print_function
  16. import sys
  17. import re
  18. try:
  19. from urllib.request import urlopen
  20. except ImportError:
  21. from urllib2 import urlopen
  22. # This variable will body of the mappings function
  23. body = ""
  24. # Reads file line-by-line, extracts Common and Simple case fold mappings and
  25. # returns a (from_char, to_char, from_name) tuple.
  26. def mappings(f):
  27. previous_from = -1
  28. expr = re.compile(r'^(.*); [CS]; (.*); # (.*)')
  29. for line in f:
  30. m = expr.match(line)
  31. if not m: continue
  32. from_char = int(m.group(1), 16)
  33. to_char = int(m.group(2), 16)
  34. from_name = m.group(3)
  35. if from_char <= previous_from:
  36. raise Exception("Duplicate or unsorted characters in input")
  37. yield from_char, to_char, from_name
  38. previous_from = from_char
  39. # Computes the shift (to_char - from_char) in a mapping.
  40. def shift(mapping):
  41. return mapping[1] - mapping[0]
  42. # Computes the stride (from_char2 - from_char1) of two mappings.
  43. def stride2(mapping1, mapping2):
  44. return mapping2[0] - mapping1[0]
  45. # Computes the stride of a list of mappings. The list should have at least two
  46. # mappings. All mappings in the list are assumed to have the same stride.
  47. def stride(block):
  48. return stride2(block[0], block[1])
  49. # b is a list of mappings. All the mappings are assumed to have the same
  50. # shift and the stride between adjecant mappings (if any) is constant.
  51. def dump_block(b):
  52. global body
  53. if len(b) == 1:
  54. # Special case for handling blocks of length 1. We don't even need to
  55. # emit the "if (C < X) return C" check below as all characters in this
  56. # range will be caught by the "C < X" check emitted by the first
  57. # non-trivial block.
  58. body += " // {2}\n if (C == {0:#06x})\n return {1:#06x};\n".format(*b[0])
  59. return
  60. first = b[0][0]
  61. last = first + stride(b) * (len(b)-1)
  62. modulo = first % stride(b)
  63. # All characters before this block map to themselves.
  64. body += " if (C < {0:#06x})\n return C;\n".format(first)
  65. body += " // {0} characters\n".format(len(b))
  66. # Generic pattern: check upper bound (lower bound is checked by the "if"
  67. # above) and modulo of C, return C+shift.
  68. pattern = " if (C <= {0:#06x} && C % {1} == {2})\n return C + {3};\n"
  69. if stride(b) == 2 and shift(b[0]) == 1 and modulo == 0:
  70. # Special case:
  71. # We can elide the modulo-check because the expression "C|1" will map
  72. # the intervening characters to themselves.
  73. pattern = " if (C <= {0:#06x})\n return C | 1;\n"
  74. elif stride(b) == 1:
  75. # Another special case: X % 1 is always zero, so don't emit the
  76. # modulo-check.
  77. pattern = " if (C <= {0:#06x})\n return C + {3};\n"
  78. body += pattern.format(last, stride(b), modulo, shift(b[0]))
  79. current_block = []
  80. f = urlopen(sys.argv[1])
  81. for m in mappings(f):
  82. if len(current_block) == 0:
  83. current_block.append(m)
  84. continue
  85. if shift(current_block[0]) != shift(m):
  86. # Incompatible shift, start a new block.
  87. dump_block(current_block)
  88. current_block = [m]
  89. continue
  90. if len(current_block) == 1 or stride(current_block) == stride2(current_block[-1], m):
  91. current_block.append(m)
  92. continue
  93. # Incompatible stride, start a new block.
  94. dump_block(current_block)
  95. current_block = [m]
  96. f.close()
  97. dump_block(current_block)
  98. print('//===---------- Support/UnicodeCaseFold.cpp -------------------------------===//')
  99. print('//')
  100. print('// This file was generated by utils/unicode-case-fold.py from the Unicode')
  101. print('// case folding database at')
  102. print('// ', sys.argv[1])
  103. print('//')
  104. print('// To regenerate this file, run:')
  105. print('// utils/unicode-case-fold.py \\')
  106. print('// "{}" \\'.format(sys.argv[1]))
  107. print('// > lib/Support/UnicodeCaseFold.cpp')
  108. print('//')
  109. print('//===----------------------------------------------------------------------===//')
  110. print('')
  111. print('#include "llvm/Support/Unicode.h"')
  112. print('')
  113. print("int llvm::sys::unicode::foldCharSimple(int C) {")
  114. print(body)
  115. print(" return C;")
  116. print("}")