Hi All,
I'm looking for an algorithm that will detect the difference between two
strings. The strings may be rather long and will probably have been read
from a text file or memo.lines.text.
The sort of 'visual differencing' feature that a good program editor offers,
with or without the GUI is what I'm looking for.
From: "Mark Meyer"
To:
Subject: Re: Difference between strings.
Date sent: Thu, 11 Mar 1999 22:03:28 -0600
Send reply to: Delphi@Kyler.com
peter...
this should do it.. i tested it on a simple file- seemed to work well - this
is only a proof of concept. needs exception handling and probably should use
streams.
i translated this into delphi from a book of algos called, "software tools"
by kernighan & plauger. the isbn is 0-201-03669-x. addison-wesley
publishing company. if you pick up the book it is on page 67. the book may
be out of print now but it is well worth having in your lib. one of those
books like "code complete" and "writing solid code" - worth it's weight in
gold and timeless.
unit Main;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure compfile(filename1, filename2 : string);
function compstr(s1, s2 : string) : boolean;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
filename1 : string;
filename2 : string;
begin
filename1 := 'c:\footer.txt';
filename2 := 'c:\junk.txt';
compfile(filename1, filename2);
end;
procedure tform1.compfile(filename1, filename2 : string);
var
f1 : system.textfile;
f2 : system.textfile;
diff : system.textfile;
buf1 : string;
buf2 : string;
l : integer;
begin
assignfile(f1, filename1);
assignfile(f2, filename2);
assignfile(diff, 'c:\$$_diff.txt');
reset(f1);
reset(f2);
rewrite(diff);
l := 1;
while not eof(f1) do begin
readln(f1, buf1);
readln(f2, buf2);
if not (compstr(buf1, buf2) )then begin
{probably write line number and contents of buf to diff file}
writeln(diff, 'line: '+ inttostr(l) + '-' + buf1);
writeln(diff, 'line: '+ inttostr(l) + '-' + buf2);
writeln(diff, ' ');
end;{if}
inc(l); {increment the line counter}
end;{while}
closefile(f1);
closefile(f2);
closefile(diff);
end;
function tform1.compstr(s1, s2 : string) : boolean;
var
i : integer;
btemp : boolean;
begin
btemp := true;
if (length(s1) <> length(s2)) then begin
btemp := false;
end{if}
else begin
for i:= 1 to length(s1) do begin
if (s1[i] <> s2[i]) then begin
btemp := false;
exit;
end;{if}
end;{for}
end;{else}
result := btemp;
end;
end.