blob: bb2b8e8d48140e17e79893e76b2bbeba91b0cecf (
plain)
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
|
#include "common.hh"
#include "pathutil.hh"
#include "strutil.hh"
#include <vector>
namespace path {
std::string cleanup(std::string_view path) {
auto trimmed_path = str::trim(path);
bool trailing = !trimmed_path.empty() && trimmed_path.back() == '/';
auto parts = str::split(trimmed_path, '/');
auto it = parts.begin();
while (it != parts.end()) {
if (it->empty() || *it == ".") {
if (it + 1 == parts.end())
trailing = true;
it = parts.erase(it);
} else if (*it == "..") {
if (it + 1 == parts.end())
trailing = true;
if (it > parts.begin()) {
it = parts.erase(it - 1, it);
} else {
it = parts.erase(it);
}
} else {
++it;
}
}
if (parts.empty())
return "/";
std::string ret;
for (auto const& part : parts) {
ret.push_back('/');
ret.append(part);
}
if (trailing)
ret.push_back('/');
return ret;
}
} // namespace path
|