Skip to content

Commit

Permalink
fix: only short-circuit when known not to match
Browse files Browse the repository at this point in the history
This fixes patterns returning `true` as soon as a substring matches, when subsequent characters do not match.
  • Loading branch information
hawkw committed Jul 15, 2019
1 parent 616762f commit 7154449
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ where
pub fn matches(mut self, s: &impl AsRef<str>) -> bool {
for &byte in s.as_ref().as_bytes() {
self.advance(byte);
if self.automaton.is_match_or_dead_state(self.state) {
return self.automaton.is_match_state(self.state);
if self.automaton.is_dead_state(self.state) {
return false;
}
}
false
self.is_matched()
}

pub fn debug_matches(mut self, d: &impl fmt::Debug) -> bool {
Expand All @@ -115,11 +115,11 @@ where
pub fn read_matches(mut self, io: impl io::Read + Sized) -> io::Result<bool> {
for r in io.bytes() {
self.advance(r?);
if self.automaton.is_match_or_dead_state(self.state) {
return Ok(self.automaton.is_match_state(self.state));
if self.automaton.is_dead_state(self.state) {
return Ok(false);
}
}
Ok(false)
Ok(self.is_matched())
}
}

Expand All @@ -131,7 +131,7 @@ where
fn write_str(&mut self, s: &str) -> fmt::Result {
for &byte in s.as_bytes() {
self.advance(byte);
if self.automaton.is_match_or_dead_state(self.state) {
if self.automaton.is_dead_state(self.state) {
break;
}
}
Expand All @@ -149,7 +149,7 @@ where
for &byte in bytes {
self.advance(byte);
i += 1;
if self.automaton.is_match_or_dead_state(self.state) {
if self.automaton.is_dead_state(self.state) {
break;
}
}
Expand Down

0 comments on commit 7154449

Please sign in to comment.