Function nom8::bytes::take_till1

source ·
pub fn take_till1<T, Input, Error: ParseError<Input>, const STREAMING: bool>(
    list: T
) -> impl Fn(Input) -> IResult<Input, <Input as IntoOutput>::Output, Error>where
    Input: InputTakeAtPosition + InputIsStreaming<STREAMING> + IntoOutput,
    T: FindToken<<Input as InputTakeAtPosition>::Item>,
Expand description

Returns the longest (at least 1) input slice till a pattern is met.

It will return Err(Err::Error((_, ErrorKind::TakeTill1))) if the input is empty or the predicate matches the first input.

Streaming version will return a Err::Incomplete(Needed::new(1)) if the match reaches the end of input or if there was not match.

Example

use nom8::bytes::take_till1;

fn till_colon(s: &str) -> IResult<&str, &str> {
  take_till1(|c| c == ':')(s)
}

assert_eq!(till_colon("latin:123"), Ok((":123", "latin")));
assert_eq!(till_colon(":empty matched"), Err(Err::Error(Error::new(":empty matched", ErrorKind::TakeTill1))));
assert_eq!(till_colon("12345"), Ok(("", "12345")));
assert_eq!(till_colon(""), Err(Err::Error(Error::new("", ErrorKind::TakeTill1))));

fn not_space(s: &str) -> IResult<&str, &str> {
  take_till1(" \t\r\n")(s)
}

assert_eq!(not_space("Hello, World!"), Ok((" World!", "Hello,")));
assert_eq!(not_space("Sometimes\t"), Ok(("\t", "Sometimes")));
assert_eq!(not_space("Nospace"), Ok(("", "Nospace")));
assert_eq!(not_space(""), Err(Err::Error(Error::new("", ErrorKind::TakeTill1))));
use nom8::bytes::take_till1;

fn till_colon(s: Streaming<&str>) -> IResult<Streaming<&str>, &str> {
  take_till1(|c| c == ':')(s)
}

assert_eq!(till_colon(Streaming("latin:123")), Ok((Streaming(":123"), "latin")));
assert_eq!(till_colon(Streaming(":empty matched")), Err(Err::Error(Error::new(Streaming(":empty matched"), ErrorKind::TakeTill1))));
assert_eq!(till_colon(Streaming("12345")), Err(Err::Incomplete(Needed::new(1))));
assert_eq!(till_colon(Streaming("")), Err(Err::Incomplete(Needed::new(1))));

fn not_space(s: Streaming<&str>) -> IResult<Streaming<&str>, &str> {
  take_till1(" \t\r\n")(s)
}

assert_eq!(not_space(Streaming("Hello, World!")), Ok((Streaming(" World!"), "Hello,")));
assert_eq!(not_space(Streaming("Sometimes\t")), Ok((Streaming("\t"), "Sometimes")));
assert_eq!(not_space(Streaming("Nospace")), Err(Err::Incomplete(Needed::new(1))));
assert_eq!(not_space(Streaming("")), Err(Err::Incomplete(Needed::new(1))));