3.3. Soft-failing string-to-numeric conversions returning Result<T; ConversionError>
The STRINGS_CONVERT module provides soft-failing string-to-numeric conversions
that return Result<T; ConversionError> instead of panicking or silently
returning zero. Use these when parsing untrusted input where you need to
distinguish between not-a-number, overflow, and trailing-garbage.
All functions and symbols are in the “strings_convert” module, use require to get access to it.
require daslib/strings_convert
Example:
require daslib/strings_convert
[export]
def main() {
let r = try_to_int("42")
if (is_ok(r)) {
print("parsed: {unwrap(r)}\n")
}
let bad = try_to_int("nope")
if (is_err(bad)) {
print("error: {unwrap_err(bad)}\n")
}
}
// output:
// parsed: 42
// error: invalid_argument
3.3.1. Enumerations
- ConversionError
enum ConversionError
3.3.2. Soft-failing conversions
try_to_double (str: string) : tuple<_is_ok:bool;_value:double;_error:ConversionError>
try_to_float (str: string) : tuple<_is_ok:bool;_value:float;_error:ConversionError>
try_to_int (str: string; hex: bool = false) : tuple<_is_ok:bool;_value:int;_error:ConversionError>
try_to_int8 (str: string; hex: bool = false) : tuple<_is_ok:bool;_value:int8;_error:ConversionError>
try_to_uint (str: string; hex: bool = false) : tuple<_is_ok:bool;_value:uint;_error:ConversionError>
- try_to_double(str: string ): tuple<_is_ok:bool;_value:double;_error:ConversionError>
Parse str as a 64-bit float.
- Arguments:
str : string implicit
- try_to_float(str: string ): tuple<_is_ok:bool;_value:float;_error:ConversionError>
Parse str as a 32-bit float.
- Arguments:
str : string implicit
- try_to_int(str: string; hex: bool = false ): tuple<_is_ok:bool;_value:int;_error:ConversionError>
Parse str as a signed 32-bit integer.
- Arguments:
str : string implicit
hex : bool
- try_to_int16(str: string; hex: bool = false ): tuple<_is_ok:bool;_value:int16;_error:ConversionError>
Parse str as a signed 16-bit integer.
- Arguments:
str : string implicit
hex : bool
- try_to_int64(str: string; hex: bool = false ): tuple<_is_ok:bool;_value:int64;_error:ConversionError>
Parse str as a signed 64-bit integer.
- Arguments:
str : string implicit
hex : bool
- try_to_int8(str: string; hex: bool = false ): tuple<_is_ok:bool;_value:int8;_error:ConversionError>
Parse str as a signed 8-bit integer.
- Arguments:
str : string implicit
hex : bool
- try_to_uint(str: string; hex: bool = false ): tuple<_is_ok:bool;_value:uint;_error:ConversionError>
Parse str as an unsigned 32-bit integer.
- Arguments:
str : string implicit
hex : bool
- try_to_uint16(str: string; hex: bool = false ): tuple<_is_ok:bool;_value:uint16;_error:ConversionError>
Parse str as an unsigned 16-bit integer.
- Arguments:
str : string implicit
hex : bool
- try_to_uint64(str: string; hex: bool = false ): tuple<_is_ok:bool;_value:uint64;_error:ConversionError>
Parse str as an unsigned 64-bit integer.
- Arguments:
str : string implicit
hex : bool
- try_to_uint8(str: string; hex: bool = false ): tuple<_is_ok:bool;_value:uint8;_error:ConversionError>
Parse str as an unsigned 8-bit integer.
- Arguments:
str : string implicit
hex : bool