Crate lexical_core[][src]

Expand description

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

Macro to automate simplify the creation of an ArrayVec.

Structs

Error type for lexical parsing.

Enums

Error code, indicating failure type.

Constants

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

Traits

Trait for numerical types that can be parsed from bytes.

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

Trait for numerical types that can be serialized to bytes.

Functions

Get default character for the exponent symbol.

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

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

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

Parse number from string.

Lossily parse number from string.

Parse number from string.

Lossily parse number from string.

Set the default character for the exponent symbol.

Set the short representation of Infinity from a byte slice.

Set the long representation of Infinity from a byte slice.

Set representation of Not a Number from a byte slice.

Write number to string.

Type Definitions

A specialized Result type for lexical operations.