3
votes

I have a TMemo which contains quite a lot of texts, 80M (about 400K lines).

The TMemo is set with WordWrap = FALSE, there is no need to find texts that wrapped in 2 lines.

I need a fast way to find a text, from the beginning, and also find next.

So, I put a TEdit for putting the text to find and a TButton to find the text in the TMemo.

I was thinking to use Pos(), checking line by line, but that will be slow. And I don't know how to determine the TMemo.Lines[index] for current cursor position.

Anyone can come up with solution?

Thanks

UPDATE:

I found a solution from here: Search thru a memo in Delphi?

The SearchText() function works, fast, and very fast. Took couple of seconds to search unique string at the bottom end.

1
If you used a rich edit control you could use EM_FINDTEXT which is wrapped in in the FindText method of TRichEdit.David Heffernan
I think using Pos function with TMemo.Lines.Text property should be faster; though it can possibly find wrapped substring too, I don't think that is a problem.kludg
Similar question on SO : search-thru-a-memo-in-delphi. I don't know if it's fast, but the answer has a solution for the index position.LU RD
@Serg: You are right. I found a function from here: stackoverflow.com/questions/4232709/…Kawaii-Hachii
Tested the above SearchText() and it was fast, very fast!Kawaii-Hachii

1 Answers

7
votes

A little addition to the previous answers: you can get the line number without selecting the pattern found, like this:

procedure TForm1.Button3Click(Sender: TObject);
var
  I, L: Integer;

begin
  Memo1.WordWrap:= False;
  Memo1.Lines.LoadFromFile('Windows.pas');
  I:= Pos('finalization', Memo1.Text);
  if I > 0 then begin
    L := SendMessage(Memo1.Handle, EM_LINEFROMCHAR, I - 1, 0);
    ShowMessage('Found at line ' + IntToStr(L));
// if you need to select the text found:
    Memo1.SelStart := I - 1;
    Memo1.SelLength := Length('finalization');
    Memo1.SetFocus;
  end;
end;

Note that line number is zero-based, also you should subtract 1 from Pos result to obtain zero-based offset for SendMessage and TMemo.SelStart.