[librsvg] parsers.rs: make the angle() nom parser number_and_units() instead
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg] parsers.rs: make the angle() nom parser number_and_units() instead
- Date: Tue, 28 Feb 2017 04:13:21 +0000 (UTC)
commit 48f48648d1db5735f257f60915ae463cf1a35e58
Author: Federico Mena Quintero <federico gnome org>
Date: Mon Feb 27 21:18:22 2017 -0600
parsers.rs: make the angle() nom parser number_and_units() instead
It can really extract any combination of a floating point number plus a
unit-like string. We'll use this for other purposes, too.
rust/src/parsers.rs | 29 +++++++++++++++++++----------
1 files changed, 19 insertions(+), 10 deletions(-)
---
diff --git a/rust/src/parsers.rs b/rust/src/parsers.rs
index 08f330a..0c96c16 100644
--- a/rust/src/parsers.rs
+++ b/rust/src/parsers.rs
@@ -80,23 +80,25 @@ named! (comma_wsp,
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct ParseAngleError;
-named! (angle<(f64, Option<&[u8]>)>,
+fn is_alphabetic_or_dash (c: u8) -> bool {
+ is_alphabetic (c) || c == '-' as u8
+}
+
+named! (number_and_units<(f64, &[u8])>,
tuple! (double,
- opt! (take_while! (is_alphabetic))));
+ take_while! (is_alphabetic_or_dash)));
pub fn angle_degrees (s: &str) -> Result <f64, ParseAngleError> {
- let r = angle (s.as_bytes ()).to_full_result ();
-
- println! ("took {:?} parsed {:?}", s, r);
+ let r = number_and_units (s.as_bytes ()).to_full_result ();
match r {
Ok ((value, unit)) => {
match unit {
- Some (b"deg") => Ok (value),
- Some (b"grad") => Ok (value * 360.0 / 400.0),
- Some (b"rad") => Ok (value * 180.0 / PI),
- Some (b"") => Ok (value),
- _ => Err (ParseAngleError)
+ b"deg" => Ok (value),
+ b"grad" => Ok (value * 360.0 / 400.0),
+ b"rad" => Ok (value * 180.0 / PI),
+ b"" => Ok (value),
+ _ => Err (ParseAngleError)
}
},
@@ -279,6 +281,13 @@ mod tests {
}
#[test]
+ fn parses_number_and_units () {
+ assert_eq! (number_and_units (b"-1"), IResult::Done (&b""[..], (-1.0, &b""[..])));
+ assert_eq! (number_and_units (b"0x"), IResult::Done (&b""[..], (0.0, &b"x"[..])));
+ assert_eq! (number_and_units (b"-55.5x-large"), IResult::Done (&b""[..], (-55.5, &b"x-large"[..])));
+ }
+
+ #[test]
fn parses_angle () {
assert_eq! (angle_degrees ("0"), Ok (0.0));
assert_eq! (angle_degrees ("15"), Ok (15.0));
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]