Function take_till

fn take_till<Set, Input, Error, impl Into<Range>: Into<crate::stream::Range>>(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>

Recognize the longest input slice (if any) till a member of a [set of tokens][ContainsToken] 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][Range]:

# use std::ops::RangeFrom;
# use winnow::prelude::*;
# use winnow::stream::ContainsToken;
# use winnow::error::ContextError;
pub fn take_till<'i>(occurrences: RangeFrom<usize>, set: impl ContainsToken<char>) -> impl Parser<&'i str, &'i str, ContextError>
# {
#     winnow::token::take_till(occurrences, set)
# }

Example

# use winnow::{error::ErrMode, error::ContextError, error::Needed};
# use winnow::prelude::*;
use winnow::token::take_till;

fn till_colon<'i>(s: &mut &'i str) -> ModalResult<&'i str> {
  take_till(0.., |c| c == ':').parse_next(s)
}

assert_eq!(till_colon.parse_peek("latin:123"), Ok((":123", "latin")));
assert_eq!(till_colon.parse_peek(":empty matched"), Ok((":empty matched", ""))); //allowed
assert_eq!(till_colon.parse_peek("12345"), Ok(("", "12345")));
assert_eq!(till_colon.parse_peek(""), Ok(("", "")));
# use winnow::{error::ErrMode, error::ContextError, error::Needed};
# use winnow::prelude::*;
# use winnow::Partial;
use winnow::token::take_till;

fn till_colon<'i>(s: &mut Partial<&'i str>) -> ModalResult<&'i str> {
  take_till(0.., |c| c == ':').parse_next(s)
}

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