// strconv — number↔string conversions. Decimal i64 to/from a fixed // buffer. Error shapes mirror Hare's strconv types: (T | invalid | // overflow) where each error is a named alias over a payload type // (Hare uses !size / !void; ww uses i32 / void without the `!` mark). // invalid — input wasn't a valid number in the requested format. // Payload is the byte index of the first offending position (Hare // strconv::invalid is `!size` carrying the same). export type invalid = i32; // overflow — input was valid but doesn't fit the target type. No // payload (a single yes/no signal). Mirrors Hare's `!void` shape. export type overflow = void; // u64tos — write `v` in decimal into `buf` and return the byte count. // Hare name; the buffer-in shape is the sanctioned Plan 9 subset of // Hare's `u64tos(u, base) const str`. Unsigned-only so callers don't // have to think about wraparound when printing a u64 with the high // bit set. export fn u64tos(buf: []u8, v: u64) i32 = { let tmp: [32]u8; let i: i32 = 0; let n: u64 = v; for (n > 0u64) { tmp[i] = ((n % 10u64) + 48u64): u8; n = n / 10u64; i += 1; }; if (i == 0) { tmp[0] = 48u8; i = 1; }; let out: i32 = 0; for (i > 0) { i -= 1; buf[out] = tmp[i]; out += 1; }; return out; }; export fn i64tos(buf: []u8, v: i64) i32 = { let neg: bool = false; let n: i64 = v; if (n < 0) { neg = true; n = -n; }; let tmp: [32]u8; let i: i32 = 0; for (n > 0) { tmp[i] = ((n % 10) + 48): u8; n = n / 10; i += 1; }; if (i == 0) { tmp[0] = 48u8; i = 1; }; let out: i32 = 0; if (neg) { buf[out] = 45u8; // '-' out += 1; }; for (i > 0) { i -= 1; buf[out] = tmp[i]; out += 1; }; return out; }; // stoi64 — Hare-style fallible signed decimal parser. No locale, no // whitespace, no underscores: a leading '-' is the only non-digit // accepted, and only at position 0. export fn stoi64(s: str) (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 v: i64 = 0; for (i < s.len) { let c: u8 = s[i]; if (c < 48u8) { return i: invalid; }; if (c > 57u8) { return i: invalid; }; v = v * 10 + ((c: i64) - 48); i += 1; }; if (neg) { v = -v; }; return v; }; // stou64 — fallible unsigned decimal parser. No leading sign. export fn stou64(s: str) (u64 | invalid | overflow) = { if (s.len == 0) { return 0: invalid; }; let v: u64 = 0u64; let i: i32 = 0; for (i < s.len) { let c: u8 = s[i]; if (c < 48u8) { return i: invalid; }; if (c > 57u8) { return i: invalid; }; v = v * 10u64 + ((c: u64) - 48u64); i += 1; }; return v; };