strtod.rs 4.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
enum State {
    Whitespace,
    IntegralPart,
    FractionalPart,
    ExponentSign,
    Exponent
}

pub fn strtod (string: &str) -> (f64, &str) {
    let mut state = State::Whitespace;
    let mut value: f64 = 0.0;
    let mut sign: f64 = 1.0;
    let mut fraction: f64 = 1.0;
    let mut exponent_sign: f64 = 1.0;
    let mut exponent: f64 = 0.0;
    let mut last_pos: usize = 0;

    for (pos, c) in string.chars ().enumerate () {
        last_pos = pos;

        match state {
            State::Whitespace => {
                if c.is_whitespace () {
                    continue;
                } else if c == '+' || c == '-' {
                    if c == '-' {
                        sign = -1.0;
                    }

                    state = State::IntegralPart;
                } else if c.is_digit (10) {
                    state = State::IntegralPart;
                    value = (c as i32 - '0' as i32) as f64;
                } else if c == '.' {
                    state = State::FractionalPart;
                } else {
                    break;
                }
            },

            State::IntegralPart => {
                if c.is_digit (10) {
                    value = value * 10.0 + (c as i32 - '0' as i32) as f64;
                } else if c == '.' {
                    state = State::FractionalPart;
                } else if c == 'e' || c == 'E' {
                    state = State::ExponentSign;
                } else {
                    break;
                }
            },

            State::FractionalPart => {
                if c.is_digit (10) {
                    fraction *= 0.1;
                    value += fraction * (c as i32 - '0' as i32) as f64;
                } else if c == 'e' || c == 'E' {
                    state = State::ExponentSign;
                } else {
                    break;
                }
            },

            State::ExponentSign => {
                if c == '+' || c == '-' {
                    if c == '-' {
                        exponent_sign = -1.0;
                    }

                    state = State::Exponent;
                } else if c.is_digit (10) {
                    exponent = (c as i32 - '0' as i32) as f64;
73
                    state = State::Exponent;
74
                } else {
75 76
                    // un-consume the 'e' or 'E', so strtod("42em") produces the expected value.
                    last_pos -= 1;
77 78 79 80 81 82 83 84 85 86 87 88
                    break;
                }
            },

            State::Exponent => {
                if c.is_digit (10) {
                    exponent = exponent * 10.0 + (c as i32 - '0' as i32) as f64;
                } else {
                    break;
                }
            }
        }
89 90

        last_pos += 1;
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    }

    // return tuple with the value and the non-matching slice

    (sign * value * 10.0f64.powf (exponent * exponent_sign),
     &string[last_pos .. ])
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn handles_empty_string () {
        let str = "";
        assert_eq! (strtod (str), (0.0f64, &str[0..]));
    }

    #[test]
    fn handles_integer () {
        let str = "-123foo";
        assert_eq! (strtod (str), (-123.0f64, &str[4..]));

        let str = "12345";
        assert_eq! (strtod (str), (12345.0f64, &str[5..]));
    }

    #[test]
    fn handles_float () {
        let str = "-123.25";
        assert_eq! (strtod (str), (-123.25f64, &str[7..]));

        let str = "123.25bar";
        assert_eq! (strtod (str), (123.25f64, &str[6..]));
    }

    #[test]
    fn handles_dot_numbers () {
        let str = "-.25foo";
        assert_eq! (strtod (str), (-0.25f64, &str[4..]));

132
        let str = ".25bar";
133
        assert_eq! (strtod (str), (0.25f64, &str[3..]));
134 135 136 137 138 139 140 141 142

        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"));
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    }

    #[test]
    fn handles_dot () {
        let str = "-.";
        assert_eq! (strtod (str), (-0.0, &str[2..]));

        let str = ".bar";
        assert_eq! (strtod (str), (0.0, &str[1..]));
    }

    #[test]
    fn handles_exponent () {
        let str = "-123.45e2foo";
        assert_eq! (strtod (str), (-12345.0, &str[9..]));

        let str = "123.45E2";
        assert_eq! (strtod (str), (12345.0, &str[8..]));
161 162 163

        let str = "123.45E10";
        assert_eq! (strtod (str), (1234500000000.0, &str[9..]));
164 165 166 167 168 169 170 171 172 173 174
    }

    #[test]
    fn handles_negative_exponent () {
        let str = "-123.25e-2";
        assert_eq! (strtod (str), (-1.2325, &str[10..]));

        let str = "123.25E-2bar";
        assert_eq! (strtod (str), (1.2325, &str[9..]));
    }
}