[librsvg/rustification] strtod.rs: Don't consume the 'e' in strtod("42em")



commit a043810fbfd178c2a2db3749a129b89f9095855d
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Nov 10 18:49:10 2016 -0600

    strtod.rs: Don't consume the 'e' in strtod("42em")
    
    If the character after the 'e' is not an exponent, consider it part of
    the substring after the parsed number.  This way "em" and "ex"
    lengths will be parsed properly.

 rust/src/strtod.rs |   17 ++++++++++++++++-
 1 files changed, 16 insertions(+), 1 deletions(-)
---
diff --git a/rust/src/strtod.rs b/rust/src/strtod.rs
index 787ea69..486f658 100644
--- a/rust/src/strtod.rs
+++ b/rust/src/strtod.rs
@@ -70,7 +70,10 @@ pub fn strtod (string: &str) -> (f64, &str) {
                     state = State::Exponent;
                 } else if c.is_digit (10) {
                     exponent = (c as i32 - '0' as i32) as f64;
+                    state = State::Exponent;
                 } else {
+                    // un-consume the 'e' or 'E', so strtod("42em") produces the expected value.
+                    last_pos -= 1;
                     break;
                 }
             },
@@ -126,8 +129,17 @@ mod tests {
         let str = "-.25foo";
         assert_eq! (strtod (str), (-0.25f64, &str[4..]));
 
-        let str = ".25";
+        let str = ".25bar";
         assert_eq! (strtod (str), (0.25f64, &str[3..]));
+
+        let str = "22.5em";
+        assert_eq! (strtod (str), (22.5, "em"));
+
+        let str = "22.5e1Ex";
+        assert_eq! (strtod (str), (225.0, "Ex"));
+
+        let str = "22.5Ex";
+        assert_eq! (strtod (str), (22.5, "Ex"));
     }
 
     #[test]
@@ -146,6 +158,9 @@ mod tests {
 
         let str = "123.45E2";
         assert_eq! (strtod (str), (12345.0, &str[8..]));
+
+        let str = "123.45E10";
+        assert_eq! (strtod (str), (1234500000000.0, &str[9..]));
     }
 
     #[test]


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]