winnow::token

Function 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(_)) if the literal wasn’t met.

[Partial version][crate::_topic::partial]: 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<'i>(s: &mut &'i str) -> ModalResult<&'i str> {
  take_until(0.., "eof").parse_next(s)
}

assert_eq!(until_eof.parse_peek("hello, worldeof"), Ok(("eof", "hello, world")));
assert!(until_eof.parse_peek("hello, world").is_err());
assert!(until_eof.parse_peek("").is_err());
assert_eq!(until_eof.parse_peek("1eof2eof"), Ok(("eof2eof", "1")));
use winnow::token::take_until;

fn until_eof<'i>(s: &mut Partial<&'i str>) -> ModalResult<&'i str> {
  take_until(0.., "eof").parse_next(s)
}

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

fn until_eof<'i>(s: &mut &'i str) -> ModalResult<&'i str> {
  take_until(1.., "eof").parse_next(s)
}

assert_eq!(until_eof.parse_peek("hello, worldeof"), Ok(("eof", "hello, world")));
assert!(until_eof.parse_peek("hello, world").is_err());
assert!(until_eof.parse_peek("").is_err());
assert_eq!(until_eof.parse_peek("1eof2eof"), Ok(("eof2eof", "1")));
assert!(until_eof.parse_peek("eof").is_err());
use winnow::token::take_until;

fn until_eof<'i>(s: &mut Partial<&'i str>) -> ModalResult<&'i str> {
  take_until(1.., "eof").parse_next(s)
}

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