pub fn take_while1<F, Input, Error>(cond: F) -> impl Fn(Input)
where Error: ParseError<Input>, Input: InputTakeAtPosition, F: Fn(<Input as InputTakeAtPosition>::Item) -> bool,
Available on crate feature mtls only.
Expand description

Returns the longest (at least 1) 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(Err::Error((_, ErrorKind::TakeWhile1))) if the pattern wasn’t met.


use nom::bytes::complete::take_while1;
use nom::character::is_alphabetic;

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

assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..])));
assert_eq!(alpha(b"latin"), Ok((&b""[..], &b"latin"[..])));
assert_eq!(alpha(b"12345"), Err(Err::Error(Error::new(&b"12345"[..], ErrorKind::TakeWhile1))));