let error_str = match e.kind() {
IntErrorKind::PosOverflow => "`limit` is too large",
- IntErrorKind::Empty | IntErrorKind::OnlySign => {
- "`limit` must be a non-negative integer"
- }
+ IntErrorKind::Empty => "`limit` must be a non-negative integer",
IntErrorKind::InvalidDigit(_) => "not a valid integer",
IntErrorKind::NegOverflow => bug!("`limit` should never underflow"),
IntErrorKind::Zero => bug!("zero is a valid `limit`"),
/// Contains an digit invalid in its context.
///
/// 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")]
InvalidDigit(#[stable(feature = "int_error_matching", since = "1.47.0")] char),
/// Integer is too large to store in target integer type.
/// would be illegal for non-zero types.
#[stable(feature = "int_error_matching", since = "1.47.0")]
Zero,
- /// The value contains nothing other than sign `+` or `-`.
- #[stable(feature = "int_error_matching", since = "1.47.0")]
- OnlySign,
}
impl ParseIntError {
IntErrorKind::PosOverflow => "number too large 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::OnlySign => "only sign without digits found in string",
}
}
}
let src = src.as_bytes();
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'-' if is_signed_ty => (false, &src[1..]),
_ => (true, src),
};
- if digits.is_empty() {
- return Err(PIE { kind: OnlySign });
- }
-
let mut result = T::from_u32(0);
if is_positive {
// The number is positive
test_parse::<u8>("Съешь", Err(IntErrorKind::InvalidDigit('Ð')));
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::<u8>("-1", Err(IntErrorKind::InvalidDigit('-')));
}
#[test]
fn test_empty() {
- test_parse::<i8>("-", Err(IntErrorKind::OnlySign));
- test_parse::<i8>("+", Err(IntErrorKind::OnlySign));
test_parse::<u8>("", Err(IntErrorKind::Empty));
}