StringUtil: Add SplitString()

This commit is contained in:
Connor McLaughlin 2022-03-22 23:20:52 +10:00 committed by refractionpcsx2
parent 551d013b63
commit e5248db844
2 changed files with 27 additions and 0 deletions

View File

@ -251,6 +251,30 @@ namespace StringUtil
return str.substr(start, end - start + 1); return str.substr(start, end - start + 1);
} }
std::vector<std::string_view> SplitString(const std::string_view& str, char delimiter, bool skip_empty /*= true*/)
{
std::vector<std::string_view> res;
std::string_view::size_type last_pos = 0;
std::string_view::size_type pos;
while (last_pos < str.size() && (pos = str.find(delimiter, last_pos)) != std::string_view::npos)
{
std::string_view part(StripWhitespace(str.substr(last_pos, pos - last_pos)));
if (!skip_empty || !part.empty())
res.push_back(std::move(part));
last_pos = pos + 1;
}
if (last_pos < str.size())
{
std::string_view part(StripWhitespace(str.substr(last_pos)));
if (!skip_empty || !part.empty())
res.push_back(std::move(part));
}
return res;
}
std::wstring UTF8StringToWideString(const std::string_view& str) std::wstring UTF8StringToWideString(const std::string_view& str)
{ {
std::wstring ret; std::wstring ret;

View File

@ -167,6 +167,9 @@ namespace StringUtil
/// Strip whitespace from the start/end of the string. /// Strip whitespace from the start/end of the string.
std::string_view StripWhitespace(const std::string_view& str); std::string_view StripWhitespace(const std::string_view& str);
/// Splits a string based on a single character delimiter.
std::vector<std::string_view> SplitString(const std::string_view& str, char delimiter, bool skip_empty = true);
/// Strided memcpy/memcmp. /// Strided memcpy/memcmp.
static inline void StrideMemCpy(void* dst, std::size_t dst_stride, const void* src, std::size_t src_stride, static inline void StrideMemCpy(void* dst, std::size_t dst_stride, const void* src, std::size_t src_stride,
std::size_t copy_size, std::size_t count) std::size_t copy_size, std::size_t count)