// strconv — number↔string conversions. // // Mirrors Hare's strconv:: surface. The *tos functions return a // `const str` view into a module-level buffer that is overwritten on // the next call to the same function; callers must copy the bytes if // they need to outlive the next invocation. See [[strings.dup]] to // duplicate. Matches Hare's strconv::*tos semantics. package strconv; import os; import strings; // invalid — input wasn't a valid number in the requested format. // Payload is the byte index of the first offending position. // Mirrors Hare's strconv::invalid = !size. export type invalid = !i32; // overflow — input was valid but doesn't fit the target type. // Mirrors Hare's strconv::overflow = !void. export type overflow = !void; // error — any error from a strconv call. Mirrors Hare's strconv::error. export type error = !(invalid | overflow); // base — numeric base for parsing/formatting. Mirrors Hare's // `strconv::base` (Hare uses `enum uint`; we pick `enum i32` since // the underlying parse/format loops index with i32). // // HEX is an alias for HEX_UPPER; HEX_LOWER is a pseudo-base that // produces lowercase a-f digits. export type base = enum i32 { DEFAULT = 0, BIN = 2, OCT = 8, DEC = 10, HEX_UPPER = 16, HEX = 16, HEX_LOWER = 17, }; fn basenum(b: base) i64 = { if (b == base.BIN) { return 2; }; if (b == base.OCT) { return 8; }; if (b == base.HEX) { return 16; }; if (b == base.HEX_UPPER) { return 16; }; if (b == base.HEX_LOWER) { return 16; }; return 10; // DEC and DEFAULT }; fn basedigit(d: i64, b: base) u8 = { if (d < 10) { return (d + 48): u8; }; let off: i64 = d - 10; if (b == base.HEX_LOWER) { return (off + 97): u8; }; return (off + 65): u8; }; // u64tos — convert v to a base-b numeric string. Returns a view into // `u64tos_buf` which is overwritten on the next call. Matches Hare's // strconv::u64tos. let u64tos_buf: [65]u8; export fn u64tos(v: u64, b: base) str = { let nb: u64 = basenum(b): u64; let tmp: [65]u8; let i: i32 = 0; let n: u64 = v; if (n == 0u64) { tmp[0] = 48u8; i = 1; }; for (n > 0u64) { let d: i64 = (n % nb): i64; tmp[i] = basedigit(d, b); n = n / nb; i += 1; }; let out: i32 = 0; for (i > 0) { i -= 1; u64tos_buf[out] = tmp[i]; out += 1; }; let r: str; r.ptr = &u64tos_buf[0]; r.len = out; return r; }; // i64tos — convert v to a base-b numeric string. Returns a view into // `i64tos_buf` which is overwritten on the next call. Independent // buffer from u64tos so i64tos's own call to u64tos doesn't clobber // the in-flight result. Matches Hare's strconv::i64tos. let i64tos_buf: [66]u8; export fn i64tos(v: i64, b: base) str = { let neg: bool = false; let n: i64 = v; if (n < 0) { neg = true; n = -n; }; let nb: i64 = basenum(b); let tmp: [65]u8; let i: i32 = 0; if (n == 0) { tmp[0] = 48u8; i = 1; }; for (n > 0) { let d: i64 = n % nb; tmp[i] = basedigit(d, b); n = n / nb; i += 1; }; let out: i32 = 0; if (neg) { i64tos_buf[out] = 45u8; out += 1; }; // '-' for (i > 0) { i -= 1; i64tos_buf[out] = tmp[i]; out += 1; }; let r: str; r.ptr = &i64tos_buf[0]; r.len = out; return r; }; export fn i32tos(v: i32, b: base) str = { return i64tos(v: i64, b); }; export fn i16tos(v: i16, b: base) str = { return i64tos(v: i64, b); }; export fn i8tos(v: i8, b: base) str = { return i64tos(v: i64, b); }; export fn u32tos(v: u32, b: base) str = { return u64tos(v: u64, b); }; export fn u16tos(v: u16, b: base) str = { return u64tos(v: u64, b); }; export fn u8tos(v: u8, b: base) str = { return u64tos(v: u64, b); }; // digval — value of digit byte `c` under base `b`, or -1 if not a // valid digit. Letters are accepted case-insensitively under HEX / // HEX_UPPER; only lowercase under HEX_LOWER. fn digval(c: u8, b: base) i32 = { if (c >= 48u8) { if (c <= 57u8) { return (c - 48u8): i32; }; }; if (b == base.HEX_LOWER) { if (c >= 97u8) { if (c <= 102u8) { return ((c - 97u8) + 10u8): i32; }; }; return -1; }; if (c >= 65u8) { if (c <= 70u8) { return ((c - 65u8) + 10u8): i32; }; }; if (c >= 97u8) { if (c <= 102u8) { return ((c - 97u8) + 10u8): i32; }; }; return -1; }; // stoi64 — parse signed base-b number. Mirrors Hare's strconv::stoi64. // No locale, no whitespace, no underscores: optional leading '-' then // digits. Returns invalid with the offending index or overflow on // out-of-range. export fn stoi64(s: str, b: base) (i64 | invalid | overflow) = { if (s.len == 0) { return 0: invalid; }; let i: i32 = 0; let neg: bool = false; if (s[0] == 45u8) { neg = true; i = 1; }; if (i >= s.len) { return i: invalid; }; let nb: i32 = basenum(b): i32; let v: i64 = 0; for (i < s.len) { let c: u8 = s[i]; let d: i32 = digval(c, b); if (d < 0) { return i: invalid; }; if (d >= nb) { return i: invalid; }; v = v * (nb: i64) + (d: i64); i += 1; }; if (neg) { v = -v; }; return v; }; // stou64 — parse unsigned base-b number. Mirrors Hare's strconv::stou64. export fn stou64(s: str, b: base) (u64 | invalid | overflow) = { if (s.len == 0) { return 0: invalid; }; let nb: u64 = basenum(b): u64; let v: u64 = 0u64; let i: i32 = 0; for (i < s.len) { let c: u8 = s[i]; let d: i32 = digval(c, b); if (d < 0) { return i: invalid; }; if ((d: u64) >= nb) { return i: invalid; }; v = v * nb + (d: u64); i += 1; }; return v; }; export fn stoi32(s: str, b: base) (i32 | invalid | overflow) = { let r = stoi64(s, b); match (r) { case let v: i64 => { if (v > 2147483647i64) { return overflow{}; }; if (v < -2147483648i64) { return overflow{}; }; return v: i32; }; case let e: invalid => return e; case let e: overflow => return e; }; return 0: invalid; // unreachable; appeases the path-cov checker }; export fn stoi16(s: str, b: base) (i16 | invalid | overflow) = { let r = stoi64(s, b); match (r) { case let v: i64 => { if (v > 32767i64) { return overflow{}; }; if (v < -32768i64) { return overflow{}; }; return v: i16; }; case let e: invalid => return e; case let e: overflow => return e; }; return 0: invalid; }; export fn stoi8(s: str, b: base) (i8 | invalid | overflow) = { let r = stoi64(s, b); match (r) { case let v: i64 => { if (v > 127i64) { return overflow{}; }; if (v < -128i64) { return overflow{}; }; return v: i8; }; case let e: invalid => return e; case let e: overflow => return e; }; return 0: invalid; }; export fn stou32(s: str, b: base) (u32 | invalid | overflow) = { let r = stou64(s, b); match (r) { case let v: u64 => { if (v > 4294967295u64) { return overflow{}; }; return v: u32; }; case let e: invalid => return e; case let e: overflow => return e; }; return 0: invalid; }; export fn stou16(s: str, b: base) (u16 | invalid | overflow) = { let r = stou64(s, b); match (r) { case let v: u64 => { if (v > 65535u64) { return overflow{}; }; return v: u16; }; case let e: invalid => return e; case let e: overflow => return e; }; return 0: invalid; }; export fn stou8(s: str, b: base) (u8 | invalid | overflow) = { let r = stou64(s, b); match (r) { case let v: u64 => { if (v > 255u64) { return overflow{}; }; return v: u8; }; case let e: invalid => return e; case let e: overflow => return e; }; return 0: invalid; }; // f64tos — graduated to the Ryū shortest-round-trippable implementation // in ftos.ww (strconv #106 fold-5). The old lossy fixed-point version // (6 fractional digits, "huge" fallback ≥9e18, no NaN/Inf) was deleted // here per the lib-note graduation rule ("replace in one go, don't keep // both"); ftos.ww's f64tos is the live one. f32tos follows in fold-5b // (task #67, gated on the #143 f32-arg-push cgen fix). // strerror — convert an strconv error to a user-readable string. // Returns owned str; release via os.free. Mirrors Hare's // strconv::strerror. export fn strerror(e: error) str = { match (e) { case let v: invalid => return strings.dup("input is not a valid number"); case let v: overflow => return strings.dup("input number doesn't fit target type"); }; return strings.dup(""); };