Function winnow::bytes::take_till0

source ·
pub fn take_till0<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) till a pattern is met.

Partial version will return a ErrMode::Incomplete(Needed::new(1)) if the match reaches the end of input or if there was not match.

Example

use winnow::bytes::take_till0;

fn till_colon(s: &str) -> IResult<&str, &str> {
  take_till0(|c| c == ':')(s)
}

assert_eq!(till_colon("latin:123"), Ok((":123", "latin")));
assert_eq!(till_colon(":empty matched"), Ok((":empty matched", ""))); //allowed
assert_eq!(till_colon("12345"), Ok(("", "12345")));
assert_eq!(till_colon(""), Ok(("", "")));
use winnow::bytes::take_till0;

fn till_colon(s: Partial<&str>) -> IResult<Partial<&str>, &str> {
  take_till0(|c| c == ':')(s)
}

assert_eq!(till_colon(Partial::new("latin:123")), Ok((Partial::new(":123"), "latin")));
assert_eq!(till_colon(Partial::new(":empty matched")), Ok((Partial::new(":empty matched"), ""))); //allowed
assert_eq!(till_colon(Partial::new("12345")), Err(ErrMode::Incomplete(Needed::new(1))));
assert_eq!(till_colon(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1))));