InsertText fastest way?

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

InsertText fastest way?

Post by jgkoehn »

Greetings,
Is this GRichViewEdit.TopLevelEditor.InsertText(LFText, False);
The fastest way to insert text into an existing document that is some 2,000,000 or more characters long?
The RichViewEdit does not need to update until the end or is it faster to work with the RVData somehow?
I am working on speeding up some working code. Thank you
Sergey Tkachenko
Site Admin
Posts: 17555
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: InsertText fastest way?

Post by Sergey Tkachenko »

InsertText is fast if you call it once or few times.
It reformats only changed paragraphs.

However, if you need to insert many text strings one by one, probably it would be faster to used non-editing methods (that do not reformat document), and at the end format the whole document once.
jgkoehn
Posts: 303
Joined: Thu Feb 20, 2020 9:32 pm

Re: InsertText fastest way?

Post by jgkoehn »

I call it many times like 70,000 etc. depends on the situation.
Can you point me in the direction for ones that don't reformat until the end?
jgkoehn
Posts: 303
Joined: Thu Feb 20, 2020 9:32 pm

Re: InsertText fastest way?

Post by jgkoehn »

I see there is Add, AddNL but I need it to add right where the caret position is at or in a linear position in the text. Ideas?
jgkoehn
Posts: 303
Joined: Thu Feb 20, 2020 9:32 pm

Re: InsertText fastest way?

Post by jgkoehn »

Here is an idea of the code I am using.
It is a code that is based in Regex you helped me with. With some modifications. The modifications being that at every regex match it stops gets the position in the string of characters. Now matches the alignment in the RichViewEdit you helped me with that.
Then inserts text.

Code: Select all

//Goes through replacing all
function TReCB.SelectReplaceAllNF(const M: TMatch):String;
var
  StartIndex, EndIndex, ItemNo1, Offs1, ItemNo2, Offs2: Integer;
  RVData1, RVData2: TCustomRVData;
  FindFontBool: Boolean;
  LFText: String;
begin
     //Stops searching unless it gets set to True later
     StartIndex := M.Index-1;
     EndIndex := StartIndex + M.Length;
     //Also update with GRegDiffPoss here to keep alignment correct with the Replaced LFText further down. Starts as 0;
     StartIndex := StartIndex + GRegDiffPos;
     EndIndex := EndIndex + GRegDiffPos;
     if LinearToRichView(GRichViewEdit, GRichViewEdit.RVData, StartIndex, RVData1, ItemNo1, Offs1) and
       LinearToRichView(GRichViewEdit, GRichViewEdit.RVData, EndIndex, RVData2, ItemNo2, Offs2) and
       (RVData1 = RVData2) then
     begin
      RVData1 := RVData1.Edit;
      //Caret after the find
      TCustomRVFormattedData(RVData1).SetSelectionBounds(ItemNo1, Offs1, ItemNo2, Offs2); //The caret is afterwards so the next search is possible.
      //Swap out text here. (This is a regex to update the matched string portion to a replaced value.)
      LFText := ReplaceRe(M.Value, GRegFindStr, GRegReplStr, GRegexOptions);

      //Now insert the new text with format if desired.
      GRichViewEdit.TopLevelEditor.InsertText(LFText, False); //False so the caret is afterwards so the next regex match is possible.
      Inc(GReplaceItemInt);

      //Compare the difference between LFText and M.Length
      //                            20 Replace      - 10 Find
      //                            10 Replace      - 20 Find
      //Alginment happens here so that the strings are kept in parallel alignment between the RegexString and the Document.
      GRegDiffPos := GRegDiffPos + (M.Length + (Length(LFText)-M.Length))-2;
      GSearchBoolFound := True;
    end else
      begin
      Application.MessageBox('The matched fragment cannot be selected OR End of Document', 'OK', MB_OK);
    end;
    Result := 'ok';
end;
Sergey Tkachenko
Site Admin
Posts: 17555
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: InsertText fastest way?

Post by Sergey Tkachenko »

A way of doing ot without using editing methods is difficult.

Basically, you can use GetItemText, modify text, and set it back using SetItemText. However, if the found substring belongs to several items, it becomes complicated (you need to modify text of the first and the last item, and delete items in the middle using DeleteItems).
If the text to insert contains multiple lines, you need to insert middle lines using RVInsertString procedure from RVInsertItems.pas (because text item cannot contain line break and tab characters)

It's quite difficult and I am not sure that it is worth efforts. May be only if there are hundreds of replacements.
jgkoehn
Posts: 303
Joined: Thu Feb 20, 2020 9:32 pm

Re: InsertText fastest way?

Post by jgkoehn »

Thank you Sergey for giving me some pointers. I see what you mean it is quite complex. It does give me some things to think about.
Post Reply