diff --git a/src/bre.rs b/src/bre.rs index bcd2a220..6e04fbcd 100644 --- a/src/bre.rs +++ b/src/bre.rs @@ -75,7 +75,7 @@ fn translate(pattern: &str) -> Result { match escaped { '(' => { open_groups += 1; - translated.push('('); + translated.push_str("(?:"); opens_expression = true; } ')' => { @@ -277,6 +277,16 @@ mod tests { assert!(!matches(r"f(x)|y+?", "fx")); } + // Only is_match() is used and back references are rejected, so a group + // has nothing to capture; capturing would cost memory for each of them. + #[test] + fn groups_do_not_capture() { + let bre = Bre::new(r"^\(a\(b\)\)\{2\}\(c\|d\)$").unwrap(); + assert_eq!(bre.regex.captures_len(), 1); + assert!(bre.is_match(b"ababd")); + assert!(!bre.is_match(b"abd")); + } + #[test] fn intervals_and_repetitions() { assert!(matches(r"^ab\{2,3\}c$", "abbc"));