FPC Unicode support

From Lazarus wiki
Revision as of 10:29, 21 September 2012 by Chronos (talk | contribs) (category)


Free Pascal compiler and RTL/FCL should natively support Unicode. Since several releases, Delphi supports Unicode. FPC must be compatible with Delphi in Unicode support.

NOTE: This page has not been updated in a while. Since FPC 2.7 (current development version), extensive Unicode support has been implemented.

Tiburon Unicode support

Currently we have some information about Tiburon's Unicode support implementation.



FPC Unicode support

FPC must have the following string types with transparent conversion between them (like current AnsiString <-> WideString conversion):

  • shortstring
  • ansistring
  • widestring
  • utf8string
  • utf16string
  • utf32string
  • ucs2string (?)
  • ucs4string (?)

Development and further maintenance of these string types must be as simple as possible. New string types must be easily added in future if needed.

Compiler uses generic structure and helper routines to handle all refcounted string types.

String header:

  TRefStringRec = packed record
    Encoding: word;    // encoding of string
    ElementSize: byte; // size in bytes of string's element (1-4)
    Ref: SizeInt;      // number of references
    Len: SizeInt;      // number of elements is string 

Helper routines will know how to handle string from its header.

Extra parameter with string type information is passed to some routines (like fpc_RefString_SetLength) to allow properly initialize new strings.

widestring type on Windows targets remains non-refcounted and OLE compatible. Minimal number of helper routines is used for it. On non-Windows targets widestring is alias to utf16string.

The compiler uses helpers for string type conversions like this:

procedure fpc_ansistring_to_utf16string(out dst: utf16string; const src: ansistring);
procedure fpc_utf32string_to_utf16string(out dst: utf16string; const src: utf32string);

The compiler generates helper procedure name from type names. The compiler does not perform any string conversion handling by itself.

Status of Unicode support in FPC so far

Currently FPC 2.3.x has a new type called UnicodeString. This is similar to a WideString type. The difference being that UnicodeString is reference counted on all platforms.

All implementation work is currently done in a separate svn branch: http://svn.freepascal.org/svn/fpc/branches/cpstrnew

User visible changes

Full support of code page aware strings is not possible without breaking some existing code. The following list tries to summarize the most important user visible changes.

  • The string header has two new fields: encoding and element size. On 32 Bit platforms this increases the header size by 4 and on 64 bit platforms by 8 bytes.
  • WideCharLenToString, UnicodeCharLenToString, WideCharToString, UnicodeCharToString and OleStrToString return an UnicodeString instead of an Ansistring before.
  • the type of the dest parameter of WideCharLenToString and UnicodeCharLenToString has been changed from Ansistring to Unicodestring
  • UTF8ToAnsi and AnsiToUTF8 take a RawByteString now

Roadmap of RTL Unicode support with UnicodeString

Topic Status Comments Assigned To
Locale Variables Not implemented Variables are all 1 byte in size and can't hold UnicodeChar size values. e.g.: The Russian thousand separator is a no-break space $00A0 which doesn't fit in the ThousandSeparator (standard Char type) variable.
TStrings Not implemented There is no UnicodeString version of TStrings
TStringList Not implemented There is no UnicodeString version of TStringList
Pos() Working

Roadmap of RTL Unicode support with UTF8String

Topic Status Comments Assigned To
UTF8String Not implemented Needs a real implementation. Is currently just an alias for ansistring.
TStrings Not implemented There is no UTF8String version of TStrings
TStringList Not implemented There is no UTF8String version of TStringList

See Also