This is not a divine revelation, but simply semblance of my personal reference.
In Delphi 2007, AnsiString was the default string type used for general-purpose string manipulation.
This means variables declared simply as string were compiled as AnsiString. This changed in Delphi 2009+, where string became an alias for UnicodeString.
AnsiString in Delphi 2007:- In Delphi 2007
Charwas an 8-bit (1-byte)AnsiChar, while in Delphi 2009+, it became a 16-bitWideChar(UTF-16) by default. - Encoding: The interpretation of these bytes depends on the operating system's current active code page (e.g., Windows-1252, or specific locales like code page 936 for simplified Chinese).
- Length:
AnsiStringwas dynamically allocated and limited only by available memory, unlike the olderShortStringthat was limited to 255 characters.
The primary distinction in Delphi 2009 and later is the shift to Unicode.
Charnow represents a 16-bit character, enabling full Unicode support.CharbecomesWideCharstringis an alias forUnicodeString(UTF-16, 2 bytes per character).AnsiStringstill exists but is used primarily for backward compatibility or interfacing with non-Unicode systems/APIs.
- AnsiString to UnicodeString: String changes, leading to potential data loss if ANSI data is assigned directly to Unicode strings.
- Explicit Casts: Use
PAnsiChar(myWideString),AnsiString(myUnicodeString)and similar, where needed, and be aware of character mapping. - Migration Required: Code using
Char,PCharneeded updates, especially with assignments between ANSI and Unicode types. - If a 1-byte buffer is in need, use
RawByteStringinstead ofstringorChar.RawByteStringisAnsiStringwith no code page set by default (AnsiString($ffff)).
Delphi and Unicode, Marco Cantù, December 2008

No comments:
Post a Comment