Re: [OFF] Convert int to ustring



"Vinzenz 'evilissimo' Feenstra" <mailinglist evilissimo-softdev de> writes:

> #include <sstream>
>
> int num = 1000;
> Glib::ustring str;
> std::stringstream strstr;
> strstr << num;
> strstr >> str;
> Now str contains a string "1000"

Using std::stringstreams with Glib::ustring in this way is dangerous.

One example where I have seen it cause a crash is with the
corresponding example with a float. The decimal point gets converted
to a > 255 char in some localec, the char is converted to a multibyte
sequence, the sequence is clipped inside the stringstream because the
char type is too small to contain it, and the clipped sequence is
outputted in the Glib::ustring. So suddenly, you have invalid UTF-8 in
it.

There are more details hidden in this bug report (I recently added an
extra conclusion at the bottom):

  http://bugzilla.gnome.org/show_bug.cgi?id=93787

One way out is my light-weight (one header) compose library

  http://www.cs.aau.dk/~olau/compose/

It's not particularly fast, though, since it uses a wstringstream
internally. Unfortunately, formatted input/output sucks in C++. The
whole situation is a complete mess. I once set out to write a
replacement for the standard IOStreams, but I never had the time. It's
not an easy task.

-- 
Ole Laursen
http://www.cs.aau.dk/~olau/



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]