Martin Schreiber wrote 22 lines
Am 28.02.2012 16:13, schrieb Sven Barth:
> Am 28.02.2012 15:40, schrieb Hans-Peter Diettrich:
>> Sven Barth schrieb:
>>
>>> Before Delphi 2009 you might have been right, but with that version
>>> Embarcadero did a cut and we now need to adapt and live with that.
>>
>> I dare to disagree. A "ByteString" only requires a dedicated encoding
>> (value), which makes it incompatible with other encodings, and thus
>> disables all conversions. Such an encoding doesn't break Delphi
>> compatibility, but allows to use all stringhandling functions with it.
>
> And instead of introducing yet another type or another special encoding
> we could just leverage the features FPC has today and use TBytes.
>
Or even better, use FPC 2.6 AnsiString and UnicodeString and drop the
encoding aware FPC 2.7 AnsiString. ;-)
BTW, please have a look into the different string concatenation
compilerproc, it is probably not so easy to model that with equal or
better performance with TBytes and operator overloading. Is it possible
with operator overloading to combine different types?
Did you look at the 2.7.1 or 2.6.0 implementation of these procs? If the 2.6.0 one then I suggest you to look at the 2.7.1 ones and reconsider your statement ;)
Nevertheless I'll try to do a speed measurement with TBytes and AnsiString both in 2.6 and 2.7.1. If we can improve the performance of FillChar (which would also benefit class creation btw) then this might be a rather performant alternative ;)
"
<TBytes>:= <TBytes> + 'stringconstant' + <TBytes> + <AnsiString>;
"
I quickly adjusted my test program:
=== source begin ===
program bytearraytest;
{$mode objfpc}
type
TBytes = array of Byte;
operator + (aLeft, aRight: TBytes): TBytes;
begin
SetLength(Result, Length(aLeft) + Length(aRight));
if Length(aLeft) > 0 then
Move(aLeft[0], Result[0], Length(aLeft) * SizeOf(Byte));
if Length(aRight) > 0 then
Move(aRight[0], Result[Length(aLeft)], Length(aRight) * SizeOf(Byte));
end;
operator + (aLeft: TBytes; aRight: AnsiString): TBytes;
begin
SetLength(Result, Length(aLeft) + Length(aRight));
if Length(aLeft) > 0 then
Move(aLeft[0], Result[0], Length(aLeft) * SizeOf(Byte));
if Length(aRight) > 0 then
Move(aRight[1], Result[Length(aLeft)], Length(aRight) * SizeOf(Char));
end;
operator + (aLeft: AnsiString; aRight: TBytes): TBytes;
begin
SetLength(Result, Length(aLeft) + Length(aRight));
if Length(aLeft) > 0 then
Move(aLeft[1], Result[0], Length(aLeft) * SizeOf(Char));
if Length(aRight) > 0 then
Move(aRight[0], Result[Length(aLeft)], Length(aRight) * SizeOf(Byte));
end;
var
arr1, arr2, arr3: TBytes;
b: Byte;
i: LongInt;
s: AnsiString;
begin
SetLength(arr1, 5);
for i := 0 to 4 do
arr1[i] := i + 1;
SetLength(arr2, 5);
for i := 0 to 4 do
arr2[i] := i + 6;
arr3 := arr1 + arr2;
for b in arr3 do
Write(b, ' ');
Writeln;
arr3 := Copy(arr2, 2, 2);
for b in arr3 do
Write(b, ' ');
Writeln;
s := 'Hello World';
arr3 := arr1 + 'stringconstant' + arr2 + s;
for b in arr3 do
Write(b, ' ');
Writeln;
end.
=== source end ===
And here is what it prints:
=== source begin ===
PS P:\tests\oneshots> .\bytearraytest.exe
1 2 3 4 5 6 7 8 9 10
8 9
1 2 3 4 5 115 116 114 105 110 103 99 111 110 115 116 97 110 116 6 7 8 9 10 72 101 108 108 111 32 87 111 114 108 100
=== source end ===
Regards,
Sven