[util] Add str::split

Used by d3d8
This commit is contained in:
Alpyne 2023-06-20 00:09:09 +01:00 committed by Joshie
parent 211d095ee4
commit 0b9acf3a25
1 changed files with 27 additions and 1 deletions

View File

@ -212,5 +212,31 @@ namespace dxvk::str {
dst[count - 1] = '\0';
}
}
/**
* \brief Split string at one or more delimiters characters
*
* \param [in] string String to split
* \param [in] delims Delimiter characters
* \returns Vector of substring views
*/
inline std::vector<std::string_view> split(std::string_view string, std::string_view delims = " ") {
std::vector<std::string_view> tokens;
for (size_t start = 0; start < string.size(); ) {
// Find first delimiter
const auto end = string.find_first_of(delims, start);
// Add non-empty tokens
if (start != end)
tokens.emplace_back(string.substr(start, end-start));
// Break at the end of string
if (end == std::string_view::npos)
break;
start = end + 1;
}
return tokens;
}
}