Skip to content
Myr

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

2 files+20 added−30 removed

modified:src/exec/mod.rs+12 added-17 removed
11 unmodified lines
12use lazy_static::lazy_static;13use regex::Regex;1415use crate::exit_codes::ExitCode;16use crate::filesystem::strip_current_dir;1718use self::command::execute_command;19use self::input::{basename, dirname, remove_extension};20pub use self::job::{batch, job};
107 unmodified lines
128 fn number_of_tokens(&self) -> usize {129 self.args.iter().filter(|arg| arg.has_tokens()).count()130 }131131 fn prepare_path(input: &Path) -> String {132 input133 .strip_prefix(".")134 .unwrap_or(input)135 .to_string_lossy()136 .into_owned()137 }138132 /// 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);138139 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 lines
156 cmd.stdin(Stdio::inherit());157 cmd.stdout(Stdio::inherit());158 cmd.stderr(Stdio::inherit());159167 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;162163 for arg in &self.args[1..] {164 if arg.has_tokens() {
1 unmodified line
166167 // 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 lines
200 _ => false,201 }202 }203211 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::*;206207 let path = path.as_ref().to_string_lossy(); // TODO208209 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 lines
modified:src/filesystem.rs+8 added-13 removed
3 unmodified lines
4use std::fs;5use std::io;6#[cfg(any(unix, target_os = "redox"))]7use std::os::unix::fs::PermissionsExt;8use std::path::{Component, Path, PathBuf};8use std::path::{Path, PathBuf};910use crate::walk;1112pub fn path_absolute_form(path: &Path) -> io::Result<PathBuf> {
70 unmodified lines
83 }84}8586/// Remove the `./` prefix from a path.87pub 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()87pub fn strip_current_dir(path: &Path) -> &Path {88 path.strip_prefix(".").unwrap_or(path)89}9091pub fn replace_path_separator<'a>(path: &str, new_path_separator: &str) -> String {92 path.replace(std::path::MAIN_SEPARATOR, &new_path_separator)
1 unmodified line
9495#[cfg(test)]96mod tests {97 use super::strip_current_dir;103 use std::path::{Path, PathBuf};98 use std::path::Path;99100 #[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}