nom icon indicating copy to clipboard operation
nom copied to clipboard

Apparent inconsistency in documented behavior of `is_not

Open Lucretiel opened this issue 4 years ago • 1 comments

The documentation for is_not includes this line:

It will return a Err::Error(("", ErrorKind::IsNot)) if the pattern wasn't met.

This seems to imply that, for is_not to succeed, the pattern must be present as a terminator. However, it also includes this example code:

assert_eq!(not_space("Nospace"), Ok(("", "Nospace")));
assert_eq!(not_space(""), Err(Err::Error(Error::new("", ErrorKind::IsNot))));

I'd expect the former case to fail the test, since there's no terminator present. It strikes me as a bit inconsistent that is_not will successfully match a non-empty string without the terminator, but will reject an empty string with the terminator.

Lucretiel avatar Jan 27 '21 22:01 Lucretiel

It will also fail if the string starts with the terminator, rather than return an empty string.

i.e.

assert_eq!(not_space(" Hello"), Ok((" Hello", "")))

Fails.

This is super bothersome as I can't find an obvious way to get this result.

EDIT: added this to my codebase

mod nom_polyfill {
    use nom::{*, error::*};

    pub fn is_not0<T, Input, Error: ParseError<Input>>(
      arr: T,
    ) -> impl Fn(Input) -> IResult<Input, Input, Error>
    where
      Input: InputTakeAtPosition,
      T: FindToken<<Input as InputTakeAtPosition>::Item>,
    {
      move |i: Input| {
        i.split_at_position_complete(|c| arr.find_token(c))
      }
    }
}

LunarLambda avatar Mar 19 '24 11:03 LunarLambda