std/numbers
Number formatting helpers.
Lives outside std/core so we can use std/builder for efficient construction.
UFCS-callable as n.to_hex() once import "std/numbers" is in scope.
Functions
is_hex_digit :: fn(c: char) -> bool True for an ASCII hex digit: 0..9, a..f, A..F.
Parameters
| c | char | codepoint to classify. |
Returns
bool true iff c is a valid hex digit.is_digit_in_base :: fn(c: char, base: i32) -> bool True when c is a valid digit in base. Supported bases : 2, 8, 10, 16.
Unsupported bases fall through to base-10.
Parameters
| c | char | codepoint to classify. |
| base | i32 | the numeric base ; 2, 8, 10, or 16. |
Returns
bool hex_digit_value :: fn(c: char) -> i32 0..15 for an ASCII hex digit, -1 for anything else. Case-insensitive.
Parameters
| c | char | codepoint to decode. |
Returns
i32 the digit's numeric value, or -1 if c isn't 0..9a..fA..F.parse_int_in_base :: fn(s: string, base: i32) -> i64 | ParseError Parse s as an i64 in the given base. s must be pure digits in
that base: no 0x/0b/0o prefix, no _ separators, no leading
sign. Empty input or any non-digit character yields an error.
Parameters
| s | string | the digits-only input. |
| base | i32 | one of 2 / 8 / 10 / 16. |
Returns
i64 | ParseError the parsed i64.Errors
ParseErroron empty input or any out-of-base digit.
parse_uint_in_base :: fn(s: string, base: i32) -> u64 | ParseError Parse an unsigned integer in the given base. Same input shape as
parse_int_in_base (digits only: caller strips prefix / underscores).
Carries the full u64 range so literals up to u64.MAX round-trip
without losing top bits. No sign accepted: the lexer wraps negative
literals in a UnaryExpr.neg over the unsigned magnitude.
Parameters
| s | string | the digits-only input. |
| base | i32 | one of 2 / 8 / 10 / 16. |
Returns
u64 | ParseError the parsed u64.Errors
ParseErroron empty input, any out-of-base digit, or a magnitude exceedingu64.MAX(carries.msg = "overflow").
hex_digit :: fn(n: u64) -> char One lowercase hex digit for a nibble 0..15. Shared with std/crypto, which
needs the lowercase form (shasum / openssl convention).
Parameters
| n | u64 | No description. |
Returns
char to_hex :: fn(self: u64) -> string Hexadecimal stringification of a u64: lowercase, no 0x prefix, no
leading zeros (special-cases 0 to "0"). Pair with
std/string.pad_start for a fixed-width hex column.
UFCS-callable as n.to_hex().
Parameters
| self | u64 | the integer to render. |
Returns
string e.g. 255_u64.to_hex() → "ff".to_bin :: fn(self: u64) -> string Binary stringification of a u64: no 0b prefix, no leading zeros.
Pair with std/string.pad_start for a fixed-width bit column.
Parameters
| self | u64 | the integer to render. |
Returns
string e.g. 5_u64.to_bin() → "101".to_compact_str :: fn(self: f64) -> string Format a float in the most compact decimal form: strips a trailing
.0 from integer-valued floats so 10.0 becomes "10". Otherwise
behaves like the default Display impl on f64. Matches
JSON.stringify semantics ; useful for snapshot-style output and any
wire format that prefers 42 over 42.0 for whole numbers.
Parameters
| self | f64 | the float to render. |
Returns
string the compact decimal representation.Example
(10.0).to_compact_str() // "10"
(3.14).to_compact_str() // "3.14"
(0.5).to_compact_str() // "0.5"
(-0.0).to_compact_str() // "0"strip_numeric_text :: fn(text: string, base: i32) -> string Strip the base prefix (0x / 0b / 0o) and every _ digit separator
from a literal's source text. The result is a pure digit string ready for
parse_int_in_base / parse_uint_in_base / parse_float.
Parameters
| text | string | the original literal text (e.g. "0xFF_FF" / "1_000"). |
| base | i32 | the literal's base: 10 leaves the leading bytes untouched, anything else strips the two-byte 0x / 0b / 0o prefix. |
Returns
string the digit-only payload, suitable for the base-aware numeric parsers in this module.Example
strip_numeric_text("0xFF", 16) // "FF"
strip_numeric_text("1_000", 10) // "1000"