Function take_while

pub fn take_while<F, I, Error: ParseError<I>>(cond: F) -> impl Parser<I, Output = I, Error = Error>
where
    I: Input,
    F: Fn(<I as Input>::Item) -> bool,

Returns the longest input slice (if any) 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).

Example

# use nom::{Err, error::ErrorKind, Needed, IResult};
use nom::bytes::complete::take_while;
use nom::character::is_alphabetic;

fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
  take_while(is_alphabetic)(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""[..])));