use std::ffi::{OsStr, OsString};
use super::{drive_len, has_ascii_drive, is_sep, path_type, windows_prefix_len, PathType};
pub(crate) fn append(base: &OsStr, rest: &OsStr, sep: &str) -> OsString {
if rest.is_empty() {
return base.to_os_string();
}
let bytes = base.as_encoded_bytes();
if is_verbatim(bytes) {
return append_verbatim(base, rest);
}
let mut out = base.to_os_string();
let ends_in_sep = bytes.last().is_some_and(|&b| is_sep(b, true));
if !bytes.is_empty() && !ends_in_sep && drive_len(bytes) != Some(bytes.len()) {
out.push(sep);
}
out.push(rest);
out
}
pub(crate) fn concat(base: &OsStr, rest: &OsStr, sep: &str) -> OsString {
let mut out = base.to_os_string();
if !base.as_encoded_bytes().last().is_some_and(|&b| is_sep(b, true)) {
out.push(sep);
}
out.push(rest);
out
}
pub(crate) fn join(base: &OsStr, name: &OsStr, sep: &str) -> OsString {
match path_type(name) {
PathType::Rooted => {
let bytes = base.as_encoded_bytes();
if is_verbatim(bytes) {
return append_verbatim(base, name);
}
let prefix = unsafe { OsStr::from_encoded_bytes_unchecked(&bytes[..windows_prefix_len(bytes)]) };
let mut out = prefix.to_os_string();
out.push(name);
out
}
PathType::Relative => append(base, name, sep),
PathType::Unc | PathType::DriveAbsolute | PathType::DriveRelative => name.to_os_string(),
}
}
pub(super) fn is_verbatim(bytes: &[u8]) -> bool {
bytes.starts_with(br"\\?\")
}
fn verbatim_prefix_len(bytes: &[u8]) -> usize {
debug_assert!(is_verbatim(bytes), "{bytes:?} must be verbatim");
let end = |at: usize| {
bytes[at..]
.iter()
.position(|&b| b == b'\\')
.map_or(bytes.len(), |i| at + i)
};
if bytes.len() >= 8 && bytes[4..7].eq_ignore_ascii_case(b"UNC") && bytes[7] == b'\\' {
let server = end(8);
if server >= bytes.len() {
return bytes.len();
}
let share = end(server + 1);
return if share == server + 1 { server } else { share };
}
if has_ascii_drive(&bytes[4..]) && bytes.get(6).is_none_or(|&b| is_sep(b, true)) {
return 6;
}
end(4)
}
#[derive(Clone, Copy, PartialEq)]
enum Part<'a> {
Cur,
Parent,
Normal(&'a [u8]),
}
fn append_verbatim(base: &OsStr, rest: &OsStr) -> OsString {
let bytes = base.as_encoded_bytes();
let prefix = verbatim_prefix_len(bytes);
let body = &bytes[prefix..];
let body = match body.first() {
Some(&b) if is_sep(b, true) => &body[1..],
_ => body,
};
let mut parts: Vec<Part<'_>> = body
.split(|&b| b == b'\\')
.filter_map(|piece| match piece {
b"" => None,
b"." => Some(Part::Cur),
b".." => Some(Part::Parent),
piece => Some(Part::Normal(piece)),
})
.collect();
let rest = rest.as_encoded_bytes();
if rest.first().is_some_and(|&b| is_sep(b, true)) {
parts.clear();
}
for piece in rest.split(|&b| is_sep(b, true)) {
match piece {
b"" | b"." => {}
b".." => {
if let Some(Part::Normal(_)) = parts.last() {
parts.pop();
}
}
piece => parts.push(Part::Normal(piece)),
}
}
let mut out: Vec<u8> = bytes[..prefix].to_vec();
out.push(b'\\');
for (i, part) in parts.iter().enumerate() {
if i > 0 {
out.push(b'\\');
}
out.extend_from_slice(match part {
Part::Cur => b".",
Part::Parent => b"..",
Part::Normal(piece) => piece,
});
}
unsafe { OsString::from_encoded_bytes_unchecked(out) }
}
#[cfg(test)]
#[path = "join_tests.rs"]
mod join_tests;