""" Verification for the review comments in docs/uce8-article.md. Run from the repo root: python3 docs/verify.py """ import gzip import os import sys sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from uce import MAX_CODE_POINT, TAIL_COUNT, decode, encode def uce(s): return b"".join(encode(ord(c)) for c in s) def blen(c): return len(encode(ord(c))) # --- 1. round-trip every Unicode code point ------------------------------ bad = 0 for cp in range(MAX_CODE_POINT + 1): if 0xD800 <= cp <= 0xDFFF: # surrogates are not encodable characters continue if decode(encode(cp)) != cp: bad += 1 print("round-trip failures:", bad) # --- 2. capacity arithmetic ---------------------------------------------- print("tier2 capacity:", 128 * TAIL_COUNT) print("tier3 capacity:", 128 * 128 * TAIL_COUNT) print("unicode total :", MAX_CODE_POINT + 1) print() # --- 3. raw vs gzipped saving on real sentences -------------------------- samples = { "Chinese (simp)": "我们每天使用的文本中,字符以一到四个字节的长度存储。", "Chinese (trad)": "我們每天使用的文本中,字符以一到四個位元組的長度儲存。", "Japanese": "私たちが毎日使うテキストでは、文字は1〜4バイトの長さで保存されます。", "Korean": "우리가 매일 사용하는 텍스트에서 문자는 1~4바이트 길이로 저장됩니다.", "Hindi": "हम हर दिन जिस पाठ का उपयोग करते हैं उसमें वर्ण संग्रहीत होते हैं।", "Thai": "ในข้อความที่เราใช้ทุกวัน อักขระจะถูกจัดเก็บด้วยความยาว", "Amharic": "በየቀኑ በምንጠቀምበት ጽሑፍ ውስጥ ቁምፊዎች ይቀመጣሉ።", "Mongolian(Cyr)": "Бидний өдөр тутмын хэрэглэдэг текст дотор 1-4 байт урттайгаар хадгалагддаг", "Mongolian(trad)": "ᠮᠣᠩᠭᠣᠯ ᠪᠢᠴᠢᠭ᠌", "Vietnamese": "Trong văn bản chúng ta sử dụng hàng ngày, các ký tự được lưu trữ với độ dài từ 1 đến 4 byte.", "JSON w/ Chinese": '{"name":"张三","city":"北京","note":"你好世界"}', } for name, text in samples.items(): u8, u = text.encode("utf-8"), uce(text) g8, gu = len(gzip.compress(u8, 9)), len(gzip.compress(u, 9)) print( f"{name:<17} utf8={len(u8):>4} uce8={len(u):>4} " f"raw={(1 - len(u) / len(u8)) * 100:>5.1f}% " f"gzip={(1 - gu / g8) * 100:>5.1f}%" ) print() # --- 4. coverage of the CJK/Hangul blocks -------------------------------- hangul = [chr(c) for c in range(0xAC00, 0xD7A4)] han = [chr(c) for c in range(0x4E00, 0xA000)] print("Hangul syllables at 2 bytes:", sum(blen(c) == 2 for c in hangul), "/", len(hangul)) print("CJK Unified at 2 bytes:", sum(blen(c) == 2 for c in han), "/", len(han)) # --- 5. trail bytes are ASCII digits/letters -> naive-search collisions --- doc = uce("नमस्ते") + b" ISO 8601" print("literal 'I' bytes in doc:", doc.count(b"I"), "(only 1 is a real letter)")