diff options
Diffstat (limited to 'src/common/fs')
| -rw-r--r-- | src/common/fs/path_util.cpp | 44 | ||||
| -rw-r--r-- | src/common/fs/path_util.h | 8 | 
2 files changed, 37 insertions, 15 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) {      return path;  } -std::vector<std::string> SplitPathComponents(std::string_view filename) { -    std::string copy(filename); -    std::replace(copy.begin(), copy.end(), '\\', '/'); -    std::vector<std::string> out; - -    std::stringstream stream(copy); -    std::string item; -    while (std::getline(stream, item, '/')) { -        out.push_back(std::move(item)); +template <typename F> +static void ForEachPathComponent(std::string_view filename, F&& cb) { +    const char* component_begin = filename.data(); +    const char* const end = component_begin + filename.size(); +    for (const char* it = component_begin; it != end; ++it) { +        const char c = *it; +        if (c == '\\' || c == '/') { +            if (component_begin != it) { +                cb(std::string_view{component_begin, it}); +            } +            component_begin = it + 1; +        } +    } +    if (component_begin != end) { +        cb(std::string_view{component_begin, end});      } +} + +std::vector<std::string_view> SplitPathComponents(std::string_view filename) { +    std::vector<std::string_view> components; +    ForEachPathComponent(filename, [&](auto component) { components.emplace_back(component); }); -    return out; +    return components; +} + +std::vector<std::string> SplitPathComponentsCopy(std::string_view filename) { +    std::vector<std::string> components; +    ForEachPathComponent(filename, [&](auto component) { components.emplace_back(component); }); + +    return components;  }  std::string SanitizePath(std::string_view path_, DirectorySeparator directory_separator) { @@ -400,9 +418,9 @@ std::string SanitizePath(std::string_view path_, DirectorySeparator directory_se      return std::string(RemoveTrailingSlash(path));  } -std::string_view GetParentPath(std::string_view path) { +std::string GetParentPath(std::string_view path) {      if (path.empty()) { -        return path; +        return std::string(path);      }  #ifdef ANDROID @@ -421,7 +439,7 @@ std::string_view GetParentPath(std::string_view path) {          name_index = std::max(name_bck_index, name_fwd_index);      } -    return path.substr(0, name_index); +    return std::string(path.substr(0, name_index));  }  std::string_view GetPathWithoutTop(std::string_view path) { diff --git a/src/common/fs/path_util.h b/src/common/fs/path_util.h index 2874ea738..59301e7ed 100644 --- a/src/common/fs/path_util.h +++ b/src/common/fs/path_util.h @@ -289,7 +289,11 @@ enum class DirectorySeparator {  // Splits the path on '/' or '\' and put the components into a vector  // i.e. "C:\Users\Yuzu\Documents\save.bin" becomes {"C:", "Users", "Yuzu", "Documents", "save.bin" } -[[nodiscard]] std::vector<std::string> SplitPathComponents(std::string_view filename); +[[nodiscard]] std::vector<std::string_view> SplitPathComponents(std::string_view filename); + +// Splits the path on '/' or '\' and put the components into a vector +// i.e. "C:\Users\Yuzu\Documents\save.bin" becomes {"C:", "Users", "Yuzu", "Documents", "save.bin" } +[[nodiscard]] std::vector<std::string> SplitPathComponentsCopy(std::string_view filename);  // Removes trailing slash, makes all '\\' into '/', and removes duplicate '/'. Makes '/' into '\\'  // depending if directory_separator is BackwardSlash or PlatformDefault and running on windows @@ -298,7 +302,7 @@ enum class DirectorySeparator {      DirectorySeparator directory_separator = DirectorySeparator::ForwardSlash);  // Gets all of the text up to the last '/' or '\' in the path. -[[nodiscard]] std::string_view GetParentPath(std::string_view path); +[[nodiscard]] std::string GetParentPath(std::string_view path);  // Gets all of the text after the first '/' or '\' in the path.  [[nodiscard]] std::string_view GetPathWithoutTop(std::string_view path);  | 
