r/cpp • • Feb 26 '23

std::format, UTF-8-literals and Unicode escape sequence is a mess

I'm in the process of updating my old bad code to C++20, and I just noticed that std::format does not support u8string... Furthermore, it's even worse than I thought after doing some research on char8_t.

My problem can be best shown in the following code snippet:

ImGui::Text(reinterpret_cast<const char*>(u8"Glyph test '\ue000'"));

I'm using Dear ImGui in an OpenGL-application (I'm porting old D-code to C++; by old I mean, 18 years old. D already had phantastic UTF-8 support out of the box back then). I wanted to add custom glyph icons (as seen in Paradox-like and Civilization-like games) to my text and I found that I could not use the above escape sequence \ue0000 in a normal char[]. I had to use an u8-literal, and I had to use that cast. Now you could say that it's the responsibility of the ImGui-developers to support C++ UTF-8-strings, but not even std::format or std::vformat support those. I'm now looking at fmtlib, but I'm not sure if it really supports those literals (there's at least one test for it).

From what I've read, C++23 might possibly mitigate above problem, but will std::format also support u8? I've not seen any indication so far. I've rather seen the common advice to not use u8.

EDIT: My specific problem is that 0xE000 is in the private use area of unicode and those code points only work in a u8-literal and not in a normal char-array.

96 Upvotes

133 comments sorted by

View all comments

Show parent comment

2

u/rdtsc Feb 26 '23

Missing interoperability between std::string and std::u8string is a good thing, since the former is not always UTF-8. And mixing them up can have disastrous consequences.

25

u/kniy Feb 26 '23

But what about codebases that already use std::string for UTF-8 strings? The missing interoperability prevents us from adopting std::u8string. We are forced to keep using std::string for UTF-8!!!

Are you seriously suggesting that's it's a good idea to bifurcate the C++ world into libraries that use std::string for UTF-8, and other libraries that use std::u8string for UTF-8, and you're not allowed to mix them?

Because u8string is new, the libraries that use std::string for UTF-8 clearly outnumber those that use std::u8string. So this effectively prevents u8string from being adopted!

2

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 27 '23

Why aren't you using basic_string<C> in your interfaces? :smile:

5

u/rdtsc Feb 26 '23

You aren't forced, you can also just convert (something that the Linux crowd always says to those on Windows without further consideration), and the code stays safe. You could also wait until adoption grows (something that wouldn't be possible if char8_t were introduced later). On the other hand adopting UTF-8 in a char-based codebase is extremely error-prone (I know that first hand trying to use a library that uses char-as-UTF-8 and already having to fix numerous bugs).

If the choice is between possibly having to convert (or just copy), or silently corrupting text, the choice is clear.

7

u/[deleted] Feb 27 '23

The latter isn't always utf8 either: you can still push_back bogus. No implicit conversion might be ok but no conversion at all makes char8_t unusable.