Improve smart case to only consider literal uppercase chars (#103)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Matthias Reitinger · committed by David Peter · parent 8fc3a83 · Browse files · 5 files·+40 added−6 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+40 added−6 removed
- tests:+8 added−0 removed
- other:+32 added−6 removed
Provenance
- Commit
b441528- Tree
c95c763- Author
Matthias Reitinger <reima@edenprime.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
8fc3a83
5 files changed+40 added−6 removed
modified:src/internal.rs+26 added
2 unmodified lines3 use std::io::Write;4 5 use lscolors::LsColors;6 use walk::FileType;7+use regex_syntax::{Expr, ExprBuilder};8 9 /// Root directory10 #[cfg(unix)]11 pub static ROOT_DIR: &'static str = "/";66 unmodified lines78 pub fn error(message: &str) -> ! {79 writeln!(&mut ::std::io::stderr(), "{}", message).expect("Failed writing to stderr");80 process::exit(1);81 }82+83+/// Determine if a regex pattern contains a literal uppercase character.84+pub fn pattern_has_uppercase_char(pattern: &str) -> bool {85+ ExprBuilder::new()86+ .parse(pattern)87+ .map(|expr| expr_has_uppercase_char(&expr))88+ .unwrap_or(false)89+}90+91+/// Determine if a regex expression contains a literal uppercase character.92+fn expr_has_uppercase_char(expr: &Expr) -> bool {93+ match *expr {94+ Expr::Literal { ref chars, .. } => chars.iter().any(|c| c.is_uppercase()),95+ Expr::Class(ref ranges) => {96+ ranges.iter().any(|r| {97+ r.start.is_uppercase() || r.end.is_uppercase()98+ })99+ }100+ Expr::Group { ref e, .. } => expr_has_uppercase_char(e),101+ Expr::Repeat { ref e, .. } => expr_has_uppercase_char(e),102+ Expr::Concat(ref es) => es.iter().any(expr_has_uppercase_char),103+ Expr::Alternate(ref es) => es.iter().any(expr_has_uppercase_char),104+ _ => false,105+ }106+}modified:src/main.rs+4 added-6 removed
1 unmodified line2 extern crate clap;3 extern crate ansi_term;4 extern crate atty;5 extern crate regex;6+extern crate regex_syntax;7 extern crate ignore;8 extern crate num_cpus;9 10 pub mod lscolors;11 unmodified lines22 23 use atty::Stream;24 use regex::RegexBuilder;25 25-use internal::{error, FdOptions, PathDisplay, ROOT_DIR};26+use internal::{error, pattern_has_uppercase_char, FdOptions, PathDisplay, ROOT_DIR};27 use lscolors::LsColors;28 use walk::FileType;29 30 fn main() {34 unmodified lines65 let root_dir = root_dir_buf.as_path();66 67 // The search will be case-sensitive if the command line flag is set or68 // if the pattern has an uppercase character (smart case).68- let case_sensitive = if !matches.is_present("ignore-case") {69- matches.is_present("case-sensitive") || pattern.chars().any(char::is_uppercase)70- } else {71- false72- };69+ let case_sensitive = !matches.is_present("ignore-case") &&70+ (matches.is_present("case-sensitive") || pattern_has_uppercase_char(pattern));71 72 let colored_output = match matches.value_of("color") {73 Some("always") => true,74 Some("never") => false,68 unmodified linesmodified:tests/tests.rs+8 added
113 unmodified lines114 115 te.assert_output(&["C.Foo"], "one/two/C.Foo2");116 117 te.assert_output(&["Foo"], "one/two/C.Foo2");118+119+ // Only literal uppercase chars should trigger case sensitivity.120+ te.assert_output(121+ &["\\Ac"],122+ "one/two/c.foo123+ one/two/C.Foo2",124+ );125+ te.assert_output(&["\\AC"], "one/two/C.Foo2");126 }127 128 /// Case sensitivity (--case-sensitive)129 #[test]289 unmodified linesmodified:Cargo.lock+1 added
7 unmodified lines8 "diff 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",9 "ignore 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",10 "num_cpus 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)",11 "regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",12+ "regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",13 "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",14 ]15 16 [[package]]349 unmodified linesmodified:Cargo.toml+1 added
23 unmodified lines2425262728293031325 unmodified lines23 unmodified linesansi_term = "0.9"clap = "2.26.0"atty = "0.2"regex = "0.2"regex-syntax = "0.4"ignore = "0.2"num_cpus = "1.6.2"
[build-dependencies]5 unmodified lines