#!/usr/bin/env python3 """Normalisation vectors for text_test, from CPython's unicodedata (an implementation independent of the ICU the engine's tables were generated from). Prints tests/norm_vectors.h: source, NFC, NFD, NFKC and NFKD of - every 3rd codepoint with a decomposition (Hangul syllables every 61st), - every codepoint that composes as the second of a pair, after a base it composes with, - seeded sequences of a base, marks in random canonical-class order, and Hangul jamo runs. Only codepoints assigned in unicodedata's version are used: the Unicode stability policy fixes the normal forms of assigned text, so the newer tables must give the same answers. python3 tests/mk_norm_vectors.py > tests/norm_vectors.h """ import random import sys import unicodedata as ud def hexs(s): return " ".join("%04X" % ord(c) for c in s) def assigned(c): return ud.category(c) != "Cn" and ud.category(c) != "Cs" def main(): rows = [] decomposable = [chr(c) for c in range(0x110000) if not 0xD800 <= c <= 0xDFFF and assigned(chr(c)) and ud.decomposition(chr(c))] hangul = [c for c in decomposable if 0xAC00 <= ord(c) <= 0xD7A3] other = [c for c in decomposable if not (0xAC00 <= ord(c) <= 0xD7A3)] for c in other[::3]: rows.append(c) for c in hangul[::61]: rows.append(c) # Pairs that compose: a canonical decomposition of two, recomposed from its parts. for c in other: d = ud.normalize("NFD", c) if len(d) == 2 and ud.normalize("NFC", d) == c and ord(c) % 5 == 0: rows.append(d) marks = [chr(c) for c in range(0x300, 0x370)] + ["֑", "ְ", "ุ", "᷀", "⃐", "〪", "ཱ", "ི"] bases = ["a", "e", "o", "A", "å", "α", "а", "א", "ก", "ᄀ", "가", "Å", "Å", "ẛ", "שּׁ"] rng = random.Random(15) for _ in range(600): s = rng.choice(bases) + "".join(rng.choice(marks) for _ in range(rng.randint(0, 4))) if rng.random() < 0.3: s += rng.choice(["ᅡ", "ᆨ", "ᅵᇂ", "x", "́"]) rows.append(s) for _ in range(100): rows.append("".join(chr(rng.choice([0x1100 + rng.randint(0, 18), 0x1161 + rng.randint(0, 20), 0x11A8 + rng.randint(0, 26)])) for _ in range(rng.randint(2, 5)))) out = sys.stdout out.write("/* norm_vectors.h -- generated by tests/mk_norm_vectors.py from CPython's unicodedata %s.\n" " * Each row: source; NFC; NFD; NFKC; NFKD, as hex codepoints. */\n" % ud.unidata_version) out.write("#pragma once\n\nstatic const char* const kNormVectors[] = {\n") for s in rows: forms = [s] + [ud.normalize(f, s) for f in ("NFC", "NFD", "NFKC", "NFKD")] out.write(' "%s",\n' % ";".join(hexs(f) for f in forms)) out.write("};\n") if __name__ == "__main__": main()