Delphi 2009 Issues

Delphi 2009 is new generation of Delphi products. Major change in this Delphi version is different string handling. Delphi 2009 is not fully compatible with previous Delphi versions. Old code in new Delphi may work, may not work or work not well.

Exists lot of topics about changing old code for new Delphi. It needs some changed in the code. When you need to made your source compatible with new and old Delphi, it is much harder!

Some libraries made separate branch for Delphi 2009 for this reason. But manage two branches of same library is not easy too and costs lot of time and energy. Synapse trying to made one source code what is compatible with all supported compilers. But sometimes you must do some additional coding in Delphi 2009.

Longstrings in older Delphi

What is longstring in Delphi? It is automaticaly managed memory buffer what can hold any bytes of datas. This datatype is called ansistring, because it is usually used for hold string with ANSI codepage. However this datatype can hold really any bytes.

In later Delphi versions exists second datatype called utf8string. It is designed for hold string in UTF-8 encoding, but technically it is just alias for ansistring. Exists some next aliases for ansistring. It is because ansistring have not care what is inside. It can hold really any binary data and you must know what datas are inside ansistring.

Longstring in Delphi 2009

Delphi 2009 changes string handling. Ansistring has been enhanced by one important information - codepage. Ansistring have care about data inside now. It know what codepage have string inside. Ansistring have defined ANSI codepage. utf8string is same ansistring, but with predefined UTF-8 codepage. Unicodestring is still same ansistring, but with defined UCS (or better UTF-16) encoding. You can define your own string type with desired codepage.

What happen when you assign utf8string to ansistring? On previous Delphi versions it just copy bytes from one string to seconds as is. But D2009 knows what codepages are used by each string and do implicit charset conversion. It not copy bytes as is, it do transformation form one codepage to second codepage.

This is major reason why old code cannot work in new Deplhi properly - simple string assignment is not simple now.

Longstring usage in Synapse

In Synapse are ansistrings used by these major kinds:

binary data buffers

Binary communication buffers are implemented as ansistrings. Data from socket goes to memory what is managed by ansistring and all next processing goes through ansistrings.

Storage for string in any charset

(and programmer must know what charset is currently used)

It is case of synachar unit. Here is one charset conversion function what getting on ansistring with some data and one parameter, where you tell what charset is uds in input string. And you got another ansistring as result with data in codepage what you want. Ansistrings are used as buffer for data in any charset or encoding.

Synapse on Delphi 2009

On Delphi 2009 we must avoid unnecessary implicit charset conversion on string assignments, because this conversion can made broken data.

binary data buffers

Binary data buffers in Synapse are strinctly defined as ansichar. Socket communication functions reading bytes directly to ansichar and all internal assignmenst are from ansistring to ansistring. No reason for implicit charset conversion here. Synapse itself have data buffers on API as ansistrings. If your application want to work with raw bytes sended/received from sockets, then use ansistrings too and Delphi do not any implicit conversion. Datas are left in ansistring as is. Just take care about usage of ansistring data type and do not use just string data type. Once you are use ansistring, then your code is fully compatible across all supported compilers.

Storage for string in any charset

Here is situation much harder. Synachar unit de-facto duplicate implicit charset conversion by explicit way. This unit can be written by totally different way when it live in Delphi 2009 only.

Because Synapse charset conversion usinf anstistrings on input and output, you must take care about assigning and reading data by way what not doing implicit charset conversion.

For example, you have utf8string and wish to convert it to something else by Synachar. Because ansistring is on input, you cannot simply assign utf8string to conversion routine, because it do implicit charset conversion frm UTF-8 to your ANSI codepage. You need to get raw bytes from utf8string and write it to input ansistring as is. Reading is similar, but in reverse.

This solution is not ideal. I know, and I plan to improve it by some more friendly way. All charset handling routines (include all MIME routines) needs to be improved to some little different interface what can be used friendly on both new and old Delphi versions.

public/howto/d2009.txt · Last modified: 2009/03/09 14:40 by geby
Driven by DokuWiki Recent changes RSS feed