I just read this in TEncoding.GetBufferEncoding:

I just read this in TEncoding.GetBufferEncoding:

function ContainsPreamble(const Buffer, Signature: array of Byte): Boolean;
var
I: Integer;
begin
Result := True;
if Length(Buffer) >= Length(Signature) then
begin
for I := 1 to Length(Signature) do
if Buffer[I - 1] <> Signature [I - 1] then
begin
Result := False;
Break;
end;
end
else
Result := False;
end;

Honestly, looping over members of an open array using a 1 based loop, and then accessing the members with I - 1. Who writes this stuff? And what's wrong with a call to CompareMem?

Comments

  1. Andreas Hausladen He's referring to one of the variants Jeroen Wiert Pluimers​ posted.

    ReplyDelete
  2. Asbjørn Heid And there is no call to "High". "Length(A) - 1" can actually lead to a faster for-loop than a "High(A)" because the compiler sees the "-1" and can apply an optimization.

    ReplyDelete

Post a Comment