Function length_value

fn length_value<I, E, F, G>(f: F, g: G) -> impl Parser<I, Output = <G as Parser<I>>::Output, Error = E>
where
    I: Clone + Input,
    <F as Parser<I>>::Output: ToUsize,
    F: Parser<I, Error = E>,
    G: Parser<I, Error = E>,
    E: ParseError<I>

Gets a number from the first parser, takes a subslice of the input of that size, then applies the second parser on that subslice. If the second parser returns Incomplete, length_value will return an error.

Arguments

# use nom::{Err, error::{Error, ErrorKind}, Needed, IResult, Parser};
use nom::number::complete::be_u16;
use nom::multi::length_value;
use nom::bytes::complete::tag;

fn parser(s: &[u8]) -> IResult<&[u8], &[u8]> {
  length_value(be_u16, tag("abc")).parse(s)
}

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