From 63d2857856fce6104b65e58a2ccd893732ebc35d Mon Sep 17 00:00:00 2001 From: Alex Huszagh Date: Thu, 10 Mar 2022 11:28:02 -0600 Subject: [PATCH] Added `from_radix` functions to simplify creating options. - Added `from_radix` for options in `lexical-write-float` and `lexical-parse-float`. --- lexical-parse-float/src/options.rs | 15 +++++++++++++++ lexical-write-float/src/options.rs | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lexical-parse-float/src/options.rs b/lexical-parse-float/src/options.rs index 88f1a70d..c05bf547 100644 --- a/lexical-parse-float/src/options.rs +++ b/lexical-parse-float/src/options.rs @@ -340,6 +340,21 @@ impl Options { unsafe { Self::builder().build_unchecked() } } + /// Create the default options for a given radix. + #[inline(always)] + #[cfg(feature = "power-of-two")] + pub const fn from_radix(radix: u8) -> Self { + // Need to determine the correct exponent character ('e' or '^'), + // since the default character is `e` normally, but this is a valid + // digit for radix >= 15. + let mut builder = Self::builder(); + if radix >= 15 { + builder = builder.exponent(b'^'); + } + // SAFETY: always safe since it uses the default arguments. + unsafe { builder.build_unchecked() } + } + // GETTERS /// Check if the options state is valid. diff --git a/lexical-write-float/src/options.rs b/lexical-write-float/src/options.rs index f33b6b01..a12cc7bc 100644 --- a/lexical-write-float/src/options.rs +++ b/lexical-write-float/src/options.rs @@ -413,6 +413,21 @@ impl Options { unsafe { Self::builder().build_unchecked() } } + /// Create the default options for a given radix. + #[inline(always)] + #[cfg(feature = "power-of-two")] + pub const fn from_radix(radix: u8) -> Self { + // Need to determine the correct exponent character ('e' or '^'), + // since the default character is `e` normally, but this is a valid + // digit for radix >= 15. + let mut builder = Self::builder(); + if radix >= 15 { + builder = builder.exponent(b'^'); + } + // SAFETY: always safe since it uses the default arguments. + unsafe { builder.build_unchecked() } + } + // GETTERS /// Check if the options state is valid.