Skip to content
Myr

Implement glob-based searches

Before adoption: this commit came in with the history the project adopted, so no landing records it.

closes #284

authored by sharkdp · committed by David Peter · parent 641594c · Browse files · 6 files+104 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+104 added−4 removed

  • tests:+74 added−0 removed
  • other:+30 added−4 removed

6 files+104 added−4 removed

modified:src/internal/mod.rs+8 added-2 removed
8 unmodified lines
9use std::borrow::Cow;10use std::ffi::{OsStr, OsString};1112use regex_syntax::hir::Hir;13use regex_syntax::Parser;13use regex_syntax::ParserBuilder;1415pub use self::file_types::FileTypes;1617mod file_types;
28 unmodified lines
46}4748/// Determine if a regex pattern contains a literal uppercase character.49pub fn pattern_has_uppercase_char(pattern: &str) -> bool {50 Parser::new()50 let mut parser = ParserBuilder::new().allow_invalid_utf8(true).build();5152 parser53 .parse(pattern)54 .map(|hir| hir_has_uppercase_char(&hir))55 .unwrap_or(false)56}
3 unmodified lines
60 use regex_syntax::hir::*;6162 match *hir.kind() {63 HirKind::Literal(Literal::Unicode(c)) => c.is_uppercase(),64 HirKind::Literal(Literal::Byte(b)) => char::from(b).is_uppercase(),65 HirKind::Class(Class::Unicode(ref ranges)) => ranges66 .iter()67 .any(|r| r.start().is_uppercase() || r.end().is_uppercase()),68 HirKind::Class(Class::Bytes(ref ranges)) => ranges69 .iter()70 .any(|r| char::from(r.start()).is_uppercase() || char::from(r.end()).is_uppercase()),71 HirKind::Group(Group { ref hir, .. }) | HirKind::Repetition(Repetition { ref hir, .. }) => {72 hir_has_uppercase_char(hir)73 }74 HirKind::Concat(ref hirs) | HirKind::Alternation(ref hirs) => {
123 unmodified lines
modified:src/app.rs+9 added
66 unmodified lines
67 .short("i")68 .overrides_with("case-sensitive"),69 )70 .arg(71 arg("glob")72 .long("glob")73 .short("g")74 .conflicts_with("fixed-strings"),75 )76 .arg(77 arg("fixed-strings")78 .long("fixed-strings")79 .short("F")80 .alias("literal"),
174 unmodified lines
255 doc!(h, "ignore-case"256 , "Case-insensitive search (default: smart case)"257 , "Perform a case-insensitive search. By default, fd uses case-insensitive searches, \258 unless the pattern contains an uppercase character (smart case).");259 doc!(h, "glob"260 , "Glob-based search (default: regular expression)"261 , "Perform a glob-based search instead of a regular expression search.");262 doc!(h, "fixed-strings"263 , "Treat the pattern as a literal string"264 , "Treat the pattern as a literal string instead of a regular expression.");265 doc!(h, "absolute-path"
134 unmodified lines
modified:src/main.rs+11 added-2 removed
22 unmodified lines
23use std::sync::Arc;24use std::time;2526use atty::Stream;27use globset::Glob;28use lscolors::LsColors;29use regex::bytes::{RegexBuilder, RegexSetBuilder};3031use crate::exec::CommandTemplate;
64 unmodified lines
96 sep = std::path::MAIN_SEPARATOR,97 );98 }9999 // Treat pattern as literal string if '--fixed-strings' is used100 let pattern_regex = if matches.is_present("fixed-strings") {100 let pattern_regex = if matches.is_present("glob") {101 let glob = match Glob::new(pattern) {102 Ok(glob) => glob,103 Err(e) => {104 print_error_and_exit!("{}", e);105 }106 };107 glob.regex().to_owned()108 } else if matches.is_present("fixed-strings") {109 // Treat pattern as literal string if '--fixed-strings' is used110 regex::escape(pattern)111 } else {112 String::from(pattern)113 };
165 unmodified lines
modified:tests/tests.rs+74 added
237 unmodified lines
238 one/two/C.Foo2",239 );240}241242/// Glob-based searches (--glob)243#[test]244fn test_glob_searches() {245 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);246247 te.assert_output(248 &["--glob", "*.foo"],249 "a.foo250 one/b.foo251 one/two/c.foo252 one/two/three/d.foo",253 );254255 te.assert_output(256 &["--glob", "[a-c].foo"],257 "a.foo258 one/b.foo259 one/two/c.foo",260 );261262 te.assert_output(263 &["--glob", "[a-c].foo*"],264 "a.foo265 one/b.foo266 one/two/C.Foo2267 one/two/c.foo",268 );269}270271/// Glob-based searches (--glob) in combination with full path searches (--full-path)272#[test]273fn test_full_path_glob_searches() {274 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);275276 te.assert_output(277 &["--glob", "--full-path", "**/one/**/*.foo"],278 "one/b.foo279 one/two/c.foo280 one/two/three/d.foo",281 );282}283284#[test]285fn test_smart_case_glob_searches() {286 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);287288 te.assert_output(289 &["--glob", "c.foo*"],290 "one/two/C.Foo2291 one/two/c.foo",292 );293294 te.assert_output(&["--glob", "C.Foo*"], "one/two/C.Foo2");295}296297/// Glob-based searches (--glob) in combination with --case-sensitive298#[test]299fn test_case_sensitive_glob_searches() {300 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);301302 te.assert_output(&["--glob", "--case-sensitive", "c.foo*"], "one/two/c.foo");303}304305/// Glob-based searches (--glob) in combination with --extension306#[test]307fn test_glob_searches_with_extension() {308 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);309310 te.assert_output(311 &["--glob", "--extension", "foo2", "[a-z].*"],312 "one/two/C.Foo2",313 );314}315316/// Full path search (--full-path)317#[test]318fn test_full_path() {319 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
1024 unmodified lines
modified:Cargo.lock+1 added
102 unmodified lines
103 "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",104 "ctrlc 3.1.3 (registry+https://github.com/rust-lang/crates.io-index)",105 "diff 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",106 "filetime 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",107 "globset 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",108 "humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",109 "ignore 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",110 "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",111 "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
362 unmodified lines
modified:Cargo.toml+1 added