tags:

views:

121

answers:

2

I'm currently spending some time converting my network unit to support unicode strings and things are going good so far (at least I think so). Unfortunately I'm working on Delphi 7 so I can't test the unicode support in action and I don't have documentation of the newer Delphi versions.

I have used a few TReader and TWriter objects in my code to stream strings with the ReadString and WriteString methods. On D7 they use the String type which equals AnsiString there. If I have understood right, in D2009/D2010 the String type equals UnicodeString. So does anyone know if TReader and TWriter automatically streams strings in UnicodeString format when using the ReadString and WriteString methods in D2009/D2010 or do I need to change something?

+4  A: 

Yes. TReader and TWriter support Unicde. In fact there is only one part of the vcl/rtl that does not support Unicode: the old style Read(Ln) and Write(Ln) file access methods which are also used to read and write directly from and to a console.

Yes, string is UnicodeString in D2009+. And PChar is PWideChar in D2009+. So if you stick to the standard types, your code will port fine.

You will only get into trouble when you base code on the assumption that one character equals one byte.

Update:

Marjan Venema
If you stick to the standard types and have already written code that expects Unicode even though it would never encounter it... then your code will port fine. If you wrote code assuming and expecting ASCII and avoiding ANSI then your code will port OK, but it won't be properly supporting Unicode. The problem is, you may have done this unwittingly. If you are using UpperCase() then you aren't supporting Unicode. To support Unicode properly you should be using ANSIUpperCase(). To give just one bizarre and potentially confusing example.
Deltics
Ah yes, good point. The confusingly named Ansi* methods
Marjan Venema
+2  A: 

I just looked at my Delphi 2010 install, and TWriter has the following methods:

procedure WriteStr(const Value: AnsiString);
procedure WriteUTF8Str(const Value: string);
procedure WriteString(const Value: UnicodeString);
procedure WriteWideString(const Value: UnicodeString);

TReader has equivalent methods. So WriteStr (and ReadStr) will work with AnsiStrings for backwards compatibility, WriteString and WriteWideString will write a unicode string out as unicode, and WriteUTF8Str accepts a unicode string and writes it out as UTF8. (Which saves a lot of space if you're using the ANSI charset.)

Mason Wheeler