strconv
The strconv module provides functions for parsing strings into numeric datatypes and formatting numbers into strings.
Index
Types
type base = enum uint {
DEFAULT = 0, BIN = 2, OCT = 8, DEC = 10, HEX_UPPER = 16, HEX = 16, HEX_LOWER = 17, };
type fflags = enum uint {
NONE = 0,
SHOW_POS = 1 << 0, SHOW_POINT = 1 << 1, UPPERCASE = 1 << 2, UPPER_EXP = 1 << 3, SHOW_POS_EXP = 1 << 4, SHOW_TWO_EXP_DIGITS = 1 << 5, };
type ffmt = enum {
G,
E,
F, };
Errors
type error = !(invalid | overflow);
type invalid = !size;
type overflow = !void;
Functions
fn f32tos(n: f32) const str;
fn f64tos(n: f64) const str;
fn fftosf(h: io::handle, n: types::floating, f: ffmt = ffmt::G, prec: (void | uint) = void, flag: fflags = fflags::NONE) (size | io::error);
fn ftosf(n: types::floating, f: ffmt = ffmt::G, prec: (void | uint) = void, flag: fflags = fflags::NONE) str;
fn i16tos(i: i16, b: base = base::DEC) const str;
fn i32tos(i: i32, b: base = base::DEC) const str;
fn i64tos(i: i64, b: base = base::DEC) const str;
fn i8tos(i: i8, b: base = base::DEC) const str;
fn itos(i: int, b: base = base::DEC) const str;
fn stof32(s: str, b: base = base::DEC) (f32 | invalid | overflow);
fn stof64(s: str, b: base = base::DEC) (f64 | invalid | overflow);
fn stoi(s: str, base: base = base::DEC) (int | invalid | overflow);
fn stoi16(s: str, base: base = base::DEC) (i16 | invalid | overflow);
fn stoi32(s: str, base: base = base::DEC) (i32 | invalid | overflow);
fn stoi64(s: str, base: base = base::DEC) (i64 | invalid | overflow);
fn stoi8(s: str, base: base = base::DEC) (i8 | invalid | overflow);
fn stou(s: str, base: base = base::DEC) (uint | invalid | overflow);
fn stou16(s: str, base: base = base::DEC) (u16 | invalid | overflow);
fn stou32(s: str, base: base = base::DEC) (u32 | invalid | overflow);
fn stou64(s: str, base: base = base::DEC) (u64 | invalid | overflow);
fn stou8(s: str, base: base = base::DEC) (u8 | invalid | overflow);
fn stoz(s: str, base: base = base::DEC) (size | invalid | overflow);
fn strerror(err: error) str;
fn u16tos(u: u16, b: base = base::DEC) const str;
fn u32tos(u: u32, b: base = base::DEC) const str;
fn u64tos(u: u64, b: base = base::DEC) const str;
fn u8tos(u: u8, b: base = base::DEC) const str;
fn uptrtos(uptr: uintptr, b: base = base::DEC) const str;
fn utos(u: uint, b: base = base::DEC) const str;
fn ztos(u: size, b: base = base::DEC) const str;
Types
type base
type base = enum uint {
DEFAULT = 0, BIN = 2, OCT = 8, DEC = 10, HEX_UPPER = 16, HEX = 16, HEX_LOWER = 17, };
The valid numeric bases for numeric conversions.
type fflags
type fflags = enum uint {
NONE = 0,
SHOW_POS = 1 << 0, SHOW_POINT = 1 << 1, UPPERCASE = 1 << 2, UPPER_EXP = 1 << 3, SHOW_POS_EXP = 1 << 4, SHOW_TWO_EXP_DIGITS = 1 << 5, };
Flags for the ftosf functions.
type ffmt
type ffmt = enum {
G,
E,
F, };
Format styles for the ftosf functions.
Errors
type error
type error = !(invalid | overflow);
Any error which may be returned from a strconv function.
type invalid
type invalid = !size;
Indicates that the input string doesn't have the requested number format (integer or float). Contains the index of the first invalid rune.
type overflow
type overflow = !void;
Indicates that the input member can't be represented by the requested data type.
Functions
fn f32tos
fn f32tos(n: f32) const str;
Converts a f32 to a string in base 10. The return value is statically allocated and will be overwritten on subsequent calls; see strings::dup to duplicate the result. The result is equivalent to ftosf with format G and precision void.
fn f64tos
fn f64tos(n: f64) const str;
Converts a f64 to a string in base 10. The return value is statically allocated and will be overwritten on subsequent calls; see strings::dup to duplicate the result. The result is equivalent to ftosf with format G and precision void.
fn fftosf
fn fftosf(h: io::handle, n: types::floating, f: ffmt = ffmt::G, prec: (void | uint) = void, flag: fflags = fflags::NONE) (size | io::error);
Converts a types::floating to a string in base 10 and writes the result to the provided handle. Format parameters are as in ftosf.
fn ftosf
fn ftosf(n: types::floating, f: ffmt = ffmt::G, prec: (void | uint) = void, flag: fflags = fflags::NONE) str;
Converts any types::floating to a string in base 10. The return value must be freed.
A precision of void yields the smallest number of digits that can be parsed into the exact same number. Otherwise, the meaning depends on f:
- ffmt::F, ffmt::E: Number of digits after the decimal point.
- ffmt::G: Number of significant digits. 0 is equivalent to 1 precision, and trailing zeros are removed.
fn i16tos
fn i16tos(i: i16, b: base = base::DEC) const str;
Converts an i16 to a string. The return value is statically allocated and will be overwritten on subsequent calls; see strings::dup to duplicate the result.
fn i32tos
fn i32tos(i: i32, b: base = base::DEC) const str;
Converts an i32 to a string. The return value is statically allocated and will be overwritten on subsequent calls; see strings::dup to duplicate the result.
fn i64tos
fn i64tos(i: i64, b: base = base::DEC) const str;
Converts an i64 to a string. The return value is statically allocated and will be overwritten on subsequent calls; see strings::dup to duplicate the result.
fn i8tos
fn i8tos(i: i8, b: base = base::DEC) const str;
Converts an i8 to a string. The return value is statically allocated and will be overwritten on subsequent calls; see strings::dup to duplicate the result.
fn itos
fn itos(i: int, b: base = base::DEC) const str;
Converts an int to a string. The return value is statically allocated and will be overwritten on subsequent calls; see strings::dup to duplicate the result.
fn stof32
fn stof32(s: str, b: base = base::DEC) (f32 | invalid | overflow);
Converts a string to a f32 in base::DEC or base::HEX. If base is not provided, base::DEC is used. If the string is not a syntactically well-formed floating-point number, invalid is returned. If the string represents a floating-point number that is larger than the largest finite f32 number, overflow is returned. Zero is returned if the string represents a floating-point number that is smaller than the f32 number nearest to zero with respective sign. Recognizes "Infinity", "+Infinity", "-Infinity", and "NaN", case insensitive.
fn stof64
fn stof64(s: str, b: base = base::DEC) (f64 | invalid | overflow);
Converts a string to a f64 in base::DEC or base::HEX. If base is not provided, base::DEC is used. If the string is not a syntactically well-formed floating-point number, invalid is returned. If the string represents a floating-point number that is larger than the largest finite f64 number, overflow is returned. Zero is returned if the string represents a floating-point number that is smaller than the f64 number nearest to zero with respective sign. Recognizes "Infinity", "+Infinity", "-Infinity", and "NaN", case insensitive.
fn stoi
fn stoi(s: str, base: base = base::DEC) (int | invalid | overflow);
Converts a string to an int. Returns invalid if the string is empty or contains invalid characters. Returns overflow if the number is too large to be represented by an int.
fn stoi16
fn stoi16(s: str, base: base = base::DEC) (i16 | invalid | overflow);
Converts a string to an i16. Returns invalid if the string is empty or contains invalid characters. Returns overflow if the number is too large to be represented by an i16.
fn stoi32
fn stoi32(s: str, base: base = base::DEC) (i32 | invalid | overflow);
Converts a string to an i32. Returns invalid if the string is empty or contains invalid characters. Returns overflow if the number is too large to be represented by an i32.
fn stoi64
fn stoi64(s: str, base: base = base::DEC) (i64 | invalid | overflow);
Converts a string to an i64. Returns invalid if the string is empty or contains invalid characters. Returns overflow if the number is too large to be represented by an i64.
fn stoi8
fn stoi8(s: str, base: base = base::DEC) (i8 | invalid | overflow);
Converts a string to an i8. Returns invalid if the string is empty or contains invalid characters. Returns overflow if the number is too large to be represented by an i8.
fn stou
fn stou(s: str, base: base = base::DEC) (uint | invalid | overflow);
Converts a string to a uint in the given base. Returns invalid if the string is empty or contains invalid characters. Returns overflow if the number is too large to be represented by a uint.
fn stou16
fn stou16(s: str, base: base = base::DEC) (u16 | invalid | overflow);
Converts a string to a u16. Returns invalid if the string is empty or contains invalid characters. Returns overflow if the number is too large to be represented by a u16.
fn stou32
fn stou32(s: str, base: base = base::DEC) (u32 | invalid | overflow);
Converts a string to a u32. Returns invalid if the string is empty or contains invalid characters. Returns overflow if the number is too large to be represented by a u32.
fn stou64
fn stou64(s: str, base: base = base::DEC) (u64 | invalid | overflow);
Converts a string to a u64. Returns invalid if the string is empty or contains invalid characters. Returns overflow if the number is too large to be represented by a u64.
fn stou8
fn stou8(s: str, base: base = base::DEC) (u8 | invalid | overflow);
Converts a string to a u8. Returns invalid if the string is empty or contains invalid characters. Returns overflow if the number is too large to be represented by a u8.
fn stoz
fn stoz(s: str, base: base = base::DEC) (size | invalid | overflow);
Converts a string to a size. Returns invalid if the string is empty or contains invalid characters. Returns overflow if the number is too large to be represented by a size.
fn strerror
fn strerror(err: error) str;
Converts an strconv error into a user-friendly string.
fn u16tos
fn u16tos(u: u16, b: base = base::DEC) const str;
Converts a u16 to a string. The return value is statically allocated and will be overwritten on subsequent calls; see strings::dup to duplicate the result.
fn u32tos
fn u32tos(u: u32, b: base = base::DEC) const str;
Converts a u32 to a string. The return value is statically allocated and will be overwritten on subsequent calls; see strings::dup to duplicate the result.
fn u64tos
fn u64tos(u: u64, b: base = base::DEC) const str;
Converts a u64 to a string. The return value is statically allocated and will be overwritten on subsequent calls; see strings::dup to duplicate the result.
fn u8tos
fn u8tos(u: u8, b: base = base::DEC) const str;
Converts a u8 to a string. The return value is statically allocated and will be overwritten on subsequent calls; see strings::dup to duplicate the result.
fn uptrtos
fn uptrtos(uptr: uintptr, b: base = base::DEC) const str;
Converts a uintptr to a string. The return value is statically allocated and will be overwritten on subsequent calls; see strings::dup to duplicate the result.
fn utos
fn utos(u: uint, b: base = base::DEC) const str;
Converts a uint to a string. The return value is statically allocated and will be overwritten on subsequent calls; see strings::dup to duplicate the result.
fn ztos
fn ztos(u: size, b: base = base::DEC) const str;
Converts a size to a string. The return value is statically allocated and will be overwritten on subsequent calls; see strings::dup to duplicate the result.