rustmax::nom::bytes::streaming

Function take_while_m_n

Source
pub fn take_while_m_n<F, I, Error>(m: usize, n: usize, cond: F) -> impl FnMut(I)
where Error: ParseError<I>, I: Input, F: Fn(<I as Input>::Item) -> bool,
Expand description

Returns the longest (m <= len <= n) input slice that matches the predicate.

The parser will return the longest slice that matches the given predicate (a function that takes the input and returns a bool).

It will return an Err::Error((_, ErrorKind::TakeWhileMN)) if the pattern wasn’t met.

§Streaming Specific

Streaming version will return a Err::Incomplete(Needed::new(1)) if the pattern reaches the end of the input or is too short.

§Example

use nom::bytes::streaming::take_while_m_n;
use nom::AsChar;

fn short_alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
  take_while_m_n(3, 6, AsChar::is_alpha)(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"), Err(Err::Incomplete(Needed::new(1))));
assert_eq!(short_alpha(b"ed"), Err(Err::Incomplete(Needed::new(1))));
assert_eq!(short_alpha(b"12345"), Err(Err::Error(Error::new(&b"12345"[..], ErrorKind::TakeWhileMN))));