Crate lexical_core

source ·
Expand description

Fast lexical conversion routines with a C FFI for a no_std environment.

Getting Started

lexical-core is a low-level, partially FFI-compatible API for number-to-string and string-to-number conversions, without requiring a system allocator. If you would like to use a convenient, high-level API, please look at lexical instead.

Getting Started

extern crate lexical_core;

// String to number using slices
// The argument is the byte string parsed.
let f = lexical_core::atof64_slice(b"3.5");   // 3.5
let i = lexical_core::atoi32_slice(b"15");    // 15

// String to number using pointer ranges, for FFI-compatible code.
// The first argument is a pointer to the start of the parsed byte array,
// and the second argument is a pointer to 1-past-the-end. It will process
// bytes in the range [first, last).
unsafe {
    let bytes = b"3.5";
    let first = bytes.as_ptr();
    let last = first.add(bytes.len());
    let f = lexical_core::atof64_range(first, last);
}

// If and only if the `radix` feature is enabled, you may use the radix
// overloads to parse non-decimal floats and strings.
#[cfg(feature = "radix")]
let f = lexical_core::atof32_radix_slice(2, b"11.1");   // 3.5
#[cfg(feature = "radix")]
let i = lexical_core::atoi32_radix_slice(2, b"1111");   // 15

// The ato*_slice and ato*_range parsers are not checked, they do not
// validate that the input data is entirely correct, and discard trailing
// bytes that are found. The explicit behavior is to wrap on overflow, and
// to discard invalid digits.
let i = lexical_core::atoi8_slice(b"256");    // 0, wraps from 256
let i = lexical_core::atoi8_slice(b"1a5");    // 1, discards "a5"

// You should prefer the checked parsers, whenever possible. These detect
// numeric overflow, and no invalid trailing digits are present.
// The error code for success is 0, all errors are less than 0.

// Ideally, everything works great.
let res = lexical_core::try_atoi8_slice(b"15");
assert_eq!(res.error.code, lexical_core::ErrorCode::Success);
assert_eq!(res.value, 15);

// However, it detects numeric overflow, setting `res.error.code`
// to the appropriate value.
let res = lexical_core::try_atoi8_slice(b"256");
assert_eq!(res.error.code, lexical_core::ErrorCode::Overflow);

// Errors occurring prematurely terminating the parser due to invalid
// digits return the index in the buffer where the invalid digit was
// seen. This may useful in contexts like serde, which require numerical
// parsers from complex data without having to extract a substring
// containing only numeric data ahead of time. If the error is set
// to a `InvalidDigit`, the value is guaranteed to be accurate up until
// that point. For example, if the trailing data is whitespace,
// the value from an invalid digit may be perfectly valid in some contexts.
let res = lexical_core::try_atoi8_slice(b"15 45");
assert_eq!(res.error.code, lexical_core::ErrorCode::InvalidDigit);
assert_eq!(res.error.index, 2);
assert_eq!(res.value, 15);

// Number to string using slices.
// The first argument is the value, the second argument is the radix,
// and the third argument is the buffer to write to.
// The function returns a subslice of the original buffer, and will
// always start at the same position (`buf.as_ptr() == slc.as_ptr()`).
let mut buf = [b'0'; lexical_core::MAX_I64_SIZE];
let slc = lexical_core::i64toa_slice(15, &mut buf);
assert_eq!(slc, b"15");

// If an insufficiently long buffer is passed, the serializer will panic.
// PANICS
let mut buf = [b'0'; 1];
//let slc = lexical_core::i64toa_slice(15, &mut buf);

// In order to guarantee the buffer is long enough, always ensure there
// are at least `MAX_XX_SIZE`, where XX is the type name in upperase,
// IE, for `isize`, `MAX_ISIZE_SIZE`.
let mut buf = [b'0'; lexical_core::MAX_F64_SIZE];
let slc = lexical_core::f64toa_slice(15.1, &mut buf);
assert_eq!(slc, b"15.1");

Structs

C-compatible error for FFI.
C-compatible result type from parsing strings-to-numbers for FFI.

Enums

Error code, indicating success or failure.
Rounding type for float-parsing.

Constants

The maximum number of bytes that any number-to-string function may write.
The minimum buffer size required to serialize any f32 value.
The minimum buffer size required to serialize any f64 value.
The minimum buffer size required to serialize any i8 value.
The minimum buffer size required to serialize any i16 value.
The minimum buffer size required to serialize any i32 value.
The minimum buffer size required to serialize any i64 value.
The minimum buffer size required to serialize any i128 value.
The minimum buffer size required to serialize any isize value.
The minimum buffer size required to serialize any u8 value.
The minimum buffer size required to serialize any u16 value.
The minimum buffer size required to serialize any u32 value.
The minimum buffer size required to serialize any u64 value.
The minimum buffer size required to serialize any u128 value.
The minimum buffer size required to serialize any usize value.

Statics

Symbol-generating constant for the maximum number of bytes that any number-to-string function may write.
Default character for scientific notation, used when the radix < 15.
The rounding scheme for float conversions.
Long infinity literal
Short infinity literal
Symbol-generating constant for the minimum buffer required to serialize any f32 value.
Symbol-generating constant for the minimum buffer required to serialize any f64 value.
Symbol-generating constant for the minimum buffer required to serialize any i8 value.
Symbol-generating constant for the minimum buffer required to serialize any i16 value.
Symbol-generating constant for the minimum buffer required to serialize any i32 value.
Symbol-generating constant for the minimum buffer required to serialize any i64 value.
Symbol-generating constant for the minimum buffer required to serialize any i128 value.
Symbol-generating constant for the minimum buffer required to serialize any isize value.
Symbol-generating constant for the minimum buffer required to serialize any u8 value.
Symbol-generating constant for the minimum buffer required to serialize any u16 value.
Symbol-generating constant for the minimum buffer required to serialize any u32 value.
Symbol-generating constant for the minimum buffer required to serialize any u64 value.
Symbol-generating constant for the minimum buffer required to serialize any u128 value.
Symbol-generating constant for the minimum buffer required to serialize any usize value.
Not a Number literal

Functions

Unchecked parser for a string-to-number conversion using pointer ranges.
Unchecked parser for a string-to-number conversion using Rust slices.
Unchecked parser for a string-to-number conversion using pointer ranges.
Unchecked parser for a string-to-number conversion using Rust slices.
Unchecked parser for a string-to-number conversion using pointer ranges.
Unchecked parser for a string-to-number conversion using Rust slices.
Unchecked parser for a string-to-number conversion using pointer ranges.
Unchecked parser for a string-to-number conversion using Rust slices.
Unchecked parser for a string-to-number conversion using pointer ranges.
Unchecked parser for a string-to-number conversion using Rust slices.
Unchecked parser for a string-to-number conversion using pointer ranges.
Unchecked parser for a string-to-number conversion using Rust slices.
Unchecked parser for a string-to-number conversion using pointer ranges.
Unchecked parser for a string-to-number conversion using Rust slices.
Unchecked parser for a string-to-number conversion using pointer ranges.
Unchecked parser for a string-to-number conversion using Rust slices.
Unchecked parser for a string-to-number conversion using pointer ranges.
Unchecked parser for a string-to-number conversion using Rust slices.
Unchecked parser for a string-to-number conversion using pointer ranges.
Unchecked parser for a string-to-number conversion using Rust slices.
Unchecked parser for a string-to-number conversion using pointer ranges.
Unchecked parser for a string-to-number conversion using Rust slices.
Unchecked parser for a string-to-number conversion using pointer ranges.
Unchecked parser for a string-to-number conversion using Rust slices.
Unchecked parser for a string-to-number conversion using pointer ranges.
Unchecked parser for a string-to-number conversion using Rust slices.
Unchecked parser for a string-to-number conversion using pointer ranges.
Unchecked parser for a string-to-number conversion using Rust slices.
Unchecked parser for a string-to-number conversion using pointer ranges.
Unchecked parser for a string-to-number conversion using Rust slices.
Unchecked parser for a string-to-number conversion using pointer ranges.
Unchecked parser for a string-to-number conversion using Rust slices.
Serializer for a number-to-string conversion using pointer ranges.
Serializer for a number-to-string conversion using Rust slices.
Serializer for a number-to-string conversion using pointer ranges.
Serializer for a number-to-string conversion using Rust slices.
Get INF_STRING as a pointer and size.
Get INFINITY_STRING as a pointer and size.
Get NAN_STRING as a pointer and size.
Serializer for a number-to-string conversion using pointer ranges.
Serializer for a number-to-string conversion using Rust slices.
Serializer for a number-to-string conversion using pointer ranges.
Serializer for a number-to-string conversion using Rust slices.
Serializer for a number-to-string conversion using pointer ranges.
Serializer for a number-to-string conversion using Rust slices.
Serializer for a number-to-string conversion using pointer ranges.
Serializer for a number-to-string conversion using Rust slices.
Serializer for a number-to-string conversion using pointer ranges.
Serializer for a number-to-string conversion using Rust slices.
Check if the error code designates an empty byte array was encountered.
Check if the error code designates an invalid digit was encountered.
Check if the error code designates integer overflow.
Check if the error code is successful.
Serializer for a number-to-string conversion using pointer ranges.
Serializer for a number-to-string conversion using Rust slices.
Set INF_STRING from a pointer and size.
Set INFINITY_STRING from a pointer and size.
Set NAN_STRING from a pointer and size.
Checked parser for a string-to-number conversion using Rust pointer ranges.
Checked parser for a string-to-number conversion using Rust slices.
Checked parser for a string-to-number conversion using Rust pointer ranges.
Checked parser for a string-to-number conversion using Rust slices.
Checked parser for a string-to-number conversion using Rust pointer ranges.
Checked parser for a string-to-number conversion using Rust slices.
Checked parser for a string-to-number conversion using Rust pointer ranges.
Checked parser for a string-to-number conversion using Rust slices.
Checked parser for a string-to-number conversion using Rust pointer ranges.
Checked parser for a string-to-number conversion using Rust slices.
Checked parser for a string-to-number conversion using Rust pointer ranges.
Checked parser for a string-to-number conversion using Rust slices.
Checked parser for a string-to-number conversion using Rust pointer ranges.
Checked parser for a string-to-number conversion using Rust slices.
Checked parser for a string-to-number conversion using Rust pointer ranges.
Checked parser for a string-to-number conversion using Rust slices.
Checked parser for a string-to-number conversion using Rust pointer ranges.
Checked parser for a string-to-number conversion using Rust slices.
Checked parser for a string-to-number conversion using Rust pointer ranges.
Checked parser for a string-to-number conversion using Rust slices.
Checked parser for a string-to-number conversion using Rust pointer ranges.
Checked parser for a string-to-number conversion using Rust slices.
Checked parser for a string-to-number conversion using Rust pointer ranges.
Checked parser for a string-to-number conversion using Rust slices.
Checked parser for a string-to-number conversion using Rust pointer ranges.
Checked parser for a string-to-number conversion using Rust slices.
Checked parser for a string-to-number conversion using Rust pointer ranges.
Checked parser for a string-to-number conversion using Rust slices.
Checked parser for a string-to-number conversion using Rust pointer ranges.
Checked parser for a string-to-number conversion using Rust slices.
Checked parser for a string-to-number conversion using Rust pointer ranges.
Checked parser for a string-to-number conversion using Rust slices.
Serializer for a number-to-string conversion using pointer ranges.
Serializer for a number-to-string conversion using Rust slices.
Serializer for a number-to-string conversion using pointer ranges.
Serializer for a number-to-string conversion using Rust slices.
Serializer for a number-to-string conversion using pointer ranges.
Serializer for a number-to-string conversion using Rust slices.
Serializer for a number-to-string conversion using pointer ranges.
Serializer for a number-to-string conversion using Rust slices.
Serializer for a number-to-string conversion using pointer ranges.
Serializer for a number-to-string conversion using Rust slices.
Serializer for a number-to-string conversion using pointer ranges.
Serializer for a number-to-string conversion using Rust slices.

Type Definitions

Expanded generic for a result type containing a value of type f32.
Expanded generic for a result type containing a value of type f64.
Expanded generic for a result type containing a value of type i8.
Expanded generic for a result type containing a value of type i16.
Expanded generic for a result type containing a value of type i32.
Expanded generic for a result type containing a value of type i64.
Expanded generic for a result type containing a value of type i128.
Expanded generic for a result type containing a value of type isize.
Expanded generic for a result type containing a value of type u8.
Expanded generic for a result type containing a value of type u16.
Expanded generic for a result type containing a value of type u32.
Expanded generic for a result type containing a value of type u64.
Expanded generic for a result type containing a value of type u128.
Expanded generic for a result type containing a value of type usize.