Function winnow::token::take_till0

source ·
pub fn take_till0<T, I, Error: ParserError<I>>(
    list: T,
) -> impl Parser<I, <I as Stream>::Slice, Error>
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::token::take_till0;

fn till_colon(s: &str) -> IResult<&str, &str> {
  take_till0(|c| c == ':').parse_peek(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::token::take_till0;

fn till_colon(s: Partial<&str>) -> IResult<Partial<&str>, &str> {
  take_till0(|c| c == ':').parse_peek(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))));