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
use std::ffi::OsString;
use std::io::{self, ErrorKind};
use std::path::{Component, Path, PathBuf, PrefixComponent};

use crate::{ParseDot, MAIN_SEPARATOR};

impl ParseDot for Path {
    #[allow(clippy::let_unit_value)]
    fn parse_dot(&self) -> io::Result<PathBuf> {
        let mut size = self.as_os_str().len();

        let mut iter = self.components();

        if let Some(first_component) = iter.next() {
            let cwd = get_cwd!();

            let mut tokens = Vec::new();

            let (has_prefix, first_is_root) = match first_component {
                Component::Prefix(prefix) => {
                    tokens.push(prefix.as_os_str());

                    if let Some(second_component) = iter.next() {
                        match second_component {
                            Component::RootDir => {
                                tokens.push(MAIN_SEPARATOR.as_os_str());

                                (true, true)
                            }
                            Component::CurDir => {
                                // may be unreachable

                                let mut cwd_iter = cwd.iter().skip(1);

                                if let Some(token) = cwd_iter.next() {
                                    tokens.push(token);
                                    size += token.len();

                                    for token in cwd_iter {
                                        tokens.push(token);
                                        size += token.len() + 1;
                                    }
                                }

                                size -= 1;

                                (true, true)
                            }
                            Component::ParentDir => {
                                match cwd.parent() {
                                    Some(cwd_parent) => {
                                        let mut cwd_parent_iter = cwd_parent.iter().skip(1);

                                        if let Some(token) = cwd_parent_iter.next() {
                                            tokens.push(token);
                                            size += token.len();

                                            for token in cwd_parent_iter {
                                                tokens.push(token);
                                                size += token.len() + 1;
                                            }
                                        }

                                        size -= 2;
                                    }
                                    None => {
                                        tokens.push(MAIN_SEPARATOR.as_os_str());

                                        size -= 1;
                                    }
                                }

                                (true, true)
                            }
                            _ => {
                                let path_str = self.as_os_str().to_str().ok_or_else(|| {
                                    io::Error::new(ErrorKind::Other, "The path is not valid UTF-8.")
                                })?;

                                if path_str[first_component.as_os_str().len()..].starts_with('.') {
                                    let mut cwd_iter = cwd.iter().skip(1);

                                    if let Some(token) = cwd_iter.next() {
                                        tokens.push(token);
                                        size += token.len();

                                        for token in cwd_iter {
                                            tokens.push(token);
                                            size += token.len() + 1;
                                        }
                                    }

                                    size -= 1;

                                    tokens.push(second_component.as_os_str());

                                    (true, true)
                                } else {
                                    tokens.push(second_component.as_os_str());

                                    (true, false)
                                }
                            }
                        }
                    } else {
                        (true, false)
                    }
                }
                Component::RootDir => {
                    tokens.push(MAIN_SEPARATOR.as_os_str());

                    (false, true)
                }
                Component::CurDir => {
                    for token in cwd.iter() {
                        tokens.push(token);
                    }

                    size += cwd.as_os_str().len() - 1;

                    (true, true)
                }
                Component::ParentDir => {
                    match cwd.parent() {
                        Some(cwd_parent) => {
                            for token in cwd_parent.iter() {
                                tokens.push(token);
                            }

                            size += cwd_parent.as_os_str().len() - 2;
                        }
                        None => {
                            let prefix = cwd.get_path_prefix().unwrap().as_os_str();
                            tokens.push(prefix);
                            size += prefix.len();

                            tokens.push(MAIN_SEPARATOR.as_os_str());
                            size -= 1;
                        }
                    }

                    (true, true)
                }
                Component::Normal(token) => {
                    tokens.push(token);

                    (false, false)
                }
            };

            for component in iter {
                match component {
                    Component::CurDir => {
                        // may be unreachable

                        size -= 2;
                    }
                    Component::ParentDir => {
                        let tokens_length = tokens.len();

                        if tokens_length > 0
                            && ((tokens_length != 1 || (!first_is_root && !has_prefix))
                                && (tokens_length != 2 || !(first_is_root && has_prefix)))
                        {
                            let removed = tokens.remove(tokens_length - 1);
                            size -= removed.len() + 4; // xxx\..\
                        } else {
                            size -= 3; // ..\
                        }
                    }
                    _ => {
                        tokens.push(component.as_os_str());
                    }
                }
            }

            debug_assert!(!tokens.is_empty());

            let mut path_string = OsString::with_capacity(size);

            let mut iter = tokens.iter();

            path_string.push(iter.next().unwrap());

            let tokens_length = tokens.len();

            if tokens_length > 1 {
                if has_prefix {
                    if let Some(token) = iter.next() {
                        path_string.push(token);

                        if tokens_length > 2 {
                            if !first_is_root {
                                path_string.push(MAIN_SEPARATOR.as_os_str());
                            }

                            for &token in iter.take(tokens_length - 3) {
                                path_string.push(token);

                                path_string.push(MAIN_SEPARATOR.as_os_str());
                            }

                            path_string.push(tokens[tokens_length - 1]);
                        }
                    }
                } else {
                    if !first_is_root {
                        path_string.push(MAIN_SEPARATOR.as_os_str());
                    }

                    for &token in iter.take(tokens_length - 2) {
                        path_string.push(token);

                        path_string.push(MAIN_SEPARATOR.as_os_str());
                    }

                    path_string.push(tokens[tokens_length - 1]);
                }
            }

            debug_assert!(size + 1 >= path_string.len()); // + 1 is for `\\server\share` -> `\\server\share\`

            let path_buf = PathBuf::from(path_string);

            Ok(path_buf)
        } else {
            Ok(PathBuf::new())
        }
    }
}

pub trait ParsePrefix {
    fn get_path_prefix(&self) -> Option<PrefixComponent>;
}

impl ParsePrefix for Path {
    #[inline]
    fn get_path_prefix(&self) -> Option<PrefixComponent> {
        let first_component = self.components().next();

        match first_component.unwrap() {
            Component::Prefix(prefix_component) => Some(prefix_component),
            _ => None,
        }
    }
}

impl ParsePrefix for PathBuf {
    #[inline]
    fn get_path_prefix(&self) -> Option<PrefixComponent> {
        let first_component = self.components().next();

        match first_component.unwrap() {
            Component::Prefix(prefix_component) => Some(prefix_component),
            _ => None,
        }
    }
}