Function winnow::token::take_until

source ·
pub fn take_until<Literal, Input, Error>(
    occurrences: impl Into<Range>,
    literal: Literal,
) -> impl Parser<Input, <Input as Stream>::Slice, Error>
where Input: StreamIsPartial + Stream + FindSlice<Literal>, Literal: Clone, Error: ParserError<Input>,
Expand description

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

Feature simd will enable the use of memchr.

It doesn’t consume the literal.

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

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

See also

§Effective Signature

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

pub fn take_until(occurrences: RangeFrom<usize>, literal: &str) -> impl Parser<&str, &str, ContextError>

§Example

use winnow::token::take_until;

fn until_eof(s: &str) -> IResult<&str, &str> {
  take_until(0.., "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_until;

fn until_eof(s: Partial<&str>) -> IResult<Partial<&str>, &str> {
  take_until(0.., "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")));
use winnow::token::take_until;

fn until_eof(s: &str) -> IResult<&str, &str> {
  take_until(1.., "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")));
assert_eq!(until_eof("eof"), Err(ErrMode::Backtrack(InputError::new("eof", ErrorKind::Slice))));
use winnow::token::take_until;

fn until_eof(s: Partial<&str>) -> IResult<Partial<&str>, &str> {
  take_until(1.., "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")));
assert_eq!(until_eof(Partial::new("eof")), Err(ErrMode::Backtrack(InputError::new(Partial::new("eof"), ErrorKind::Slice))));