@strconv
Import this module with import @strconv.
String-to-type and type-to-string conversion functions with proper error handling.
Fallible Conversions (string → type)
Section titled “Fallible Conversions (string → type)”| Function | Signature | Description |
|---|---|---|
to_i64 |
(s string, base i64 = 10) -> (i64, Error) |
Parse string as signed integer in given base |
to_u64 |
(s string, base i64 = 10) -> (u64, Error) |
Parse string as unsigned integer in given base |
to_f64 |
(s string) -> (f64, Error) |
Parse string as floating-point number |
to_bool |
(s string) -> (bool, Error) |
Parse string as boolean |
Behavior:
- These are fallible functions. Single-variable assignment (
mut n i64 = strconv.to_i64("42")) is a compile-time error (E3089); the result must be destructured. mut n, err = strconv.to_i64(s)— inspecterr(non-nil on invalid input).mut n, _ = strconv.to_i64(s)— discard the error; on invalid inputnis the zero value (0), no panic.
to_i64 / to_u64 rules:
- The
baseparameter must be an integer between 2 and 36 (inclusive). Invalid bases produce a compile-time error when passed as a literal, or a runtime panic when passed as a variable. - Leading/trailing whitespace is not tolerated; the entire string must be a valid representation.
to_u64rejects strings containing a-sign (returns a non-nil error).- For bases > 10, letters
A–Z(case-insensitive) represent digits 10–35.
to_f64 rules:
- Accepts standard decimal notation (e.g.
"3.14","-0.5","100"). - Accepts
"inf","infinity", and"nan"(case-insensitive), returning±Inf/NaNrespectively. This matches the underlyingstrtodsemantics used by the implementation. - Does not accept hex floats.
to_bool rules:
- Accepts
"true"and"false"(case-insensitive:"TRUE","True","FALSE", etc. are valid). - All other strings produce a non-nil error. There is no implicit truthiness;
"1","0","yes","no"are not valid.
Type-to-String Conversions (type → string)
Section titled “Type-to-String Conversions (type → string)”| Function | Signature | Description |
|---|---|---|
from_i64 |
(n i64) -> string |
Convert integer to decimal string |
from_u64 |
(n u64) -> string |
Convert unsigned integer to decimal string |
from_f64 |
(f f64) -> string |
Convert f64 to string (shortest representation) |
from_bool |
(b bool) -> string |
Convert boolean to "true" or "false" |
format_i64 |
(n i64, base i64) -> string |
Convert signed integer to a string in any base 2–36 |
format_u64 |
(n u64, base i64) -> string |
Convert unsigned integer to a string in any base 2–36 |
These functions never fail, except format_i64 / format_u64 panic when base is
outside 2–36 (a compile-time error when the base is a literal).
format_i64 / format_u64 rules:
basemust be an integer between 2 and 36 (inclusive).- Digits above 9 are lowercase letters
a–z. format_i64prefixes negative values with-;format_u64treats its argument as unsigned.- The inverse of
to_i64/to_u64:to_i64(format_i64(n, b), b) == n. fmt.i64_to_hex/fmt.i64_to_binary/fmt.i64_to_octalremain available; they format the raw two’s-complement bit pattern for their fixed base, whereasformat_i64produces a signed representation.
Quoting (string ↔ quoted literal)
Section titled “Quoting (string ↔ quoted literal)”| Function | Signature | Description |
|---|---|---|
quote |
(s string) -> string |
Wrap s in double quotes, escaping special characters |
unquote |
(s string) -> (string, Error) |
Remove surrounding double quotes from s and interpret escapes |
quote rules:
- Wraps the result in
"and escapes\,", newline, carriage return, and tab; other bytes below0x20and0x7fare emitted as\xNN. All other bytes are copied unchanged.
unquote rules:
- Fallible: returns
(string, Error)and must be destructured (mut v, err = ...ormut v, _ = ...); single-variable assignment is a compile error. - Requires
sto begin and end with". - Interprets
\n \t \r \\ \" \' \0 \a \b \f \v \$and\xNN(two hex digits). - An unescaped
", a trailing\, or an unknown escape produces an error. - The inverse of
quote:unquote(quote(s))returnss.
Query Functions
Section titled “Query Functions”| Function | Signature | Description |
|---|---|---|
is_numeric |
(s string) -> bool |
Returns true if string is a valid numeric representation (integer or decimal) |
is_integer |
(s string) -> bool |
Returns true if string is a valid integer (digits only, optional leading sign) |
is_numeric rules:
- Accepts optional leading
+or-, followed by digits with at most one.decimal point. - Empty strings return
false. - Does not accept scientific notation, hex prefixes, or whitespace.
is_integer rules:
- Accepts optional leading
+or-, followed by one or more digits (0–9). - Empty strings return
false. - Does not validate whether the value fits in an
i64oru64.
Constants
Section titled “Constants”| Constant | Value | Description |
|---|---|---|
BASE_2 |
2 |
Binary |
BASE_8 |
8 |
Octal |
BASE_10 |
10 |
Decimal (default) |
BASE_16 |
16 |
Hexadecimal |
BASE_36 |
36 |
Base-36 (digits + full alphabet) |
Constants can be used qualified (strconv.BASE_16) or bare after import and use @strconv. Any integer value between 2 and 36 is also accepted directly as the base argument.