1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// This file is part of helpers4.
// Copyright (C) 2025 baxyz
// SPDX-License-Identifier: LGPL-3.0-or-later
use parts;
use ;
/// Cleans a path lexically: drops `.` components and folds `..` into the component before it.
///
/// Nothing touches the file system, so this works for paths that do not exist, but symbolic
/// links are not resolved (use [`std::fs::canonicalize`] for that). A `..` at the start of a
/// relative path is kept, a `..` right after the root is dropped, and a path that cleans to
/// nothing becomes `.`.
///
/// # Arguments
///
/// - `path` - The path to clean.
///
/// # Returns
///
/// The cleaned path.
///
/// # Examples
///
/// ```
/// use helpers4::fs::normalize;
/// use std::path::{Path, PathBuf};
///
/// assert_eq!(normalize(Path::new("a/./b/../c")), PathBuf::from("a/c"));
/// assert_eq!(normalize(Path::new("../a")), PathBuf::from("../a"));
/// assert_eq!(normalize(Path::new("a/..")), PathBuf::from("."));
/// ```