Why are string classes implemented in several different ways and what are the advantages and disadvantages? I have seen it done several differents ways
- Only using a simple
char(most basic way). - Supporting UTF8 and UTF16 through a templated string, such as
string<UTF8>. WhereUTF8is acharandUTF16is anunsigned short. - Having both a UTF8 and UTF16 in the string class.
Are there any other ways to implement a string class that may be better?
As far as I know
std::basic_string<wchar_t>wheresizeof(wchar_t) == 2is not UTF16 encoding. There are more than 2^16 characters in unicode, and codes go at least up to0xFFFFFwhich is >0xFFFF(2bytewchar_tcapacity). As a result, proper UTF16 should use variable number of bytes per letter (one 2bytewchar_tor two of them), which is not the case withstd::basic_stringand similar classes that assume thatone string element==one character.As far as I know there are two ways to deal with unicode strings.
sizeof(wchar_t) == 4), so you’ll be able to enjoy “benefits” (basically, easy string length calculation and nothing else) ofstd::string-like classes.As long as you don’t use
charit doesn’t matter which method you use.char-based strings are likely to cause trouble on machines with different 8bit codepage, if you weren’t careful enough to take care of that (It is safe to assume that you’ll forget about it and won’t be careful enough – Microsoft Applocale was created for a reason).Unicode contains plenty of non-printable characters (control and formatting characters in unicode), so that pretty much defeats any benefit method #1 could provide. Regardless, if you decide to use method #1, you should remember that
wchar_tis not big enough to fit all possible characters on some compilers/platforms (windows/microsoft compiler), and thatstd::basic_string<wchar_t>is not a perfect solution because of that.Rendering internationalized text is PAIN, so the best idea would be just to grab whatever unicode-compatible string class (like QString) there is that hopefully comes with text layout engine (that can properly handle control characters and bidirectional text) and concentrate on more interesting programming problems instead.
-Update-
UTF16 is variable-length character encoding. UTF16 uses 1..2 2-byte (i.e.
uint16_t, 16 bit) elements per character. I.e. number of of elements in UTF16 string != number of characters in string for UTF16. You can’t calculate string length by counting elements.UTF8 is another variable-length encoding, based on 1byte elements (8 bit, 1 byte or “unsigned char”). One unicode character (“code point”) in UTF8 takes 1..4
uint8_telements. Once again, number of elements in string != number of characters in string. The advantage of UTF8 is characters that exist within ASCII take exactly 1 byte per character in UTF8, which saves a bit of space, while in UTF16, character always takes at least 2 bytes.UTF32 is fixed-length character encoding, that always uses 32bit (4 bytes or
uint32_t) per character. Currently any unicode character can fit into single UTF32 element, and UTF32 will probably remain fixed-length for a long time (I don’t think that all languages of Earth combined would produce 2^31 different characters). It wastes more memory, but number of elements in string == number of characters in string.Also, keep in mind, that C++ standard doesn’t specify how big “int” or “short” should be.