about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2018-02-25 21:36:46 +0800
committerkennytm <kennytm@gmail.com>2018-02-25 21:36:46 +0800
commit0652af21b5b2cedacff71c6e613addbd8d17ec58 (patch)
tree704067324b813ed1ef3e3a0746bf6af5ff918424 /src/libcore
parent268b6d61894d535f8414eee923ac740d8d3fef81 (diff)
parentc0e87f13a4d2f6fcef92c8c01bdc8dda9e0549a5 (diff)
downloadrust-0652af21b5b2cedacff71c6e613addbd8d17ec58.tar.gz
rust-0652af21b5b2cedacff71c6e613addbd8d17ec58.zip
Rollup merge of #48235 - varkor:parse-float-lonely-exponent, 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 #40654. As mentioned in the issue, this is technically a breaking change... but clearly incorrect behaviour at present.
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/num/dec2flt/parse.rs3
-rw-r--r--src/libcore/tests/num/dec2flt/mod.rs6
2 files changed, 8 insertions, 1 deletions
diff --git a/src/libcore/num/dec2flt/parse.rs b/src/libcore/num/dec2flt/parse.rs
index d20986faa0f..69418434ebe 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 9934e1dab96..17b2f59cd4d 100644
--- a/src/libcore/tests/num/dec2flt/mod.rs
+++ b/src/libcore/tests/num/dec2flt/mod.rs
@@ -102,6 +102,12 @@ fn lonely_dot() {
 }
 
 #[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());
     assert!("-".parse::<f64>().is_err());