/
nv-lang
/
nova
Обзор
Документация
Войти
/
nv-lang
/
nova
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
main
std/src/runtime/char.nv
60 строк
3 KB
Evgeniy Golovin
style(225.1): вертикальный ритм — sweep классов a-e (std/src)
26 июл 2026, 02:46
26 июл 2026, 02:46
38ecf16
Код
Авторство
О чём код?
// std/runtime/char.nv — char ↔ str (UTF-8 encode/decode) + int/char/u8 // scalar conversions (D54/D77). // // [M-compiler-nv-porting-wave] (2026-07-07) item A: NOT auto-gen anymore. // Was mixed (str.from/str.from_codepoint auto-gen'd by `nova-codegen // emit-runtime-stubs` from runtime_registry.rs + char.from/u8.try_from // Nova-implemented via a `nova_body: Some("...")` Rust string literal // rendered verbatim into this file). Auto-gen writes exactly ONE file per // module (single_file form, `module_to_path`) — it cannot coexist with a // hand-maintained sibling for the SAME module (Nova import resolution is // single-file XOR folder per module path; it does NOT scan a directory // for arbitrary same-module sibling files — `sync.nv`+`sync_test.nv` only // merge via the `*_test.nv` test-peer special case). So: same path // StringBuilder/WriteBuffer/ReadBuffer already took (Plan 91.12/109) — // `char_runtime()` in runtime_registry.rs now returns `vec![]`, and this // whole file — all four declarations — is hand-maintained. Single file, // single source of truth. module runtime.char // ─── str ─── // Plan 174.2 (str.from-ретракция): `export extern "nova" fn str.from(c char)` // — the compiler-generated engine hook that used to back `str.from(char)` — // is RETRACTED. `char` needs no concrete `@to_str()` override either: the // bare-T blanket (`fn[T] T @to_str() -> str => "${@}"`, // std/runtime/string/core.nv) covers it — interpolating a `char` value // lowers DIRECTLY to `nova_char_to_str` in emit_c.rs::emit_interpolated_str // (primitive dispatch table, same C function the retracted extern used to // name), so `c.to_str()` is byte-identical to the old `str.from(c)`. UTF-8 // encode codepoint в 1-4 байта — used by interpolation (C-level) and the // Display protocol default body (`w.write(@to_str())`, protocols.nv). // Unchecked UTF-8 encode codepoint в 1-4 байта. Caller гарантирует cp ∈ [0, 0x10FFFF]. Invalid codepoint → empty str (silent — predictable degradation). Используется JSON unicode escape parser-ами + другими decoders. export extern "nova" fn str.from_codepoint(cp int) -> str // ─── char ─── // D54: int → char, validates unicode scalar value [0, 0x10FFFF] excluding // surrogates. Ресивер-форма `@to_char()` (решение владельца 2026-07-09, // цепочечность: `(32 + off).to_char()?`) — бывший `int.to_char()` // ретрактирован по §22 (одно имя = одна дверь); каст `int as char` запрещён D54. export fn int @to_char() -> Result[char, CharFromError] { if @ < 0 || @ > 0x10FFFF || (@ >= 0xD800 && @ <= 0xDFFF) { Err(CharFromError) } else { Ok(unsafe { @ as char }) } } // ─── u8 ─── // D54/D77: char → u8, fails if codepoint > 0xFF (non-Latin-1). export fn u8.try_from(c char) -> Result[u8, TryFromCharError] { if c as int > 0xFF { Err(TryFromCharError) } else { Ok(c as int as u8) } }