diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/file_util.cpp | 82 | ||||
-rw-r--r-- | src/common/file_util.h | 13 |
2 files changed, 55 insertions, 40 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index a427372c9..1bc291cf9 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -792,66 +792,80 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam } } -std::vector<std::string> SplitPathComponents(const std::string& filename) { - auto copy(filename); +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(filename); + std::stringstream stream(copy); std::string item; - while (std::getline(stream, item, '/')) + while (std::getline(stream, item, '/')) { out.push_back(std::move(item)); + } return out; } -std::string GetParentPath(const std::string& path) { - auto out = path; - const auto name_bck_index = out.find_last_of('\\'); - const auto name_fwd_index = out.find_last_of('/'); +std::string_view GetParentPath(std::string_view path) { + const auto name_bck_index = path.rfind('\\'); + const auto name_fwd_index = path.rfind('/'); size_t name_index; - if (name_bck_index == std::string::npos || name_fwd_index == std::string::npos) - name_index = std::min<size_t>(name_bck_index, name_fwd_index); - else - name_index = std::max<size_t>(name_bck_index, name_fwd_index); - return out.erase(name_index); + if (name_bck_index == std::string_view::npos || name_fwd_index == std::string_view::npos) { + name_index = std::min(name_bck_index, name_fwd_index); + } else { + name_index = std::max(name_bck_index, name_fwd_index); + } + + return path.substr(0, name_index); } -std::string GetPathWithoutTop(std::string path) { - if (path.empty()) - return ""; +std::string_view GetPathWithoutTop(std::string_view path) { + if (path.empty()) { + return path; + } + while (path[0] == '\\' || path[0] == '/') { - path = path.substr(1); - if (path.empty()) - return ""; + path.remove_suffix(1); + if (path.empty()) { + return path; + } } - const auto name_bck_index = path.find_first_of('\\'); - const auto name_fwd_index = path.find_first_of('/'); + + const auto name_bck_index = path.find('\\'); + const auto name_fwd_index = path.find('/'); return path.substr(std::min(name_bck_index, name_fwd_index) + 1); } -std::string GetFilename(std::string path) { - std::replace(path.begin(), path.end(), '\\', '/'); - auto name_index = path.find_last_of('/'); - if (name_index == std::string::npos) - return ""; +std::string_view GetFilename(std::string_view path) { + const auto name_index = path.find_last_of("\\/"); + + if (name_index == std::string_view::npos) { + return {}; + } + return path.substr(name_index + 1); } -std::string GetExtensionFromFilename(const std::string& name) { - size_t index = name.find_last_of('.'); - if (index == std::string::npos) - return ""; +std::string_view GetExtensionFromFilename(std::string_view name) { + const size_t index = name.rfind('.'); + + if (index == std::string_view::npos) { + return {}; + } return name.substr(index + 1); } -std::string RemoveTrailingSlash(const std::string& path) { - if (path.empty()) +std::string_view RemoveTrailingSlash(std::string_view path) { + if (path.empty()) { return path; - if (path.back() == '\\' || path.back() == '/') - return path.substr(0, path.size() - 1); + } + + if (path.back() == '\\' || path.back() == '/') { + path.remove_suffix(1); + return path; + } return path; } diff --git a/src/common/file_util.h b/src/common/file_util.h index 090907c03..abfa79eae 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -9,6 +9,7 @@ #include <fstream> #include <functional> #include <string> +#include <string_view> #include <type_traits> #include <vector> #include "common/common_types.h" @@ -151,22 +152,22 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam // 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" } -std::vector<std::string> SplitPathComponents(const std::string& filename); +std::vector<std::string> SplitPathComponents(std::string_view filename); // Gets all of the text up to the last '/' or '\' in the path. -std::string GetParentPath(const std::string& path); +std::string_view GetParentPath(std::string_view path); // Gets all of the text after the first '/' or '\' in the path. -std::string GetPathWithoutTop(std::string path); +std::string_view GetPathWithoutTop(std::string_view path); // Gets the filename of the path -std::string GetFilename(std::string path); +std::string_view GetFilename(std::string_view path); // Gets the extension of the filename -std::string GetExtensionFromFilename(const std::string& name); +std::string_view GetExtensionFromFilename(std::string_view name); // Removes the final '/' or '\' if one exists -std::string RemoveTrailingSlash(const std::string& path); +std::string_view RemoveTrailingSlash(std::string_view path); // Creates a new vector containing indices [first, last) from the original. template <typename T> |