Crate lexical_core[][src]

Fast lexical conversion routines for a no_std environment.

lexical-core is a low-level 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 Rust slices.
// The argument is the byte string parsed.
let f: f32 = lexical_core::parse(b"3.5").unwrap();   // 3.5
let i: i32 = lexical_core::parse(b"15").unwrap();    // 15

// All lexical_core parsers are checked, they validate the
// input data is entirely correct, and stop parsing when invalid data
// is found, or upon numerical overflow.
let r = lexical_core::parse::<u8>(b"256"); // Err(ErrorCode::Overflow.into())
let r = lexical_core::parse::<u8>(b"1a5"); // Err(ErrorCode::InvalidDigit.into())

// In order to extract and parse a number from a substring of the input
// data, use `parse_partial`. These functions return the parsed value and
// the number of processed digits, allowing you to extract and parse the
// number in a single pass.
let r = lexical_core::parse_partial::<i8>(b"3a5"); // Ok((3, 1))

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

// In order to guarantee the buffer is long enough, always ensure there
// are at least `T::FORMATTED_SIZE` bytes, which requires the
// `lexical_core::Number` trait to be in scope.
use lexical_core::Number;
let mut buf = [b'0'; f64::FORMATTED_SIZE];
let slc = lexical_core::write::<f64>(15.1, &mut buf);
assert_eq!(slc, b"15.1");

// When the `radix` feature is enabled, for decimal floats, using
// `T::FORMATTED_SIZE` may significantly overestimate the space
// required to format the number. Therefore, the
// `T::FORMATTED_SIZE_DECIMAL` constants allow you to get a much
// tighter bound on the space required.
let mut buf = [b'0'; f64::FORMATTED_SIZE_DECIMAL];
let slc = lexical_core::write::<f64>(15.1, &mut buf);
assert_eq!(slc, b"15.1");

Conversion API

To String

From String

Configuration Settings

Get Configuration

Set Configuration

Macros

arrvec

Macro to automate simplify the creation of an ArrayVec.

Structs

Error

Error type for lexical parsing.

Enums

ErrorCode

Error code, indicating failure type.

Constants

BUFFER_SIZE

Maximum number of bytes required to serialize any number to string.

Traits

FromLexical

Trait for numerical types that can be parsed from bytes.

FromLexicalLossy

Trait for floating-point types that can be parsed using lossy algorithms from bytes.

ToLexical

Trait for numerical types that can be serialized to bytes.

Functions

get_exponent_default_char

Get default character for the exponent symbol.

get_inf_string

Get the short representation of an Infinity literal as a byte slice.

get_infinity_string

Get the long representation of an Infinity literal as a byte slice.

get_nan_string

Get string representation of Not a Number as a byte slice.

parse

Parse number from string.

parse_lossy

Lossily parse number from string.

parse_partial

Parse number from string.

parse_partial_lossy

Lossily parse number from string.

set_exponent_default_char

Set the default character for the exponent symbol.

set_inf_string

Set the short representation of Infinity from a byte slice.

set_infinity_string

Set the long representation of Infinity from a byte slice.

set_nan_string

Set representation of Not a Number from a byte slice.

write

Write number to string.

Type Definitions

Result

A specialized Result type for lexical operations.