?
Solved

Looking to speed up the find replace process.

Posted on 2003-03-09
6
Medium Priority
?
192 Views
Last Modified: 2010-04-04
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
Comment
Question by:delphi3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8100785
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
 
LVL 4

Author Comment

by:delphi3
ID: 8101123
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8101151
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Author Comment

by:delphi3
ID: 8109049
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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 200 total points
ID: 8109141
? 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
 
LVL 4

Author Comment

by:delphi3
ID: 8109251
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question