| 1 |
# Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
# This file is part of GCC. |
| 4 |
|
| 5 |
# GCC is free software; you can redistribute it and/or modify it under |
| 6 |
# the terms of the GNU General Public License as published by the Free |
| 7 |
# Software Foundation; either version 3, or (at your option) any later |
| 8 |
# version. |
| 9 |
|
| 10 |
# GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
# for more details. |
| 14 |
|
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with GCC; see the file COPYING3. If not see |
| 17 |
# <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
# Run this program as |
| 20 |
# python ./make-rust-unicode.py UnicodeData.txt \ |
| 21 |
# DerivedNormalizationProps.txt DerivedCoreProperties.txt \ |
| 22 |
# > rust-unicode-data.h |
| 23 |
|
| 24 |
import sys |
| 25 |
from typing import Tuple |
| 26 |
|
| 27 |
Codepoint = int |
| 28 |
Range = Tuple[Codepoint, Codepoint] |
| 29 |
|
| 30 |
COPYRIGHT = ( |
| 31 |
"// Copyright (C) 2020-2025 Free Software Foundation, Inc.\n" |
| 32 |
"\n" |
| 33 |
"// This file is part of GCC.\n" |
| 34 |
"\n" |
| 35 |
"// GCC is free software; you can redistribute it and/or modify it under\n" |
| 36 |
"// the terms of the GNU General Public License as published by the Free\n" |
| 37 |
"// Software Foundation; either version 3, or (at your option) any later\n" |
| 38 |
"// version.\n" |
| 39 |
"\n" |
| 40 |
"// GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n" |
| 41 |
"// WARRANTY; without even the implied warranty of MERCHANTABILITY or\n" |
| 42 |
"// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n" |
| 43 |
"// for more details.\n" |
| 44 |
"\n" |
| 45 |
"// You should have received a copy of the GNU General Public License\n" |
| 46 |
"// along with GCC; see the file COPYING3. If not see\n" |
| 47 |
"// <http://www.gnu.org/licenses/>." |
| 48 |
) |
| 49 |
|
| 50 |
# Decomposition_Mapping table |
| 51 |
decomposition_map: dict[Codepoint, list[Codepoint]] = {} |
| 52 |
# Canonical_Combining_Class table |
| 53 |
ccc_table: dict[Codepoint, int] = {} |
| 54 |
# Ranges of codepoints with the Full_Composition_Exclusion property |
| 55 |
composition_exclusion_ranges: list[Range] = [] |
| 56 |
# Ranges of codepoints with the Full_Composition_Exclusion property |
| 57 |
alphabetic_ranges: list[Range] = [] |
| 58 |
# Ranges of codepoints with NFC_QC=No |
| 59 |
nfc_qc_no_ranges: list[Range] = [] |
| 60 |
# Ranges of codepoints with NFC_QC=Maybe |
| 61 |
nfc_qc_maybe_ranges: list[Range] = [] |
| 62 |
numeric_codepoints: list[Codepoint] = [] |
| 63 |
|
| 64 |
# Note that an element of range `[m, n]` (a list in python) represents [m, n) |
| 65 |
|
| 66 |
|
| 67 |
def binary_search_ranges(ranges: list[Range], target: Codepoint) -> int: |
| 68 |
low: int = 0 |
| 69 |
high: int = len(ranges) - 1 |
| 70 |
while low <= high: |
| 71 |
mid = (low + high) // 2 |
| 72 |
start, end = ranges[mid] |
| 73 |
if start <= target <= end - 1: |
| 74 |
return mid # target found. returns index. |
| 75 |
elif target < start: |
| 76 |
high = mid - 1 |
| 77 |
else: |
| 78 |
low = mid + 1 |
| 79 |
# target not found. |
| 80 |
return -1 |
| 81 |
|
| 82 |
|
| 83 |
# Utility function to parse '<codepoint>...<codepoint>' or '<codepoint>' |
| 84 |
def parse_codepoint_range(range_str: str) -> Range: |
| 85 |
codepoint_range: list[str] = range_str.split("..") |
| 86 |
assert len(codepoint_range) == 1 or len(codepoint_range) == 2, "Invalid format" |
| 87 |
start_cp, end_cp = 0, 0 |
| 88 |
if len(codepoint_range) == 1: |
| 89 |
# m..n => [m, n+1) |
| 90 |
start_cp = int(codepoint_range[0], 16) |
| 91 |
end_cp = start_cp + 1 |
| 92 |
else: |
| 93 |
# m => [m, m+1) |
| 94 |
start_cp = int(codepoint_range[0], 16) |
| 95 |
end_cp = int(codepoint_range[1], 16) + 1 |
| 96 |
return start_cp, end_cp |
| 97 |
|
| 98 |
|
| 99 |
def read_unicode_data_txt(filepath: str) -> None: |
| 100 |
def process_line(line: str) -> None: |
| 101 |
rows = line.split(";") |
| 102 |
if len(rows) != 15: |
| 103 |
return |
| 104 |
# Parse codepoint |
| 105 |
cp = int(rows[0], 16) |
| 106 |
# Parse general category |
| 107 |
category = rows[2] |
| 108 |
if category == "Nd" or category == "Nl" or category == "No": |
| 109 |
numeric_codepoints.append(cp) |
| 110 |
|
| 111 |
# Parse CCC |
| 112 |
ccc = int(rows[3], 10) |
| 113 |
if ccc != 0: |
| 114 |
ccc_table[cp] = ccc |
| 115 |
# Parse decomposition mapping |
| 116 |
# Ignore compatibility decomposition mapping because |
| 117 |
# it is not required for **NFC** normalization. |
| 118 |
if not rows[5].startswith("<"): |
| 119 |
decomp_cp_strs = rows[5].split(" ") |
| 120 |
decomp_cps = [] |
| 121 |
for s in decomp_cp_strs: |
| 122 |
if s == "": |
| 123 |
continue |
| 124 |
decomp_cps.append(int(s, 16)) |
| 125 |
assert ( |
| 126 |
len(decomp_cps) <= 2 |
| 127 |
), "Decomposition_Mapping must not contain more than 2 characters." |
| 128 |
if len(decomp_cps) > 0: |
| 129 |
decomposition_map[cp] = decomp_cps |
| 130 |
|
| 131 |
with open(filepath, "r", encoding="UTF-8") as file: |
| 132 |
while line := file.readline(): |
| 133 |
process_line(line.rstrip()) |
| 134 |
|
| 135 |
|
| 136 |
def read_derived_norm_props_txt(filepath: str) -> None: |
| 137 |
def process_line(line) -> None: |
| 138 |
# Ignore comments |
| 139 |
line = line.split("#")[0] |
| 140 |
rows = line.split(";") |
| 141 |
# Too few rows. Skipped. |
| 142 |
if len(rows) < 2: |
| 143 |
return |
| 144 |
rows[0] = rows[0].lstrip().rstrip() |
| 145 |
rows[1] = rows[1].lstrip().rstrip() |
| 146 |
cp_range = parse_codepoint_range(rows[0]) |
| 147 |
if rows[1] == "Full_Composition_Exclusion": |
| 148 |
composition_exclusion_ranges.append(cp_range) |
| 149 |
elif rows[1] == "NFC_QC": |
| 150 |
assert len(rows) >= 3, "Too few rows for NFC_QC" |
| 151 |
rows[2] = rows[2].lstrip().rstrip() |
| 152 |
if rows[2] == "N": |
| 153 |
nfc_qc_no_ranges.append(cp_range) |
| 154 |
elif rows[2] == "M": |
| 155 |
nfc_qc_maybe_ranges.append(cp_range) |
| 156 |
else: |
| 157 |
raise RuntimeError("Value of NFC_QC must be N or M") |
| 158 |
|
| 159 |
with open(filepath, "r", encoding="UTF-8") as file: |
| 160 |
while line := file.readline(): |
| 161 |
process_line(line.rstrip()) |
| 162 |
|
| 163 |
|
| 164 |
def read_derived_core_props_txt(filepath: str) -> None: |
| 165 |
def process_line(line: str) -> None: |
| 166 |
# Ignore comments |
| 167 |
line = line.split("#")[0] |
| 168 |
rows = line.split(";") |
| 169 |
# Too few rows. Skipped. |
| 170 |
if len(rows) < 2: |
| 171 |
return |
| 172 |
rows[0] = rows[0].lstrip().rstrip() |
| 173 |
rows[1] = rows[1].lstrip().rstrip() |
| 174 |
if rows[1] != "Alphabetic": |
| 175 |
return |
| 176 |
cp_range: Range = parse_codepoint_range(rows[0]) |
| 177 |
alphabetic_ranges.append(cp_range) |
| 178 |
|
| 179 |
with open(filepath, "r", encoding="UTF-8") as file: |
| 180 |
while line := file.readline(): |
| 181 |
process_line(line.rstrip()) |
| 182 |
|
| 183 |
|
| 184 |
def write_decomposition() -> None: |
| 185 |
print("const std::map<uint32_t, std::vector<uint32_t>> DECOMPOSITION_MAP = {") |
| 186 |
print(" // clang-format off") |
| 187 |
for cp in sorted(decomposition_map): |
| 188 |
print(" {{{:#06x}, ".format(cp), end="") |
| 189 |
print("{", end="") |
| 190 |
for decomp_cp in decomposition_map[cp]: |
| 191 |
print("{:#06x}, ".format(decomp_cp), end="") |
| 192 |
print("}},") |
| 193 |
print(" // clang-format on") |
| 194 |
print("};") |
| 195 |
|
| 196 |
|
| 197 |
def write_recomposition() -> None: |
| 198 |
print( |
| 199 |
"const std::map<std::pair<uint32_t, uint32_t>, uint32_t> RECOMPOSITION_MAP = {{" |
| 200 |
) |
| 201 |
print(" // clang-format off") |
| 202 |
for cp in decomposition_map: |
| 203 |
if binary_search_ranges(composition_exclusion_ranges, cp) != -1: |
| 204 |
continue |
| 205 |
d1: Codepoint |
| 206 |
d2: Codepoint |
| 207 |
if len(decomposition_map[cp]) == 1: |
| 208 |
d1 = decomposition_map[cp][0] |
| 209 |
d2 = 0 |
| 210 |
else: |
| 211 |
d1 = decomposition_map[cp][0] |
| 212 |
d2 = decomposition_map[cp][1] |
| 213 |
print(" {{{{{:#06x}, {:#06x}}}, {:#06x}}},".format(d1, d2, cp)) |
| 214 |
print(" // clang-format on") |
| 215 |
print("}};") |
| 216 |
|
| 217 |
|
| 218 |
def write_ccc() -> None: |
| 219 |
print("const std::map<uint32_t, int32_t> CCC_TABLE = {") |
| 220 |
print(" // clang-format off") |
| 221 |
for cp in ccc_table: |
| 222 |
print(" {{{:#06x}, {}}},".format(cp, ccc_table[cp])) |
| 223 |
print(" // clang-format on") |
| 224 |
print("};") |
| 225 |
|
| 226 |
|
| 227 |
def write_alphabetic() -> None: |
| 228 |
print( |
| 229 |
"const std::array<std::pair<uint32_t, uint32_t>, NUM_ALPHABETIC_RANGES> ALPHABETIC_RANGES = {{" |
| 230 |
) |
| 231 |
print(" // clang-format off") |
| 232 |
for r in alphabetic_ranges: |
| 233 |
print(" {{{:#06x}, {:#06x}}},".format(r[0], r[1])) |
| 234 |
print(" // clang-format on") |
| 235 |
print("}};") |
| 236 |
|
| 237 |
|
| 238 |
def write_numeric() -> None: |
| 239 |
print("const std::array<uint32_t, NUM_NUMERIC_CODEPOINTS> NUMERIC_CODEPOINTS = {{") |
| 240 |
print(" // clang-format off") |
| 241 |
for i, cp in enumerate(numeric_codepoints): |
| 242 |
if i % 16 == 0: |
| 243 |
print(" ", end="") |
| 244 |
print("{:#06x}, ".format(cp), end="") |
| 245 |
if i % 16 == 15: |
| 246 |
print() |
| 247 |
if i % 16 != 15: |
| 248 |
print() |
| 249 |
print(" // clang-format on") |
| 250 |
print("}};") |
| 251 |
|
| 252 |
|
| 253 |
def write_nfc_qc(): |
| 254 |
print( |
| 255 |
"const std::array<std::pair<uint32_t, uint32_t>, {}> NFC_QC_NO_RANGES = {{{{".format( |
| 256 |
len(nfc_qc_no_ranges) |
| 257 |
) |
| 258 |
) |
| 259 |
print(" // clang-format off") |
| 260 |
for r in nfc_qc_no_ranges: |
| 261 |
print(" {{{:#06x}, {:#06x}}},".format(r[0], r[1])) |
| 262 |
print(" // clang-format on") |
| 263 |
print("}};") |
| 264 |
|
| 265 |
print( |
| 266 |
"const std::array<std::pair<uint32_t, uint32_t>, {}> NFC_QC_MAYBE_RANGES = {{{{".format( |
| 267 |
len(nfc_qc_maybe_ranges) |
| 268 |
) |
| 269 |
) |
| 270 |
print(" // clang-format off") |
| 271 |
for r in nfc_qc_maybe_ranges: |
| 272 |
print(" {{{:#06x}, {:#06x}}},".format(r[0], r[1])) |
| 273 |
print(" // clang-format on") |
| 274 |
print("}};") |
| 275 |
|
| 276 |
|
| 277 |
def main() -> None: |
| 278 |
if len(sys.argv) != 4: |
| 279 |
print("too few arguments", file=sys.stderr) |
| 280 |
exit(-1) |
| 281 |
unicode_txt_path: str = sys.argv[1] |
| 282 |
norm_props_txt_path: str = sys.argv[2] |
| 283 |
core_props_txt_path: str = sys.argv[3] |
| 284 |
|
| 285 |
read_unicode_data_txt(unicode_txt_path) |
| 286 |
read_derived_norm_props_txt(norm_props_txt_path) |
| 287 |
read_derived_core_props_txt(core_props_txt_path) |
| 288 |
|
| 289 |
print(COPYRIGHT) |
| 290 |
print() |
| 291 |
|
| 292 |
print('#include "rust-system.h"\n') |
| 293 |
print("namespace Rust {\n") |
| 294 |
print("const uint32_t NUM_ALPHABETIC_RANGES = {};".format(len(alphabetic_ranges))) |
| 295 |
print( |
| 296 |
"const uint32_t NUM_NUMERIC_CODEPOINTS = {};\n".format(len(numeric_codepoints)) |
| 297 |
) |
| 298 |
|
| 299 |
write_decomposition() |
| 300 |
print() |
| 301 |
write_recomposition() |
| 302 |
print() |
| 303 |
write_ccc() |
| 304 |
print() |
| 305 |
write_alphabetic() |
| 306 |
print() |
| 307 |
write_numeric() |
| 308 |
print() |
| 309 |
write_nfc_qc() |
| 310 |
print() |
| 311 |
|
| 312 |
print("} // namespace Rust") |
| 313 |
|
| 314 |
|
| 315 |
if __name__ == "__main__": |
| 316 |
main() |