### Install Rust Toolchain and Components Source: https://github.com/alexhuszagh/rust-lexical/blob/main/docs/Development.md Install the nightly Rust toolchain and essential components like clippy, rustfmt, and miri. This setup is required for comprehensive development and testing. ```bash rustup toolchain install nightly rustup +nightly component add clippy rustup +nightly component add rustfmt rustup +nightly component add miri ``` -------------------------------- ### no_std Environment Usage Source: https://github.com/alexhuszagh/rust-lexical/blob/main/lexical-core/README.md Provides instructions and code examples for using `lexical-core` in a `no_std` environment, including necessary `Cargo.toml` configurations and basic usage of `write` and `parse` functions with byte slices. ```APIDOC ## no_std Usage ### Description Provides instructions and code examples for using `lexical-core` in a `no_std` environment, including necessary `Cargo.toml` configurations and basic usage of `write` and `parse` functions with byte slices. ### Cargo.toml Configuration ```toml [dependencies.lexical-core] version = "1.0.0" default-features = false # Can select only desired parsing/writing features. features = ["write-integers", "write-floats", "parse-integers", "parse-floats"] ``` ### Code Example ```rust // A constant for the maximum number of bytes a formatter will write. use lexical_core::BUFFER_SIZE; let mut buffer = [b'0'; BUFFER_SIZE]; // Number to string. The underlying buffer must be a slice of bytes. let count = lexical_core::write(3.0, &mut buffer); assert_eq!(buffer[..count], b"3.0"); let count = lexical_core::write(3i32, &mut buffer); assert_eq!(buffer[..count], b"3"); // String to number. The input must be a slice of bytes. let i: i32 = lexical_core::parse(b"3")?; let f: f32 = lexical_core::parse(b"3.5")?; let d: f64 = lexical_core::parse(b"3.5")?; let d: f64 = lexical_core::parse(b"3a")?; ``` ``` -------------------------------- ### Install Cargo-Spellcheck for Doc Comment Editing Source: https://github.com/alexhuszagh/rust-lexical/blob/main/docs/Development.md Install cargo-spellcheck for editing documentation comments. This tool requires a Linux or macOS environment and specific system packages. ```bash # Only if editing doc comments. This requires a Linux or macOS install. # On Ubuntu, the packages `libclang-dev` and `llvm` are required. cargo install cargo-spellcheck ``` -------------------------------- ### OCaml Examples of Leading Digit Separators Source: https://github.com/alexhuszagh/rust-lexical/blob/main/docs/DigitSeparators.md Illustrates valid syntax for leading digit separators in OCaml, including consecutive separators and those adjacent to decimal points. ```ocaml _1 __1 _1.0 __1.0 1._0 1.__0 1.0e_5 1.0e__5 ``` -------------------------------- ### Install Cargo-Count for Unsafe Code Analysis Source: https://github.com/alexhuszagh/rust-lexical/blob/main/docs/Development.md Install cargo-count from a specific Git revision to analyze the amount of unsafe code. This requires an older nightly version for installation. ```bash # This is only needed if checking the number of lines of unsafe code, # which uses a deprecated binary that requires an old nightly to # install. cargo +nightly install cargo-count --git https://github.com/kbknapp/cargo-count --rev eebe6f8 --locked ``` -------------------------------- ### Build Custom Number Format Source: https://github.com/alexhuszagh/rust-lexical/blob/main/README.md Shows how to construct a custom number format using `NumberFormatBuilder`. This example disables exponent notation and all special numbers, ensuring strict adherence to the defined format. ```rust const FORMAT: u128 = lexical_core::NumberFormatBuilder::new() // Disable exponent notation. .no_exponent_notation(true) // Disable all special numbers, such as Nan and Inf. .no_special(true) .build_strict(); ``` -------------------------------- ### Install Development Cargo Tools Source: https://github.com/alexhuszagh/rust-lexical/blob/main/docs/Development.md Install additional cargo tools required for development, such as cargo-valgrind, cargo-tarpaulin, and cargo-fuzz. These tools aid in code analysis and testing. ```bash cargo +nightly install cargo-valgrind cargo +nightly install cargo-tarpaulin cargo +nightly install cargo-fuzz ``` -------------------------------- ### OCaml Examples of Internal Digit Separators Source: https://github.com/alexhuszagh/rust-lexical/blob/main/docs/DigitSeparators.md Shows valid syntax for internal digit separators in OCaml, including consecutive separators and those between digits or adjacent to decimal points/exponents. ```ocaml 1_2 1__2 1_2.0 1__2.0 1.0_2 1.0__2 1.0e5_4 1.0e5__4 ``` -------------------------------- ### OCaml Examples of Trailing Digit Separators Source: https://github.com/alexhuszagh/rust-lexical/blob/main/docs/DigitSeparators.md Demonstrates valid syntax for trailing digit separators in OCaml, including consecutive separators and those preceding decimal points or exponent indicators. ```ocaml 1_ 1__ 1_.0 1__.0 1.0_ 1.0__ 1.0e5_ 1.0e5__ ``` -------------------------------- ### Add lexical to Cargo.toml Source: https://github.com/alexhuszagh/rust-lexical/blob/main/README.md Include the lexical-core crate as a dependency in your Cargo.toml file to start using its features. ```toml [dependencies] lexical-core = "^1.0" ``` -------------------------------- ### Parse Numbers with Custom Options (JSON) Source: https://context7.com/alexhuszagh/rust-lexical/llms.txt Parses a number using compile-time format constants and run-time `Options`. This example demonstrates JSON-specific parsing rules, rejecting leading zeros and trailing decimals. ```rust use lexical_core::{ParseFloatOptions, format}; // ── JSON: rejects leading zeros, trailing decimals, NaN, Inf ────────────────── let ok: f64 = lexical_core::parse_with_options::<_, { format::JSON }>( b"3.14e2", &ParseFloatOptions::new(), ).unwrap(); assert_eq!(ok, 314.0); // JSON rejects ".5" (no leading digit) assert!(lexical_core::parse_with_options::( b".5", &ParseFloatOptions::new(), ).is_err()); ``` -------------------------------- ### Build and Check Safety Features Source: https://github.com/alexhuszagh/rust-lexical/blob/main/docs/Development.md Builds the project with linting enabled and checks rustfmt and clippy compliance. Miri tests are skipped for most commits due to performance. ```bash RUSTFLAGS="--deny warnings" cargo +nightly build --features=lint scripts/check.sh SKIP_MIRI=1 scripts/test.sh ``` -------------------------------- ### Basic Usage: Number to String and String to Number Source: https://github.com/alexhuszagh/rust-lexical/blob/main/lexical-core/README.md Demonstrates the fundamental usage of `lexical_core::write` for converting numbers to strings and `lexical_core::parse` for converting strings to numbers. It also shows auto-type deduction and basic error handling for parsing. ```APIDOC ## Basic Usage ### Description Demonstrates the fundamental usage of `lexical_core::write` for converting numbers to strings and `lexical_core::parse` for converting strings to numbers. It also shows auto-type deduction and basic error handling for parsing. ### Code Example ```rust // Number to string use lexical_core::BUFFER_SIZE; let mut buffer = [b'0'; BUFFER_SIZE]; lexical_core::write(3.0, &mut buffer); // "3.0", always has a fraction suffix, lexical_core::write(3, &mut buffer); // "3" // String to number. let i: i32 = lexical_core::parse("3")?; let f: f32 = lexical_core::parse("3.5")?; let d: f64 = lexical_core::parse("3.5")?; let d: f64 = lexical_core::parse("3a")?; ``` ``` -------------------------------- ### Configure lexical-core for no_std Environments Source: https://github.com/alexhuszagh/rust-lexical/blob/main/lexical/README.md Specifies how to configure the lexical-core crate for `no_std` environments by disabling default features and enabling specific features like integer and float parsing/writing. ```toml [dependencies.lexical-core] version = "1.0.0" default-features = false # Can select only desired parsing/writing features. features = ["write-integers", "write-floats", "parse-integers", "parse-floats"] ``` -------------------------------- ### Build Custom WriteFloatOptions in Rust Source: https://github.com/alexhuszagh/rust-lexical/blob/main/README.md Demonstrates how to build a custom `WriteFloatOptions` struct using the builder pattern to configure float writing behavior, such as significant digit limits, decimal point style, and string representations for NaN and infinity. Requires `std::num` and `lexical_core` imports. ```rust use std::num; const OPTIONS: lexical_core::WriteFloatOptions = lexical_core::WriteFloatOptions::builder() // Only write up to 5 significant digits, IE, `1.23456` becomes `1.2345`. .max_significant_digits(num::NonZeroUsize::new(5)) // Never write less than 5 significant digits, `1.1` becomes `1.1000`. .min_significant_digits(num::NonZeroUsize::new(5)) // Trim the trailing `.0` from integral float strings. .trim_floats(true) // Use a European-style decimal point. .decimal_point(b',') // Panic if we try to write NaN as a string. .nan_string(None) // Write infinity as "Infinity". .inf_string(Some(b"Infinity")) .build_strict(); ``` -------------------------------- ### Case-Insensitive Starts With Check in Assembly Source: https://github.com/alexhuszagh/rust-lexical/blob/main/extras/asm/README.md This assembly code implements a case-insensitive string comparison, likely for a `starts_with` or `case_insensitive_starts_with` function. It compares bytes after converting them to a common case. ```asm lea rdx, [rip + .Lanon.c5e496e177efefe6ca3af6b5e2dec4d8.14] .p2align 4, 0x90 .LBB31_9: cmp rdi, 3 je .LBB31_46 cmp rsi, rdi je .LBB31_12 movzx eax, byte ptr [rbx + rdi] xor al, byte ptr [rdi + rdx] add rdi, 1 test al, -33 je .LBB31_9 .Lanon.c5e496e177efefe6ca3af6b5e2dec4d8.14: .ascii "NaN" ``` -------------------------------- ### Build and Test Lexical with Nightly Rust Source: https://github.com/alexhuszagh/rust-lexical/blob/main/docs/Development.md Use the nightly Rust toolchain for building and testing the project to ensure compatibility with the latest features and tools. ```bash cargo +nightly build ``` ```bash cargo +nightly test ``` -------------------------------- ### Compact Integer Writing Algorithm Source: https://github.com/alexhuszagh/rust-lexical/blob/main/lexical-write-integer/docs/Algorithm.md This naive algorithm prioritizes code size over performance by writing one digit at a time. It is simple to verify and analogous to manual conversion. ```rust let mut index = buffer.len(); while value >= radix { let r = value % radix; value /= radix; index -= 1; buffer[index] = digit_to_char(r); } let r = value % radix; index -= 1; buffer[index] = digit_to_char(r); ``` -------------------------------- ### Complete Parsers Source: https://github.com/alexhuszagh/rust-lexical/blob/main/lexical-core/README.md Explains and demonstrates the use of complete parsers in `lexical-core`, which require the entire input buffer to be consumed for successful parsing. Errors indicate the index of the first invalid character. ```APIDOC ## Complete Parsers ### Description Explains and demonstrates the use of complete parsers in `lexical-core`, which require the entire input buffer to be consumed for successful parsing. Errors indicate the index of the first invalid character. ### Code Example ```rust // This will return Err(Error::InvalidDigit(3)), indicating // the first invalid character occurred at the index 3 in the input // string (the space character). let x: i32 = lexical_core::parse(b"123 456")?; ``` ``` -------------------------------- ### Digit Separator Format (Python-style) Source: https://context7.com/alexhuszagh/rust-lexical/llms.txt Configure `NumberFormatBuilder` to use a digit separator character, like underscores, for improved readability in numbers. This example demonstrates Python-style underscores for internal digit separation. ```rust // ── Digit separator: Python-style underscores ───────────────────────────────── const PYTHON: u128 = NumberFormatBuilder::new() .digit_separator(num::NonZeroU8::new(b'_')) .internal_digit_separator(true) .build_strict(); let v: i64 = lexical_core::parse_with_options::<_, { PYTHON }> (b"1_000_000", &lexical_core::ParseIntegerOptions::new(), ).unwrap(); assert_eq!(v, 1_000_000); ``` -------------------------------- ### Write Numbers with Custom Options (Significant Digits) Source: https://context7.com/alexhuszagh/rust-lexical/llms.txt Formats a number with a limited number of significant digits and trims trailing zeros. For example, `3.141592` becomes `3.142` and `3.0` becomes `3`. ```rust use std::num; use lexical_core::{WriteFloatOptions, FormattedSize, ToLexicalWithOptions, format}; // ── Limit significant digits and trim trailing zeros ───────────────────────── const OPTS: WriteFloatOptions = WriteFloatOptions::builder() .max_significant_digits(num::NonZeroUsize::new(4)) .trim_floats(true) // "3.0" → "3" .build_strict(); let result = lexical::to_string_with_options::<_, { format::STANDARD }>(3.141592f64, &OPTS); assert_eq!(result, "3.142"); let result = lexical::to_string_with_options::<_, { format::STANDARD }>(3.0f64, &OPTS); assert_eq!(result, "3"); ``` -------------------------------- ### Basic Usage: Number to String and String to Number Source: https://github.com/alexhuszagh/rust-lexical/blob/main/README.md Demonstrates the fundamental `write` and `parse` functions for converting numbers to strings and vice versa. `write` requires a mutable byte buffer, while `parse` supports automatic type deduction and returns a `Result`. ```rust // Number to string use lexical_core::BUFFER_SIZE; let mut buffer = [b'0'; BUFFER_SIZE]; lexical_core::write(3.0, &mut buffer); // "3.0", always has a fraction suffix, lexical_core::write(3, &mut buffer); // "3" // String to number. let i: i32 = lexical_core::parse("3")?; let f: f32 = lexical_core::parse("3.5")?; let d: f64 = lexical_core::parse("3.5")?; let d: f64 = lexical_core::parse("3a")?; ``` -------------------------------- ### Julia Valid/Invalid Float Literals with Digit Separators Source: https://github.com/alexhuszagh/rust-lexical/blob/main/docs/DigitSeparators.md Examples of valid and invalid float literals in Julia with digit separators. Julia restricts digit separators to significant digits and not the exponent part. ```java double x = 1.0_3_4_5; double x = 1.0__3; double x = 1.0__3e4_5; // invalid double x = 1_.0; // invalid double x = 1._0; // invalid ``` -------------------------------- ### Build and Lint with Nightly Rust Source: https://github.com/alexhuszagh/rust-lexical/blob/main/docs/Development.md Ensures all safety sections and other features are properly documented and that rustfmt and clippy checks pass using a nightly Rust compiler. ```bash # Check all safety sections and other features are properly documented. RUSTFLAGS="--deny warnings" cargo +nightly build --features=lint # Ensure all rustfmt and clippy checks pass. scripts/check.sh # Ensure all tests pass with common feature combinations. # Miri is too slow, so skip those tests for most commits. SKIP_MIRI=1 scripts/test.sh ``` -------------------------------- ### No-std Configuration for Lexical Core Source: https://context7.com/alexhuszagh/rust-lexical/llms.txt Configure `lexical-core` for `no_std` environments by disabling default features and selecting specific ones in `Cargo.toml`. Use `FormattedSize` for type-specific buffer sizes or `BUFFER_SIZE` as a universal fallback. ```toml # Cargo.toml — no_std, no allocator, select only needed sub-features [dependencies.lexical-core] version = "1.0" default-features = false features = ["write-integers", "write-floats", "parse-integers", "parse-floats"] ``` ```rust #!/usr/bin/env rust #![no_std] use lexical_core::{FormattedSize, BUFFER_SIZE}; // Type-specific minimum buffer let mut f32_buf = [0u8; f32::FORMATTED_SIZE]; let written = lexical_core::write(1.23f32, &mut f32_buf); // Universal safe upper bound (works for any type) let mut buf = [0u8; BUFFER_SIZE]; let written = lexical_core::write(-9_876_543_210i64, &mut buf); // assert_eq!(written, b"-9876543210"); // buffer_size_const gives a tight bound for custom write options use lexical_core::WriteFloatOptions; const OPTS: WriteFloatOptions = WriteFloatOptions::builder() .max_significant_digits(core::num::NonZeroUsize::new(8)) .build_strict(); const EXACT_SIZE: usize = OPTS.buffer_size_const::(); let mut tight_buf = [0u8; EXACT_SIZE]; let written = lexical_core::write_with_options::( 1.23456789e100, &mut tight_buf, &OPTS, ); ``` -------------------------------- ### Parse with JSON Float Options - Rust Source: https://github.com/alexhuszagh/rust-lexical/blob/main/lexical-write-float/README.md Use `lexical_core::parse_with_options` to parse floats according to JSON specifications. This example demonstrates accepting standard JSON float formats while rejecting variations not permitted by JSON. ```rust use lexical_core::ParseFloatOptions; // Valid in Rust strings. // Not valid in JSON. let f: f64 = lexical_core::parse(b"3.e7")?; // Let's only accept JSON floats. const JSON: u128 = lexical_core::format::JSON; const OPTIONS: ParseFloatOptions = ParseFloatOptions::new(); let f: f64 = lexical_core::parse_with_options::<_, JSON>(b"3.0e7", &OPTIONS)?; let f: f64 = lexical_core::parse_with_options::<_, JSON>(b"3.e7", &OPTIONS)?; ``` -------------------------------- ### Java Valid/Invalid Float Literals with Digit Separators Source: https://github.com/alexhuszagh/rust-lexical/blob/main/docs/DigitSeparators.md Examples of valid and invalid float literals in Java when using digit separators. Note that Java allows internal digit separators but not adjacent to decimal points. ```java double x = 1.0_3_4_5; double x = 1.0__3; double x = 1.0__3e4_5; double x = 1_.0; // invalid double x = 1._0; // invalid ``` -------------------------------- ### Build Custom Number Format - Rust Source: https://github.com/alexhuszagh/rust-lexical/blob/main/lexical-write-float/README.md Construct a custom number format using `lexical_core::NumberFormatBuilder`. This example disables exponent notation and all special numbers (NaN, Infinity) for strict parsing. The builder panics if the resulting format is invalid. ```rust use lexical_core::NumberFormatBuilder; // this will panic if the format is invalid const FORMAT: u128 = lexical_core::NumberFormatBuilder::new() // Disable exponent notation. .no_exponent_notation(true) // Disable all special numbers, such as Nan and Inf. .no_special(true) .build_strict(); ``` -------------------------------- ### no_std Configuration for lexical-core Source: https://github.com/alexhuszagh/rust-lexical/blob/main/README.md Configures the `lexical-core` crate for `no_std` environments by disabling default features and explicitly enabling desired parsing and writing features in `Cargo.toml`. ```toml [dependencies.lexical-core] version = "1.0.0" default-features = false # Can select only desired parsing/writing features. features = ["write-integers", "write-floats", "parse-integers", "parse-floats"] ``` -------------------------------- ### no_std Usage Source: https://github.com/alexhuszagh/rust-lexical/blob/main/lexical-util/README.md Details on how to configure and use `lexical-core` in a `no_std` environment by managing features in `Cargo.toml`. ```APIDOC ## no_std Environment `lexical-core` is designed to work in `no_std` environments without depending on the standard library or a system allocator. To enable this, configure your `Cargo.toml`: ```toml [dependencies.lexical-core] version = "1.0.0" default-features = false # Select only the features you need, e.g.: features = ["write-integers", "write-floats", "parse-integers", "parse-floats"] ``` Usage in a `no_std` environment is similar to the standard usage, focusing on byte slices for input and output: ```rust // Constant for the maximum number of bytes a formatter will write. use lexical_core::BUFFER_SIZE; let mut buffer = [b'0'; BUFFER_SIZE]; // Number to string conversion using a byte buffer. let count = lexical_core::write(3.0, &mut buffer); assert_eq!(buffer[..count], b"3.0"); let count = lexical_core::write(3i32, &mut buffer); assert_eq!(buffer[..count], b"3"); // String to number conversion from a byte slice. let i: i32 = lexical_core::parse(b"3")?; let f: f32 = lexical_core::parse(b"3.5")?; let d: f64 = lexical_core::parse(b"3.5")?; let d: f64 = lexical_core::parse(b"3a")?; // This will return Err(Error(_)) ``` ``` -------------------------------- ### no_std Usage: Number to String and String to Number Source: https://github.com/alexhuszagh/rust-lexical/blob/main/lexical/README.md Demonstrates `lexical-core` usage in a `no_std` environment with byte slices as input and output. It shows formatting numbers into a byte buffer and parsing byte slices into numbers. ```rust // A constant for the maximum number of bytes a formatter will write. use lexical_core::BUFFER_SIZE; let mut buffer = [b'0'; BUFFER_SIZE]; // Number to string. The underlying buffer must be a slice of bytes. let count = lexical_core::write(3.0, &mut buffer); assert_eq!(buffer[..count], b"3.0"); let count = lexical_core::write(3i32, &mut buffer); assert_eq!(buffer[..count], b"3"); // String to number. The input must be a slice of bytes. let i: i32 = lexical_core::parse(b"3")?; let f: f32 = lexical_core::parse(b"3.5")?; let d: f64 = lexical_core::parse(b"3.5")?; let d: f64 = lexical_core::parse(b"3a")?; ``` -------------------------------- ### no_std Usage: Number to String and String to Number Source: https://github.com/alexhuszagh/rust-lexical/blob/main/README.md Demonstrates `lexical-core` usage in a `no_std` environment, emphasizing the use of byte slices for input/output and the `BUFFER_SIZE` constant for buffer allocation. ```rust // A constant for the maximum number of bytes a formatter will write. use lexical_core::BUFFER_SIZE; let mut buffer = [b'0'; BUFFER_SIZE]; // Number to string. The underlying buffer must be a slice of bytes. let count = lexical_core::write(3.0, &mut buffer); assert_eq!(buffer[..count], b"3.0"); let count = lexical_core::write(3i32, &mut buffer); assert_eq!(buffer[..count], b"3"); // String to number. The input must be a slice of bytes. let i: i32 = lexical_core::parse(b"3")?; let f: f32 = lexical_core::parse(b"3.5")?; let d: f64 = lexical_core::parse(b"3.5")?; let d: f64 = lexical_core::parse(b"3a")?; ``` -------------------------------- ### Algorithm M Naive Implementation Source: https://github.com/alexhuszagh/rust-lexical/blob/main/lexical-parse-float/docs/Algorithm.md A naive Python implementation of Algorithm M, which uses arbitrary-precision integers to represent the significant digits of a float as a fraction. This is useful for understanding the core logic before optimization. ```python def algorithm_m(num, b): # Ensure numerator >= 2**52 bits = int(math.ceil(math.log2(num))) if bits <= 53: num <<= 53 b -= 53 # Track number of steps required (optional). steps = 0 while True: steps += 1 c = num//b if c < 2**52: b //= 2 elif c >= 2**53: b *= 2 else: break return (num, b, steps-1) ``` -------------------------------- ### Basic Usage: Number to String and String to Number Source: https://github.com/alexhuszagh/rust-lexical/blob/main/lexical/README.md Demonstrates fundamental usage of lexical for converting numbers to strings and vice versa. The `write` function formats numbers into a byte buffer, while `parse` converts byte slices to numbers with automatic type deduction. ```rust // Number to string use lexical_core::BUFFER_SIZE; let mut buffer = [b'0'; BUFFER_SIZE]; lexical_core::write(3.0, &mut buffer); // "3.0", always has a fraction suffix, lexical_core::write(3, &mut buffer); // "3" // String to number. let i: i32 = lexical_core::parse("3")?; let f: f32 = lexical_core::parse("3.5")?; let d: f64 = lexical_core::parse("3.5")?; let d: f64 = lexical_core::parse("3a")?; ``` -------------------------------- ### no_std: Write with Options to Buffer Source: https://context7.com/alexhuszagh/rust-lexical/llms.txt Demonstrates writing a floating-point number to a fixed buffer using custom options in a `no_std` environment. The buffer size is determined at compile time using `buffer_size_const`. ```rust use std::num; use lexical_core::{WriteFloatOptions, FormattedSize, ToLexicalWithOptions, format}; // ── no_std: write_with_options to a fixed buffer ───────────────────────────── const BUF_SIZE: usize = EU_OPTS.buffer_size_const::(); let mut buf = [0u8; BUF_SIZE]; let written = 9876.5f64.to_lexical_with_options::<{ format::STANDARD }>(&mut buf, &EU_OPTS); assert_eq!(core::str::from_utf8(written).unwrap(), "9876,5"); ``` -------------------------------- ### Parse with JSON Options Source: https://github.com/alexhuszagh/rust-lexical/blob/main/lexical/README.md Demonstrates parsing a float string using specific JSON formatting options. Errors will occur if the input does not conform to the specified format. ```rust const JSON: u128 = lexical_core::format::JSON; const OPTIONS: ParseFloatOptions = ParseFloatOptions::new(); let f: f64 = lexical_core::parse_with_options::<_, JSON>(b"3.0e7", &OPTIONS)?; ``` ```rust let f: f64 = lexical_core::parse_with_options::<_, JSON>(b"3.e7", &OPTIONS)?; ``` -------------------------------- ### Baseline Main Function for Size Detection Source: https://github.com/alexhuszagh/rust-lexical/blob/main/extras/size/README.md This baseline `main` function reads a line from stdin, trims it, and prints its length. It's used to ensure that the conversion routines are not optimized out by the compiler. Ensure `std::io::BufRead` is imported. ```rust use std::io::BufRead; pub fn main() { println!("{}", std::io::stdin() .lock() .lines() .next() .unwrap() .unwrap() .trim() .len() ); } ``` -------------------------------- ### Partial Parsers: Flexible Parsing Source: https://github.com/alexhuszagh/rust-lexical/blob/main/README.md Shows how partial parsers work by parsing as many valid digits as possible from the input. They return a tuple containing the parsed value and the count of digits successfully parsed. ```rust // This will return Ok((123, 3)), indicating that 3 digits were successfully // parsed, and that the returned value is `123`. let (x, count): (i32, usize) = lexical_core::parse_partial(b"123 456")?; ``` -------------------------------- ### Complete vs. Partial Parsers Source: https://github.com/alexhuszagh/rust-lexical/blob/main/lexical-util/README.md Explains the difference between complete parsers (which require the entire input to be valid) and partial parsers (which parse as much as possible and return the count of parsed digits). ```APIDOC ## Partial/Complete Parsers Lexical provides both complete and partial parsers for handling numeric strings. ### Complete Parsers Complete parsers ensure that the entire input buffer is consumed and valid. If any part of the input is invalid, an error is returned with the index of the first invalid character. ```rust // This will return Err(Error::InvalidDigit(3)), indicating // the first invalid character occurred at the index 3 in the input // string (the space character). let x: i32 = lexical_core::parse(b"123 456")?; ``` ### Partial Parsers Partial parsers parse as many valid numeric characters as possible from the beginning of the input. They return a tuple containing the parsed value and the number of digits successfully parsed. ```rust // This will return Ok((123, 3)), indicating that 3 digits were successfully // parsed, and that the returned value is `123`. let (x, count): (i32, usize) = lexical_core::parse_partial(b"123 456")?; ``` ```