[fpc-pascal] Best way to insert bytes into a TBytes variable?

Bo Berglund bo.berglund at gmail.com
Tue Jul 25 10:54:30 CEST 2017


I am working on moving away from using AnsiString as communications
buffer in an old application, so I need to write efficient
replacements for certain string functions (Delete, Insert, Copy etc).

Now I am wondering if there is a better way to do the Insert() command
than this:

procedure InsertBytes(var Dest: TBytes; Src: TBytes; Index: integer);
var
  Len, LenA: integer;
begin
  Len := Length(Dest);
  LenA := Length(Src);
  if LenA = 0 then exit; //Do nothing
  if Index > Len then Index := Len;  //Make an append instead
  SetLength(Dest, Len + LenA);
  Move(Dest[Index], Dest[Index + LenA], LenA);  //Create space
  Move(Src[0], Dest[Index], LenA);  //Insert data
end;

Ideally the function should be portable between FPC and Delphi XE5...


-- 
Bo Berglund
Developer in Sweden




More information about the fpc-pascal mailing list