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 | |
| parent | 543bb03d3ec864cfe47a9afd761101ea95f628f6 (diff) | |
| download | rust-33f3c52d32e6f91494bc305fd25f9d5ae5a11702.tar.gz rust-33f3c52d32e6f91494bc305fd25f9d5ae5a11702.zip | |
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.
| -rw-r--r-- | src/libcore/num/dec2flt/parse.rs | 17 | ||||
| -rw-r--r-- | 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 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..]), diff --git a/src/libcoretest/num/dec2flt/mod.rs b/src/libcoretest/num/dec2flt/mod.rs index 0c92b2fe2a7..7b25333e21e 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::<f32>().is_err()); + assert!(".".parse::<f64>().is_err()); } #[test] |
