diff options
| author | Toby Scrace <toby.scrace@gmail.com> | 2016-01-03 20:08:53 +0000 |
|---|---|---|
| committer | Toby Scrace <toby.scrace@gmail.com> | 2016-01-04 18:23:33 +0000 |
| commit | 33f3c52d32e6f91494bc305fd25f9d5ae5a11702 (patch) | |
| tree | 7fd8c5619b3d9b8d45374751d53681929a66c21f /src/libcore | |
| parent | 543bb03d3ec864cfe47a9afd761101ea95f628f6 (diff) | |
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.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/num/dec2flt/parse.rs | 17 |
1 files changed, 9 insertions, 8 deletions
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..]), |
