File size: 1,524 Bytes
63dd946 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | # Copyright (c) 2026 The Anuvani-Diarization authors
# SPDX-License-Identifier: MIT OR Apache-2.0
"""Generate crates/diar-audio/src/pyprintable.rs: the code points CPython 3.10's `str.isprintable()` rejects (its
Unicode 13.0 database), as sorted inclusive ranges, so that Rust's `repr()` of a file name escapes exactly what
Python's does. Run with the reference interpreter:
source env/canonical.sh && python -s tools/gen_pyprintable.py
"""
import sys
import unicodedata
from pathlib import Path
assert sys.version_info[:2] == (3, 10), sys.version
ranges, start = [], None
for cp in range(0x110000):
np_ = not chr(cp).isprintable()
if np_ and start is None:
start = cp
if not np_ and start is not None:
ranges.append((start, cp - 1)); start = None
if start is not None:
ranges.append((start, 0x10FFFF))
out = Path(__file__).resolve().parents[1] / "crates/diar-audio/src/pyprintable.rs"
body = ",\n".join(f" (0x{a:X}, 0x{b:X})" for a, b in ranges)
out.write_text(f"""//! Generated by tools/gen_pyprintable.py from CPython {sys.version.split()[0]} (Unicode {unicodedata.unidata_version}):
//! the inclusive code-point ranges for which `str.isprintable()` is False. Do not edit.
pub const NOT_PRINTABLE: [(u32, u32); {len(ranges)}] = [
{body},
];
/// `Py_UNICODE_ISPRINTABLE(c)`.
pub fn is_printable(c: u32) -> bool {{
let i = NOT_PRINTABLE.partition_point(|&(_, end)| end < c);
!(i < NOT_PRINTABLE.len() && NOT_PRINTABLE[i].0 <= c)
}}
""")
print(len(ranges), "ranges ->", out)
|