#include "common.hh" #include "pathutil.hh" #include "strutil.hh" #include 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