nom8::bytes

Function take_while

Source
pub fn take_while<T, Input, Error: ParseError<Input>, const STREAMING: bool>(
    list: T,
) -> impl Fn(Input) -> IResult<Input, <Input as IntoOutput>::Output, Error>
Expand description

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

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

ยงExample

use nom8::bytes::take_while;
use nom8::input::AsChar;

fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
  take_while(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 nom8::bytes::take_while;
use nom8::input::AsChar;

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

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