Function nom8::bytes::take_until

source ·
pub fn take_until<T, Input, Error: ParseError<Input>, const STREAMING: bool>(
    tag: T
) -> impl Fn(Input) -> IResult<Input, <Input as IntoOutput>::Output, Error>where
    Input: InputTake + InputLength + FindSubstring<T> + InputIsStreaming<STREAMING> + IntoOutput,
    T: InputLength + Clone,
Expand description

Returns the input slice up to the first occurrence of the pattern.

It doesn’t consume the pattern.

Complete version: It will return Err(Err::Error((_, ErrorKind::TakeUntil))) if the pattern wasn’t met.

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

Example

use nom8::bytes::take_until;

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

assert_eq!(until_eof("hello, worldeof"), Ok(("eof", "hello, world")));
assert_eq!(until_eof("hello, world"), Err(Err::Error(Error::new("hello, world", ErrorKind::TakeUntil))));
assert_eq!(until_eof(""), Err(Err::Error(Error::new("", ErrorKind::TakeUntil))));
assert_eq!(until_eof("1eof2eof"), Ok(("eof2eof", "1")));
use nom8::bytes::take_until;

fn until_eof(s: Streaming<&str>) -> IResult<Streaming<&str>, &str> {
  take_until("eof")(s)
}

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