From 0e20aff140f327151f60c931c6826e621a363460 Mon Sep 17 00:00:00 2001 From: Mohammad Kanaan Date: Mon, 27 Jul 2026 20:44:53 +0300 Subject: [PATCH] fix: build regex once instead of once per file --- src/parser.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index cab8874..83cd964 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -17,11 +17,16 @@ pub fn process_files( pattern: &str, cli: &Cli, ) -> anyhow::Result { + let reg = RegexBuilder::new(pattern) + .case_insensitive(cli.ignore_case) + .build() + .with_context(|| format!("invalid regex: {:?}", pattern))?; + let mut matches = Matches { items: vec![], pattern: String::from(pattern) }; for file_path in file_paths { let lines_iter = read_lines(file_path)?; - let mut _matches = match_pattern(lines_iter, pattern, file_path, cli)?; + let mut _matches = match_pattern(lines_iter, pattern, file_path, ®, cli)?; matches.append(&mut _matches); } @@ -60,19 +65,15 @@ pub fn match_pattern( iter: impl Iterator>, pattern: &str, file_path: &Path, + regex: &Regex, cli: &Cli, ) -> anyhow::Result { - let reg = RegexBuilder::new(pattern) - .case_insensitive(cli.ignore_case) - .build() - .with_context(|| format!("invalid regex: {:?}", pattern))?; - let mut matches = Vec::new(); for (idx, line) in iter.enumerate() { let line = line?; - if reg.is_match(&line) { + if regex.is_match(&line) { let m = Match { line_number: idx + 1, text: line,