RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
System.MoveChars Function

Copies bytes from a source to a destination.

Pascal
procedure MoveChars(const Source; var Dest; Length: Integer);
C++
MoveChars(const  Source,  Dest, int Length);

MoveChars copies Length bytes from Source to Dest. MoveChars compensates for overlaps between the source and destination blocks.

Note: MoveChars assumes that the size of Char is 2 Bytes (when Unicode is enabled). Use the Move function to copy characters from a string that has character size different from SizeOf(Char).
 

C++ Examples: 

 

/*
The following code demostrates the use of memory management functions in C++. Three
edit boxes and a button are expected onthe form.
*/
String __fastcall FastStrCat(String s1, String s2)
{
  /*
   Allocated enough space in FinalStr to copy the contents
   of the initial string + $00 character
  */
  wchar_t* finalStr = (wchar_t *)GetMemory((s1.Length() + 1) * sizeof(wchar_t));

  // Copy the contents of the first string
  MoveChars(s1.data(), finalStr, s1.Length() + 1);

  // And now expand the final string + $00 character
  finalStr = (wchar_t *)ReallocMemory(finalStr, (s1.Length() + s2.Length() + 1) * sizeof(wchar_t));

  // Copy the contents of the second string
  MoveChars(s2.data(), finalStr + s1.Length(), s2.Length() + 1);

  // Get the result in String
  String result = String(finalStr);
  FreeMemory(finalStr);

  return result;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  // Concatenate 2 string
  Edit3->Text = FastStrCat(Edit1->Text, Edit2->Text);
}

 

Delphi Examples: 

{
The following code demostrates the use of ReallocMem function. Three
edit boxes and a button are expected onthe form.
}
function FastStrCat(const S1, S2: String): String;
var
  FinalStr: PChar;
begin
  {
   Allocated enough space in FinalStr to copy the contents
   of the initial string + $00 character
  }
  GetMem(FinalStr, (Length(S1) + 1) * SizeOf(Char));

  { Copy the contents of the first string }
  MoveChars(S1[1], FinalStr^, Length(S1) + 1);

  { And now expand the final string + $00 character }
  ReallocMem(FinalStr, (Length(S1) + Length(S2) + 1) * SizeOf(Char));

  { Copy the contents of the second string }
  MoveChars(S2[1], FinalStr[Length(S1)], Length(S2) + 1);

  { Get the result in String }
  SetString(Result, FinalStr, Length(S1) + Length(S2));
  FreeMem(FinalStr);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  { Concatenate 2 string }
  Edit3.Text := FastStrCat(Edit1.Text, Edit2.Text);
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!