Function winnow::token::take_till

source ·
pub fn take_till<Set, Input, Error>(
    occurrences: impl Into<Range>,
    set: Set
) -> impl Parser<Input, <Input as Stream>::Slice, Error>
where Input: StreamIsPartial + Stream, Set: ContainsToken<<Input as Stream>::Token>, Error: ParserError<Input>,
Expand description

Recognize the longest input slice (if any) till a member of a set of tokens is found.

It doesn’t consume the terminating token from the set.

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

See also

§Effective Signature

Assuming you are parsing a &str Stream with 0.. or 1.. ranges:

pub fn take_till<'i>(occurrences: RangeFrom<usize>, set: impl ContainsToken<char>) -> impl Parser<&'i str, &'i str, ContextError>

§Example

use winnow::token::take_till;

fn till_colon(s: &str) -> IResult<&str, &str> {
  take_till(0.., |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_till;

fn till_colon(s: Partial<&str>) -> IResult<Partial<&str>, &str> {
  take_till(0.., |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))));