Skip to main content

dumap_core/
path_util.rs

1use std::path::PathBuf;
2
3/// Strip the Windows extended-length path prefix (`\\?\`) if present.
4///
5/// `Path::canonicalize()` on Windows produces paths like `\\?\C:\Users\...`.
6/// This prefix is valid but looks ugly in UI and HTML output. This function
7/// strips it when the remaining path is a simple absolute path (drive letter).
8pub fn clean_path(path: PathBuf) -> PathBuf {
9    _clean_path(path)
10}
11
12#[cfg(windows)]
13fn _clean_path(path: PathBuf) -> PathBuf {
14    let s = path.to_string_lossy();
15    if let Some(stripped) = s.strip_prefix(r"\\?\") {
16        // Only strip if what remains looks like a normal absolute path (e.g. C:\...)
17        // to avoid breaking UNC paths like \\?\UNC\server\share
18        if stripped.len() >= 3 && stripped.as_bytes()[1] == b':' {
19            return PathBuf::from(stripped);
20        }
21    }
22    path
23}
24
25#[cfg(not(windows))]
26fn _clean_path(path: PathBuf) -> PathBuf {
27    path
28}
29
30#[cfg(test)]
31#[path = "path_util_tests.rs"]
32mod path_util_tests;