2016-10-08 20:47:27 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate nom;
|
|
|
|
|
2019-04-26 14:12:36 +00:00
|
|
|
use nom::character::streaming::digit1 as digit;
|
2016-10-08 20:47:27 +00:00
|
|
|
|
|
|
|
use std::str;
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
2018-02-17 14:06:43 +00:00
|
|
|
named!(
|
|
|
|
unsigned_float<f32>,
|
2016-10-08 20:47:27 +00:00
|
|
|
map_res!(
|
2018-02-17 14:06:43 +00:00
|
|
|
map_res!(
|
|
|
|
recognize!(alt!(
|
|
|
|
delimited!(digit, tag!("."), opt!(digit)) | delimited!(opt!(digit), tag!("."), digit)
|
|
|
|
)),
|
|
|
|
str::from_utf8
|
2016-10-08 20:47:27 +00:00
|
|
|
),
|
2018-02-17 14:06:43 +00:00
|
|
|
FromStr::from_str
|
|
|
|
)
|
|
|
|
);
|
2016-10-08 20:47:27 +00:00
|
|
|
|
2018-02-17 14:06:43 +00:00
|
|
|
named!(
|
|
|
|
float<f32>,
|
|
|
|
map!(
|
|
|
|
pair!(opt!(alt!(tag!("+") | tag!("-"))), unsigned_float),
|
|
|
|
|(sign, value): (Option<&[u8]>, f32)| sign
|
|
|
|
.and_then(|s| if s[0] == b'-' { Some(-1f32) } else { None })
|
2020-04-10 09:01:58 +00:00
|
|
|
.unwrap_or(1f32)
|
|
|
|
* value
|
2018-02-17 14:06:43 +00:00
|
|
|
)
|
|
|
|
);
|
2016-10-13 10:29:48 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unsigned_float_test() {
|
2018-01-15 15:51:29 +00:00
|
|
|
assert_eq!(unsigned_float(&b"123.456;"[..]), Ok((&b";"[..], 123.456)));
|
2018-02-17 14:06:43 +00:00
|
|
|
assert_eq!(unsigned_float(&b"0.123;"[..]), Ok((&b";"[..], 0.123)));
|
|
|
|
assert_eq!(unsigned_float(&b"123.0;"[..]), Ok((&b";"[..], 123.0)));
|
|
|
|
assert_eq!(unsigned_float(&b"123.;"[..]), Ok((&b";"[..], 123.0)));
|
|
|
|
assert_eq!(unsigned_float(&b".123;"[..]), Ok((&b";"[..], 0.123)));
|
2016-10-13 10:29:48 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 20:47:27 +00:00
|
|
|
#[test]
|
|
|
|
fn float_test() {
|
2018-02-17 14:06:43 +00:00
|
|
|
assert_eq!(float(&b"123.456;"[..]), Ok((&b";"[..], 123.456)));
|
2018-01-15 15:51:29 +00:00
|
|
|
assert_eq!(float(&b"+123.456;"[..]), Ok((&b";"[..], 123.456)));
|
|
|
|
assert_eq!(float(&b"-123.456;"[..]), Ok((&b";"[..], -123.456)));
|
2016-10-08 20:47:27 +00:00
|
|
|
}
|