Core/StringExt: Add IterateOnCodepoints to remove std::u32string allocations

This commit is contained in:
SirLynix
2023-08-24 08:42:25 +02:00
parent bd53245f42
commit ad738a2803
6 changed files with 187 additions and 164 deletions

View File

@@ -259,6 +259,29 @@ namespace Nz
return {};
}
void IterateOnCodepoints(std::string_view str, FunctionRef<bool(const char32_t* characters, std::size_t characterCount)> callback)
{
std::array<char32_t, 32> buffer;
std::size_t charCount = 0;
utf8::unchecked::iterator<const char*> it(str.data());
utf8::unchecked::iterator<const char*> end(str.data() + str.size());
for (; it != end; ++it)
{
buffer[charCount++] = *it;
if (charCount == buffer.size())
{
if (!callback(&buffer[0], charCount))
return;
charCount = 0;
}
}
if (charCount != 0)
callback(&buffer[0], charCount);
}
bool MatchPattern(std::string_view str, std::string_view pattern)
{
if (str.empty() || pattern.empty())