Board index » cppbuilder » Several "search & replace" actions in TRichEdit at once - performance problem...?

Several "search & replace" actions in TRichEdit at once - performance problem...?


2003-08-01 05:24:16 AM
cppbuilder47
hi dear builders,
I'm using several "search & replace" actions in a TRichEdit at once with
the following code..
My program is not responding when I use 25 of these actions on a text
capacity of about 2 mbyte...
well after 20 minutes its returns back to life :-)
So, my question now...is there a better way doing several actions at once ?
Threads maybe ??
If yes, how can I handle it with a Therad ??
Hope someone have a little hint on this..
Oren
/*************************************************************/
void __fastcall TfrmStringReplacement::cmdStartClick(TObject *Sender)
{
//...
AnsiString SearchString = txtSearch->Text;
AnsiString ReplaceString = txtReplace->Text;
RichEdit1->Lines->Text = StringReplace(RichEdit1->Lines->Text,
SearchString, ReplaceString, TReplaceFlags() << rfReplaceAll);
//....
}
/*************************************************************/
 
 

Re:Several "search & replace" actions in TRichEdit at once - performance problem...?

"Oren (Halvani.de)" < XXXX@XXXXX.COM >wrote in message
Quote
RichEdit1->Lines->Text = StringReplace(RichEdit1->Lines->Text,
SearchString, ReplaceString, TReplaceFlags() << rfReplaceAll);
That is not very efficient. It has to allocate a temporary AnsiString to
contain the entire contents of the RichEdit, then alter the temporary
(potentially creating another temporary internally), and then replaces the
entire contents of the RichEdit as a whole.
I would suggest you look at TRichEdit's FindText() method. Using that in
conjunction with the SelStart, SelLength, and SelText properties, you can
update the RichEdit text directly without all of the unnecessary overhead of
StringReplace().
For example (untested):
void __fastcall TfrmStringReplacement::cmdStartClick(TObject *Sender)
{
//...
AnsiString SearchString = txtSearch->Text;
int iSearchLength = SearchString.Length();
AnsiString ReplaceString = txtReplace->Text;
int iReplaceLength = ReplaceString.Length();
int iRichEditLength = RichEdit1->GetTextLen();
TSearchTypes options; // fill in as desired
int pos = RichEdit1->FindText(SearchString, 0, iRichEditLength,
options);
if( pos>-1 )
{
RichEdit1->Lines->BeginUpdate();
try
{
do
{
RichEdit1->SelStart = pos;
RichEdit1->SelLength = iSearchLength;
RichEdit1->SelText = ReplaceString;
pos = RichEdit1->FindText(SearchString,
pos+iReplaceLength, iRichEditLength-(pos+iReplaceLength), options);
}
while( pos>-1 );
}
__finally {
RichEdit1->Lines->EndUpdate();
}
}
//....
}
Gambit