Function length_repeat

fn length_repeat<Input, Output, Accumulator, Count, Error, CountParser, ParseNext>(count: CountParser, parser: ParseNext) -> impl Parser<Input, Accumulator, Error>
where
    Input: Stream,
    Count: ToUsize,
    Accumulator: Accumulate<Output>,
    CountParser: Parser<Input, Count, Error>,
    ParseNext: Parser<Input, Output, Error>,
    Error: ParserError<Input>

Accumulate a length-prefixed sequence of values (TLV)

If the length represents token counts, see instead length_take

Example

# #[cfg(feature = "std")] {
# use winnow::prelude::*;
# use winnow::{error::ErrMode, error::InputError, error::Needed};
# use winnow::prelude::*;
use winnow::Bytes;
use winnow::binary::u8;
use winnow::binary::length_repeat;

type Stream<'i> = &'i Bytes;

fn stream(b: &[u8]) -> Stream<'_> {
    Bytes::new(b)
}

fn parser<'i>(s: &mut Stream<'i>) -> ModalResult<Vec<&'i [u8]>> {
  length_repeat(u8.map(|i| {
     println!("got number: {}", i);
     i
  }), "abc").parse_next(s)
}

assert_eq!(parser.parse_peek(stream(b"\x02abcabcabc")), Ok((stream(b"abc"), vec![&b"abc"[..], &b"abc"[..]])));
assert!(parser.parse_peek(stream(b"\x03123123123")).is_err());
# }