| 37 | } |
| 38 | |
| 39 | std::string normalisePath(const std::string& path) { |
| 40 | std::vector<std::string_view> components; |
| 41 | components.reserve(8); // Eight nested folders is more than we might expect |
| 42 | |
| 43 | size_t start = 0; |
| 44 | for (size_t i = 0; i <= path.length(); ++i) { |
| 45 | if (i == path.length() || path[i] == '/') { |
| 46 | if (i > start) { |
| 47 | std::string_view component(path.data() + start, i - start); |
| 48 | if (component == "..") { |
| 49 | if (!components.empty()) { |
| 50 | components.pop_back(); |
| 51 | } |
| 52 | } else { |
| 53 | components.push_back(component); |
| 54 | } |
| 55 | } |
| 56 | start = i + 1; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if (components.empty()) { |
| 61 | return ""; |
| 62 | } |
| 63 | |
| 64 | size_t total_len = 0; |
| 65 | for (const auto& c : components) { |
| 66 | total_len += c.length() + 1; |
| 67 | } |
| 68 | |
| 69 | std::string result; |
| 70 | result.reserve(total_len - 1); |
| 71 | |
| 72 | for (size_t i = 0; i < components.size(); ++i) { |
| 73 | if (i > 0) { |
| 74 | result += '/'; |
| 75 | } |
| 76 | result.append(components[i].data(), components[i].length()); |
| 77 | } |
| 78 | |
| 79 | return result; |
| 80 | } |
| 81 | |
| 82 | void sortFileList(std::vector<std::string>& strs) { |
| 83 | std::sort(begin(strs), end(strs), [](const std::string& str1, const std::string& str2) { |
no test coverage detected