diff --git a/common/StringUtil.cpp b/common/StringUtil.cpp index 8b1d7434e5..33f0bc32d5 100644 --- a/common/StringUtil.cpp +++ b/common/StringUtil.cpp @@ -251,6 +251,30 @@ namespace StringUtil return str.substr(start, end - start + 1); } + std::vector SplitString(const std::string_view& str, char delimiter, bool skip_empty /*= true*/) + { + std::vector 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 ret; diff --git a/common/StringUtil.h b/common/StringUtil.h index 1f0d0c9560..f71bd4daaa 100644 --- a/common/StringUtil.h +++ b/common/StringUtil.h @@ -167,6 +167,9 @@ namespace StringUtil /// Strip whitespace from the start/end of the string. std::string_view StripWhitespace(const std::string_view& str); + /// Splits a string based on a single character delimiter. + std::vector SplitString(const std::string_view& str, char delimiter, bool skip_empty = true); + /// Strided memcpy/memcmp. 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)