Function nom8::bytes::take_while_m_n
source · pub fn take_while_m_n<T, Input, Error: ParseError<Input>, const STREAMING: bool>(
m: usize,
n: usize,
list: T
) -> impl Fn(Input) -> IResult<Input, <Input as IntoOutput>::Output, Error>where
Input: InputTake + InputIter + InputLength + Slice<RangeFrom<usize>> + InputIsStreaming<STREAMING> + IntoOutput,
T: FindToken<<Input as InputIter>::Item>,
Expand description
Returns the longest (m <= len <= n) input slice that matches the pattern
It will return an Err::Error((_, ErrorKind::TakeWhileMN))
if the pattern wasn’t met or is out
of range (m <= len <= n).
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 nom8::bytes::take_while_m_n;
use nom8::input::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"), Ok((&b""[..], &b"latin"[..])));
assert_eq!(short_alpha(b"ed"), Err(Err::Error(Error::new(&b"ed"[..], ErrorKind::TakeWhileMN))));
assert_eq!(short_alpha(b"12345"), Err(Err::Error(Error::new(&b"12345"[..], ErrorKind::TakeWhileMN))));
use nom8::bytes::take_while_m_n;
use nom8::input::AsChar;
fn short_alpha(s: Streaming<&[u8]>) -> IResult<Streaming<&[u8]>, &[u8]> {
take_while_m_n(3, 6, AsChar::is_alpha)(s)
}
assert_eq!(short_alpha(Streaming(b"latin123")), Ok((Streaming(&b"123"[..]), &b"latin"[..])));
assert_eq!(short_alpha(Streaming(b"lengthy")), Ok((Streaming(&b"y"[..]), &b"length"[..])));
assert_eq!(short_alpha(Streaming(b"latin")), Err(Err::Incomplete(Needed::new(1))));
assert_eq!(short_alpha(Streaming(b"ed")), Err(Err::Incomplete(Needed::new(1))));
assert_eq!(short_alpha(Streaming(b"12345")), Err(Err::Error(Error::new(Streaming(&b"12345"[..]), ErrorKind::TakeWhileMN))));