Removes the last component from a path string (if possible) and returns the result with one trailing separator.
| 316 | |
| 317 | // Removes the last component from a path string (if possible) and returns the result with one trailing separator. |
| 318 | std::string popPath(const std::string& path) { |
| 319 | int i = path.size() - 1; |
| 320 | // Skip over any trailing separators |
| 321 | while (i >= 0 && path[i] == CANONICAL_PATH_SEPARATOR) { |
| 322 | --i; |
| 323 | } |
| 324 | // Skip over non separators |
| 325 | while (i >= 0 && path[i] != CANONICAL_PATH_SEPARATOR) { |
| 326 | --i; |
| 327 | } |
| 328 | // Skip over trailing separators again |
| 329 | bool foundSeparator = false; |
| 330 | while (i >= 0 && path[i] == CANONICAL_PATH_SEPARATOR) { |
| 331 | --i; |
| 332 | foundSeparator = true; |
| 333 | } |
| 334 | |
| 335 | if (foundSeparator) { |
| 336 | ++i; |
| 337 | } else { |
| 338 | // If absolute then we popped off the only path component so return "/" |
| 339 | if (!path.empty() && path.front() == CANONICAL_PATH_SEPARATOR) { |
| 340 | return "/"; |
| 341 | } |
| 342 | } |
| 343 | return path.substr(0, i + 1); |
| 344 | } |
| 345 | |
| 346 | std::string abspath(std::string const& path, bool resolveLinks = true) { |
| 347 | if (path.empty()) { |
no test coverage detected