This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
UTF-8 support - char or wchar_t?
- From: Ole Laursen <olau at hardworking dot dk>
- To: libstdc++ at gcc dot gnu dot org
- Date: Sun, 20 Jun 2004 18:23:47 +0200
- Subject: UTF-8 support - char or wchar_t?
Hi,
I reported a bug (#16006) which was closed as invalid, which makes me
wonder - do the iostreams in libstdc++ support UTF-8? And if so how
is one supposed to use them to produce UTF-8?
More specifically, what I want to do is to use an ostringstream to
convert numbers and so forth to UTF-8 encoded strings. I need this to
define a template which is used like this
Glib::ustring str = compose("Workspace %1", workspace_no);
since the iostream model without a printf-like string unfortunately is
completely useless for i18n reasons. Until now, I've been doing
something like this for the actual conversion:
template <typename T>
std::string stringify(T obj)
{
std::ostringstream os;
os.imbue(std::locale("")); // use the user's locale for the stream
os << obj;
return Glib::locale_to_utf8(os.str());
}
Yes, not exactly efficient or beautiful, but it has worked. Until
someone used a Finnish UTF-8 locale where the thousands separator is
0xA0 which is two bytes in UTF-8. Apparently, libstdc++ then only
outputs the first of these two bytes, e.g. 1,224 becomes 1Â224 (and
should have been 1Â 224). This is obviously incorrect UTF-8, and makes
my GTK+ programs crash horribly.
The closing comments in my bug report seem to suggest that I should
use the wchar_t equivalents of the stringstream classes. As far as I
know, that would mean that I get 4 byte wide character strings in
UCS-32 (but am I guaranteed that?). I can perhaps find a way to
convert this to UTF-8, but doesn't libstdc++ support UTF-8 in a more
direct manner?
--
Ole Laursen
http://www.cs.aau.dk/~olau/