pub fn go_path_clean(p: &str) -> String {
if p.is_empty() {
return ".".to_owned();
}
let rooted = p.starts_with('/');
let mut elems: Vec<&str> = Vec::new();
for elem in p.split('/') {
match elem {
"" | "." => {}
".." => {
if elems.last().is_some_and(|last| *last != "..") {
elems.pop();
} else if !rooted {
elems.push("..");
}
}
real => elems.push(real),
}
}
let joined = elems.join("/");
match (rooted, joined.is_empty()) {
(true, _) => format!("/{joined}"),
(false, true) => ".".to_owned(),
(false, false) => joined,
}
}
pub fn go_path_join<S: AsRef<str>>(elems: &[S]) -> String {
let parts: Vec<&str> = elems
.iter()
.map(AsRef::as_ref)
.filter(|e| !e.is_empty())
.collect();
if parts.is_empty() {
return String::new();
}
go_path_clean(&parts.join("/"))
}
pub fn go_path_dir(p: &str) -> String {
let end = p.rfind('/').map_or(0, |i| i + 1);
go_path_clean(&p[..end])
}
pub fn go_path_base(p: &str) -> String {
if p.is_empty() {
return ".".to_owned();
}
let trimmed = p.trim_end_matches('/');
if trimmed.is_empty() {
return "/".to_owned();
}
match trimmed.rfind('/') {
Some(i) => trimmed[i + 1..].to_owned(),
None => trimmed.to_owned(),
}
}
pub fn go_filepath_rel(basepath: &str, targpath: &str) -> Result<String, String> {
let base_clean = go_path_clean(basepath);
let targ_clean = go_path_clean(targpath);
if targ_clean == base_clean {
return Ok(".".to_owned());
}
let base: &[u8] = if base_clean == "." {
b""
} else {
base_clean.as_bytes()
};
let targ = targ_clean.as_bytes();
let cant = || format!("Rel: can't make {targpath} relative to {basepath}");
let base_slashed = base.first() == Some(&b'/');
let targ_slashed = targ.first() == Some(&b'/');
if base_slashed != targ_slashed {
return Err(cant());
}
let (bl, tl) = (base.len(), targ.len());
let (mut b0, mut bi, mut t0, mut ti) = (0, 0, 0, 0);
loop {
while bi < bl && base[bi] != b'/' {
bi += 1;
}
while ti < tl && targ[ti] != b'/' {
ti += 1;
}
if targ[t0..ti] != base[b0..bi] {
break;
}
if bi < bl {
bi += 1;
}
if ti < tl {
ti += 1;
}
b0 = bi;
t0 = ti;
}
if &base[b0..bi] == b".." {
return Err(cant());
}
if b0 != bl {
let seps = base[b0..bl].iter().filter(|&&c| c == b'/').count();
let mut buf = String::from("..");
for _ in 0..seps {
buf.push_str("/..");
}
if t0 != tl {
buf.push('/');
buf.push_str(&String::from_utf8_lossy(&targ[t0..]));
}
return Ok(buf);
}
Ok(String::from_utf8_lossy(&targ[t0..]).into_owned())
}
pub fn go_filepath_is_local(path: &str) -> bool {
if path.starts_with('/') || path.is_empty() {
return false;
}
let has_dots = path.split('/').any(|part| part == "." || part == "..");
let cleaned;
let path = if has_dots {
cleaned = go_path_clean(path);
cleaned.as_str()
} else {
path
};
!(path == ".." || path.starts_with("../"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn filepath_is_local_matches_go() {
for (path, want) in [
("", false),
(".", true),
("..", false),
("../a", false),
("/", false),
("/a", false),
("/a/../..", false),
("a", true),
("a/../a", true),
("a/", true),
("a/.", true),
("a/./b/./c", true),
("a/../b:/../../c", false),
("a/../b:/../c", true),
("a\\b", true),
("NUL", true),
("com1", true),
] {
assert_eq!(go_filepath_is_local(path), want, "IsLocal({path:?})");
}
}
#[test]
fn path_clean_matches_go() {
for (input, want) in [
("", "."),
("abc", "abc"),
("abc/def", "abc/def"),
("a/b/c", "a/b/c"),
(".", "."),
("..", ".."),
("../..", "../.."),
("../../abc", "../../abc"),
("/abc", "/abc"),
("/", "/"),
("abc/", "abc"),
("abc/def/", "abc/def"),
("a/b/c/", "a/b/c"),
("./", "."),
("../", ".."),
("../../", "../.."),
("/abc/", "/abc"),
("abc//def//ghi", "abc/def/ghi"),
("//abc", "/abc"),
("///abc", "/abc"),
("//abc//", "/abc"),
("abc//", "abc"),
("abc/./def", "abc/def"),
("/./abc/def", "/abc/def"),
("abc/.", "abc"),
("abc/def/ghi/../jkl", "abc/def/jkl"),
("abc/def/../ghi/../jkl", "abc/jkl"),
("abc/def/..", "abc"),
("abc/def/../..", "."),
("/abc/def/../..", "/"),
("abc/def/../../..", ".."),
("/abc/def/../../..", "/"),
("abc/def/../../../ghi/jkl/../../../mno", "../../mno"),
("abc/./../def", "def"),
("abc//./../def", "def"),
("abc/../../././../def", "../../def"),
] {
assert_eq!(go_path_clean(input), want, "Clean({input:?})");
}
}
#[test]
fn path_join_matches_go() {
let cases: &[(&[&str], &str)] = &[
(&[], ""),
(&[""], ""),
(&["a"], "a"),
(&["a", "b"], "a/b"),
(&["a", ""], "a"),
(&["", "b"], "b"),
(&["/", "a"], "/a"),
(&["/", ""], "/"),
(&["a/", "b"], "a/b"),
(&["a/", ""], "a"),
(&["", ""], ""),
(&["/a/../b", "./ws"], "/b/ws"),
];
for (elems, want) in cases {
assert_eq!(go_path_join(elems), *want, "Join({elems:?})");
}
}
#[test]
fn path_dir_and_base_match_go() {
for (p, dir, base) in [
("", ".", "."),
(".", ".", "."),
("/.", "/", "."),
("/", "/", "/"),
("//", "/", "/"),
("///", "/", "/"),
("////", "/", "/"),
("/foo", "/", "foo"),
("x/", "x", "x"),
("abc", ".", "abc"),
("abc/def", "abc", "def"),
("abc////def", "abc", "def"),
("a/b/.x", "a/b", ".x"),
("a/b/c.", "a/b", "c."),
("a/b/c.x", "a/b", "c.x"),
("/a/b", "/a", "b"),
("/a/b/", "/a/b", "b"),
("/a/b//", "/a/b", "b"),
("/a/b/root_agent.yaml", "/a/b", "root_agent.yaml"),
("/root_agent.yaml", "/", "root_agent.yaml"),
("root_agent.yaml", ".", "root_agent.yaml"),
("a//b/../c/x.yaml", "a/c", "x.yaml"),
] {
assert_eq!(go_path_dir(p), dir, "Dir({p:?})");
assert_eq!(go_path_base(p), base, "Base({p:?})");
}
assert_eq!(
go_path_base(&go_path_dir("/w/x/dup/root_agent.yaml")),
"dup"
);
assert_eq!(go_path_base(&go_path_dir("/root_agent.yaml")), "/");
}
}