[Base] Add split_string.

This commit is contained in:
gibbed 2019-08-04 06:43:39 -05:00
parent 2048239f30
commit 01c656b77b
2 changed files with 42 additions and 13 deletions

View File

@ -103,6 +103,40 @@ std::wstring format_string(const wchar_t* format, va_list args) {
}
}
std::vector<std::string> split_string(const std::string& path,
const std::string& delimiters) {
std::vector<std::string> 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<std::wstring> split_string(const std::wstring& path,
const std::wstring& delimiters) {
std::vector<std::wstring> 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<std::string> split_path(const std::string& path) {
std::vector<std::string> 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,

View File

@ -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<std::string> split_string(const std::string& path,
const std::string& delimiters);
std::vector<std::wstring> 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);