diff --git a/include/Nazara/Core/StringExt.hpp b/include/Nazara/Core/StringExt.hpp index c07f796c0..c4b314fb8 100644 --- a/include/Nazara/Core/StringExt.hpp +++ b/include/Nazara/Core/StringExt.hpp @@ -37,6 +37,9 @@ namespace Nz NAZARA_CORE_API bool StartsWith(const std::string_view& str, const std::string_view& s, CaseIndependent); NAZARA_CORE_API bool StartsWith(const std::string_view& str, const std::string_view& s, CaseIndependent, UnicodeAware); + template bool SplitString(const std::string_view& str, const std::string_view& token, F&& func); + template bool SplitStringAny(const std::string_view& str, const std::string_view& token, F&& func); + inline std::string ToLower(const char* str); NAZARA_CORE_API std::string ToLower(const std::string_view& str); diff --git a/include/Nazara/Core/StringExt.inl b/include/Nazara/Core/StringExt.inl index 72ce07a60..71a04ebff 100644 --- a/include/Nazara/Core/StringExt.inl +++ b/include/Nazara/Core/StringExt.inl @@ -46,6 +46,40 @@ namespace Nz #endif } + template + bool SplitString(const std::string_view& str, const std::string_view& token, F&& func) + { + std::size_t pos = 0; + std::size_t previousPos = 0; + while ((pos = str.find(token, previousPos)) != std::string::npos) + { + std::size_t splitPos = previousPos; + previousPos = pos + token.size(); + + if (!func(str.substr(splitPos, pos - splitPos))) + return false; + } + + return func(str.substr(previousPos)); + } + + template + bool SplitStringAny(const std::string_view& str, const std::string_view& token, F&& func) + { + std::size_t pos = 0; + std::size_t previousPos = 0; + while ((pos = str.find_first_of(token, previousPos)) != std::string::npos) + { + std::size_t splitPos = previousPos; + previousPos = pos + 1; + + if (!func(str.substr(splitPos, pos - splitPos))) + return false; + } + + return func(str.substr(previousPos)); + } + inline std::string ToLower(const char* str) { std::size_t size = std::strlen(str);