Page 1 of 1

Fastest way to get text from an RichViewEdit

Posted: Wed Oct 09, 2024 7:36 pm
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;

Re: Fastest way to get text from an RichViewEdit

Posted: Thu Oct 10, 2024 12:27 pm
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).