Skip to content

Commit

Permalink
Implement glob-based searches
Browse files Browse the repository at this point in the history
closes #284
  • Loading branch information
sharkdp committed Sep 15, 2019
1 parent fb42623 commit 2e2a756
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ regex-syntax = "0.6"
ctrlc = "3.1"
humantime = "1.1.1"
lscolors = "0.5"
globset = "0.4"

[dependencies.clap]
version = "2.31.2"
Expand Down
9 changes: 9 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ pub fn build_app() -> App<'static, 'static> {
.short("i")
.overrides_with("case-sensitive"),
)
.arg(
arg("glob")
.long("glob")
.short("g")
.conflicts_with("fixed-strings"),
)
.arg(
arg("fixed-strings")
.long("fixed-strings")
Expand Down Expand Up @@ -250,6 +256,9 @@ fn usage() -> HashMap<&'static str, Help> {
, "Case-insensitive search (default: smart case)"
, "Perform a case-insensitive search. By default, fd uses case-insensitive searches, \
unless the pattern contains an uppercase character (smart case).");
doc!(h, "glob"
, "Glob-based search (default: regular expression)"
, "Perform a glob-based search instead of a regular expression search.");
doc!(h, "fixed-strings"
, "Treat the pattern as a literal string"
, "Treat the pattern as a literal string instead of a regular expression.");
Expand Down
10 changes: 8 additions & 2 deletions src/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::borrow::Cow;
use std::ffi::{OsStr, OsString};

use regex_syntax::hir::Hir;
use regex_syntax::Parser;
use regex_syntax::ParserBuilder;

pub use self::file_types::FileTypes;

Expand Down Expand Up @@ -47,7 +47,9 @@ pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {

/// Determine if a regex pattern contains a literal uppercase character.
pub fn pattern_has_uppercase_char(pattern: &str) -> bool {
Parser::new()
let mut parser = ParserBuilder::new().allow_invalid_utf8(true).build();

parser
.parse(pattern)
.map(|hir| hir_has_uppercase_char(&hir))
.unwrap_or(false)
Expand All @@ -59,9 +61,13 @@ fn hir_has_uppercase_char(hir: &Hir) -> bool {

match *hir.kind() {
HirKind::Literal(Literal::Unicode(c)) => c.is_uppercase(),
HirKind::Literal(Literal::Byte(b)) => char::from(b).is_uppercase(),
HirKind::Class(Class::Unicode(ref ranges)) => ranges
.iter()
.any(|r| r.start().is_uppercase() || r.end().is_uppercase()),
HirKind::Class(Class::Bytes(ref ranges)) => ranges
.iter()
.any(|r| char::from(r.start()).is_uppercase() || char::from(r.end()).is_uppercase()),
HirKind::Group(Group { ref hir, .. }) | HirKind::Repetition(Repetition { ref hir, .. }) => {
hir_has_uppercase_char(hir)
}
Expand Down
13 changes: 11 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use std::sync::Arc;
use std::time;

use atty::Stream;
use globset::Glob;
use lscolors::LsColors;
use regex::bytes::{RegexBuilder, RegexSetBuilder};

Expand Down Expand Up @@ -96,8 +97,16 @@ fn main() {
);
}

// Treat pattern as literal string if '--fixed-strings' is used
let pattern_regex = if matches.is_present("fixed-strings") {
let pattern_regex = if matches.is_present("glob") {
let glob = match Glob::new(pattern) {
Ok(glob) => glob,
Err(e) => {
print_error_and_exit!("{}", e);
}
};
glob.regex().to_owned()
} else if matches.is_present("fixed-strings") {
// Treat pattern as literal string if '--fixed-strings' is used
regex::escape(pattern)
} else {
String::from(pattern)
Expand Down
74 changes: 74 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,80 @@ fn test_case_insensitive() {
);
}

/// Glob-based searches (--glob)
#[test]
fn test_glob_searches() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);

te.assert_output(
&["--glob", "*.foo"],
"a.foo
one/b.foo
one/two/c.foo
one/two/three/d.foo",
);

te.assert_output(
&["--glob", "[a-c].foo"],
"a.foo
one/b.foo
one/two/c.foo",
);

te.assert_output(
&["--glob", "[a-c].foo*"],
"a.foo
one/b.foo
one/two/C.Foo2
one/two/c.foo",
);
}

/// Glob-based searches (--glob) in combination with full path searches (--full-path)
#[test]
fn test_full_path_glob_searches() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);

te.assert_output(
&["--glob", "--full-path", "**/one/**/*.foo"],
"one/b.foo
one/two/c.foo
one/two/three/d.foo",
);
}

#[test]
fn test_smart_case_glob_searches() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);

te.assert_output(
&["--glob", "c.foo*"],
"one/two/C.Foo2
one/two/c.foo",
);

te.assert_output(&["--glob", "C.Foo*"], "one/two/C.Foo2");
}

/// Glob-based searches (--glob) in combination with --case-sensitive
#[test]
fn test_case_sensitive_glob_searches() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);

te.assert_output(&["--glob", "--case-sensitive", "c.foo*"], "one/two/c.foo");
}

/// Glob-based searches (--glob) in combination with --extension
#[test]
fn test_glob_searches_with_extension() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);

te.assert_output(
&["--glob", "--extension", "foo2", "[a-z].*"],
"one/two/C.Foo2",
);
}

/// Full path search (--full-path)
#[test]
fn test_full_path() {
Expand Down

0 comments on commit 2e2a756

Please sign in to comment.