Of String and Unicode String

The change in the definition of the Char type is important because it is tied to the change in the definition of the string type. Unlike characters, though, string is mapped to a brand new data type that didn't exist before, called UnicodeString. As we'll see, its internal representation is also quite different from that of the classic AnsiString29 type.

As there was already a WideString type in the language, representing strings based on the WideChar type, why bother defining a new data type? WideString was (and still is) not reference counted and is extremely poor in terms of performance and flexibility (for example, it uses the Windows global memory allocator rather than the native FastMM4).

Like AnsiString, UnicodeString is reference counted, uses copy-on-write semantics and is quite performs quite well. Unlike AnsiString, UnicodeString uses two-bytes per character30 and is based on UTF-16.

29 I'm using the specific terms classic AnsiString type, to refer to the string type as it used to work from Delphi 2 until Delphi 2007. AnsiString type is still part of Delphi 2009, but it has a modified behavior, so when referring to its past structure I'll use the term classic AnsiString.

30 Actually UTF-16 is a variable length encoding, and at times UnicodeString uses two WideChar surrogate elements (that is, four bytes) to represent a single Unicode code point.

The string type is now mapped to UnicodeString in a hard-coded way as is the Char type and for the same reasons. There is no compiler directive or other trick to change that. If you have code that needs to continue to use the old string type, just replace it with an explicit declaration of the AnsiString type.

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment