Skip to main content
gamedev.net
🔒 Locked

Unicode String

Started by blanky 10 replies 800+ views
Original Post
blanky
blanky
I need to know how to enable Unicode support in the String STL class. I read it in a book that it supports Unicode, or WCHAR, but I haven't found anything on the topic, thanks.
SiCrane
SiCrane
You may want to try using std::wstring, which is like std::string, except that it uses wchar_ts intead of chars. However, the standard doesn't guarantee that std::wstring will use a unicode encoding. Typical C++ library implementations use UCS-2 encodings which can be considered a Unicode subset.
Code-R
Code-R
std::basic_string is a template. you can support the latest 31bit unicode by doing:
typedef ustring std::basic_string<int32>;

:D:D
Fruny
Fruny
Assuming you have a type named int32. There's int32_t in stdint.h, but that's C99, not C++ (yet).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
zoggo
zoggo
Firstly, anyone who declares typedefs like that and gets away with it needs their compiler looking at...
typedef ustring std::basic_string<int32>;
should have the order of names reversed.

Secondly, the standard is a little sketchy about what wchar_t actually is:
Quote:
values can represent distince codes for all memebers of the largest extended character set specified among supported locales.


This has the unfortunate consequence of meaning your wide string use 2 bytes per character on Windows and 4 bytes per character on most *nix (including Mac OSX) systems.
SiCrane
SiCrane
Quote:
Original post by Code-R
std::basic_string is a template. you can support the latest 31bit unicode by doing:
typedef ustring std::basic_string<int32>;

:D:D


That typedef does not guarantee a unicode encoding. Furthermore, typedefs for basic_string other than std::string or std::wstring are inherently difficult to use since you then have to define nearly all the functions necessary to use them. Ex: specialize std::ctype, create your own versions of tolower() and toupper(), etc. This means you'll need to essentially write your own Unicode support from scratch. The typedef by itself essentially gives you nothing.
njpaul
njpaul
The only time I had problems with unicode strings was when I was trying to write them to a file and I would get a space after every letter. I switched to the Windows way of writing files and it worked.
blanky
blanky
I'm kinda confused as to what to use, thanks though.
SiCrane
SiCrane
Personally for my Unicode needs I use IBM's ICU library. It provides a UnicodeString class, with all the trimmings, including normalization.
blanky
blanky
Is it cross-platform? Thanks.
SiCrane
SiCrane
ICU is available for Windows, Linux, AIX, Solaris, HP-UX, Mac OS X, FreeBSD, NetBSD, OpenBSD, QNX, BeOS, IRIX and even OS/390.

So yes.
zoggo
zoggo
Quote:
Personally for my Unicode needs I use IBM's ICU library.


Cool! I've been looking for a decent unicode library for ages. Cheers SiCrane

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.