1
0

gen-unicode-data.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. from __future__ import annotations
  2. import array
  3. import unicodedata
  4. import requests
  5. MAX_CODEPOINTS = 0x110000
  6. UNICODE_DATA_URL = "https://www.unicode.org/Public/UCD/latest/ucd/UnicodeData.txt"
  7. # see https://www.unicode.org/L2/L1999/UnicodeData.html
  8. def unicode_data_iter():
  9. res = requests.get(UNICODE_DATA_URL)
  10. res.raise_for_status()
  11. data = res.content.decode()
  12. prev = []
  13. for line in data.splitlines():
  14. # ej: 0000;<control>;Cc;0;BN;;;;;N;NULL;;;;
  15. line = line.split(";")
  16. cpt = int(line[0], base=16)
  17. assert cpt < MAX_CODEPOINTS
  18. cpt_lower = int(line[-2] or "0", base=16)
  19. assert cpt_lower < MAX_CODEPOINTS
  20. cpt_upper = int(line[-3] or "0", base=16)
  21. assert cpt_upper < MAX_CODEPOINTS
  22. categ = line[2].strip()
  23. assert len(categ) == 2
  24. bidir = line[4].strip()
  25. assert len(categ) == 2
  26. name = line[1]
  27. if name.endswith(", First>"):
  28. prev = (cpt, cpt_lower, cpt_upper, categ, bidir)
  29. continue
  30. if name.endswith(", Last>"):
  31. assert prev[1:] == (0, 0, categ, bidir)
  32. for c in range(prev[0], cpt):
  33. yield (c, cpt_lower, cpt_upper, categ, bidir)
  34. yield (cpt, cpt_lower, cpt_upper, categ, bidir)
  35. # see definition in unicode.h
  36. CODEPOINT_FLAG_UNDEFINED = 0x0001 #
  37. CODEPOINT_FLAG_NUMBER = 0x0002 # \p{N}
  38. CODEPOINT_FLAG_LETTER = 0x0004 # \p{L}
  39. CODEPOINT_FLAG_SEPARATOR = 0x0008 # \p{Z}
  40. CODEPOINT_FLAG_MARK = 0x0010 # \p{M}
  41. CODEPOINT_FLAG_PUNCTUATION = 0x0020 # \p{P}
  42. CODEPOINT_FLAG_SYMBOL = 0x0040 # \p{S}
  43. CODEPOINT_FLAG_CONTROL = 0x0080 # \p{C}
  44. UNICODE_CATEGORY_TO_FLAG = {
  45. "Cn": CODEPOINT_FLAG_UNDEFINED, # Undefined
  46. "Cc": CODEPOINT_FLAG_CONTROL, # Control
  47. "Cf": CODEPOINT_FLAG_CONTROL, # Format
  48. "Co": CODEPOINT_FLAG_CONTROL, # Private Use
  49. "Cs": CODEPOINT_FLAG_CONTROL, # Surrrogate
  50. "Ll": CODEPOINT_FLAG_LETTER, # Lowercase Letter
  51. "Lm": CODEPOINT_FLAG_LETTER, # Modifier Letter
  52. "Lo": CODEPOINT_FLAG_LETTER, # Other Letter
  53. "Lt": CODEPOINT_FLAG_LETTER, # Titlecase Letter
  54. "Lu": CODEPOINT_FLAG_LETTER, # Uppercase Letter
  55. "L&": CODEPOINT_FLAG_LETTER, # Cased Letter
  56. "Mc": CODEPOINT_FLAG_MARK, # Spacing Mark
  57. "Me": CODEPOINT_FLAG_MARK, # Enclosing Mark
  58. "Mn": CODEPOINT_FLAG_MARK, # Nonspacing Mark
  59. "Nd": CODEPOINT_FLAG_NUMBER, # Decimal Number
  60. "Nl": CODEPOINT_FLAG_NUMBER, # Letter Number
  61. "No": CODEPOINT_FLAG_NUMBER, # Other Number
  62. "Pc": CODEPOINT_FLAG_PUNCTUATION, # Connector Punctuation
  63. "Pd": CODEPOINT_FLAG_PUNCTUATION, # Dash Punctuation
  64. "Pe": CODEPOINT_FLAG_PUNCTUATION, # Close Punctuation
  65. "Pf": CODEPOINT_FLAG_PUNCTUATION, # Final Punctuation
  66. "Pi": CODEPOINT_FLAG_PUNCTUATION, # Initial Punctuation
  67. "Po": CODEPOINT_FLAG_PUNCTUATION, # Other Punctuation
  68. "Ps": CODEPOINT_FLAG_PUNCTUATION, # Open Punctuation
  69. "Sc": CODEPOINT_FLAG_SYMBOL, # Currency Symbol
  70. "Sk": CODEPOINT_FLAG_SYMBOL, # Modifier Symbol
  71. "Sm": CODEPOINT_FLAG_SYMBOL, # Math Symbol
  72. "So": CODEPOINT_FLAG_SYMBOL, # Other Symbol
  73. "Zl": CODEPOINT_FLAG_SEPARATOR, # Line Separator
  74. "Zp": CODEPOINT_FLAG_SEPARATOR, # Paragraph Separator
  75. "Zs": CODEPOINT_FLAG_SEPARATOR, # Space Separator
  76. }
  77. codepoint_flags = array.array('H', [CODEPOINT_FLAG_UNDEFINED]) * MAX_CODEPOINTS
  78. table_whitespace = []
  79. table_lowercase = []
  80. table_uppercase = []
  81. table_nfd = []
  82. for (cpt, cpt_lower, cpt_upper, categ, bidir) in unicode_data_iter():
  83. # convert codepoint to unicode character
  84. char = chr(cpt)
  85. # codepoint category flags
  86. codepoint_flags[cpt] = UNICODE_CATEGORY_TO_FLAG[categ]
  87. # lowercase conversion
  88. if cpt_lower:
  89. table_lowercase.append((cpt, cpt_lower))
  90. # uppercase conversion
  91. if cpt_upper:
  92. table_uppercase.append((cpt, cpt_upper))
  93. # NFD normalization
  94. norm = ord(unicodedata.normalize('NFD', char)[0])
  95. if cpt != norm:
  96. table_nfd.append((cpt, norm))
  97. # whitespaces, see "<White_Space>" https://www.unicode.org/Public/UCD/latest/ucd/PropList.txt
  98. table_whitespace.extend(range(0x0009, 0x000D + 1))
  99. table_whitespace.extend(range(0x2000, 0x200A + 1))
  100. table_whitespace.extend([0x0020, 0x0085, 0x00A0, 0x1680, 0x2028, 0x2029, 0x202F, 0x205F, 0x3000])
  101. # sort by codepoint
  102. table_whitespace.sort()
  103. table_lowercase.sort()
  104. table_uppercase.sort()
  105. table_nfd.sort()
  106. # group ranges with same flags
  107. ranges_flags: list[tuple[int, int]] = [(0, codepoint_flags[0])] # start, flags
  108. for codepoint, flags in enumerate(codepoint_flags):
  109. if flags != ranges_flags[-1][1]:
  110. ranges_flags.append((codepoint, flags))
  111. ranges_flags.append((MAX_CODEPOINTS, 0x0000))
  112. # group ranges with same nfd
  113. ranges_nfd: list[tuple[int, int, int]] = [(0, 0, 0)] # start, last, nfd
  114. for codepoint, norm in table_nfd:
  115. start = ranges_nfd[-1][0]
  116. if ranges_nfd[-1] != (start, codepoint - 1, norm):
  117. ranges_nfd.append(None) # type: ignore[arg-type] # dummy, will be replaced below
  118. start = codepoint
  119. ranges_nfd[-1] = (start, codepoint, norm)
  120. # Generate 'unicode-data.cpp':
  121. # python ./scripts//gen-unicode-data.py > unicode-data.cpp
  122. def out(line=""):
  123. print(line, end='\n') # noqa
  124. out("""\
  125. // generated with scripts/gen-unicode-data.py
  126. #include "unicode-data.h"
  127. #include <cstdint>
  128. #include <vector>
  129. #include <unordered_map>
  130. #include <unordered_set>
  131. """)
  132. out("const std::vector<std::pair<uint32_t, uint16_t>> unicode_ranges_flags = { // start, flags // last=next_start-1")
  133. for codepoint, flags in ranges_flags:
  134. out("{0x%06X, 0x%04X}," % (codepoint, flags))
  135. out("};\n")
  136. out("const std::unordered_set<uint32_t> unicode_set_whitespace = {")
  137. for codepoint in table_whitespace:
  138. out("0x%06X," % codepoint)
  139. out("};\n")
  140. out("const std::unordered_map<uint32_t, uint32_t> unicode_map_lowercase = {")
  141. for tuple_lw in table_lowercase:
  142. out("{0x%06X, 0x%06X}," % tuple_lw)
  143. out("};\n")
  144. out("const std::unordered_map<uint32_t, uint32_t> unicode_map_uppercase = {")
  145. for tuple_up in table_uppercase:
  146. out("{0x%06X, 0x%06X}," % tuple_up)
  147. out("};\n")
  148. out("const std::vector<range_nfd> unicode_ranges_nfd = { // start, last, nfd")
  149. for triple in ranges_nfd:
  150. out("{0x%06X, 0x%06X, 0x%06X}," % triple)
  151. out("};\n")