Implement multiple suffixes extension support with tests (#214)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Martin Larralde · committed by David Peter · parent b1bb60e · Browse files · 4 files·+71 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+71 added−5 removed
- tests:+22 added−0 removed
- other:+49 added−5 removed
Provenance
- Commit
86fe997- Tree
b3b5f6b- Author
Martin Larralde <martin.larralde@ens-cachan.fr>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
b1bb60e
4 files changed+71 added−5 removed
modified:src/main.rs+2 added-1 removed
23 unmodified lines24 mod app;25 mod exec;26 mod internal;27 mod output;28+mod utils;29 mod walk;30 31 #[cfg(windows)]32 mod windows;119 unmodified lines152 })153 .collect(),154 },155 extensions: matches.values_of("extension").map(|exts| {155- exts.map(|e| e.trim_left_matches('.').to_lowercase())156+ exts.map(|e| String::from(".") + &e.trim_left_matches('.'))157 .collect()158 }),159 command,160 exclude_patterns: matches14 unmodified linesadded:src/utils.rs+45 added
1+// Copyright (c) 2017 fd developers2+// Licensed under the Apache License, Version 2.03+// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>4+// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,5+// at your option. All files in the project carrying such6+// notice may not be copied, modified, or distributed except7+// according to those terms.8+9+use std::path::Path;10+use std::iter::Iterator;11+12+/// Determine if an os string ends with any of the given extensions (case insensitive).13+pub fn path_has_any_extension<'a, I>(path: &Path, exts: I) -> bool14+where15+ I: 'a + Iterator<Item = &'a String> + Clone,16+{17+ // TODO: remove these two lines when we drop support for Rust version < 1.23.18+ #[allow(unused_imports)]19+ use std::ascii::AsciiExt;20+21+ if let Some(ref name) = path.file_name() {22+ if let Some(ref name_str) = name.to_str() {23+ exts.clone().any(|x| {24+ let mut it = name_str.chars().rev();25+26+ if x.chars()27+ .rev()28+ .zip(&mut it)29+ .all(|(a, b)| a.eq_ignore_ascii_case(&b))30+ {31+ match it.next() {32+ Some('/') | None => false,33+ _ => true,34+ }35+ } else {36+ false37+ }38+ })39+ } else {40+ false41+ }42+ } else {43+ false44+ }45+}modified:src/walk.rs+2 added-4 removed
9 unmodified lines10 11 use exec;12 use fshelper;13 use internal::{error, FdOptions, EXITCODE_SIGINT, MAX_BUFFER_LENGTH};14+use utils::path_has_any_extension;15 use output;16 17 use std::process;18 use std::path::PathBuf;195 unmodified lines214 }215 216 // Filter out unwanted extensions.217 if let Some(ref filter_exts) = config.extensions {217- let entry_ext = entry_path218- .extension()219- .map(|e| e.to_string_lossy().to_lowercase());220- if entry_ext.map_or(true, |ext| !filter_exts.contains(&ext)) {218+ if !path_has_any_extension(entry_path, filter_exts.iter()) {219 return ignore::WalkState::Continue;220 }221 }222 31 unmodified linesmodified:tests/tests.rs+22 added
528 unmodified lines529 530 te.assert_output(&["--extension", ".foo", "a"], "a.foo");531 532 te.assert_output(&["--extension", "foo2"], "one/two/C.Foo2");533+534+ let te2 = TestEnv::new(&[], &["spam.bar.baz", "egg.bar.baz", "yolk.bar.baz.sig"]);535+536+ te2.assert_output(537+ &["--extension", ".bar.baz"],538+ "spam.bar.baz539+ egg.bar.baz",540+ );541+542+ te2.assert_output(&["--extension", "sig"], "yolk.bar.baz.sig");543+544+ te2.assert_output(&["--extension", "bar.baz.sig"], "yolk.bar.baz.sig");545+546+ let te3 = TestEnv::new(&[], &["latin1.e\u{301}xt", "smiley.☻"]);547+548+ te3.assert_output(&["--extension", "☻"], "smiley.☻");549+550+ te3.assert_output(&["--extension", ".e\u{301}xt"], "latin1.e\u{301}xt");551+552+ let te4 = TestEnv::new(&[], &[".hidden", "test.hidden"]);553+554+ te4.assert_output(&["--hidden", "--extension", ".hidden"], "test.hidden");555 }556 557 /// Symlinks misc558 #[test]235 unmodified lines