pub fn length_count<I, O, N, E, F, G>(
f: F,
g: G,
) -> impl FnMut(I) -> IResult<I, Vec<O>, E>
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 nom8::number::u8;
use nom8::multi::length_count;
use nom8::bytes::tag;
use nom8::combinator::map;
fn parser(s: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
length_count(u8.map(|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))));