from __future__ import annotations
import sys
from pathlib import Path
import unicodedata2 as unicodedata
sys.path.insert(0, str(Path(__file__).resolve().parent))
from generate_unicodedata_tables import UNICODE_VERSION
REPO_ROOT = Path(__file__).resolve().parent.parent
OUT_PATH = (
REPO_ROOT
/ "hegel-c"
/ "tests"
/ "embedded"
/ "native"
/ "unicodedata_python_reference_data.rs"
)
MAX_CP = 0x10FFFF
def main() -> None:
if unicodedata.unidata_version != UNICODE_VERSION:
sys.exit(
f"unicodedata2 is Unicode {unicodedata.unidata_version}, "
f"the engine's tables are {UNICODE_VERSION}"
)
ranges: list[tuple[int, int, str]] = []
current_cat = unicodedata.category(chr(0))
current_start = 0
for cp in range(1, MAX_CP + 1):
cat = unicodedata.category(chr(cp))
if cat != current_cat:
ranges.append((current_start, cp - 1, current_cat))
current_cat = cat
current_start = cp
ranges.append((current_start, MAX_CP, current_cat))
lines = [
"// Auto-generated by scripts/generate_unicodedata_python_reference.py.",
"// Do not edit by hand; regenerate when upgrading the Unicode tables.",
f"// Source: unicodedata2 {unicodedata.unidata_version} unicodedata2.category().",
"// Run-length-encoded: each tuple is (end_inclusive, category_code),",
"// covering `0..=0x10FFFF` contiguously.",
"[",
]
for _, end, cat in ranges:
lines.append(f' ({end:#07x}, "{cat}"),')
lines.append("]")
lines.append("")
OUT_PATH.parent.mkdir(parents=True, exist_ok=True)
OUT_PATH.write_text("\n".join(lines))
print(f"Wrote {OUT_PATH} with {len(ranges)} ranges "
f"(Unicode {unicodedata.unidata_version}).")
if __name__ == "__main__":
main()