use std::borrow::Cow;
pub(crate) fn windows_separators(path: &str, windows: bool) -> Cow<'_, str> {
if !windows || !path.contains('\\') {
return Cow::Borrowed(path);
}
let path = path.replace('\\', "/");
Cow::Owned(match path.strip_prefix("//?/") {
Some(rest) => match rest.get(..4) {
Some(unc) if unc.eq_ignore_ascii_case("UNC/") => format!("//{}", &rest[4..]),
_ => rest.to_owned(),
},
None => path,
})
}
pub(crate) fn local_root(path: &str, windows: bool) -> Option<(String, &str)> {
if windows {
if let Some(share) = path.strip_prefix("//") {
let mut parts = share.splitn(3, '/');
let (server, name) = (parts.next()?, parts.next()?);
return (!server.is_empty() && !name.is_empty()).then(|| {
(
format!("//{server}/{name}/"),
parts.next().unwrap_or_default(),
)
});
}
let drive = path.strip_prefix('/').unwrap_or(path);
if let [letter, b':', b'/', ..] = drive.as_bytes()
&& letter.is_ascii_alphabetic()
{
return Some((
format!("{}:/", char::from(letter.to_ascii_uppercase())),
&drive[3..],
));
}
}
path.strip_prefix('/').map(|rest| ("/".to_owned(), rest))
}
pub(crate) fn windows_file_url_path(rest: &str, windows: bool) -> Cow<'_, str> {
let on_localhost = rest
.get(.."localhost/".len())
.filter(|host| host.eq_ignore_ascii_case("localhost/"))
.map(|host| &rest[host.len()..]);
match (windows, rest.strip_prefix('/'), on_localhost) {
(false, _, _) => Cow::Borrowed(rest),
(true, Some(path), _) if is_drive(path) => Cow::Borrowed(path),
(true, Some(_), _) => Cow::Borrowed(rest),
(true, None, Some(path)) if is_drive(path) => Cow::Borrowed(path),
(true, None, _) => Cow::Owned(format!("//{rest}")),
}
}
pub(crate) fn share_by_address(root: &str, windows: bool) -> Cow<'_, str> {
if !windows {
return Cow::Borrowed(root);
}
let separated = windows_separators(root, windows);
let on_localhost = separated.strip_prefix("//").and_then(|share| {
share
.get(.."localhost/".len())
.filter(|host| host.eq_ignore_ascii_case("localhost/"))
.map(|host| &share[host.len()..])
});
match on_localhost {
Some(rest) => Cow::Owned(format!(r"\\127.0.0.1\{}", rest.replace('/', r"\"))),
None => Cow::Borrowed(root),
}
}
fn is_drive(path: &str) -> bool {
matches!(path.as_bytes(), [letter, b':', ..] if letter.is_ascii_alphabetic())
}
pub(crate) fn strip_folders<'a>(prefix: &'a str, folders: &str, windows: bool) -> Option<&'a str> {
let head = prefix.get(..folders.len())?;
let same = match windows {
true => head.eq_ignore_ascii_case(folders),
false => head == folders,
};
same.then(|| &prefix[folders.len()..])
}