From 33f3c52d32e6f91494bc305fd25f9d5ae5a11702 Mon Sep 17 00:00:00 2001 From: Toby Scrace Date: Sun, 3 Jan 2016 20:08:53 +0000 Subject: 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 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/num/dec2flt/parse.rs b/src/libcore/num/dec2flt/parse.rs index 414bcc874ea..fce1c250a02 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..]), -- cgit 1.4.1-3-g733a5