Something went wrong. Try again.
A URI parser and formatter for Zig, modelled on Python's hyperlink package, with LDAP URLs, file: URIs and sdns:// DNS stamps beside it.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149#!/usr/bin/env python3# SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us># SPDX-License-Identifier: MIT"""Generate src/unicode/vectors.zig: a committed subset of Unicode's IDNA andnormalization conformance suites.
Usage: ./tools/generate_unicode_vectors.py IdnaTestV2.txt NormalizationTest.txt \ > src/unicode/vectors.zig
The full suites are ~3.5 MB, too large to ship to every consumer of thislibrary. A deterministic subset is committed instead: every case that expectsan error, which is where the interesting rules live, plus an evenly spacedsample of the cases that are expected to succeed. See README for how to runthe complete suites.
The suites are fetched from https://www.unicode.org/Public/<version>/idna/IdnaTestV2.txtand https://www.unicode.org/Public/<version>/ucd/NormalizationTest.txt .
Run `zig fmt` on the result afterwards; this script does not format itsoutput."""import sys
ERROR_SAMPLE = 13 # keep 1 in N error casesOK_SAMPLE = 90 # keep 1 in N success cases
def zig_str(s): out = [] for b in s.encode("utf-8"): if b == 0x22: out.append('\\"') elif b == 0x5C: out.append("\\\\") elif 0x20 <= b < 0x7F: out.append(chr(b)) else: out.append(f"\\x{b:02x}") return '"' + "".join(out) + '"'
def unescape(s): if s == '""': return "" out, i = [], 0 while i < len(s): if s.startswith("\\u", i) and i + 6 <= len(s): try: cp = int(s[i + 2:i + 6], 16) if 0xD800 <= cp <= 0xDFFF: return None # lone surrogate: not representable out.append(chr(cp)) i += 6 continue except ValueError: pass out.append(s[i]) i += 1 return "".join(out)
def idna_cases(path): keep, n_err, n_ok = [], 0, 0 for line in open(path, encoding="utf-8"): line = line.split("#", 1)[0].strip() if not line: continue f = [x.strip() for x in line.split(";")] if len(f) < 5: continue src = unescape(f[0]) uni = unescape(f[1] if f[1] else f[0]) asc = unescape(f[3] if f[3] else (f[1] if f[1] else f[0])) if src is None or uni is None or asc is None: continue uni_err = bool(f[2]) asc_err = bool(f[4]) or uni_err if uni_err or asc_err: n_err += 1 if n_err % ERROR_SAMPLE: continue else: n_ok += 1 if n_ok % OK_SAMPLE: continue keep.append((src, uni, uni_err, asc, asc_err)) return keep
def nfc_cases(path): keep, n = [], 0 for line in open(path, encoding="utf-8"): line = line.split("#", 1)[0].strip() if not line or line.startswith("@"): continue f = [x.strip() for x in line.split(";")] if len(f) < 5: continue n += 1 if n % 55: continue def cps(field): return "".join(chr(int(x, 16)) for x in field.split()) try: keep.append((cps(f[0]), cps(f[1]))) except ValueError: continue return keep
def main(): idna = idna_cases(sys.argv[1]) nfc = nfc_cases(sys.argv[2]) w = sys.stdout.write # REUSE-IgnoreStart -- the lines below are the *generated* file's headers, # not this script's own licensing. w("// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>\n") w("// SPDX-FileCopyrightText: © 1991-2026 Unicode, Inc.\n") w("// SPDX-License-Identifier: MIT AND Unicode-3.0\n\n") # REUSE-IgnoreEnd w("//! Conformance vectors, generated -- do not edit by hand.\n//!\n") w("//! A deterministic subset of Unicode's IdnaTestV2.txt and\n") w("//! NormalizationTest.txt: every Nth case that expects an error, where\n") w("//! the interesting rules live, plus a spaced sample of those expected to\n") w("//! succeed. The full suites are ~3.5 MB and are not shipped to consumers;\n") w("//! see the README for running them in full.\n//!\n") w("//! Regenerate with `./tools/generate_unicode_vectors.py`.\n\n")
w("pub const IdnaCase = struct {\n source: []const u8,\n") w(" to_unicode: []const u8,\n to_unicode_errors: bool,\n") w(" to_ascii: []const u8,\n to_ascii_errors: bool,\n};\n\n") w(f"/// {len(idna)} cases, with UseSTD3ASCIIRules and non-transitional processing.\n") w("pub const idna_cases = [_]IdnaCase{\n") for src, uni, ue, asc, ae in idna: w(f" .{{ .source = {zig_str(src)}, .to_unicode = {zig_str(uni)}, " f".to_unicode_errors = {'true' if ue else 'false'}, " f".to_ascii = {zig_str(asc)}, .to_ascii_errors = {'true' if ae else 'false'} }},\n") w("};\n\n")
w("pub const NfcCase = struct { source: []const u8, nfc: []const u8 };\n\n") w(f"/// {len(nfc)} cases: column 1 of NormalizationTest.txt and its NFC.\n") w("pub const nfc_cases = [_]NfcCase{\n") for src, out in nfc: w(f" .{{ .source = {zig_str(src)}, .nfc = {zig_str(out)} }},\n") w("};\n")
main()