Fastest way to get text from an RichViewEdit

General TRichView support forum. Please post your questions here
Post Reply
jgkoehn
Posts: 303
Joined: Thu Feb 20, 2020 9:32 pm

Fastest way to get text from an RichViewEdit

Post by jgkoehn »

I am trying to determine which function would be faster.
Any tips?

Code: Select all

function TFmBibleSearch.GetInputText: UnicodeString;
begin
  Result := RVGetTextRange(rvInput, 0, RVGetTextLength(rvInput));

  //remove new  lines
  Result :=GlobalUtils.StringReplaceU(Result, #13, '');
  Result :=GlobalUtils.StringReplaceU(Result, #10, '');

  //remove zero width no-space chars
    Result :=GlobalUtils.StringReplaceU(Result, ZWNBSP, '');
end;

Code: Select all

function TFmBibleSearch.GetInputText: UnicodeString;
var
  ms: TMemoryStream;
begin
  ms := TMemoryStream.Create;
  rvInput.SaveTextToStreamW('', ms, 0, False, true);
  ms.Position := 0;
  if ms.Size >0 then begin
    SetLength(Result, ms.Size div SizeOf(Char));
    ms.ReadBuffer(Result[1], ms.Size);
  end
  else
    Result := '';
  ms.Free;

  //remove new  lines
  Result :=GlobalUtils.StringReplaceU(Result, #13, '');
  Result :=GlobalUtils.StringReplaceU(Result, #10, '');

  //remove zero width no-space chars
    Result :=GlobalUtils.StringReplaceU(Result, ZWNBSP, '');
end;
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Fastest way to get text from an RichViewEdit

Post by Sergey Tkachenko »

The main difference is not in speed.
RVGetTextRange and SaveTextToStreamW produce different text.
SaveTextToStreamW produces text for saving to text files. Non-text items are saved as their text representation.
RVGetTextRange produces text that has 1:1 correspondence to the document (knowing the position in text, you can get the position in the document; and vice versa). Non-text items are not saved (except for tabs and tables).
Post Reply