Function take_until

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

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][Range]:

# use std::ops::RangeFrom;
# use winnow::prelude::*;;
# use winnow::error::ContextError;
pub fn take_until(occurrences: RangeFrom<usize>, literal: &str) -> impl Parser<&str, &str, ContextError>
# {
#     winnow::token::take_until(occurrences, literal)
# }

Example

# use winnow::{error::ErrMode, error::ContextError, error::Needed};
# use winnow::prelude::*;
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::{error::ErrMode, error::ContextError, error::Needed};
# use winnow::prelude::*;
# use winnow::Partial;
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::{error::ErrMode, error::ContextError, error::Needed};
# use winnow::prelude::*;
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::{error::ErrMode, error::ContextError, error::Needed};
# use winnow::prelude::*;
# use winnow::Partial;
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());