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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
use std::path::{is_separator, Component, Path, PathBuf};

use super::helpers;

const EXPAND_STR: &str = if cfg!(windows) { r"..\" } else { "../" };

fn handle_dots_push(string: &mut String, count: u8) {
    if count < 1 {
        return;
    }

    if count == 1 {
        string.push('.');
        return;
    }

    for _ in 0..(count - 1) {
        string.push_str(EXPAND_STR);
    }

    string.pop(); // remove last '/'
}

/// Expands any occurrence of more than two dots into a sequence of ../ (or ..\ on windows), e.g.,
/// "..." into "../..", "...." into "../../../", etc.
pub fn expand_ndots(path: impl AsRef<Path>) -> PathBuf {
    // Check if path is valid UTF-8 and if not, return it as it is to avoid breaking it via string
    // conversion.
    let path_str = match path.as_ref().to_str() {
        Some(s) => s,
        None => return path.as_ref().into(),
    };

    // find if we need to expand any >2 dot paths and early exit if not
    let mut dots_count = 0u8;
    let mut not_separator_before_dot = false;
    let ndots_present = {
        for chr in path_str.chars() {
            if chr == '.' {
                dots_count += 1;
            } else {
                if is_separator(chr) && (dots_count > 2) {
                    // this path component had >2 dots
                    break;
                }
                not_separator_before_dot = !(is_separator(chr) || chr.is_whitespace());
                dots_count = 0;
            }
        }

        dots_count > 2
    };

    if !ndots_present || not_separator_before_dot {
        return path.as_ref().into();
    }

    let mut dots_count = 0u8;
    let mut expanded = String::new();
    for chr in path_str.chars() {
        if chr == '.' {
            dots_count += 1;
        } else {
            if is_separator(chr) {
                // check for dots expansion only at path component boundaries
                handle_dots_push(&mut expanded, dots_count);
                dots_count = 0;
            } else {
                // got non-dot within path component => do not expand any dots
                while dots_count > 0 {
                    expanded.push('.');
                    dots_count -= 1;
                }
            }
            expanded.push(chr);
        }
    }

    handle_dots_push(&mut expanded, dots_count);

    expanded.into()
}

/// Expand "." and ".." into nothing and parent directory, respectively.
pub fn expand_dots(path: impl AsRef<Path>) -> PathBuf {
    let path = path.as_ref();

    // Early-exit if path does not contain '.' or '..'
    if !path
        .components()
        .any(|c| std::matches!(c, Component::CurDir | Component::ParentDir))
    {
        return path.into();
    }

    let mut result = PathBuf::with_capacity(path.as_os_str().len());

    // Only pop/skip path elements if the previous one was an actual path element
    let prev_is_normal = |p: &Path| -> bool {
        p.components()
            .next_back()
            .map(|c| std::matches!(c, Component::Normal(_)))
            .unwrap_or(false)
    };

    path.components().for_each(|component| match component {
        Component::ParentDir if prev_is_normal(&result) => {
            result.pop();
        }
        Component::CurDir if prev_is_normal(&result) => {}
        _ => result.push(component),
    });

    helpers::simiplified(&result)
}

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

    #[test]
    fn expand_two_dots() {
        let path = Path::new("/foo/bar/..");

        assert_eq!(
            PathBuf::from("/foo"), // missing path
            expand_dots(path)
        );
    }

    #[test]
    fn expand_dots_with_curdir() {
        let path = Path::new("/foo/./bar/./baz");

        assert_eq!(PathBuf::from("/foo/bar/baz"), expand_dots(path));
    }

    fn check_ndots_expansion(expected: &str, s: &str) {
        let expanded = expand_ndots(Path::new(s));
        assert_eq!(Path::new(expected), &expanded);
    }

    // common tests
    #[test]
    fn string_without_ndots() {
        check_ndots_expansion("../hola", "../hola");
    }

    #[test]
    fn string_with_three_ndots_and_chars() {
        check_ndots_expansion("a...b", "a...b");
    }

    #[test]
    fn string_with_two_ndots_and_chars() {
        check_ndots_expansion("a..b", "a..b");
    }

    #[test]
    fn string_with_one_dot_and_chars() {
        check_ndots_expansion("a.b", "a.b");
    }

    #[test]
    fn expand_dots_double_dots_no_change() {
        // Can't resolve this as we don't know our parent dir
        assert_eq!(Path::new(".."), expand_dots(Path::new("..")));
    }

    #[test]
    fn expand_dots_single_dot_no_change() {
        // Can't resolve this as we don't know our current dir
        assert_eq!(Path::new("."), expand_dots(Path::new(".")));
    }

    #[test]
    fn expand_dots_multi_single_dots_no_change() {
        assert_eq!(Path::new("././."), expand_dots(Path::new("././.")));
    }

    #[test]
    fn expand_multi_double_dots_no_change() {
        assert_eq!(Path::new("../../../"), expand_dots(Path::new("../../../")));
    }

    #[test]
    fn expand_dots_no_change_with_dirs() {
        // Can't resolve this as we don't know our parent dir
        assert_eq!(
            Path::new("../../../dir1/dir2/"),
            expand_dots(Path::new("../../../dir1/dir2"))
        );
    }

    #[test]
    fn expand_dots_simple() {
        assert_eq!(Path::new("/foo"), expand_dots(Path::new("/foo/bar/..")));
    }

    #[test]
    fn expand_dots_complex() {
        assert_eq!(
            Path::new("/test"),
            expand_dots(Path::new("/foo/./bar/../../test/././test2/../"))
        );
    }

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

        #[test]
        fn string_with_three_ndots() {
            check_ndots_expansion(r"..\..", "...");
        }

        #[test]
        fn string_with_mixed_ndots_and_chars() {
            check_ndots_expansion(
                r"a...b/./c..d/../e.f/..\..\..//.",
                "a...b/./c..d/../e.f/....//.",
            );
        }

        #[test]
        fn string_with_three_ndots_and_final_slash() {
            check_ndots_expansion(r"..\../", ".../");
        }

        #[test]
        fn string_with_three_ndots_and_garbage() {
            check_ndots_expansion(r"ls ..\../ garbage.*[", "ls .../ garbage.*[");
        }
    }

    #[cfg(not(windows))]
    mod non_windows {
        use super::*;
        #[test]
        fn string_with_three_ndots() {
            check_ndots_expansion(r"../..", "...");
        }

        #[test]
        fn string_with_mixed_ndots_and_chars() {
            check_ndots_expansion(
                "a...b/./c..d/../e.f/../../..//.",
                "a...b/./c..d/../e.f/....//.",
            );
        }

        #[test]
        fn string_with_three_ndots_and_final_slash() {
            check_ndots_expansion("../../", ".../");
        }

        #[test]
        fn string_with_three_ndots_and_garbage() {
            check_ndots_expansion("ls ../../ garbage.*[", "ls .../ garbage.*[");
        }
    }
}