Skip to content

Commit 36599c3

Browse files
committed
Adds shell of logic for toggling sign logic.
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.
1 parent 85059f6 commit 36599c3

File tree

6 files changed

+545
-38
lines changed

6 files changed

+545
-38
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- Added `NumberFormatBuilder::none()` for create a format with no flags set (#215).
2222
- 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).
2323
- Added many more pre-defined formatting constants (#215).
24+
- Added additional sign configurations, with `no_unsigned_negative_sign`, `no_mantissa_sign`, and `no_exponent_sign` (#215).
2425

2526
### Changed
2627

lexical-util/src/error.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,16 @@ pub enum Error {
3232
EmptyFraction(usize),
3333
/// Invalid positive mantissa sign was found.
3434
InvalidPositiveMantissaSign(usize),
35+
/// Invalid negative mantissa sign was found.
36+
InvalidNegativeMantissaSign(usize),
3537
/// Mantissa sign was required(usize), but not found.
3638
MissingMantissaSign(usize),
3739
/// Exponent was present but not allowed.
3840
InvalidExponent(usize),
3941
/// Invalid positive exponent sign was found.
4042
InvalidPositiveExponentSign(usize),
43+
/// Invalid negative exponent sign was found.
44+
InvalidNegativeExponentSign(usize),
4145
/// Exponent sign was required(usize), but not found.
4246
MissingExponentSign(usize),
4347
/// Exponent was present without fraction component.
@@ -159,9 +163,11 @@ impl Error {
159163
Self::EmptyInteger(_) => "'invalid float with no integer digits'",
160164
Self::EmptyFraction(_) => "'invalid float with no fraction digits'",
161165
Self::InvalidPositiveMantissaSign(_) => "'invalid `+` sign before significant digits'",
166+
Self::InvalidNegativeMantissaSign(_) => "'invalid `-` sign before significant digits'",
162167
Self::MissingMantissaSign(_) => "'missing required `+/-` sign for significant digits'",
163168
Self::InvalidExponent(_) => "'exponent found but not allowed'",
164169
Self::InvalidPositiveExponentSign(_) => "'invalid `+` sign in exponent'",
170+
Self::InvalidNegativeExponentSign(_) => "'invalid `-` sign in exponent'",
165171
Self::MissingExponentSign(_) => "'missing required `+/-` sign for exponent'",
166172
Self::ExponentWithoutFraction(_) => "'invalid float containing exponent without fraction'",
167173
Self::ExponentWithoutIntegerDigits(_) => "'invalid float containing exponent without integer digits'",
@@ -227,9 +233,11 @@ impl Error {
227233
Self::EmptyInteger(index) => Some(index),
228234
Self::EmptyFraction(index) => Some(index),
229235
Self::InvalidPositiveMantissaSign(index) => Some(index),
236+
Self::InvalidNegativeMantissaSign(index) => Some(index),
230237
Self::MissingMantissaSign(index) => Some(index),
231238
Self::InvalidExponent(index) => Some(index),
232239
Self::InvalidPositiveExponentSign(index) => Some(index),
240+
Self::InvalidNegativeExponentSign(index) => Some(index),
233241
Self::MissingExponentSign(index) => Some(index),
234242
Self::ExponentWithoutFraction(index) => Some(index),
235243
Self::ExponentWithoutIntegerDigits(index) => Some(index),
@@ -290,9 +298,11 @@ impl Error {
290298
is_error_type!(is_empty_integer, EmptyInteger(_));
291299
is_error_type!(is_empty_fraction, EmptyFraction(_));
292300
is_error_type!(is_invalid_positive_mantissa_sign, InvalidPositiveMantissaSign(_));
301+
is_error_type!(is_invalid_negative_mantissa_sign, InvalidNegativeMantissaSign(_));
293302
is_error_type!(is_missing_mantissa_sign, MissingMantissaSign(_));
294303
is_error_type!(is_invalid_exponent, InvalidExponent(_));
295304
is_error_type!(is_invalid_positive_exponent_sign, InvalidPositiveExponentSign(_));
305+
is_error_type!(is_invalid_negative_exponent_sign, InvalidNegativeExponentSign(_));
296306
is_error_type!(is_missing_exponent_sign, MissingExponentSign(_));
297307
is_error_type!(is_exponent_without_fraction, ExponentWithoutFraction(_));
298308
is_error_type!(is_invalid_leading_zeros, InvalidLeadingZeros(_));
@@ -380,11 +390,17 @@ impl fmt::Display for Error {
380390
Self::InvalidPositiveMantissaSign(index) => {
381391
write_parse_error!(formatter, description, index)
382392
},
393+
Self::InvalidNegativeMantissaSign(index) => {
394+
write_parse_error!(formatter, description, index)
395+
},
383396
Self::MissingMantissaSign(index) => write_parse_error!(formatter, description, index),
384397
Self::InvalidExponent(index) => write_parse_error!(formatter, description, index),
385398
Self::InvalidPositiveExponentSign(index) => {
386399
write_parse_error!(formatter, description, index)
387400
},
401+
Self::InvalidNegativeExponentSign(index) => {
402+
write_parse_error!(formatter, description, index)
403+
},
388404
Self::MissingExponentSign(index) => write_parse_error!(formatter, description, index),
389405
Self::ExponentWithoutFraction(index) => {
390406
write_parse_error!(formatter, description, index)

lexical-util/src/feature_format.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,97 @@ impl<const FORMAT: u128> NumberFormat<FORMAT> {
820820
Self::REQUIRED_BASE_SUFFIX
821821
}
822822

823+
/// If a negative sign before an unsigned integer is not allowed.
824+
///
825+
/// See [`no_unsigned_negative_sign`][Self::no_unsigned_negative_sign].
826+
pub const NO_UNSIGNED_NEGATIVE_SIGN: bool = from_flag!(FORMAT, NO_UNSIGNED_NEGATIVE_SIGN);
827+
828+
/// If a negative sign before an unsigned integer is not allowed.
829+
///
830+
/// Can only be modified with [`feature`][crate#features] `format`. This does
831+
/// not apply to signed integers or floating point numbers. Defaults to [`true`].
832+
///
833+
/// # Examples
834+
///
835+
/// | Input | Valid? |
836+
/// |:-:|:-:|
837+
/// | `-12` | ❌ |
838+
/// | `+12` | ✔️ |
839+
/// | `12` | ✔️ |
840+
///
841+
/// # Used For
842+
///
843+
/// - Parse Integer
844+
#[inline(always)]
845+
pub const fn no_unsigned_negative_sign(&self) -> bool {
846+
Self::NO_UNSIGNED_NEGATIVE_SIGN
847+
}
848+
849+
/// If positive or negative signs before the significant digits are not allowed.
850+
///
851+
/// See [`no_mantissa_sign`][Self::no_mantissa_sign].
852+
pub const NO_MANTISSA_SIGN: bool = from_flag!(FORMAT, NO_MANTISSA_SIGN);
853+
854+
/// If positive or negative signs before the significant digits are not allowed.
855+
///
856+
/// Can only be modified with [`feature`][crate#features] `format`. if enabled, then
857+
/// the type cannot represent negative literal or string values (although they may
858+
/// be computed via mathematical operations). Defaults to [`false`].
859+
///
860+
/// If you only want to disable positive signs, see [`no_positive_mantissa_sign`].
861+
/// If you wish to disable negative signs on unsigned integers, see
862+
/// [`no_unsigned_negative_sign`].
863+
///
864+
/// [`no_positive_mantissa_sign`]: Self::no_positive_mantissa_sign
865+
/// [`no_unsigned_negative_sign`]: Self::no_unsigned_negative_sign
866+
///
867+
/// # Examples
868+
///
869+
/// | Input | Valid? |
870+
/// |:-:|:-:|
871+
/// | `-12` | ❌ |
872+
/// | `+12` | ❌ |
873+
/// | `12` | ✔️ |
874+
///
875+
/// # Used For
876+
///
877+
/// - Parse Integer
878+
/// - Parse Float
879+
#[inline(always)]
880+
pub const fn no_mantissa_sign(&self) -> bool {
881+
Self::NO_MANTISSA_SIGN
882+
}
883+
884+
/// If positive or negative signs before an exponent are not allowed.
885+
///
886+
/// See [`no_exponent_sign`][Self::no_exponent_sign].
887+
pub const NO_EXPONENT_SIGN: bool = from_flag!(FORMAT, NO_EXPONENT_SIGN);
888+
889+
/// If positive or negative signs before an exponent are not allowed.
890+
///
891+
/// Can only be modified with [`feature`][crate#features] `format`. Defaults
892+
/// to [`false`].
893+
///
894+
/// If you only want to disable positive signs, see [`no_positive_exponent_sign`].
895+
///
896+
/// [`no_positive_exponent_sign`]: Self::no_positive_exponent_sign
897+
///
898+
/// # Examples
899+
///
900+
/// | Input | Valid? |
901+
/// |:-:|:-:|
902+
/// | `1.0e-12` | ❌ |
903+
/// | `1.0e+12` | ❌ |
904+
/// | `1.0e12` | ✔️ |
905+
///
906+
/// # Used For
907+
///
908+
/// - Parse Float
909+
#[inline(always)]
910+
pub const fn no_exponent_sign(&self) -> bool {
911+
Self::NO_EXPONENT_SIGN
912+
}
913+
823914
// DIGIT SEPARATOR FLAGS & MASKS
824915

825916
/// If digit separators are allowed at the absolute start of the number.

0 commit comments

Comments
 (0)