Skip to content

Commit

Permalink
simplify building regexps for multiple expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Uthar committed Nov 8, 2022
1 parent f207e53 commit 0324526
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use atty::Stream;
use clap::{CommandFactory, Parser};
use globset::GlobBuilder;
use lscolors::LsColors;
use regex::bytes::{RegexBuilder, RegexSetBuilder};
use regex::bytes::{RegexBuilder, RegexSetBuilder, Regex};

use crate::cli::{ColorWhen, Opts};
use crate::config::Config;
Expand Down Expand Up @@ -84,35 +84,24 @@ fn run() -> Result<ExitCode> {
let pattern = &opts.pattern;
let exprs = &opts.exprs;
let empty = Vec::new();
let patterns = exprs

let pattern_regexps = exprs
.as_ref()
.unwrap_or(&empty)
.into_iter()
.chain(vec![pattern])
.collect::<Vec<&String>>();
let pattern_regexps = patterns
.iter()
.map(|pat| {
let re = build_pattern_regex(pat, &opts);
match re {
Ok(r) => r,
Err(e) => panic!("Bad pattern: {}", e),
}
})
.collect::<Vec<String>>();
.chain([pattern])
.map(|pat| { build_pattern_regex(pat, &opts) })
.collect::<Result<Vec<String>>>()?;

let config = construct_config(opts, &pattern_regexps)?;

ensure_use_hidden_option_for_leading_dot_pattern(&config, &pattern_regexps)?;

let regexps = pattern_regexps
.iter()
.map(|pat| {
let regex = build_regex(pat.to_string(), &config);
match regex {
Ok(r) => r,
Err(e) => panic!("Bad regex: {}", e),
}
})
.collect();
.map(|pat| { build_regex(pat.to_string(), &config) })
.collect::<Result<Vec<Regex>>>()?;

walk::scan(&search_paths, Arc::new(regexps), Arc::new(config))
}

Expand Down Expand Up @@ -192,7 +181,7 @@ fn check_path_separator_length(path_separator: Option<&str>) -> Result<()> {
}
}

fn construct_config(mut opts: Opts, pattern_regexps: &Vec<String>) -> Result<Config> {
fn construct_config(mut opts: Opts, pattern_regexps: &[String]) -> Result<Config> {
// The search will be case-sensitive if the command line flag is set or
// if the pattern has an uppercase character (smart case).
let case_sensitive = !opts.ignore_case
Expand Down Expand Up @@ -438,7 +427,7 @@ fn extract_time_constraints(opts: &Opts) -> Result<Vec<TimeFilter>> {

fn ensure_use_hidden_option_for_leading_dot_pattern(
config: &Config,
pattern_regexps: &Vec<String>,
pattern_regexps: &[String],
) -> Result<()> {
if cfg!(unix)
&& config.ignore_hidden
Expand Down

0 comments on commit 0324526

Please sign in to comment.