Remove duplicate 'strip prefix' function
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 e44f2f8 · Browse files · 2 files·+20 added−30 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+20 added−30 removed
- other:+20 added−30 removed
Provenance
- Commit
ed45d1c- Tree
8c1f39e- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
e44f2f8
2 files changed+20 added−30 removed
modified:src/exec/mod.rs+12 added-17 removed
11 unmodified lines12 use lazy_static::lazy_static;13 use regex::Regex;14 15 use crate::exit_codes::ExitCode;16+use crate::filesystem::strip_current_dir;17 18 use self::command::execute_command;19 use self::input::{basename, dirname, remove_extension};20 pub use self::job::{batch, job};107 unmodified lines128 fn number_of_tokens(&self) -> usize {129 self.args.iter().filter(|arg| arg.has_tokens()).count()130 }131 131- fn prepare_path(input: &Path) -> String {132- input133- .strip_prefix(".")134- .unwrap_or(input)135- .to_string_lossy()136- .into_owned()137- }138-132 /// Generates and executes a command.133 ///134 /// Using the internal `args` field, and a supplied `input` variable, a `Command` will be135 /// build. Once all arguments have been processed, the command is executed.136 pub fn generate_and_execute(&self, input: &Path, out_perm: Arc<Mutex<()>>) -> ExitCode {144- let input = Self::prepare_path(input);137+ let input = strip_current_dir(input);138 139 let mut cmd = Command::new(self.args[0].generate(&input).as_ref());140 for arg in &self.args[1..] {141 cmd.arg(arg.generate(&input).as_ref());14 unmodified lines156 cmd.stdin(Stdio::inherit());157 cmd.stdout(Stdio::inherit());158 cmd.stderr(Stdio::inherit());159 167- let mut paths: Vec<String> = paths.map(|p| Self::prepare_path(&p)).collect();160+ let mut paths: Vec<_> = paths.collect();161 let mut has_path = false;162 163 for arg in &self.args[1..] {164 if arg.has_tokens() {1 unmodified line166 167 // A single `Tokens` is expected168 // So we can directly consume the iterator once and for all169 for path in &mut paths {177- cmd.arg(arg.generate(&path).as_ref());170+ cmd.arg(arg.generate(strip_current_dir(path)).as_ref());171 has_path = true;172 }173 } else {174 cmd.arg(arg.generate("").as_ref());25 unmodified lines200 _ => false,201 }202 }203 211- pub fn generate<'a>(&'a self, path: &str) -> Cow<'a, str> {204+ pub fn generate<'a>(&'a self, path: impl AsRef<Path>) -> Cow<'a, str> {205 use self::Token::*;206 207+ let path = path.as_ref().to_string_lossy(); // TODO208+209 match *self {210 ArgumentTemplate::Tokens(ref tokens) => {211 let mut s = String::new();212 for token in tokens {213 match *token {219- Basename => s += basename(path),220- BasenameNoExt => s += remove_extension(basename(path)),221- NoExt => s += remove_extension(path),222- Parent => s += dirname(path),223- Placeholder => s += path,214+ Basename => s += basename(&path),215+ BasenameNoExt => s += remove_extension(basename(&path)),216+ NoExt => s += remove_extension(&path),217+ Parent => s += dirname(&path),218+ Placeholder => s += &path,219 Text(ref string) => s += string,220 }221 }222 Cow::Owned(s)118 unmodified linesmodified:src/filesystem.rs+8 added-13 removed
3 unmodified lines4 use std::fs;5 use std::io;6 #[cfg(any(unix, target_os = "redox"))]7 use std::os::unix::fs::PermissionsExt;8-use std::path::{Component, Path, PathBuf};8+use std::path::{Path, PathBuf};9 10 use crate::walk;11 12 pub fn path_absolute_form(path: &Path) -> io::Result<PathBuf> {70 unmodified lines83 }84 }85 86 /// Remove the `./` prefix from a path.87-pub fn strip_current_dir(pathbuf: &PathBuf) -> &Path {88- let mut iter = pathbuf.components();89- let mut iter_next = iter.clone();90- if iter_next.next() == Some(Component::CurDir) {91- iter.next();92- }93- iter.as_path()87+pub fn strip_current_dir(path: &Path) -> &Path {88+ path.strip_prefix(".").unwrap_or(path)89 }90 91 pub fn replace_path_separator<'a>(path: &str, new_path_separator: &str) -> String {92 path.replace(std::path::MAIN_SEPARATOR, &new_path_separator)1 unmodified line94 95 #[cfg(test)]96 mod tests {97 use super::strip_current_dir;103- use std::path::{Path, PathBuf};98+ use std::path::Path;99 100 #[test]101 fn strip_current_dir_basic() {107- assert_eq!(strip_current_dir(&PathBuf::from("./foo")), Path::new("foo"));108- assert_eq!(strip_current_dir(&PathBuf::from("foo")), Path::new("foo"));102+ assert_eq!(strip_current_dir(Path::new("./foo")), Path::new("foo"));103+ assert_eq!(strip_current_dir(Path::new("foo")), Path::new("foo"));104 assert_eq!(110- strip_current_dir(&PathBuf::from("./foo/bar/baz")),105+ strip_current_dir(Path::new("./foo/bar/baz")),106 Path::new("foo/bar/baz")107 );108 assert_eq!(114- strip_current_dir(&PathBuf::from("foo/bar/baz")),109+ strip_current_dir(Path::new("foo/bar/baz")),110 Path::new("foo/bar/baz")111 );112 }113 }