Implement -F/--fixed-strings/--literal
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by David Peter · parent 089c183 · Browse files · 5 files·+53 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+53 added−3 removed
- tests:+30 added−0 removed
- other:+23 added−3 removed
Provenance
- Commit
814d370- Tree
f99d51e- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
089c183
5 files changed+53 added−3 removed
modified:doc/fd.1+3 added
45 unmodified lines46 .TP47 .B \-i, \-\-ignore\-case48 Perform a case-insensitive search (default: smart case).49 .TP50+.B \-F, \-\-fixed\-strings51+Treat the pattern as a literal string instead of a regular expression.52+.TP53 .B \-a, \-\-absolute\-path54 Show absolute instead of relative paths.55 .TP56 .B \-L, \-\-follow95 unmodified linesmodified:src/app.rs+9 added
59 unmodified lines60 .long("ignore-case")61 .short("i")62 .overrides_with("case-sensitive"),63 )64+ .arg(65+ arg("fixed-strings")66+ .long("fixed-strings")67+ .short("F")68+ .alias("literal"),69+ )70 .arg(arg("absolute-path").long("absolute-path").short("a"))71 .arg(arg("follow").long("follow").short("L").alias("dereference"))72 .arg(arg("full-path").long("full-path").short("p"))73 .arg(arg("null_separator").long("print0").short("0"))84 unmodified lines158 doc!(h, "ignore-case"159 , "Case-insensitive search (default: smart case)"160 , "Perform a case-insensitive search. By default, fd uses case-insensitive searches, \161 unless the pattern contains an uppercase character (smart case).");162+ doc!(h, "fixed-strings"163+ , "Treat the pattern as a literal string"164+ , "Treat the pattern as a literal string instead of a regular expression.");165 doc!(h, "absolute-path"166 , "Show absolute instead of relative paths"167 , "Shows the full path starting from the root as opposed to relative paths.");168 doc!(h, "follow"67 unmodified linesmodified:src/main.rs+9 added-2 removed
79 unmodified lines80 .map(|path_buffer| fshelper::absolute_path(path_buffer).unwrap())81 .collect();82 }83 84+ // Treat pattern as literal string if '--fixed-strings' is used85+ let pattern_regex = if matches.is_present("fixed-strings") {86+ regex::escape(pattern)87+ } else {88+ String::from(pattern)89+ };90+91 // The search will be case-sensitive if the command line flag is set or92 // if the pattern has an uppercase character (smart case).93 let case_sensitive = !matches.is_present("ignore-case")87- && (matches.is_present("case-sensitive") || pattern_has_uppercase_char(pattern));94+ && (matches.is_present("case-sensitive") || pattern_has_uppercase_char(&pattern_regex));95 96 let colored_output = match matches.value_of("color") {97 Some("always") => true,98 Some("never") => false,76 unmodified lines175 .map(|v| v.map(|p| String::from("!") + p).collect())176 .unwrap_or_else(|| vec![]),177 };178 172- match RegexBuilder::new(pattern)179+ match RegexBuilder::new(&pattern_regex)180 .case_insensitive(!config.case_sensitive)181 .dot_matches_new_line(true)182 .build()183 {4 unmodified linesmodified:tests/tests.rs+30 added
789 unmodified lines790 791 te.assert_output(&["e1", exec_style, "printf", "%s.%s\n"], "e1 e2.");792 }793 }794+795+/// Literal search (--fixed-strings)796+#[test]797+fn test_fixed_strings() {798+ let dirs = &["test1", "test2"];799+ let files = &["test1/a.foo", "test1/a_foo", "test2/Download (1).tar.gz"];800+ let te = TestEnv::new(dirs, files);801+802+ // Regex search, dot is treated as "any character"803+ te.assert_output(804+ &["a.foo"],805+ "test1/a.foo806+ test1/a_foo",807+ );808+809+ // Literal search, dot is treated as character810+ te.assert_output(&["--fixed-strings", "a.foo"], "test1/a.foo");811+812+ // Regex search, parens are treated as group813+ te.assert_output(&["download (1)"], "");814+815+ // Literal search, parens are treated as characters816+ te.assert_output(817+ &["--fixed-strings", "download (1)"],818+ "test2/Download (1).tar.gz",819+ );820+821+ // Combine with --case-sensitive822+ te.assert_output(&["--fixed-strings", "--case-sensitive", "download (1)"], "");823+}modified:README.md+2 added-1 removed
209 unmodified lines210 -I, --no-ignore Do not respect .(git)ignore files211 --no-ignore-vcs Do not respect .gitignore files212 -s, --case-sensitive Case-sensitive search (default: smart case)213 -i, --ignore-case Case-insensitive search (default: smart case)214+ -F, --fixed-strings Treat the pattern as a literal string215 -a, --absolute-path Show absolute instead of relative paths216 -L, --follow Follow symbolic links217 -p, --full-path Search full path (default: file-/dirname only)218 -0, --print0 Separate results by the null character3 unmodified lines222 OPTIONS:223 -d, --max-depth <depth> Set maximum search depth (default: none)224 -t, --type <filetype>... Filter by type: f(ile), d(irectory), (sym)l(ink)225 -e, --extension <ext>... Filter by file extension225- -x, --exec <cmd>... Execute a command for each search result226+ -x, --exec <cmd> Execute a command for each search result227 -E, --exclude <pattern>... Exclude entries that match the given glob pattern228 -c, --color <when> When to use colors: never, *auto*, always229 -j, --threads <num> Set number of threads to use for searching & executing230 127 unmodified lines