Skip to content

Commit

Permalink
Adds shell of logic for toggling sign logic.
Browse files Browse the repository at this point in the history
This enables toggling no unsigned integer negative signs, and having no signs (even negative) for mantissas and exponents. This commit is broken and will not work for the new additions in parsing.
  • Loading branch information
Alexhuszagh committed Jan 24, 2025
1 parent 85059f6 commit 36599c3
Show file tree
Hide file tree
Showing 6 changed files with 545 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 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
16 changes: 16 additions & 0 deletions lexical-util/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ pub enum Error {
EmptyFraction(usize),
/// Invalid positive mantissa sign was found.
InvalidPositiveMantissaSign(usize),
/// Invalid negative mantissa sign was found.
InvalidNegativeMantissaSign(usize),
/// Mantissa sign was required(usize), but not found.
MissingMantissaSign(usize),
/// Exponent was present but not allowed.
InvalidExponent(usize),
/// Invalid positive exponent sign was found.
InvalidPositiveExponentSign(usize),
/// Invalid negative exponent sign was found.
InvalidNegativeExponentSign(usize),
/// Exponent sign was required(usize), but not found.
MissingExponentSign(usize),
/// Exponent was present without fraction component.
Expand Down Expand Up @@ -159,9 +163,11 @@ impl Error {
Self::EmptyInteger(_) => "'invalid float with no integer digits'",
Self::EmptyFraction(_) => "'invalid float with no fraction digits'",
Self::InvalidPositiveMantissaSign(_) => "'invalid `+` sign before significant digits'",
Self::InvalidNegativeMantissaSign(_) => "'invalid `-` sign before significant digits'",
Self::MissingMantissaSign(_) => "'missing required `+/-` sign for significant digits'",
Self::InvalidExponent(_) => "'exponent found but not allowed'",
Self::InvalidPositiveExponentSign(_) => "'invalid `+` sign in exponent'",
Self::InvalidNegativeExponentSign(_) => "'invalid `-` sign in exponent'",
Self::MissingExponentSign(_) => "'missing required `+/-` sign for exponent'",
Self::ExponentWithoutFraction(_) => "'invalid float containing exponent without fraction'",
Self::ExponentWithoutIntegerDigits(_) => "'invalid float containing exponent without integer digits'",
Expand Down Expand Up @@ -227,9 +233,11 @@ impl Error {
Self::EmptyInteger(index) => Some(index),
Self::EmptyFraction(index) => Some(index),
Self::InvalidPositiveMantissaSign(index) => Some(index),
Self::InvalidNegativeMantissaSign(index) => Some(index),
Self::MissingMantissaSign(index) => Some(index),
Self::InvalidExponent(index) => Some(index),
Self::InvalidPositiveExponentSign(index) => Some(index),
Self::InvalidNegativeExponentSign(index) => Some(index),
Self::MissingExponentSign(index) => Some(index),
Self::ExponentWithoutFraction(index) => Some(index),
Self::ExponentWithoutIntegerDigits(index) => Some(index),
Expand Down Expand Up @@ -290,9 +298,11 @@ impl Error {
is_error_type!(is_empty_integer, EmptyInteger(_));
is_error_type!(is_empty_fraction, EmptyFraction(_));
is_error_type!(is_invalid_positive_mantissa_sign, InvalidPositiveMantissaSign(_));
is_error_type!(is_invalid_negative_mantissa_sign, InvalidNegativeMantissaSign(_));
is_error_type!(is_missing_mantissa_sign, MissingMantissaSign(_));
is_error_type!(is_invalid_exponent, InvalidExponent(_));
is_error_type!(is_invalid_positive_exponent_sign, InvalidPositiveExponentSign(_));
is_error_type!(is_invalid_negative_exponent_sign, InvalidNegativeExponentSign(_));
is_error_type!(is_missing_exponent_sign, MissingExponentSign(_));
is_error_type!(is_exponent_without_fraction, ExponentWithoutFraction(_));
is_error_type!(is_invalid_leading_zeros, InvalidLeadingZeros(_));
Expand Down Expand Up @@ -380,11 +390,17 @@ impl fmt::Display for Error {
Self::InvalidPositiveMantissaSign(index) => {
write_parse_error!(formatter, description, index)
},
Self::InvalidNegativeMantissaSign(index) => {
write_parse_error!(formatter, description, index)
},
Self::MissingMantissaSign(index) => write_parse_error!(formatter, description, index),
Self::InvalidExponent(index) => write_parse_error!(formatter, description, index),
Self::InvalidPositiveExponentSign(index) => {
write_parse_error!(formatter, description, index)
},
Self::InvalidNegativeExponentSign(index) => {
write_parse_error!(formatter, description, index)
},
Self::MissingExponentSign(index) => write_parse_error!(formatter, description, index),
Self::ExponentWithoutFraction(index) => {
write_parse_error!(formatter, description, index)
Expand Down
91 changes: 91 additions & 0 deletions lexical-util/src/feature_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,97 @@ impl<const FORMAT: u128> NumberFormat<FORMAT> {
Self::REQUIRED_BASE_SUFFIX
}

/// If a negative sign before an unsigned integer is not allowed.
///
/// See [`no_unsigned_negative_sign`][Self::no_unsigned_negative_sign].
pub const NO_UNSIGNED_NEGATIVE_SIGN: bool = from_flag!(FORMAT, NO_UNSIGNED_NEGATIVE_SIGN);

/// If a negative sign before an unsigned integer is not allowed.
///
/// Can only be modified with [`feature`][crate#features] `format`. This does
/// not apply to signed integers or floating point numbers. Defaults to [`true`].
///
/// # Examples
///
/// | Input | Valid? |
/// |:-:|:-:|
/// | `-12` | ❌ |
/// | `+12` | ✔️ |
/// | `12` | ✔️ |
///
/// # Used For
///
/// - Parse Integer
#[inline(always)]
pub const fn no_unsigned_negative_sign(&self) -> bool {
Self::NO_UNSIGNED_NEGATIVE_SIGN
}

/// If positive or negative signs before the significant digits are not allowed.
///
/// See [`no_mantissa_sign`][Self::no_mantissa_sign].
pub const NO_MANTISSA_SIGN: bool = from_flag!(FORMAT, NO_MANTISSA_SIGN);

/// If positive or negative signs before the significant digits are not allowed.
///
/// Can only be modified with [`feature`][crate#features] `format`. if enabled, then
/// the type cannot represent negative literal or string values (although they may
/// be computed via mathematical operations). Defaults to [`false`].
///
/// If you only want to disable positive signs, see [`no_positive_mantissa_sign`].
/// If you wish to disable negative signs on unsigned integers, see
/// [`no_unsigned_negative_sign`].
///
/// [`no_positive_mantissa_sign`]: Self::no_positive_mantissa_sign
/// [`no_unsigned_negative_sign`]: Self::no_unsigned_negative_sign
///
/// # Examples
///
/// | Input | Valid? |
/// |:-:|:-:|
/// | `-12` | ❌ |
/// | `+12` | ❌ |
/// | `12` | ✔️ |
///
/// # Used For
///
/// - Parse Integer
/// - Parse Float
#[inline(always)]
pub const fn no_mantissa_sign(&self) -> bool {
Self::NO_MANTISSA_SIGN
}

/// If positive or negative signs before an exponent are not allowed.
///
/// See [`no_exponent_sign`][Self::no_exponent_sign].
pub const NO_EXPONENT_SIGN: bool = from_flag!(FORMAT, NO_EXPONENT_SIGN);

/// If positive or negative signs before an exponent are not allowed.
///
/// Can only be modified with [`feature`][crate#features] `format`. Defaults
/// to [`false`].
///
/// If you only want to disable positive signs, see [`no_positive_exponent_sign`].
///
/// [`no_positive_exponent_sign`]: Self::no_positive_exponent_sign
///
/// # Examples
///
/// | Input | Valid? |
/// |:-:|:-:|
/// | `1.0e-12` | ❌ |
/// | `1.0e+12` | ❌ |
/// | `1.0e12` | ✔️ |
///
/// # Used For
///
/// - Parse Float
#[inline(always)]
pub const fn no_exponent_sign(&self) -> bool {
Self::NO_EXPONENT_SIGN
}

// DIGIT SEPARATOR FLAGS & MASKS

/// If digit separators are allowed at the absolute start of the number.
Expand Down
Loading

0 comments on commit 36599c3

Please sign in to comment.