From 33f3c52d32e6f91494bc305fd25f9d5ae5a11702 Mon Sep 17 00:00:00 2001 From: Toby Scrace Date: Sun, 3 Jan 2016 20:08:53 +0000 Subject: [PATCH] Make float parsing "." return Err This makes both of the following return Err: ".".parse::() ".".parse::() This is a [breaking-change], which the libs team have classified as a bug fix. --- src/libcore/num/dec2flt/parse.rs | 17 +++++++++-------- src/libcoretest/num/dec2flt/mod.rs | 3 ++- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/libcore/num/dec2flt/parse.rs b/src/libcore/num/dec2flt/parse.rs index 414bcc874eac1..fce1c250a022e 100644 --- a/src/libcore/num/dec2flt/parse.rs +++ b/src/libcore/num/dec2flt/parse.rs @@ -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..]), diff --git a/src/libcoretest/num/dec2flt/mod.rs b/src/libcoretest/num/dec2flt/mod.rs index 0c92b2fe2a7dc..7b25333e21ed2 100644 --- a/src/libcoretest/num/dec2flt/mod.rs +++ b/src/libcoretest/num/dec2flt/mod.rs @@ -98,7 +98,8 @@ fn fast_path_correct() { #[test] fn lonely_dot() { - assert_eq!(".".parse(), Ok(0.0)); + assert!(".".parse::().is_err()); + assert!(".".parse::().is_err()); } #[test]