My code is
void utf8_append(UChar32 cp, std::string& str) {
size_t offset = str.size();
str.resize(offset + U8_LENGTH(cp));
auto ptr = reinterpret_cast<uint8_t*>(&str[0]);
U8_APPEND_UNSAFE(ptr, offset, static_cast<uint32_t>(cp));
}
This works but seems ugly. Maybe I am overlooking a simpler approach?
Relevant documentation: https://unicode-org.github.io/icu/userguide/strings/utf-8.html and https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/utf8_8h.html.