pub(super) fn at_or_below(inner: &str, outer: &str) -> bool {
let inner = inner.trim_end_matches('/');
let outer = outer.trim_end_matches('/');
if outer.is_empty() {
return false;
}
inner == outer || inner.strip_prefix(outer).is_some_and(|rest| rest.starts_with('/'))
}
pub(super) fn has_hidden_component(remainder: &str) -> bool {
remainder.split('/').any(|seg| seg.len() > 1 && seg.starts_with('.'))
}
pub(super) fn ci_starts_with(haystack: &str, prefix: &str) -> bool {
haystack.len() >= prefix.len() && haystack.as_bytes()[..prefix.len()].eq_ignore_ascii_case(prefix.as_bytes())
}
#[cfg(test)]
mod tests {
use super::{at_or_below, ci_starts_with, has_hidden_component};
use proptest::prelude::*;
proptest! {
#[test]
fn a_path_is_below_a_root_only_past_a_component_boundary(
root in "(~|/[a-z]{1,4})(/[.a-z]{1,5}){0,3}",
rest in "[.a-z]{1,5}(/[.a-z]{1,5}){0,2}",
) {
prop_assert!(at_or_below(&root, &root));
let (below, neighbour) = (format!("{root}/{rest}"), format!("{root}{rest}"));
prop_assert!(at_or_below(&below, &root));
prop_assert!(!at_or_below(&neighbour, &root));
prop_assert!(!at_or_below(&root, ""));
}
#[test]
fn the_folded_prefix_agrees_with_lowercasing(haystack in "[a-zA-Z./]{0,10}", prefix in "[a-zA-Z./]{0,6}") {
let oracle = haystack.to_ascii_lowercase().starts_with(&prefix.to_ascii_lowercase());
prop_assert_eq!(ci_starts_with(&haystack, &prefix), oracle);
}
#[test]
fn the_current_folder_is_not_a_hidden_component(parts in prop::collection::vec("[a-z]{1,4}", 0..4), at in 0usize..4) {
let mut with = parts.clone();
with.insert(at.min(parts.len()), ".".to_string());
prop_assert!(!has_hidden_component(&with.join("/")));
}
#[test]
fn a_hidden_component_is_found_wherever_it_is(parts in prop::collection::vec("[a-z]{1,4}", 0..4), at in 0usize..4, name in "[a-z]{1,4}") {
let plain = parts.join("/");
prop_assert!(!has_hidden_component(&plain));
let mut with = parts.clone();
let hidden = format!(".{name}");
with.insert(at.min(parts.len()), hidden);
prop_assert!(has_hidden_component(&with.join("/")));
}
}
}