[fpc-devel] Delphi new AnsiStrings are incredibly broken :-(

Hans-Peter Diettrich DrDiettrich1 at aol.com
Fri Oct 14 17:58:50 CEST 2011


Paul Ishenin schrieb:

>> A general function
>>  function Pos(SubStr: T1; Str: T2): integer;
>> should return the character index of SubStr in Str, i.e. Str[i] should 
>> definitely be the begin of SubStr within Str.
> Just think of Pos() as it only compares bytes.

Ah, that may be correct for the old (ASCII/Ansi) model. Thanks for the
reminder :-)

> AnsiPos() should care 
> about encoding too. As I remember AnsiPos() has AnsiString arguments 
> instead of RawByteString and therefore the conversion will be made 
> before the byte compare.

AnsiPos *was* designed for dealing with MBCS encodings, but in Unicode
Delphi the implementation changed to UnicodeString arguments, the "Ansi"
names only have been retained for compatibility with existing code.

Thus AnsiPos *now* only accepts UnicodeStrings, with obviously wrong
results when called with (automatically converted) AnsiStrings :-(


There exists an AnsiStrings unit, with another AnsiPos function, which 
accepts an AnsiString. Unfortunately I couldn't make it work right now, 
dunno why, and it only accepts native (CP_ACP) AnsiStrings - no solution 
for UTF-8 strings :-(

DoDi




More information about the fpc-devel mailing list