From c0e87f13a4d2f6fcef92c8c01bdc8dda9e0549a5 Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 15 Feb 2018 15:54:40 +0000 Subject: [PATCH] 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()`. --- src/libcore/num/dec2flt/parse.rs | 3 ++- src/libcore/tests/num/dec2flt/mod.rs | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/libcore/num/dec2flt/parse.rs b/src/libcore/num/dec2flt/parse.rs index d20986faa0fc..69418434ebea 100644 --- a/src/libcore/num/dec2flt/parse.rs +++ b/src/libcore/num/dec2flt/parse.rs @@ -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; } diff --git a/src/libcore/tests/num/dec2flt/mod.rs b/src/libcore/tests/num/dec2flt/mod.rs index 9934e1dab966..17b2f59cd4df 100644 --- a/src/libcore/tests/num/dec2flt/mod.rs +++ b/src/libcore/tests/num/dec2flt/mod.rs @@ -101,6 +101,12 @@ fn lonely_dot() { assert!(".".parse::().is_err()); } +#[test] +fn exponentiated_dot() { + assert!(".e0".parse::().is_err()); + assert!(".e0".parse::().is_err()); +} + #[test] fn lonely_sign() { assert!("+".parse::().is_err());