Core: Add SplitString(Ext)

This commit is contained in:
Lynix 2020-04-15 19:37:41 +02:00
parent dd74e5ecc1
commit b58b35c322
2 changed files with 37 additions and 0 deletions

View File

@ -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<typename F> bool SplitString(const std::string_view& str, const std::string_view& token, F&& func);
template<typename F> 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);

View File

@ -46,6 +46,40 @@ namespace Nz
#endif
}
template<typename F>
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<typename F>
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);