#1 2024-09-16 08:32:23

tfopc
Member
Registered: 2024-01-08
Posts: 11

DynArray IndexOf - D7 vs D12.1 - Different Results

I have detected an unexpected behavior on old Delphi compiler vs. newer one.

var s:string;
    dynArr:TRawUtf8DynArray;
begin
  s := 'test1';
  setLength(dynArr,1);
  dynArr[0]='test1';
  Result := DynArray(TypeInfo(TRawUtf8DynArray),dynArr).IndexOf(s,true);
end;

-> Delphi7: Result=0, found
-> Delphi12: Result=-1, not found

If i cast the string to RawUtf8 or use RawUtf8 directly it is working.

var u:RawUtf8;
    dynArr:TRawUtf8DynArray;
begin
  u := 'test1';
  setLength(dynArr,1);
  dynArr[0]='test1';
  Result := DynArray(TypeInfo(TRawUtf8DynArray),dynArr).IndexOf(u,true);
end;

-> Delphi7: Result=0, found
-> Delphi12: Result=0, found

Offline

#2 2024-09-16 09:49:47

Chaa
Member
Registered: 2011-03-26
Posts: 248

Re: DynArray IndexOf - D7 vs D12.1 - Different Results

It's expected behavior.

String type is AnsiString in Delphi 7 and UnicodeString in Delphi 12, but RawUtf8 is AnsiString(65001) in both.

So, code in first example is wrong. DynArray.IndexOf parameter must be of the same exact type as the dynamic array element - i.e. RawUtf8.

Last edited by Chaa (2024-09-16 09:50:41)

Offline

#3 2024-09-16 15:21:00

tfopc
Member
Registered: 2024-01-08
Posts: 11

Re: DynArray IndexOf - D7 vs D12.1 - Different Results

Hi Chaa,

thank you for the clarification. I was already thinking something like that. Due to the code base that is used by Delphi 7 and Delphi 12, many things have not yet been converted to RawUTF8. So we must must be careful in various places. Some of the framework's operations also work with the D7:string without any problems.

Offline

#4 2024-09-16 16:51:02

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,655
Website

Re: DynArray IndexOf - D7 vs D12.1 - Different Results

Just use RawUtf8 everywhere.

And only switch to string when you reach the VCL / UI level.

Online

Board footer

Powered by FluxBB