Use RegexSet instead of hand-written parser
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Martin Larralde · committed by David Peter · parent 86fe997 · Browse files · 4 files·+17 added−54 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+17 added−54 removed
- other:+17 added−54 removed
Provenance
- Commit
370d9f0- Tree
e1e1d36- Author
Martin Larralde <martin.larralde@ens-cachan.fr>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
86fe997
4 files changed+17 added−54 removed
modified:src/internal.rs+2 added-1 removed
14 unmodified lines15 use exec::CommandTemplate;16 use lscolors::LsColors;17 use walk::FileType;18 use regex_syntax::{Expr, ExprBuilder};19+use regex::RegexSet;20 21 /// Configuration options for *fd*.22 pub struct FdOptions {23 /// Whether the search is case-sensitive or case-insensitive.41 unmodified lines65 66 /// The extension to search for. Only entries matching the extension will be included.67 ///68 /// The value (if present) will be a lowercase string without leading dots.68- pub extensions: Option<HashSet<String>>,69+ pub extensions: Option<RegexSet>,70 71 /// If a value is supplied, each item found will be used to generate and execute commands.72 pub command: Option<CommandTemplate>,73 156 unmodified linesmodified:src/main.rs+10 added-4 removed
23 unmodified lines24 mod app;25 mod exec;26 mod internal;27 mod output;28-mod utils;28 mod walk;29 30 #[cfg(windows)]31 mod windows;4 unmodified lines36 use std::sync::Arc;37 use std::time;38 39 use atty::Stream;41-use regex::RegexBuilder;40+use regex::{RegexBuilder, RegexSetBuilder};41 42 use exec::CommandTemplate;43 use internal::{error, pattern_has_uppercase_char, transform_args_with_exec, FdOptions};44 use lscolors::LsColors;106 unmodified lines151 })152 .collect(),153 },154 extensions: matches.values_of("extension").map(|exts| {156- exts.map(|e| String::from(".") + &e.trim_left_matches('.'))157- .collect()155+ let patterns = exts.map(|e| e.trim_left_matches('.'))156+ .map(|e| format!(r".\.{}$", regex::escape(e)));157+ match RegexSetBuilder::new(patterns)158+ .case_insensitive(true)159+ .build()160+ {161+ Ok(re) => re,162+ Err(err) => error(err.description()),163+ }164 }),165 command,166 exclude_patterns: matches167 .values_of("exclude")13 unmodified linesdeleted:src/utils.rs-45 removed
1-// Copyright (c) 2017 fd developers2-// Licensed under the Apache License, Version 2.03-// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>4-// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,5-// at your option. All files in the project carrying such6-// notice may not be copied, modified, or distributed except7-// according to those terms.8-9-use std::path::Path;10-use std::iter::Iterator;11-12-/// Determine if an os string ends with any of the given extensions (case insensitive).13-pub fn path_has_any_extension<'a, I>(path: &Path, exts: I) -> bool14-where15- I: 'a + Iterator<Item = &'a String> + Clone,16-{17- // TODO: remove these two lines when we drop support for Rust version < 1.23.18- #[allow(unused_imports)]19- use std::ascii::AsciiExt;20-21- if let Some(ref name) = path.file_name() {22- if let Some(ref name_str) = name.to_str() {23- exts.clone().any(|x| {24- let mut it = name_str.chars().rev();25-26- if x.chars()27- .rev()28- .zip(&mut it)29- .all(|(a, b)| a.eq_ignore_ascii_case(&b))30- {31- match it.next() {32- Some('/') | None => false,33- _ => true,34- }35- } else {36- false37- }38- })39- } else {40- false41- }42- } else {43- false44- }45-}modified:src/walk.rs+5 added-4 removed
9 unmodified lines10 11 use exec;12 use fshelper;13 use internal::{error, FdOptions, EXITCODE_SIGINT, MAX_BUFFER_LENGTH};14-use utils::path_has_any_extension;14 use output;15 16 use std::process;17 use std::path::PathBuf;194 unmodified lines212 return ignore::WalkState::Continue;213 }214 215 // Filter out unwanted extensions.217- if let Some(ref filter_exts) = config.extensions {218- if !path_has_any_extension(entry_path, filter_exts.iter()) {219- return ignore::WalkState::Continue;216+ if let Some(ref exts_regex) = config.extensions {217+ if let Some(path_str) = entry_path.file_name().map_or(None, |s| s.to_str()) {218+ if !exts_regex.is_match(path_str) {219+ return ignore::WalkState::Continue;220+ }221 }222 }223 224 let search_str_o = if config.search_full_path {30 unmodified lines