diff options
Diffstat (limited to 'src/common/fs/path_util.cpp')
| -rw-r--r-- | src/common/fs/path_util.cpp | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/src/common/fs/path_util.cpp b/src/common/fs/path_util.cpp index c3a81f9a9..4f69db6f5 100644 --- a/src/common/fs/path_util.cpp +++ b/src/common/fs/path_util.cpp | |||
| @@ -354,18 +354,36 @@ std::string_view RemoveTrailingSlash(std::string_view path) { | |||
| 354 | return path; | 354 | return path; |
| 355 | } | 355 | } |
| 356 | 356 | ||
| 357 | std::vector<std::string> SplitPathComponents(std::string_view filename) { | 357 | template <typename F> |
| 358 | std::string copy(filename); | 358 | static void ForEachPathComponent(std::string_view filename, F&& cb) { |
| 359 | std::replace(copy.begin(), copy.end(), '\\', '/'); | 359 | const char* component_begin = filename.data(); |
| 360 | std::vector<std::string> out; | 360 | const char* const end = component_begin + filename.size(); |
| 361 | 361 | for (const char* it = component_begin; it != end; ++it) { | |
| 362 | std::stringstream stream(copy); | 362 | const char c = *it; |
| 363 | std::string item; | 363 | if (c == '\\' || c == '/') { |
| 364 | while (std::getline(stream, item, '/')) { | 364 | if (component_begin != it) { |
| 365 | out.push_back(std::move(item)); | 365 | cb(std::string_view{component_begin, it}); |
| 366 | } | ||
| 367 | component_begin = it + 1; | ||
| 368 | } | ||
| 369 | } | ||
| 370 | if (component_begin != end) { | ||
| 371 | cb(std::string_view{component_begin, end}); | ||
| 366 | } | 372 | } |
| 373 | } | ||
| 374 | |||
| 375 | std::vector<std::string_view> SplitPathComponents(std::string_view filename) { | ||
| 376 | std::vector<std::string_view> components; | ||
| 377 | ForEachPathComponent(filename, [&](auto component) { components.emplace_back(component); }); | ||
| 367 | 378 | ||
| 368 | return out; | 379 | return components; |
| 380 | } | ||
| 381 | |||
| 382 | std::vector<std::string> SplitPathComponentsCopy(std::string_view filename) { | ||
| 383 | std::vector<std::string> components; | ||
| 384 | ForEachPathComponent(filename, [&](auto component) { components.emplace_back(component); }); | ||
| 385 | |||
| 386 | return components; | ||
| 369 | } | 387 | } |
| 370 | 388 | ||
| 371 | std::string SanitizePath(std::string_view path_, DirectorySeparator directory_separator) { | 389 | std::string SanitizePath(std::string_view path_, DirectorySeparator directory_separator) { |
| @@ -400,9 +418,9 @@ std::string SanitizePath(std::string_view path_, DirectorySeparator directory_se | |||
| 400 | return std::string(RemoveTrailingSlash(path)); | 418 | return std::string(RemoveTrailingSlash(path)); |
| 401 | } | 419 | } |
| 402 | 420 | ||
| 403 | std::string_view GetParentPath(std::string_view path) { | 421 | std::string GetParentPath(std::string_view path) { |
| 404 | if (path.empty()) { | 422 | if (path.empty()) { |
| 405 | return path; | 423 | return std::string(path); |
| 406 | } | 424 | } |
| 407 | 425 | ||
| 408 | #ifdef ANDROID | 426 | #ifdef ANDROID |
| @@ -421,7 +439,7 @@ std::string_view GetParentPath(std::string_view path) { | |||
| 421 | name_index = std::max(name_bck_index, name_fwd_index); | 439 | name_index = std::max(name_bck_index, name_fwd_index); |
| 422 | } | 440 | } |
| 423 | 441 | ||
| 424 | return path.substr(0, name_index); | 442 | return std::string(path.substr(0, name_index)); |
| 425 | } | 443 | } |
| 426 | 444 | ||
| 427 | std::string_view GetPathWithoutTop(std::string_view path) { | 445 | std::string_view GetPathWithoutTop(std::string_view path) { |