1use anyhow::{Result, bail};
2use std::path::{Component, Path};
3#[cfg(windows)]
4use unicode_casefold::{Locale, UnicodeCaseFold, Variant};
5
6pub(super) fn normalize_absolute_for_match(path: &str) -> String {
8 let normalized = if cfg!(windows) {
9 path.replace('\\', "/")
10 } else {
11 path.to_owned()
12 };
13 if cfg!(windows) {
14 if let Some(unc_path) = normalized.strip_prefix("//?/UNC/") {
15 return format!("//{unc_path}");
16 }
17 if let Some(path) = normalized.strip_prefix("//?/") {
18 return path.to_owned();
19 }
20 }
21 normalized
22}
23
24pub(super) fn path_matches_prefix(path: &str, prefix: &str) -> bool {
26 if prefix.is_empty() {
27 return true;
28 }
29 strip_path_prefix(path, prefix)
30 .is_some_and(|suffix| suffix.is_empty() || suffix.starts_with('/'))
31}
32
33pub(super) fn paths_equal(left: &str, right: &str) -> bool {
34 #[cfg(windows)]
35 {
36 left.case_fold_with(Variant::Simple, Locale::NonTurkic)
37 .eq(right.case_fold_with(Variant::Simple, Locale::NonTurkic))
38 }
39 #[cfg(not(windows))]
40 {
41 left == right
42 }
43}
44
45fn strip_path_prefix<'a>(path: &'a str, prefix: &str) -> Option<&'a str> {
46 let mut path_chars = path.char_indices();
47 let mut prefix_bytes = 0;
48 for _ in prefix.chars() {
49 let (index, character) = path_chars.next()?;
50 prefix_bytes = index + character.len_utf8();
51 }
52 paths_equal(&path[..prefix_bytes], prefix).then_some(&path[prefix_bytes..])
53}
54
55pub(super) fn is_absolute_pattern(pattern: &str) -> bool {
56 Path::new(pattern).is_absolute()
57 || (cfg!(windows)
58 && (matches!(pattern.as_bytes().first(), Some(b'/' | b'\\'))
59 || (pattern.as_bytes().get(1) == Some(&b':')
60 && pattern
61 .as_bytes()
62 .first()
63 .is_some_and(u8::is_ascii_alphabetic)
64 && pattern
65 .as_bytes()
66 .get(2)
67 .is_some_and(|separator| matches!(separator, b'/' | b'\\')))))
68}
69
70pub(super) fn is_drive_relative_path(path: &str) -> bool {
71 let bytes = path.as_bytes();
72 cfg!(windows)
73 && bytes.first().is_some_and(u8::is_ascii_alphabetic)
74 && bytes.get(1) == Some(&b':')
75 && !bytes
76 .get(2)
77 .is_some_and(|separator| matches!(separator, b'/' | b'\\'))
78}
79
80pub(super) fn is_filesystem_root(path: &Path) -> bool {
81 path.components()
82 .all(|component| !matches!(component, Component::Normal(_)))
83}
84
85pub(super) fn ensure_unicode_path(path: &Path) -> Result<()> {
86 if path.to_str().is_none() {
87 bail!("Path cannot be represented as Unicode; no plan was created: {path:?}");
88 }
89 Ok(())
90}
91
92pub(super) fn display_path(path: &Path) -> String {
94 let mut display_path = String::with_capacity(path.as_os_str().len());
95 for component in path.components() {
96 if let Component::Normal(value) = component {
97 if !display_path.is_empty() {
98 display_path.push('/');
99 }
100 display_path.push_str(&value.to_string_lossy());
101 }
102 }
103 display_path
104}
105
106#[cfg(test)]
107mod tests {
108 use super::{display_path, path_matches_prefix, paths_equal};
109 use std::path::Path;
110
111 #[test]
112 fn display_path_joins_normal_components_and_skips_others() {
113 assert_eq!(
114 display_path(Path::new("./keep/../nested/file.txt")),
115 "keep/nested/file.txt"
116 );
117 }
118
119 #[test]
120 fn path_case_comparison_matches_platform_policy() {
121 assert_eq!(paths_equal("Ä", "ä"), cfg!(windows));
122 assert_eq!(path_matches_prefix("Ä/file.txt", "ä"), cfg!(windows));
123 assert!(!path_matches_prefix("Äother/file.txt", "ä"));
124 }
125
126 #[test]
127 fn prefix_matching_respects_component_boundaries() {
128 assert!(path_matches_prefix("keep", "keep"));
129 assert!(path_matches_prefix("keep/file.txt", "keep"));
130 assert!(!path_matches_prefix("keeper/file.txt", "keep"));
131 assert!(!path_matches_prefix("other/keep/file.txt", "keep"));
132 }
133
134 #[cfg(windows)]
135 #[test]
136 fn prefix_comparison_handles_different_utf8_byte_lengths() {
137 assert!(path_matches_prefix("Keep/file.txt", "Keep"));
138 assert!(path_matches_prefix("Keep", "Keep"));
139 assert!(paths_equal("K", "k"));
140 assert!(!paths_equal("ß", "SS"));
141 }
142}