Function nom8::multi::length_data

source ·
pub fn length_data<I, N, E, F, const STREAMING: bool>(
    f: F
) -> impl FnMut(I) -> IResult<I, <I as IntoOutput>::Output, E>where
    I: InputLength + InputTake + InputIter + IntoOutput + InputIsStreaming<STREAMING>,
    N: ToUsize,
    F: Parser<I, N, E>,
    E: ParseError<I>,
Expand description

Gets a number from the parser and returns a subslice of the input of that size.

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.

Arguments

  • f The parser to apply.
use nom8::number::be_u16;
use nom8::multi::length_data;
use nom8::bytes::tag;

fn parser(s: Streaming<&[u8]>) -> IResult<Streaming<&[u8]>, &[u8]> {
  length_data(be_u16)(s)
}

assert_eq!(parser(Streaming(b"\x00\x03abcefg")), Ok((Streaming(&b"efg"[..]), &b"abc"[..])));
assert_eq!(parser(Streaming(b"\x00\x03a")), Err(Err::Incomplete(Needed::new(2))));