From 13af9e0fccdbdb1ded8401623d4633415ff8033b Mon Sep 17 00:00:00 2001 From: John Turner Date: Sat, 1 Nov 2025 18:14:13 +0000 Subject: impl Find combinator --- src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index d963e36..d2af3ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -198,6 +198,10 @@ pub trait Parser: Sized { delimiter, } } + + fn find(self) -> impl ParserIter { + Find { parser: self } + } } pub trait ParserIter { @@ -951,6 +955,35 @@ where } } +struct Find

{ + parser: P, +} + +impl ParserIter for Find

+where + I: Input, + P: Parser, +{ + type Item = P::Output; + + fn next( + &self, + mut it: InputIter, + ) -> Option> { + while !it.is_finished() { + it = match self.parser.run::(it.clone()) { + Ok((rest, output)) => return Some(Ok((rest, output))), + Err(_) => match take(1).check(it.clone()) { + Ok((rest, _)) => rest, + Err(_) => return None, + }, + }; + } + + None + } +} + pub fn eof() -> impl Parser where I: Input, @@ -1099,6 +1132,17 @@ mod test { .unwrap(); } + #[test] + fn test_find() { + let input = "hello world"; + let it = InputIter::new(input); + + assert_eq!( + alpha1().find().at_least(1).parse_finished(it).unwrap(), + vec!["hello", "world"] + ); + } + #[test] #[cfg(feature = "regex")] fn test_regex_parser() { -- cgit v1.2.3