Created
January 11, 2017 17:45
-
-
Save paul-english/0c82aca73008066f9d723b8bf419d75d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn int_from_bytes(x: &[u8]) -> i64 { | |
from_utf8(x).unwrap().parse::<i64>().unwrap() | |
} | |
pub fn float_from_bytes(x: &[u8]) -> f64 { | |
from_utf8(x).unwrap().parse::<f64>().unwrap() | |
} | |
named!(pub number<&[u8], i64>, | |
do_parse!(opt!(multispace) >> | |
n: digit >> | |
opt!(complete!(multispace)) >> | |
(int_from_bytes(n)))); | |
named!(pub negative_number<&[u8], i64>, | |
do_parse!(negative: opt!(tag!("-")) >> | |
n: digit >> | |
({ | |
let number = int_from_bytes(n); | |
match negative { | |
Some(_) => -number, | |
None => number | |
} | |
}))); | |
named!(real_exponent<&[u8], ()>, | |
complete!(do_parse!(tag_no_case!("e") >> negative_number >> ()))); | |
named!(pub real<&[u8], f64>, | |
do_parse!(n: recognize!(do_parse!(opt!(tag!("-")) >> | |
alt_complete!(do_parse!(digit >> tag!(".") >> digit >> opt!(real_exponent) >> ()) | |
| do_parse!(digit >> tag!(".") >> opt!(real_exponent) >> ()) | |
| do_parse!(tag!(".") >> digit >> opt!(real_exponent) >> ()) | |
| do_parse!(digit >> real_exponent >> ()) | |
) >> ())) >> | |
(float_from_bytes(n)))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment