pub fn length_count<I, O, N, E, F, G>(
    f: F,
    g: G
) -> impl FnMut(I) -> IResult<I, Vec<O>, E> where
    I: Clone,
    N: ToUsize,
    F: Parser<I, N, E>,
    G: Parser<I, O, E>,
    E: ParseError<I>, 
Expand description

Gets a number from the first parser, then applies the second parser that many times.

Arguments

  • f The parser to apply to obtain the count.
  • g The parser to apply repeatedly.
use nom::number::complete::u8;
use nom::multi::length_count;
use nom::bytes::complete::tag;
use nom::combinator::map;

fn parser(s: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
  length_count(map(u8, |i| {
     println!("got number: {}", i);
     i
  }), tag("abc"))(s)
}

assert_eq!(parser(&b"\x02abcabcabc"[..]), Ok(((&b"abc"[..], vec![&b"abc"[..], &b"abc"[..]]))));
assert_eq!(parser(b"\x03123123123"), Err(Err::Error(Error::new(&b"123123123"[..], ErrorKind::Tag))));