unicode-case-fold.py 4.5 KB

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