Skip to content
Myr

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

4 files+17 added−54 removed

modified:src/internal.rs+2 added-1 removed
14 unmodified lines
15use exec::CommandTemplate;16use lscolors::LsColors;17use walk::FileType;18use regex_syntax::{Expr, ExprBuilder};19use regex::RegexSet;2021/// Configuration options for *fd*.22pub struct FdOptions {23 /// Whether the search is case-sensitive or case-insensitive.
41 unmodified lines
6566 /// 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>,7071 /// If a value is supplied, each item found will be used to generate and execute commands.72 pub command: Option<CommandTemplate>,73
156 unmodified lines
modified:src/main.rs+10 added-4 removed
23 unmodified lines
24mod app;25mod exec;26mod internal;27mod output;28mod utils;28mod walk;2930#[cfg(windows)]31mod windows;
4 unmodified lines
36use std::sync::Arc;37use std::time;3839use atty::Stream;41use regex::RegexBuilder;40use regex::{RegexBuilder, RegexSetBuilder};4142use exec::CommandTemplate;43use internal::{error, pattern_has_uppercase_char, transform_args_with_exec, FdOptions};44use lscolors::LsColors;
106 unmodified lines
151 })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 lines
deleted: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.89use std::path::Path;10use std::iter::Iterator;1112/// Determine if an os string ends with any of the given extensions (case insensitive).13pub fn path_has_any_extension<'a, I>(path: &Path, exts: I) -> bool14where15 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;2021 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();2526 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 lines
1011use exec;12use fshelper;13use internal::{error, FdOptions, EXITCODE_SIGINT, MAX_BUFFER_LENGTH};14use utils::path_has_any_extension;14use output;1516use std::process;17use std::path::PathBuf;
194 unmodified lines
212 return ignore::WalkState::Continue;213 }214215 // 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 }223224 let search_str_o = if config.search_full_path {
30 unmodified lines