Function winnow::bytes::take_while0

source ·
pub fn take_while0<T, I, Error: ParseError<I>>(
    list: T
) -> impl FnMut(I) -> IResult<I, <I as Stream>::Slice, Error>where
    I: StreamIsPartial + Stream,
    T: ContainsToken<<I as Stream>::Token>,
Expand description

Recognize the longest input slice (if any) that matches the pattern

Partial version: will return a ErrMode::Incomplete(Needed::new(1)) if the pattern reaches the end of the input.

To recognize a series of tokens, use many0 to Accumulate into a () and then Parser::recognize.

Example

use winnow::bytes::take_while0;
use winnow::stream::AsChar;

fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
  take_while0(AsChar::is_alpha)(s)
}

assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..])));
assert_eq!(alpha(b"12345"), Ok((&b"12345"[..], &b""[..])));
assert_eq!(alpha(b"latin"), Ok((&b""[..], &b"latin"[..])));
assert_eq!(alpha(b""), Ok((&b""[..], &b""[..])));
use winnow::bytes::take_while0;
use winnow::stream::AsChar;

fn alpha(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> {
  take_while0(AsChar::is_alpha)(s)
}

assert_eq!(alpha(Partial::new(b"latin123")), Ok((Partial::new(&b"123"[..]), &b"latin"[..])));
assert_eq!(alpha(Partial::new(b"12345")), Ok((Partial::new(&b"12345"[..]), &b""[..])));
assert_eq!(alpha(Partial::new(b"latin")), Err(ErrMode::Incomplete(Needed::new(1))));
assert_eq!(alpha(Partial::new(b"")), Err(ErrMode::Incomplete(Needed::new(1))));