Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

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

0
delphi3
Asked:
delphi3
  • 3
  • 3
1 Solution
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now