From 599444a68e006313ffc105418ea6bfe52a759ffb Mon Sep 17 00:00:00 2001 From: binhvq Date: Wed, 11 Nov 2026 14:43:47 +0700 Subject: [PATCH] diff: translate -F/-p groups to non-capturing groups Only is_match() is called and back references are rejected, so nothing reads the captures, yet the regex engine kept slots for every group. A pattern with many repeated groups used over ten times the memory it needs. --- src/bre.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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"));