3
\$\begingroup\$

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.

asked Oct 21, 2020 at 16:46
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Beauty is in the eye of the beholder. I say it is perfectly valid and correct code! The only thing you might get rid of is the static_cast<uint32_t>, as an UChar32, which is an alias forint32_t, will implicitly cast to uint32_t without warnings. You could also use append() instead of resize(), avoiding the addition, and remove the temporary ptr, to finally get:

void utf8_append(UChar32 cp, std::string& str) {
 auto offset = str.size();
 str.append(U8_LENGTH(cp), {});
 U8_APPEND_UNSAFE(reinterpret_cast<uint8_t *>(&str[0]), offset, cp);
}

If you can use C++17, str.data() is slightly nicer than &str[0] in my opinion. Or you could write &str.front().

answered Oct 21, 2020 at 18:19
\$\endgroup\$
2
  • \$\begingroup\$ "without warnings" Not with this project's warning settings. And no C++17, unfortunately. \$\endgroup\$ Commented Oct 21, 2020 at 18:37
  • \$\begingroup\$ Ah ok. Well if they are that strict then you're stuck with the static_cast of course. You could consider using std::basic_string<uint8_t> to get rid of both casts, but it will probably open up a can of worms elsewhere in your codebase. \$\endgroup\$ Commented Oct 21, 2020 at 18:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.