fix: build regex once instead of once per file
This commit is contained in:
parent
8452eae0f4
commit
0e20aff140
1 changed files with 8 additions and 7 deletions
|
|
@ -17,11 +17,16 @@ pub fn process_files(
|
|||
pattern: &str,
|
||||
cli: &Cli,
|
||||
) -> anyhow::Result<Matches, anyhow::Error> {
|
||||
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<Item = std::io::Result<String>>,
|
||||
pattern: &str,
|
||||
file_path: &Path,
|
||||
regex: &Regex,
|
||||
cli: &Cli,
|
||||
) -> anyhow::Result<Matches, anyhow::Error> {
|
||||
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,
|
||||
|
|
|
|||
Loading…
Reference in a new issue