Skip to content

Commit

Permalink
Auto merge of #30681 - Toby-S:master, r=bluss
Browse files Browse the repository at this point in the history
Make `".".parse::<f32>()` and `".".parse::<f64>()` return Err

This fixes #30344.

This is a [breaking-change], which the libs team have classified as a
bug fix.
  • Loading branch information
bors committed Jan 4, 2016
2 parents 41611ba + 33f3c52 commit d5e2290
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
17 changes: 9 additions & 8 deletions src/libcore/num/dec2flt/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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..]),
Expand Down
3 changes: 2 additions & 1 deletion src/libcoretest/num/dec2flt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit d5e2290

Please sign in to comment.