From 8be89649f3623e80cfe9f357750ae9d90b77200f Mon Sep 17 00:00:00 2001 From: Matt Bilker Date: Thu, 24 Oct 2019 17:53:52 +0000 Subject: [PATCH] types(string): integer values can be specified with hexadecimal notation --- src/types/string.rs | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/types/string.rs b/src/types/string.rs index 149a4b5..dfd1550 100644 --- a/src/types/string.rs +++ b/src/types/string.rs @@ -66,7 +66,7 @@ impl FromKbinString for Ipv4Addr { } } -macro_rules! basic_parse { +macro_rules! basic_int_parse { ( $($type:ty),*$(,)? ) => { @@ -75,7 +75,33 @@ macro_rules! basic_parse { fn from_kbin_string(input: &str) -> Result { space_check(input)?; - input.parse::<$type>().context(KbinErrorKind::StringParse(stringify!($type))).map_err(Into::into) + if input.starts_with("0x") { + <$type>::from_str_radix(&input[2..], 16) + .context(KbinErrorKind::StringParse(stringify!($type))) + .map_err(Into::into) + } else { + input.parse::<$type>() + .context(KbinErrorKind::StringParse(stringify!($type))) + .map_err(Into::into) + } + } + } + )* + }; +} + +macro_rules! basic_float_parse { + ( + $($type:ty),*$(,)? + ) => { + $( + impl FromKbinString for $type { + fn from_kbin_string(input: &str) -> Result { + space_check(input)?; + + input.parse::<$type>() + .context(KbinErrorKind::StringParse(stringify!($type))) + .map_err(Into::into) } } )* @@ -124,11 +150,14 @@ macro_rules! tuple_parse { }; } -basic_parse! { +basic_int_parse! { i8, u8, i16, u16, i32, u32, i64, u64, +} + +basic_float_parse! { f32, f64, }