Core/StringExt: Add IterateOnCodepoints to remove std::u32string allocations
This commit is contained in:
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user