Skip to content

Commit

Permalink
retry regex matching when first match fails
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Dec 24, 2023
1 parent 4cd9cfd commit e02aa62
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 4 additions & 0 deletions crates/dash_regex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
16 changes: 13 additions & 3 deletions crates/dash_regex/src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions crates/dash_regex/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit e02aa62

Please sign in to comment.