devilutionX/Source/utils/utf8.cpp
Gleb Mazovetskiy e9a9daa794 DrawString: Stop allocating
Switch to a state-machine UTF-8 decoder from the branchless one.
This allows us to avoid copying the string on every `DrawString` call.
2021-11-07 04:17:50 +01:00

27 lines
582 B
C++

#include "utils/utf8.hpp"
#include <cstddef>
#include <hoehrmann_utf8.h>
namespace devilution {
char32_t DecodeFirstUtf8CodePoint(string_view input, uint8_t *len)
{
uint32_t codepoint = 0;
uint32_t state = UTF8_ACCEPT;
for (std::size_t i = 0; i < input.size(); ++i) {
state = utf8_decode_step(state, static_cast<uint8_t>(input[i]), &codepoint);
if (state == UTF8_ACCEPT) {
*len = i + 1;
return codepoint;
}
if (state == UTF8_REJECT) {
*len = i + 1;
return Utf8DecodeError;
}
}
return codepoint;
}
} // namespace devilution