summaryrefslogtreecommitdiff
path: root/src/common/fs/path_util.cpp
diff options
context:
space:
mode:
authorGravatar BreadFish642023-12-05 23:17:19 -0500
committerGravatar Liam2023-12-05 23:17:19 -0500
commitd5de9402ee22eea985d6842baafd8922e15d3784 (patch)
treef302a3a87aad12213c36ef00cd28c86e3e27ba61 /src/common/fs/path_util.cpp
parentMerge pull request #12271 from liamwhite/pretext-fix (diff)
downloadyuzu-d5de9402ee22eea985d6842baafd8922e15d3784.tar.gz
yuzu-d5de9402ee22eea985d6842baafd8922e15d3784.tar.xz
yuzu-d5de9402ee22eea985d6842baafd8922e15d3784.zip
Improve path splitting speed
Diffstat (limited to '')
-rw-r--r--src/common/fs/path_util.cpp38
1 files changed, 28 insertions, 10 deletions
diff --git a/src/common/fs/path_util.cpp b/src/common/fs/path_util.cpp
index c3a81f9a9..d2f50432a 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
357std::vector<std::string> SplitPathComponents(std::string_view filename) { 357template <typename F>
358 std::string copy(filename); 358static 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 }
366 } 369 }
370 if (component_begin != end) {
371 cb(std::string_view{component_begin, end});
372 }
373}
374
375std::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); });
378
379 return components;
380}
381
382std::vector<std::string> SplitPathComponentsCopy(std::string_view filename) {
383 std::vector<std::string> components;
384 ForEachPathComponent(filename, [&](auto component) { components.emplace_back(component); });
367 385
368 return out; 386 return components;
369} 387}
370 388
371std::string SanitizePath(std::string_view path_, DirectorySeparator directory_separator) { 389std::string SanitizePath(std::string_view path_, DirectorySeparator directory_separator) {