nu_path/
trailing_slash.rs

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::{
    borrow::Cow,
    path::{Path, PathBuf},
};

/// Strip any trailing slashes from a non-root path. This is required in some contexts, for example
/// for the `PWD` environment variable.
pub fn strip_trailing_slash(path: &Path) -> Cow<Path> {
    if has_trailing_slash(path) {
        // If there are, the safest thing to do is have Rust parse the path for us and build it
        // again. This will correctly handle a root directory, but it won't add the trailing slash.
        let mut out = PathBuf::with_capacity(path.as_os_str().len());
        out.extend(path.components());
        Cow::Owned(out)
    } else {
        // The path is safe and doesn't contain any trailing slashes.
        Cow::Borrowed(path)
    }
}

/// `true` if the path has a trailing slash, including if it's the root directory.
#[cfg(windows)]
pub fn has_trailing_slash(path: &Path) -> bool {
    use std::os::windows::ffi::OsStrExt;

    let last = path.as_os_str().encode_wide().last();
    last == Some(b'\\' as u16) || last == Some(b'/' as u16)
}

/// `true` if the path has a trailing slash, including if it's the root directory.
#[cfg(unix)]
pub fn has_trailing_slash(path: &Path) -> bool {
    use std::os::unix::ffi::OsStrExt;

    let last = path.as_os_str().as_bytes().last();
    last == Some(&b'/')
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg_attr(not(unix), ignore = "only for Unix")]
    #[test]
    fn strip_root_unix() {
        assert_eq!(Path::new("/"), strip_trailing_slash(Path::new("/")));
    }

    #[cfg_attr(not(unix), ignore = "only for Unix")]
    #[test]
    fn strip_non_trailing_unix() {
        assert_eq!(
            Path::new("/foo/bar"),
            strip_trailing_slash(Path::new("/foo/bar"))
        );
    }

    #[cfg_attr(not(unix), ignore = "only for Unix")]
    #[test]
    fn strip_trailing_unix() {
        assert_eq!(
            Path::new("/foo/bar"),
            strip_trailing_slash(Path::new("/foo/bar/"))
        );
    }

    #[cfg_attr(not(windows), ignore = "only for Windows")]
    #[test]
    fn strip_root_windows() {
        assert_eq!(Path::new(r"C:\"), strip_trailing_slash(Path::new(r"C:\")));
    }

    #[cfg_attr(not(windows), ignore = "only for Windows")]
    #[test]
    fn strip_non_trailing_windows() {
        assert_eq!(
            Path::new(r"C:\foo\bar"),
            strip_trailing_slash(Path::new(r"C:\foo\bar"))
        );
    }

    #[cfg_attr(not(windows), ignore = "only for Windows")]
    #[test]
    fn strip_non_trailing_windows_unc() {
        assert_eq!(
            Path::new(r"\\foo\bar"),
            strip_trailing_slash(Path::new(r"\\foo\bar"))
        );
    }

    #[cfg_attr(not(windows), ignore = "only for Windows")]
    #[test]
    fn strip_trailing_windows() {
        assert_eq!(
            Path::new(r"C:\foo\bar"),
            strip_trailing_slash(Path::new(r"C:\foo\bar\"))
        );
    }

    #[cfg_attr(not(windows), ignore = "only for Windows")]
    #[test]
    fn strip_trailing_windows_unc() {
        assert_eq!(
            Path::new(r"\\foo\bar"),
            strip_trailing_slash(Path::new(r"\\foo\bar\"))
        );
    }
}