Function winnow::token::take_until0

source ·
pub fn take_until0<T, I, Error: ParserError<I>>(
    tag: T,
) -> impl Parser<I, <I as Stream>::Slice, Error>
Expand description

Recognize the input slice up to the first occurrence of the literal.

It doesn’t consume the pattern.

Complete version: It will return Err(ErrMode::Backtrack(InputError::new(_, ErrorKind::Slice))) if the pattern wasn’t met.

Partial version: will return a ErrMode::Incomplete(Needed::new(N)) if the input doesn’t contain the pattern or if the input is smaller than the pattern.

§Example

use winnow::token::take_until0;

fn until_eof(s: &str) -> IResult<&str, &str> {
  take_until0("eof").parse_peek(s)
}

assert_eq!(until_eof("hello, worldeof"), Ok(("eof", "hello, world")));
assert_eq!(until_eof("hello, world"), Err(ErrMode::Backtrack(InputError::new("hello, world", ErrorKind::Slice))));
assert_eq!(until_eof(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice))));
assert_eq!(until_eof("1eof2eof"), Ok(("eof2eof", "1")));
use winnow::token::take_until0;

fn until_eof(s: Partial<&str>) -> IResult<Partial<&str>, &str> {
  take_until0("eof").parse_peek(s)
}

assert_eq!(until_eof(Partial::new("hello, worldeof")), Ok((Partial::new("eof"), "hello, world")));
assert_eq!(until_eof(Partial::new("hello, world")), Err(ErrMode::Incomplete(Needed::Unknown)));
assert_eq!(until_eof(Partial::new("hello, worldeo")), Err(ErrMode::Incomplete(Needed::Unknown)));
assert_eq!(until_eof(Partial::new("1eof2eof")), Ok((Partial::new("eof2eof"), "1")));