Skip to content
Myr

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

5 files+40 added−6 removed

modified:src/internal.rs+26 added
2 unmodified lines
3use std::io::Write;45use lscolors::LsColors;6use walk::FileType;7use regex_syntax::{Expr, ExprBuilder};89/// Root directory10#[cfg(unix)]11pub static ROOT_DIR: &'static str = "/";
66 unmodified lines
78pub fn error(message: &str) -> ! {79 writeln!(&mut ::std::io::stderr(), "{}", message).expect("Failed writing to stderr");80 process::exit(1);81}8283/// Determine if a regex pattern contains a literal uppercase character.84pub 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}9091/// Determine if a regex expression contains a literal uppercase character.92fn 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 line
2extern crate clap;3extern crate ansi_term;4extern crate atty;5extern crate regex;6extern crate regex_syntax;7extern crate ignore;8extern crate num_cpus;910pub mod lscolors;
11 unmodified lines
2223use atty::Stream;24use regex::RegexBuilder;2525use internal::{error, FdOptions, PathDisplay, ROOT_DIR};26use internal::{error, pattern_has_uppercase_char, FdOptions, PathDisplay, ROOT_DIR};27use lscolors::LsColors;28use walk::FileType;2930fn main() {
34 unmodified lines
65 let root_dir = root_dir_buf.as_path();6667 // 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));7172 let colored_output = match matches.value_of("color") {73 Some("always") => true,74 Some("never") => false,
68 unmodified lines
modified:tests/tests.rs+8 added
113 unmodified lines
114115 te.assert_output(&["C.Foo"], "one/two/C.Foo2");116117 te.assert_output(&["Foo"], "one/two/C.Foo2");118119 // 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}127128/// Case sensitivity (--case-sensitive)129#[test]
289 unmodified lines
modified:Cargo.lock+1 added
7 unmodified lines
8 "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]1516[[package]]
349 unmodified lines
modified:Cargo.toml+1 added