Skip to content

Commit

Permalink
Merge branch 'issue-10' (fix #10)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Jul 3, 2022
2 parents 424c83c + 9179390 commit a72dc60
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
16 changes: 14 additions & 2 deletions src/lib.rs
Expand Up @@ -65,6 +65,18 @@ use std::borrow::Cow;
use std::ffi::OsStr;
use std::path::{Path, PathBuf, MAIN_SEPARATOR};

#[cfg(target_os = "windows")]
mod windows {
use super::*;
use std::os::windows::ffi::OsStrExt;

// Workaround for Windows. There is no way to extract raw byte sequence from `OsStr` (in `Path`).
// And `OsStr::to_string_lossy` may cause extra heap allocation.
pub(crate) fn ends_with_main_sep(p: &Path) -> bool {
p.as_os_str().encode_wide().last() == Some(MAIN_SEPARATOR as u16)
}
}

fn str_to_path(s: &str, sep: char) -> Cow<'_, Path> {
let mut buf = String::new();

Expand Down Expand Up @@ -155,7 +167,7 @@ impl PathExt for Path {
buf.push('/');
}

if buf != "/" && buf.ends_with('/') {
if !windows::ends_with_main_sep(self) && buf != "/" && buf.ends_with('/') {
buf.pop(); // Pop last '/'
}

Expand Down Expand Up @@ -204,7 +216,7 @@ impl PathExt for Path {
buf.push('/');
}

if buf != "/" && buf.ends_with('/') {
if !windows::ends_with_main_sep(self) && buf != "/" && buf.ends_with('/') {
buf.pop(); // Pop last '/'
}

Expand Down
8 changes: 4 additions & 4 deletions src/test.rs
Expand Up @@ -13,8 +13,8 @@ lazy_static! {
("//", "/"),
("foo", "foo"),
("/foo", "/foo"),
("foo/", "foo"),
("/foo/", "/foo"),
("foo/", "foo/"),
("/foo/", "/foo/"),
("./foo", "./foo"),
("../foo", "../foo"),
("foo/.", "foo/."),
Expand Down Expand Up @@ -116,8 +116,8 @@ lazy_static! {
"/",
"foo",
"/foo",
"foo",
"/foo",
"foo/",
"/foo/",
"./foo",
"../foo",
"foo/..",
Expand Down

0 comments on commit a72dc60

Please sign in to comment.