Implement smart-case, closes #7

This commit is contained in:
sharkdp 2017-05-12 13:32:30 +02:00
parent fba5e02c25
commit 6482f3ac09
2 changed files with 9 additions and 3 deletions

View File

@ -5,6 +5,8 @@ A modern, convenient and fast replacement for `find`.
* Easy syntax: `fd PATTERN` instead of `find -iname '*PATTERN*'`.
* Colored output.
* Regular expressions.
* Smart case: the search is case-insensitive by default, but will be
case-sensitive if the pattern contains an uppercase character.
* The command name is *50%* shorter than `find` :-).
## Examples

View File

@ -82,8 +82,6 @@ fn main() {
process::exit(1);
}
let case_insensitive = !matches.opt_present("s");
let empty = String::new();
let pattern = matches.free.get(0).unwrap_or(&empty);
@ -93,8 +91,14 @@ fn main() {
};
let current_dir = current_dir_buf.as_path();
// The search will be case-sensitive if the command line flag is set or if
// the pattern has an uppercase character (smart case).
let case_sensitive =
matches.opt_present("s") ||
pattern.chars().any(char::is_uppercase);
match RegexBuilder::new(pattern)
.case_insensitive(case_insensitive)
.case_insensitive(!case_sensitive)
.build() {
Ok(re) => scan(current_dir, &re),
Err(err) => error(err.description())