Make float parsing "." return Err

This makes both of the following return Err:

    ".".parse::<f32>()
    ".".parse::<f64>()

This is a [breaking-change], which the libs team have classified as a
bug fix.
This commit is contained in:
Toby Scrace 2016-01-03 20:08:53 +00:00
parent 543bb03d3e
commit 33f3c52d32
2 changed files with 11 additions and 9 deletions

View File

@ -56,27 +56,28 @@ pub enum ParseResult<'a> {
/// Check if the input string is a valid floating point number and if so, locate the integral
/// part, the fractional part, and the exponent in it. Does not handle signs.
pub fn parse_decimal(s: &str) -> ParseResult {
if s.is_empty() {
return Invalid;
}
let s = s.as_bytes();
let (integral, s) = eat_digits(s);
match s.first() {
None => {
if integral.is_empty() {
return Invalid; // No digits at all
}
Valid(Decimal::new(integral, b"", 0))
}
None => Valid(Decimal::new(integral, b"", 0)),
Some(&b'e') | Some(&b'E') => {
if integral.is_empty() {
return Invalid; // No digits before 'e'
}
parse_exp(integral, b"", &s[1..])
}
Some(&b'.') => {
let (fractional, s) = eat_digits(&s[1..]);
if integral.is_empty() && fractional.is_empty() && s.is_empty() {
// For historic reasons "." is a valid input.
return Valid(Decimal::new(b"", b"", 0));
return Invalid;
}
match s.first() {
None => Valid(Decimal::new(integral, fractional, 0)),
Some(&b'e') | Some(&b'E') => parse_exp(integral, fractional, &s[1..]),

View File

@ -98,7 +98,8 @@ fn fast_path_correct() {
#[test]
fn lonely_dot() {
assert_eq!(".".parse(), Ok(0.0));
assert!(".".parse::<f32>().is_err());
assert!(".".parse::<f64>().is_err());
}
#[test]