std/strconv
String-to-number and number-to-string conversions.
from "std/strconv" import { parseInt, parseFloat, formatFloat, i64ToHex, quoteString }Example
let n = parseInt("42") ?? 0
let s = formatFloat(3.14159, 2) // "3.14"
print(quoteString("tab\there")) // "tab\there"
print(i64ToHex(255)) // ffAPI reference
formatFloat
pub fn formatFloat(n: f64, decimals: i32): stringFormat n with exactly decimals digits after the decimal point.
i64ToBin
pub fn i64ToBin(n: i64): stringBinary text for n, no prefix.
i64ToHex
pub fn i64ToHex(n: i64): stringLowercase hexadecimal text for n, no "0x" prefix.
i64ToOct
pub fn i64ToOct(n: i64): stringOctal text for n, no prefix.
parseBool
pub fn parseBool(s: &string): Option<bool>Parse a boolean, or None if s is not one of the accepted spellings.
Accepted, ASCII-case-insensitively and with no surrounding whitespace: "true", "t", "1" for true and "false", "f", "0" for false. Nothing else — "yes", "on", "" and " true" are all None. Trim before calling if your input can carry whitespace; a parser that silently trims cannot tell you that the field it read had a stray space in a config file.
parseFloat
pub fn parseFloat(s: string): Option<f64>Parse a floating-point number, or None if the string isn't a complete float literal. A named alias for the s.parseF64() builtin — same parser.
parseInt
pub fn parseInt(s: string): Option<i64>Parse a base-10 integer strictly: Some(n) only if the whole string is an optionally-signed run of digits that fits in i64, else None.
This is a named alias for the s.parseInt() builtin, not a second parser — the two used to disagree about what counts as a number, and one of them had to be the answer.
parseIntRadix
pub fn parseIntRadix(s: string, base: i32): Option<i64>Parse an integer in base (2, 8, 10, or 16), or None if invalid. Every digit must be legal in base and the whole string must be consumed; strtoll on its own stops at the first bad byte and reports that as a successful 0.
quoteString
pub fn quoteString(s: &string): stringA double-quoted string literal whose escapes are exactly Milo's: \\, \", \n, \r, \t, \0, and \xHH for every other byte below 0x20 or equal to 0x7F. The result always parses back to s through unquoteString.
Bytes at or above 0x80 are copied through unchanged, so valid UTF-8 stays readable rather than becoming a wall of hex. That means the output is only safe to embed in a context that is byte-transparent — it is a literal, not an escaping function for a document format. HTML, shell and SQL each need their own escaper; this one is for source text, log lines and REPL echo.
unquoteString
pub fn unquoteString(s: &string): Option<string>The inverse of quoteString: the bytes of a double-quoted literal, or None if s is not one.
Strict on purpose. s must start and end with ", contain no unescaped " and no raw byte below 0x20, and use only the escapes quoteString emits — an unknown escape such as \q is None, not a literal q. (The compiler's own lexer is lenient there; a decoder over untrusted text should not be, because the lenient reading silently deletes a backslash the author meant to keep.)
\xHH yields that raw byte, so a hand-written literal can decode to bytes that are not valid UTF-8. quoteString never emits such an escape, so a round trip cannot produce one.