Function winnow::bytes::take_while_m_n
source · pub fn take_while_m_n<T, I, Error: ParseError<I>>(
m: usize,
n: usize,
list: T
) -> impl Parser<I, <I as Stream>::Slice, Error>where
I: StreamIsPartial + Stream,
T: ContainsToken<<I as Stream>::Token>,
Expand description
Recognize the longest (m <= len <= n) input slice that matches the pattern
It will return an ErrMode::Backtrack(Error::new(_, ErrorKind::Slice))
if the pattern wasn’t met or is out
of range (m <= len <= n).
Partial version will return a ErrMode::Incomplete(Needed::new(1))
if the pattern reaches the end of the input or is too short.
To recognize a series of tokens, use many_m_n
to Accumulate
into a ()
and then Parser::recognize
.
Example
use winnow::bytes::take_while_m_n;
use winnow::stream::AsChar;
fn short_alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
take_while_m_n(3, 6, AsChar::is_alpha).parse_next(s)
}
assert_eq!(short_alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..])));
assert_eq!(short_alpha(b"lengthy"), Ok((&b"y"[..], &b"length"[..])));
assert_eq!(short_alpha(b"latin"), Ok((&b""[..], &b"latin"[..])));
assert_eq!(short_alpha(b"ed"), Err(ErrMode::Backtrack(Error::new(&b"ed"[..], ErrorKind::Slice))));
assert_eq!(short_alpha(b"12345"), Err(ErrMode::Backtrack(Error::new(&b"12345"[..], ErrorKind::Slice))));
use winnow::bytes::take_while_m_n;
use winnow::stream::AsChar;
fn short_alpha(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> {
take_while_m_n(3, 6, AsChar::is_alpha).parse_next(s)
}
assert_eq!(short_alpha(Partial::new(b"latin123")), Ok((Partial::new(&b"123"[..]), &b"latin"[..])));
assert_eq!(short_alpha(Partial::new(b"lengthy")), Ok((Partial::new(&b"y"[..]), &b"length"[..])));
assert_eq!(short_alpha(Partial::new(b"latin")), Err(ErrMode::Incomplete(Needed::new(1))));
assert_eq!(short_alpha(Partial::new(b"ed")), Err(ErrMode::Incomplete(Needed::new(1))));
assert_eq!(short_alpha(Partial::new(b"12345")), Err(ErrMode::Backtrack(Error::new(Partial::new(&b"12345"[..]), ErrorKind::Slice))));