pub fn f32<I, E: ParseError<I>, const STREAMING: bool>(
endian: Endianness
) -> fn(_: I) -> IResult<I, f32, E>where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength + InputIsStreaming<STREAMING>,
Expand description
Recognizes a 4 byte floating point number
If the parameter is nom8::number::Endianness::Big
, parse a big endian f32 float,
otherwise if nom8::number::Endianness::Little
parse a little endian f32 float.
Complete version: returns an error if there is not enough input data
Streaming version: Will return Err(nom8::Err::Incomplete(_))
if there is not enough data.
Example
use nom8::number::f32;
let be_f32 = |s| {
f32(nom8::number::Endianness::Big)(s)
};
assert_eq!(be_f32(&[0x41, 0x48, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
assert_eq!(be_f32(&b"abc"[..]), Err(Err::Error((&b"abc"[..], ErrorKind::Eof))));
let le_f32 = |s| {
f32(nom8::number::Endianness::Little)(s)
};
assert_eq!(le_f32(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5)));
assert_eq!(le_f32(&b"abc"[..]), Err(Err::Error((&b"abc"[..], ErrorKind::Eof))));
use nom8::number::f32;
let be_f32 = |s| {
f32::<_, (_, ErrorKind), true>(nom8::number::Endianness::Big)(s)
};
assert_eq!(be_f32(Streaming(&[0x41, 0x48, 0x00, 0x00][..])), Ok((Streaming(&b""[..]), 12.5)));
assert_eq!(be_f32(Streaming(&b"abc"[..])), Err(Err::Incomplete(Needed::new(1))));
let le_f32 = |s| {
f32::<_, (_, ErrorKind), true>(nom8::number::Endianness::Little)(s)
};
assert_eq!(le_f32(Streaming(&[0x00, 0x00, 0x48, 0x41][..])), Ok((Streaming(&b""[..]), 12.5)));
assert_eq!(le_f32(Streaming(&b"abc"[..])), Err(Err::Incomplete(Needed::new(1))));