Skip to content

Commit c5bae99

Browse files
committed
Improve the floating point implementation to not depend on the ParseTo trait
1 parent 35be971 commit c5bae99

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "nom-parse-trait"
33
description = "Adding traits to make parsing types easier"
4-
version = "0.3.1"
4+
version = "0.3.2"
55
license = "MIT/Apache-2.0"
66
keywords = ["nom", "parser", "parsable"]
77
categories = ["parsing"]

src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,24 +108,32 @@ macro_rules! signed_parsable {
108108
signed_parsable!(i8 i16 i32 i64 i128);
109109

110110
macro_rules! floating_parsable {
111-
($($ty:ty => $fn:tt),+) => {
111+
($($ty:tt)+) => {
112112
$(
113113
impl<I, E: error::ParseError<I>> ParseFrom<I, E> for $ty
114114
where
115-
I: Input + Offset + AsBytes + ParseTo<$ty> + Compare<&'static str>,
115+
I: Input + Offset + AsBytes + Compare<&'static str>,
116116
<I as Input>::Item: AsChar,
117117
<I as Input>::Iter: Clone,
118118
I: for<'a> Compare<&'a [u8]>,
119119
{
120120
fn parse(input: I) -> nom::IResult<I, Self, E> {
121-
nom::number::complete::$fn(input)
121+
use std::str::FromStr;
122+
use nom::number::complete::recognize_float_or_exceptions;
123+
use std::str::from_utf8;
124+
125+
let (i, s) = recognize_float_or_exceptions(input)?;
126+
match from_utf8(s.as_bytes()).ok().and_then(|s| $ty::from_str(s).ok()) {
127+
Some(f) => Ok((i, f)),
128+
None => Err(nom::Err::Error(E::from_error_kind(i, nom::error::ErrorKind::Float))),
129+
}
122130
}
123131
}
124132
)*
125133
}
126134
}
127135

128-
floating_parsable!(f32 => float, f64 => double);
136+
floating_parsable!(f32 f64);
129137

130138
/// Support reading the words "true" or "false" from the input and interpreting them as boolean values.
131139
impl<I, E: error::ParseError<I>> ParseFrom<I, E> for bool

0 commit comments

Comments
 (0)