diff --git a/crates/dash_regex/src/lib.rs b/crates/dash_regex/src/lib.rs index 89b667b8..9e861bcc 100644 --- a/crates/dash_regex/src/lib.rs +++ b/crates/dash_regex/src/lib.rs @@ -29,4 +29,8 @@ pub fn test() { assert!(matches(HEX_REGEX, "#aabbccdd")); assert!(!matches(HEX_REGEX, "#AAb")); assert!(matches(HEX_REGEX, "#aBcDEEf0")); + + assert!(matches("\\d", "a1")); + assert!(matches("V\\dX", "aV1aVaXaV1Xs")); + assert!(!matches("V\\dX", "aV1aVaXaV?Xs")); } diff --git a/crates/dash_regex/src/matcher.rs b/crates/dash_regex/src/matcher.rs index 75383830..2448975f 100644 --- a/crates/dash_regex/src/matcher.rs +++ b/crates/dash_regex/src/matcher.rs @@ -16,12 +16,22 @@ impl<'a> Matcher<'a> { } pub fn matches(&mut self) -> bool { - while !self.nodes.is_eof() { + let mut index = self.text.index(); + + while index < self.text.len() { + if self.nodes.is_eof() { + // all regex nodes matched + return true; + } + if !self.matches_single() { - return false; + index += 1; + self.nodes.set_index(0); + self.text.set_index(index); } } - true + + false } pub fn matches_single(&mut self) -> bool { diff --git a/crates/dash_regex/src/stream.rs b/crates/dash_regex/src/stream.rs index 5b35bf0c..e8502d61 100644 --- a/crates/dash_regex/src/stream.rs +++ b/crates/dash_regex/src/stream.rs @@ -26,6 +26,10 @@ impl<'a, T> BorrowedStream<'a, T> { self.index } + pub fn len(&self) -> usize { + self.source.len() + } + pub fn set_index(&mut self, i: usize) { self.index = i; }