Function winnow::ascii::float

source ·
pub fn float<I, O, E: ParserError<I>>(input: &mut I) -> PResult<O, E>
where I: StreamIsPartial + Stream + Compare<&'static str> + AsBStr, <I as Stream>::Slice: ParseSlice<O>, <I as Stream>::Token: AsChar + Clone, <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::ascii::float;

fn parser<'s>(s: &mut &'s str) -> PResult<f64, InputError<&'s str>> {
  float(s)
}

assert_eq!(parser.parse_peek("11e-1"), Ok(("", 1.1)));
assert_eq!(parser.parse_peek("123E-02"), Ok(("", 1.23)));
assert_eq!(parser.parse_peek("123K-01"), Ok(("K-01", 123.0)));
assert_eq!(parser.parse_peek("abc"), Err(ErrMode::Backtrack(InputError::new("abc", ErrorKind::Tag))));
use winnow::ascii::float;

fn parser<'s>(s: &mut Partial<&'s str>) -> PResult<f64, InputError<Partial<&'s str>>> {
  float(s)
}

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