Function winnow::ascii::float

source ·
pub fn float<Input, Output, Error>(input: &mut Input) -> PResult<Output, Error>
where Input: StreamIsPartial + Stream + Compare<Caseless<&'static str>> + Compare<char> + AsBStr, <Input as Stream>::Slice: ParseSlice<Output>, <Input as Stream>::Token: AsChar + Clone, <Input as Stream>::IterOffsets: Clone, Error: ParserError<Input>,
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][crate::_topic::partial]: Will return Err(winnow::error::ErrMode::Incomplete(_)) if there is not enough data.

§Effective Signature

Assuming you are parsing a &str Stream into an f64:

pub fn float(input: &mut &str) -> PResult<f64>

§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))));