#![deny(unsafe_code)]
use super::Platform;
pub const fn sep(p: Platform) -> char {
if p.is_windows() {
'\\'
} else {
'/'
}
}
pub fn is_sep(p: Platform, c: char) -> bool {
c == '/' || (p.is_windows() && c == '\\')
}
fn is_sep_b(p: Platform, b: u8) -> bool {
b == b'/' || (p.is_windows() && b == b'\\')
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Parts {
pub prefix: String,
pub rooted: bool,
pub comps: Vec<String>,
}
pub fn prefix_len(p: Platform, s: &str) -> usize {
if !p.is_windows() {
return 0;
}
let b = s.as_bytes();
if b.len() >= 2 && b[1] == b':' && b[0].is_ascii_alphabetic() {
return 2;
}
if b.len() >= 2 && is_sep_b(p, b[0]) && is_sep_b(p, b[1]) {
let verbatim = b.len() > 3 && (b[2] == b'?' || b[2] == b'.') && is_sep_b(p, b[3]);
let first = comp_end(p, b, if verbatim { 4 } else { 2 });
if verbatim || first >= b.len() {
return first;
}
return comp_end(p, b, first + 1);
}
0
}
fn comp_end(p: Platform, b: &[u8], mut i: usize) -> usize {
while i < b.len() && !is_sep_b(p, b[i]) {
i += 1;
}
i
}
pub fn split_prefix(p: Platform, s: &str) -> (&str, &str) {
s.split_at(prefix_len(p, s))
}
pub fn is_absolute(p: Platform, s: &str) -> bool {
let (prefix, rest) = split_prefix(p, s);
let rooted = rest.starts_with(|c| is_sep(p, c));
if p.is_windows() {
!prefix.is_empty() && rooted
} else {
rooted
}
}
#[derive(Clone, Copy, PartialEq)]
enum Up {
Keep,
Clamp,
Refuse,
}
fn fold_into(p: Platform, s: &str, out: &mut Vec<String>, up: Up) -> Option<()> {
for part in s.split(|c| is_sep(p, c)) {
match part {
"" | "." => {}
".." => match out.last().map(String::as_str) {
Some(name) if name != ".." => {
out.pop();
}
_ => match up {
Up::Keep => out.push("..".to_string()),
Up::Clamp => {}
Up::Refuse => return None,
},
},
other => out.push(other.to_string()),
}
}
Some(())
}
pub fn parse(p: Platform, s: &str) -> Option<Parts> {
let (prefix, rest) = split_prefix(p, s);
let rooted = rest.starts_with(|c| is_sep(p, c));
let mut comps = Vec::new();
let up = match rooted {
true => Up::Clamp,
false => Up::Keep,
};
fold_into(p, rest, &mut comps, up)?;
Some(Parts {
prefix: prefix.to_string(),
rooted,
comps,
})
}
pub fn render(p: Platform, parts: &Parts) -> String {
let sep = sep(p);
let mut out = String::with_capacity(parts.prefix.len() + parts.comps.len() * 8);
out.push_str(&parts.prefix);
if parts.rooted {
out.push(sep);
}
for (i, c) in parts.comps.iter().enumerate() {
if i > 0 {
out.push(sep);
}
out.push_str(c);
}
out
}
pub fn join(p: Platform, base: &str, rel: &str) -> Option<String> {
let mut parts = parse(p, base)?;
fold_into(p, rel, &mut parts.comps, Up::Refuse)?;
Some(render(p, &parts))
}
fn join_or(p: Platform, base: &str, rel: &str, given: &str) -> String {
join(p, base, rel).unwrap_or_else(|| given.to_string())
}
fn fold_or(p: Platform, s: &str) -> String {
parse(p, s).map(|q| render(p, &q)).unwrap_or_else(|| s.to_string())
}
pub fn markdown_is_rooted(p: Platform, s: &str) -> bool {
s.starts_with(|c| is_sep(p, c))
}
pub fn markdown_trim_root(p: Platform, s: &str) -> &str {
s.trim_start_matches(|c| is_sep(p, c))
}
pub fn absolutize(p: Platform, cwd: &str, path: &str) -> String {
let (prefix, rest) = split_prefix(p, path);
let rooted = rest.starts_with(|c| is_sep(p, c));
if is_absolute(p, path) {
return fold_or(p, path);
}
if rooted {
let (cwd_prefix, _) = split_prefix(p, cwd);
return fold_or(p, &format!("{cwd_prefix}{path}"));
}
if !prefix.is_empty() {
let (cwd_prefix, _) = split_prefix(p, cwd);
if eq(p, cwd_prefix, prefix) {
return join_or(p, cwd, rest, path);
}
return fold_or(p, &format!("{prefix}{}{rest}", sep(p)));
}
join_or(p, cwd, path, path)
}
fn eq(p: Platform, a: &str, b: &str) -> bool {
if p.is_windows() {
a.eq_ignore_ascii_case(b)
} else {
a == b
}
}
pub fn same(p: Platform, a: &str, b: &str) -> bool {
match (parse(p, a), parse(p, b)) {
(Some(x), Some(y)) => {
x.rooted == y.rooted
&& eq(p, &x.prefix, &y.prefix)
&& x.comps.len() == y.comps.len()
&& x.comps.iter().zip(&y.comps).all(|(u, v)| eq(p, u, v))
}
_ => eq(p, a, b),
}
}
pub fn contains(p: Platform, root: &str, child: &str) -> bool {
strip(p, root, child).is_some()
}
fn strip(p: Platform, root: &str, child: &str) -> Option<Vec<String>> {
let (r, c) = (parse(p, root)?, parse(p, child)?);
if r.rooted != c.rooted || !eq(p, &r.prefix, &c.prefix) || c.comps.len() < r.comps.len() {
return None;
}
if !r.comps.iter().zip(&c.comps).all(|(u, v)| eq(p, u, v)) {
return None;
}
Some(c.comps[r.comps.len()..].to_vec())
}
pub fn common(p: Platform, a: &str, b: &str) -> Option<String> {
let (x, y) = (parse(p, a)?, parse(p, b)?);
if x.rooted != y.rooted || !eq(p, &x.prefix, &y.prefix) {
return None;
}
let shared = x
.comps
.iter()
.zip(&y.comps)
.take_while(|(u, v)| eq(p, u, v) && *u != "..")
.count();
Some(render(
p,
&Parts {
prefix: x.prefix,
rooted: x.rooted,
comps: x.comps[..shared].to_vec(),
},
))
}
pub fn rel_to(p: Platform, root: &str, child: &str) -> String {
match strip(p, root, child) {
Some(rest) => rest.join(&sep(p).to_string()),
None => child.to_string(),
}
}