1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-22 14:24:33 +01:00

Implement C++ solution to truncate UTF8 strings

This commit is contained in:
Duncanspumpkin
2021-03-18 21:56:51 +01:00
committed by Gymnasiast
parent 645685de18
commit 0957542503
3 changed files with 48 additions and 5 deletions

View File

@@ -807,6 +807,22 @@ namespace String
return res;
#endif
}
std::string_view UTF8Truncate(std::string_view v, size_t size)
{
auto trunc = v.substr(0, size);
for (size_t i = 0; i < trunc.size();)
{
auto length = UTF8GetCodePointSize(trunc.substr(i, trunc.size()));
if (!length.has_value())
{
return trunc.substr(0, i);
}
i += *length;
}
return trunc;
}
} // namespace String
char32_t CodepointView::iterator::GetNextCodepoint(const char* ch, const char** next)