import subprocess
import urllib.request
VERSION = "17.0.0"
URL = f"https://www.unicode.org/Public/{VERSION}/ucd/DerivedCoreProperties.txt"
ranges = {"ID_Start": [], "ID_Continue": []}
for line in urllib.request.urlopen(URL).read().decode("utf-8").splitlines():
line = line.split("#", 1)[0].strip()
if not line:
continue
cps, prop = [f.strip() for f in line.split(";")[:2]]
if prop in ranges:
lo, _, hi = cps.partition("..")
lo, hi = int(lo, 16), int(hi or lo, 16)
if hi >= 0x80:
ranges[prop].append([max(lo, 0x80), hi])
def merge(rs):
out = []
for lo, hi in sorted(rs):
if out and lo <= out[-1][1] + 1:
out[-1][1] = max(out[-1][1], hi)
else:
out.append([lo, hi])
return out
out = [
"// SPDX-License-Identifier: Apache-2.0\n",
"// Copyright 2026 James Conway\n",
"\n",
f"// Generated by scripts/gen-unicode.py from the Unicode {VERSION} UCD. Don't edit.\n",
"\n",
"//! ID_Start and ID_Continue above ASCII, as sorted, merged ranges,\n",
"//! flat: lo, hi, lo, hi.\n",
"\n",
]
for name, const in [("ID_Start", "ID_START"), ("ID_Continue", "ID_CONTINUE")]:
merged = merge(ranges[name])
flat = ", ".join(f"{lo:#x}, {hi:#x}" for lo, hi in merged)
out.append(f"pub(crate) static {const}: &[u32] = &[{flat}];\n\n")
with open("src/unicode.rs", "w") as f:
f.write("".join(out).rstrip("\n") + "\n")
subprocess.run(["rustfmt", "--edition", "2024", "src/unicode.rs"], check=True)
print({k: len(merge(v)) for k, v in ranges.items()}, "ranges")