Skip to content

Commit

Permalink
Added from_radix functions to simplify creating options.
Browse files Browse the repository at this point in the history
- Added `from_radix` for options in `lexical-write-float` and
  `lexical-parse-float`.
  • Loading branch information
Alexhuszagh committed Mar 10, 2022
1 parent efcb7f6 commit 63d2857
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lexical-parse-float/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions lexical-write-float/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 63d2857

Please sign in to comment.