1
Fork 0

remove OnlySign in favour of InvalidDigit

This commit is contained in:
Ethan Brierley 2020-10-06 22:42:33 +01:00
parent 8eaf0de1f4
commit 1e7e2e40e4
4 changed files with 11 additions and 14 deletions

View file

@ -49,9 +49,7 @@ fn update_limit(
let error_str = match e.kind() { let error_str = match e.kind() {
IntErrorKind::PosOverflow => "`limit` is too large", IntErrorKind::PosOverflow => "`limit` is too large",
IntErrorKind::Empty | IntErrorKind::OnlySign => { IntErrorKind::Empty => "`limit` must be a non-negative integer",
"`limit` must be a non-negative integer"
}
IntErrorKind::InvalidDigit(_) => "not a valid integer", IntErrorKind::InvalidDigit(_) => "not a valid integer",
IntErrorKind::NegOverflow => bug!("`limit` should never underflow"), IntErrorKind::NegOverflow => bug!("`limit` should never underflow"),
IntErrorKind::Zero => bug!("zero is a valid `limit`"), IntErrorKind::Zero => bug!("zero is a valid `limit`"),

View file

@ -95,7 +95,10 @@ pub enum IntErrorKind {
/// Contains an digit invalid in its context. /// Contains an digit invalid in its context.
/// ///
/// Among other causes, this variant will be constructed when parsing a string that /// Among other causes, this variant will be constructed when parsing a string that
/// contains a letter. /// contains a non-asci char.
///
/// This variant is also constructed when a `+` or `-` is misplaced within a sting
/// either on its own or in the middle of a number.
#[stable(feature = "int_error_matching", since = "1.47.0")] #[stable(feature = "int_error_matching", since = "1.47.0")]
InvalidDigit(#[stable(feature = "int_error_matching", since = "1.47.0")] char), InvalidDigit(#[stable(feature = "int_error_matching", since = "1.47.0")] char),
/// Integer is too large to store in target integer type. /// Integer is too large to store in target integer type.
@ -110,9 +113,6 @@ pub enum IntErrorKind {
/// would be illegal for non-zero types. /// would be illegal for non-zero types.
#[stable(feature = "int_error_matching", since = "1.47.0")] #[stable(feature = "int_error_matching", since = "1.47.0")]
Zero, Zero,
/// The value contains nothing other than sign `+` or `-`.
#[stable(feature = "int_error_matching", since = "1.47.0")]
OnlySign,
} }
impl ParseIntError { impl ParseIntError {
@ -135,7 +135,6 @@ impl ParseIntError {
IntErrorKind::PosOverflow => "number too large to fit in target type", IntErrorKind::PosOverflow => "number too large to fit in target type",
IntErrorKind::NegOverflow => "number too small to fit in target type", IntErrorKind::NegOverflow => "number too small to fit in target type",
IntErrorKind::Zero => "number would be zero for non-zero type", IntErrorKind::Zero => "number would be zero for non-zero type",
IntErrorKind::OnlySign => "only sign without digits found in string",
} }
} }
} }

View file

@ -830,15 +830,14 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
let src = src.as_bytes(); let src = src.as_bytes();
let (is_positive, digits) = match src[0] { let (is_positive, digits) = match src[0] {
b'+' | b'-' if src[1..].is_empty() => {
return Err(PIE { kind: InvalidDigit(src[0] as char) });
}
b'+' => (true, &src[1..]), b'+' => (true, &src[1..]),
b'-' if is_signed_ty => (false, &src[1..]), b'-' if is_signed_ty => (false, &src[1..]),
_ => (true, src), _ => (true, src),
}; };
if digits.is_empty() {
return Err(PIE { kind: OnlySign });
}
let mut result = T::from_u32(0); let mut result = T::from_u32(0);
if is_positive { if is_positive {
// The number is positive // The number is positive

View file

@ -122,12 +122,13 @@ fn test_invalid() {
test_parse::<u8>("Съешь", Err(IntErrorKind::InvalidDigit('Ð'))); test_parse::<u8>("Съешь", Err(IntErrorKind::InvalidDigit('Ð')));
test_parse::<u8>("123Hello", Err(IntErrorKind::InvalidDigit('H'))); test_parse::<u8>("123Hello", Err(IntErrorKind::InvalidDigit('H')));
test_parse::<i8>("--", Err(IntErrorKind::InvalidDigit('-'))); test_parse::<i8>("--", Err(IntErrorKind::InvalidDigit('-')));
test_parse::<i8>("-", Err(IntErrorKind::InvalidDigit('-')));
test_parse::<i8>("+", Err(IntErrorKind::InvalidDigit('+')));
test_parse::<u8>("-1", Err(IntErrorKind::InvalidDigit('-')));
} }
#[test] #[test]
fn test_empty() { fn test_empty() {
test_parse::<i8>("-", Err(IntErrorKind::OnlySign));
test_parse::<i8>("+", Err(IntErrorKind::OnlySign));
test_parse::<u8>("", Err(IntErrorKind::Empty)); test_parse::<u8>("", Err(IntErrorKind::Empty));
} }