Function winnow::character::float

source ·
pub fn float<I, O, E: ParseError<I>>(input: I) -> IResult<I, O, E>where
    I: StreamIsPartial + Stream + Offset + Compare<&'static str> + AsBStr,
    <I as Stream>::Slice: ParseSlice<O>,
    <I as Stream>::Token: AsChar + Copy,
    <I as Stream>::IterOffsets: Clone,
Expand description

Recognizes floating point number in text format and returns a f32 or f64.

Complete version: Can parse until the end of input.

Partial version: Will return Err(winnow::error::ErrMode::Incomplete(_)) if there is not enough data.

Example

use winnow::character::float;

fn parser(s: &str) -> IResult<&str, f64> {
  float(s)
}

assert_eq!(parser("11e-1"), Ok(("", 1.1)));
assert_eq!(parser("123E-02"), Ok(("", 1.23)));
assert_eq!(parser("123K-01"), Ok(("K-01", 123.0)));
assert_eq!(parser("abc"), Err(ErrMode::Backtrack(Error::new("abc", ErrorKind::Float))));
use winnow::character::float;

fn parser(s: Partial<&str>) -> IResult<Partial<&str>, f64> {
  float(s)
}

assert_eq!(parser(Partial::new("11e-1 ")), Ok((Partial::new(" "), 1.1)));
assert_eq!(parser(Partial::new("11e-1")), Err(ErrMode::Incomplete(Needed::new(1))));
assert_eq!(parser(Partial::new("123E-02")), Err(ErrMode::Incomplete(Needed::new(1))));
assert_eq!(parser(Partial::new("123K-01")), Ok((Partial::new("K-01"), 123.0)));
assert_eq!(parser(Partial::new("abc")), Err(ErrMode::Backtrack(Error::new(Partial::new("abc"), ErrorKind::Float))));