From 01c656b77bffa43de4894a7396155cb03e28eac9 Mon Sep 17 00:00:00 2001 From: gibbed Date: Sun, 4 Aug 2019 06:43:39 -0500 Subject: [PATCH] [Base] Add split_string. --- src/xenia/base/string.cc | 48 +++++++++++++++++++++++++++++----------- src/xenia/base/string.h | 7 ++++++ 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/xenia/base/string.cc b/src/xenia/base/string.cc index ded97ffa5..44b3ce6c5 100644 --- a/src/xenia/base/string.cc +++ b/src/xenia/base/string.cc @@ -103,6 +103,40 @@ std::wstring format_string(const wchar_t* format, va_list args) { } } +std::vector split_string(const std::string& path, + const std::string& delimiters) { + std::vector parts; + size_t n = 0; + size_t last = 0; + while ((n = path.find_first_of(delimiters, last)) != path.npos) { + if (last != n) { + parts.push_back(path.substr(last, n - last)); + } + last = n + 1; + } + if (last != path.size()) { + parts.push_back(path.substr(last)); + } + return parts; +} + +std::vector split_string(const std::wstring& path, + const std::wstring& delimiters) { + std::vector parts; + size_t n = 0; + size_t last = 0; + while ((n = path.find_first_of(delimiters, last)) != path.npos) { + if (last != n) { + parts.push_back(path.substr(last, n - last)); + } + last = n + 1; + } + if (last != path.size()) { + parts.push_back(path.substr(last)); + } + return parts; +} + std::string::size_type find_first_of_case(const std::string& target, const std::string& search) { const char* str = target.c_str(); @@ -132,19 +166,7 @@ std::wstring to_absolute_path(const std::wstring& path) { } std::vector split_path(const std::string& path) { - std::vector parts; - size_t n = 0; - size_t last = 0; - while ((n = path.find_first_of("\\/", last)) != path.npos) { - if (last != n) { - parts.push_back(path.substr(last, n - last)); - } - last = n + 1; - } - if (last != path.size()) { - parts.push_back(path.substr(last)); - } - return parts; + return split_string(path, "\\/"); } std::string join_paths(const std::string& left, const std::string& right, diff --git a/src/xenia/base/string.h b/src/xenia/base/string.h index 0d8eef2c5..a61a98d4c 100644 --- a/src/xenia/base/string.h +++ b/src/xenia/base/string.h @@ -40,6 +40,13 @@ inline std::wstring format_string(const wchar_t* format, ...) { return result; } +// Splits the given string on any delimiters and returns all parts. +std::vector split_string(const std::string& path, + const std::string& delimiters); + +std::vector split_string(const std::wstring& path, + const std::wstring& delimiters); + // find_first_of string, case insensitive. std::string::size_type find_first_of_case(const std::string& target, const std::string& search);