src/prepend_prefix.rs in faster_path-0.3.7 vs src/prepend_prefix.rs in faster_path-0.3.8
- old
+ new
@@ -1,18 +1,19 @@
+use std::borrow::Cow;
use dirname::dirname;
-use basename::basename;
-use std::path::MAIN_SEPARATOR as SEP;
+use path_parsing::{SEP, contains_sep};
+use std::path::MAIN_SEPARATOR;
-pub fn prepend_prefix(prefix: &str, relpath: &str) -> String {
- let mut prefix: String = prefix.to_string();
+pub fn prepend_prefix<'a>(prefix: &'a str, relpath: &str) -> Cow<'a, str> {
if relpath.is_empty() {
- dirname(&prefix).to_string()
- } else if prefix.contains(&SEP.to_string()[..]) {
- prefix = dirname(&prefix).to_string();
- if basename(&format!("{}{}", &prefix, "a"), "") != "a" {
- prefix = format!("{}{}", prefix, &SEP);
+ dirname(prefix).into()
+ } else if contains_sep(prefix.as_bytes()) {
+ let prefix_dirname = dirname(prefix);
+ match prefix_dirname.as_bytes().last() {
+ None => relpath.to_string().into(),
+ Some(&SEP) => format!("{}{}", prefix_dirname, relpath).into(),
+ _ => format!("{}{}{}", prefix_dirname, MAIN_SEPARATOR, relpath).into()
}
- format!("{}{}", prefix, relpath)
} else {
- format!("{}{}", prefix, relpath)
+ format!("{}{}", prefix, relpath).into()
}
}