Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional format versions and enhancements. #215

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
31951b0
Add support for requiring integer or fraction digits with exponents.
Alexhuszagh Jan 12, 2025
0489452
Add logic for formats that may support only a few conversions.
Alexhuszagh Jan 12, 2025
b7cafa2
Add support for requiring base prefixes and suffixes.
Alexhuszagh Jan 12, 2025
743fa5e
Add number format flag for requiring significant digits with exponents.
Alexhuszagh Jan 13, 2025
e5a4f09
Add `NumberFormatBuilder::none()` method.
Alexhuszagh Jan 13, 2025
0bc4df4
Improve parsing of sign characters.
Alexhuszagh Jan 13, 2025
df3d60a
Add in minor optimizations for parsing integers.
Alexhuszagh Jan 13, 2025
ff1c832
Add in many more digit separator flags.
Alexhuszagh Jan 14, 2025
236a00a
Add in our implementation for parsing signs with digit separators.
Alexhuszagh Jan 15, 2025
82ac816
Migrate to using raw digits to simplify our APIs.
Alexhuszagh Jan 17, 2025
dab5e53
Improve parsing floats to use our better base prefix logic.
Alexhuszagh Jan 17, 2025
5b3c720
Turn off LTO for benches.
Alexhuszagh Jan 19, 2025
79287ac
Add in heavy optimizations for parsing integers.
Alexhuszagh Jan 19, 2025
b8d2f48
Improve parsing of base prefixes and suffixes.
Alexhuszagh Jan 22, 2025
b655c04
Add explicit tests that using leading zeros works with digit separato…
Alexhuszagh Jan 22, 2025
55f53bf
Remove `supports_parsing_*` and `supports_writing_*`.
Alexhuszagh Jan 23, 2025
1b955e2
Adds shell of logic for toggling sign logic.
Alexhuszagh Jan 24, 2025
a023bef
Add minor patches for the correct indexes on format errors in parsing…
Alexhuszagh Jan 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `build_checked` to our `Options` API (#204).
- Added `has_digit_separator` to `NumberFormat` (#204).
- Re-export `NumberFormat` to our other crates (#204).
- Add `Options::from_radix` for all options for similar APIs for each (#208).
- Added `Options::from_radix` for all options for similar APIs for each (#208).
- Support for `required_integer_digits_with_exponent`, `required_fraction_digits_with_exponent`, and `required_mantissa_digits_with_exponent`, that is,`1.e5` and `.1e5`, as opposed to just requiring`1e5` (#215).
- Added `required_base_prefix` and `required_base_suffix` for our number formats, requiring base prefixes and/or suffixes when parsing, and allowing writing base prefixes and/or suffixes (#215).
- Added `NumberFormatBuilder::none()` for create a format with no flags set (#215).
- Added in many more digit separator flags for the `NumberFormat`, including for signs, base prefixes, base suffixes, and restricting digit separators at the start of the number (#215).
- Added many more pre-defined formatting constants (#215).
- Added additional sign configurations, with `no_unsigned_negative_sign`, `no_mantissa_sign`, and `no_exponent_sign` (#215).

### Changed

Expand Down
1 change: 0 additions & 1 deletion extras/benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ resolver = "2"
opt-level = 3
debug = false
debug-assertions = false
lto = true
6 changes: 6 additions & 0 deletions lexical-parse-float/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ macro_rules! float_from_lexical {
options: &Self::Options,
) -> lexical_util::result::Result<(Self, usize)>
{
let format = NumberFormat::<{ FORMAT }> {};
if !format.is_valid() {
return Err(format.error());
} else if !is_valid_options_punctuation(FORMAT, options.exponent(), options.decimal_point()) {
return Err(Error::InvalidPunctuation);
}
Self::parse_partial::<FORMAT>(bytes, options)
}
}
Expand Down
14 changes: 8 additions & 6 deletions lexical-parse-float/src/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn binary<F: RawFloat, const FORMAT: u128>(num: &Number, lossy: bool) -> Ext
#[cfg_attr(not(feature = "compact"), inline(always))]
#[allow(unused_mut)]
pub fn parse_u64_digits<'a, Iter, const FORMAT: u128>(
mut iter: Iter,
iter: &mut Iter,
mantissa: &mut u64,
step: &mut usize,
overflowed: &mut bool,
Expand All @@ -118,7 +118,7 @@ pub fn parse_u64_digits<'a, Iter, const FORMAT: u128>(
debug_assert!(radix < 16, "larger radices will wrap on radix^8");
let radix8 = format.radix8() as u64;
while *step > 8 {
if let Some(v) = algorithm::try_parse_8digits::<u64, _, FORMAT>(&mut iter) {
if let Some(v) = algorithm::try_parse_8digits::<u64, _, FORMAT>(iter) {
*mantissa = mantissa.wrapping_mul(radix8).wrapping_add(v);
*step -= 8;
} else {
Expand Down Expand Up @@ -169,9 +169,10 @@ pub fn slow_binary<F: RawFloat, const FORMAT: u128>(num: Number) -> ExtendedFloa
// Parse the integer digits.
let mut step = u64_step(radix);
let mut integer = num.integer.bytes::<FORMAT>();
integer.integer_iter().skip_zeros();
let mut integer_iter = integer.integer_iter();
integer_iter.skip_zeros();
parse_u64_digits::<_, FORMAT>(
integer.integer_iter(),
&mut integer_iter,
&mut mantissa,
&mut step,
&mut overflow,
Expand All @@ -181,11 +182,12 @@ pub fn slow_binary<F: RawFloat, const FORMAT: u128>(num: Number) -> ExtendedFloa
// Parse the fraction digits.
if let Some(fraction) = num.fraction {
let mut fraction = fraction.bytes::<FORMAT>();
let mut fraction_iter = fraction.fraction_iter();
if mantissa == 0 {
fraction.fraction_iter().skip_zeros();
fraction_iter.skip_zeros();
}
parse_u64_digits::<_, FORMAT>(
fraction.fraction_iter(),
&mut fraction_iter,
&mut mantissa,
&mut step,
&mut overflow,
Expand Down
11 changes: 4 additions & 7 deletions lexical-parse-float/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
//! [Comprehensive Configuration]: #comprehensive-configuration
//!
//! ```rust
//! # #[cfg(feature = "radix")] {
//! # #[cfg(feature = "format")] {
//! # use core::str;
//! use lexical_parse_float::{Error, FromLexicalWithOptions, NumberFormatBuilder, Options};
//!
Expand All @@ -78,7 +78,7 @@
//!
//! let value = "1.234e+300";
//! let result = f64::from_lexical_with_options::<FORMAT>(value.as_bytes(), &OPTIONS);
//! assert_eq!(result, Err(Error::MissingSign(0)));
//! assert_eq!(result, Err(Error::MissingMantissaSign(0)));
//! # }
//! ```
//!
Expand Down Expand Up @@ -132,7 +132,7 @@
//! }
//!
//! assert_eq!(parse_json_float(b"-1"), Ok(-1.0));
//! assert_eq!(parse_json_float(b"+1"), Err(Error::InvalidPositiveSign(0)));
//! assert_eq!(parse_json_float(b"+1"), Err(Error::InvalidPositiveMantissaSign(0)));
//! assert_eq!(parse_json_float(b"1"), Ok(1.0));
//! assert_eq!(parse_json_float(b"1."), Err(Error::EmptyFraction(2)));
//! assert_eq!(parse_json_float(b"0.1"), Ok(0.1));
Expand Down Expand Up @@ -193,7 +193,7 @@
//! assert_eq!(value.map(|x| x.is_nan()), Ok(true));
//!
//! let value = f64::from_lexical_with_options::<FORMAT>(b"+1_2.3_4", &OPTIONS);
//! assert_eq!(value, Err(Error::InvalidPositiveSign(0)));
//! assert_eq!(value, Err(Error::InvalidPositiveMantissaSign(0)));
//!
//! let value = f64::from_lexical_with_options::<FORMAT>(b"0.3_4", &OPTIONS);
//! assert_eq!(value, Ok(0.34));
Expand Down Expand Up @@ -566,9 +566,6 @@ mod table_lemire;
mod table_radix;
mod table_small;

#[macro_use(parse_sign)]
extern crate lexical_parse_integer;

// Re-exports
#[cfg(feature = "f16")]
pub use lexical_util::bf16::bf16;
Expand Down
Loading
Loading