fix: excessive separator at the end of the path

This commit is contained in:
alan910127 2024-11-02 03:46:14 +08:00
parent 13f7d888fd
commit 2840d5ba2b

View file

@ -97,25 +97,34 @@ fn path_to_os_str(path: &Path, separator: PathSeparator) -> Cow<'_, OsStr> {
use std::path::Component; use std::path::Component;
let mut s = OsString::with_capacity(path.as_os_str().len()); let mut s = String::with_capacity(path.as_os_str().len());
for component in path.components() { for component in path.components() {
match component { match component {
Component::RootDir => {} Component::RootDir => {}
Component::CurDir => s.push("."), Component::CurDir => s.push('.'),
Component::ParentDir => s.push(".."), Component::ParentDir => s.push_str(".."),
Component::Normal(path) => s.push(path), Component::Normal(path) => s.push_str(&path.to_string_lossy()),
Component::Prefix(prefix) => { Component::Prefix(prefix) => {
// "C:\foo" => [Prefix("C:"), RootDir, Normal(foo)] // "C:\foo" => [Prefix("C:"), RootDir, Normal(foo)]
s.push(prefix.as_os_str()); s.push_str(&prefix.as_os_str().to_string_lossy());
// If we push a "/" below, we will met a RootDir and push a "/" // If we push a "/" below, we will met a RootDir and push a "/"
// again resulting in "C://". So we need to skip that. // again resulting in "C://". So we need to skip that.
continue; continue;
} }
}; };
s.push("/"); s.push('/');
} }
return Cow::Owned(s); use std::os::windows::ffi::OsStrExt as _;
use std::path::MAIN_SEPARATOR;
let path_ends_with_main_sep =
path.as_os_str().encode_wide().last() == Some(MAIN_SEPARATOR as u16);
if !path_ends_with_main_sep && s != "/" && s.ends_with('/') {
s.pop();
}
return Cow::Owned(OsString::from(s));
} }
#[cfg(test)] #[cfg(test)]