Skip to content

Commit

Permalink
Rollup merge of rust-lang#48235 - varkor:parse-float-lonely-exponent,…
Browse files Browse the repository at this point in the history
… r=alexcrichton

Make ".e0" not parse as 0.0

This forces floats to have either a digit before the separating point, or after. Thus `".e0"` is invalid like `"."`, when using `parse()`. Fixes rust-lang#40654. As mentioned in the issue, this is technically a breaking change... but clearly incorrect behaviour at present.
  • Loading branch information
kennytm committed Feb 25, 2018
2 parents 268b6d6 + c0e87f1 commit 0652af2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/libcore/num/dec2flt/parse.rs
Expand Up @@ -73,7 +73,8 @@ pub fn parse_decimal(s: &str) -> ParseResult {
}
Some(&b'.') => {
let (fractional, s) = eat_digits(&s[1..]);
if integral.is_empty() && fractional.is_empty() && s.is_empty() {
if integral.is_empty() && fractional.is_empty() {
// We require at least a single digit before or after the point.
return Invalid;
}

Expand Down
6 changes: 6 additions & 0 deletions src/libcore/tests/num/dec2flt/mod.rs
Expand Up @@ -101,6 +101,12 @@ fn lonely_dot() {
assert!(".".parse::<f64>().is_err());
}

#[test]
fn exponentiated_dot() {
assert!(".e0".parse::<f32>().is_err());
assert!(".e0".parse::<f64>().is_err());
}

#[test]
fn lonely_sign() {
assert!("+".parse::<f32>().is_err());
Expand Down

0 comments on commit 0652af2

Please sign in to comment.