From 71544493613049009f33f4885a1287eee0f21aa4 Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Mon, 15 Jul 2019 10:54:50 -0700 Subject: [PATCH] fix: only short-circuit when known not to match This fixes patterns returning `true` as soon as a substring matches, when subsequent characters do not match. --- src/lib.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5d67c94..f1110b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -93,11 +93,11 @@ where pub fn matches(mut self, s: &impl AsRef) -> 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 { @@ -115,11 +115,11 @@ where pub fn read_matches(mut self, io: impl io::Read + Sized) -> io::Result { 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()) } } @@ -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; } } @@ -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; } }