Looking to speed up the find replace process.

Hi All,

While this is a demo only, I am looking to speed up the process of finding, replacing the text in a very large file (this Memo duplicated  at least 100+  times.)

At the beginning of the process with the very large file placed in the memo1,it moves at a snails pace. Can you help with any improvements?

TIA
Delphi3

unit UsingSearchReplaceUnit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    ReplaceDialog1: TReplaceDialog;
    Memo1: TMemo;
    Process: TButton;
    Close: TButton;
    procedure ProcessClick(Sender: TObject);
    procedure CloseClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ProcessClick(Sender: TObject);
var
  SelPos: Integer;
  FindText, ReplaceText: string;
begin
  FindText := '';
  ReplaceText := '';
  FindText := 'Judy';
  ReplaceText := 'Jennifer';
  { Perform a global case-sensitive search for FindText in Memo1 }
  SelPos := Pos(FindText, Memo1.Lines.Text);
  while SelPos > 0 do
  begin
    Memo1.SelStart := SelPos - 1;
    Memo1.SelLength := Length(FindText);
      { Replace selected text with ReplaceText }
    Memo1.SelText := ReplaceText;
    SelPos := Pos(FindText, Memo1.Lines.Text);
  end;
    //else MessageDlg(Concat('Could not find "', FindText, '" in Memo1.'), mtError, [mbOk], 0);
end;


procedure TForm1.CloseClick(Sender: TObject);
begin
  Application.Terminate;
end;

end.

.dfm as text
object Form1: TForm1
  Left = 195
  Top = 173
  Width = 493
  Height = 391
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 120
  TextHeight = 16
  object Memo1: TMemo
    Left = 24
    Top = 24
    Width = 433
    Height = 297
    Lines.Strings = (
      'Hi Chris,'
      'Sorry I missed you and Judy.'
      'Are you still working in the evenings?'
      'If you are, maybe I could come over and '
      'visit with Judy one evening next week.'
      'Monday, Wednesday, or Thursday '
      'evening next week would work for me.'
      'Please let me know.'
      ''
      'Bob'
      ''
      'Hi Chris,'
      'Sorry I missed you and Judy.'
      'Are you still working in the evenings?'
      'If you are, maybe I could come over and '
      'visit with Judy one evening next week.'
      'Monday, Wednesday, or Thursday '
      'evening next week would work for me.'
      'Please let me know.'
      ''
      'Bob'
      ''
      'Hi Chris,'
      'Sorry I missed you and Judy.'
      'Are you still working in the evenings?'
      'If you are, maybe I could come over and '
      'visit with Judy one evening next week.'
      'Monday, Wednesday, or Thursday '
      'evening next week would work for me.'
      'Please let me know.'
      ''
      'Bob'
      ''
      'Hi Chris,'
      'Sorry I missed you and Judy.'
      'Are you still working in the evenings?'
      'If you are, maybe I could come over and '
      'visit with Judy one evening next week.'
      'Monday, Wednesday, or Thursday '
      'evening next week would work for me.'
      'Please let me know.'
      ''
      'Bob'
      ''
      'Hi Chris,'
      'Sorry I missed you and Judy.'
      'Are you still working in the evenings?'
      'If you are, maybe I could come over and '
      'visit with Judy one evening next week.'
      'Monday, Wednesday, or Thursday '
      'evening next week would work for me.'
      'Please let me know.'
      ''
      'Bob'
      ''
      'Hi Chris,'
      'Sorry I missed you and Judy.'
      'Are you still working in the evenings?'
      'If you are, maybe I could come over and '
      'visit with Judy one evening next week.'
      'Monday, Wednesday, or Thursday '
      'evening next week would work for me.'
      'Please let me know.'
      ''
      'Bob'
      ''
      ''
      '')
    TabOrder = 0
  end
  object Process: TButton
    Left = 256
    Top = 328
    Width = 75
    Height = 25
    Caption = 'Process'
    TabOrder = 1
    OnClick = ProcessClick
  end
  object Close: TButton
    Left = 360
    Top = 328
    Width = 75
    Height = 25
    Caption = 'Close'
    TabOrder = 2
    OnClick = CloseClick
  end
  object ReplaceDialog1: TReplaceDialog
    FindText = 'Judy'
    Options = [frDown, frFindNext, frMatchCase, frReplace, frReplaceAll, frWholeWord]
    ReplaceText = 'Mary'
    Left = 16
    Top = 328
  end
end

LVL 4
delphi3Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
take a look to the stringreplace-function

from delphi helpfile

Returns a string with occurrences of one substring replaced by another substring.

Unit

Sysutils

Category

string handling routines

type
  TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);
function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;

Description

StringReplace replaces occurrences of the substring specified by OldPattern with the substring specified by NewPattern. StringReplace assumes that the source string, specified by S, may contain Multibyte characters.

If the Flags parameter does not include rfReplaceAll, StringReplace only replaces the first occurrence of OldPattern in S. Otherwise, all instances of OldPattern are replaced by NewPattern.

If the Flags parameter includes rfIgnoreCase, The comparison operation is case insensitive.

meikl ;-)
0
delphi3Author Commented:
meikl,
I tried several different arrangements of the statements.

type
  TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);
........

function TForm1.StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;
 begin
 end;

procedure TForm1.ProcessClick(Sender: TObject);
var
  S, FindText, ReplaceText: string;
begin
  FindText := '';
  ReplaceText := '';
  FindText := 'Judy';
  ReplaceText := 'Jennifer';
  S := Memo1.Text;
  StringReplace(S, FindText, ReplaceText,frReplaceAll);
end;


I am out of ideas how to handle this. Can you 'doctor up' what I have or make a further suggestion.

Grr, I am frustrated.

Thanks for you help so far.

Delphi3
0
kretzschmarCommented:
a sample

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if opendialog1.Execute then
    memo1.lines.LoadFromFile(opendialog1.FileName);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  //its a function, which returns the new text
  memo1.Text := stringReplace(memo1.Text,edit1.Text,edit2.Text,[rfReplaceAll]);
end;

end.

meikl ;-)
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

delphi3Author Commented:
meikl,

My net search found:

I can't believe the speed with which this FastStrings.pas operates (assembly code).
What took 300 seconds with StringReplace above takes just near one second.

source:

http://www.delphidevelopernewsletter.com/dd/DDMag.nsf/W
ebIndexByIssue/75EDEE6470ADB1BF852568F2005E2269?
opendocument

in the article: Download MORRIS.ZIP

Delphi3
0
kretzschmarCommented:
? what is the question

the difference is that stringreplace works
also with unicode-strings, this may cause
an overhead, but is more general

you may also take a look to

http://help.madshi.net/madStringsUnit.htm

meikl ;-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
delphi3Author Commented:
meikl,
Thanks for your response.

My Q for this string replace originated in another post
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20462136.html
where ginsonic handles his html codes to DBGrid a certain way. He contended that it took about 10 seconds to translate the html page into db items.


With this latest find asm code, I can translate the page near 1 seconds and the methods of presenting it using IE page.

No matter,

I give you the points and close the Question.

Cheers,

Delphi3  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.