Have you ever wondered how utilities like Beyond Compare or DIFF are comparing files? They do it (I guess) by solving the longest common subsequence (LCS) problem.
After reading the Wikipedia article linked above, I obtained an overall view of the problem and I looked at the possible resolutions. So, I decided to implement a Delphi class to do the string comparison trick, which is the base for the text file comparison.
Let me put it as follows: given two strings to be compared, I want to highlight in blue the characters added to the first string and in red the characters removed from it. The common (unchanged) characters will keep the default color.
String 1 = Delphi allows both structural and object oriented programming.
String 2 = Does Delphi allow object oriented programming?
Does Delphi allows both structural and object oriented programming.?
The Delphi class looks like this:
TDiff = record
CharStatus: Char; //Possible values: [+, -, =]
TStringComparer = class
class function Compare(aString1, aString2: string): TList<TDiff>;
When you call TStringComparer.Compare, a generic list of TDiff records is created. A TDiff record contains a character and whether this character was added (CharStatus = ‘+’), removed (CharStatus = ‘-’) or unchanged (CharStatus = ‘=’) in both strings under comparison.
Let’s drop two edits (Edit1, Edit2), a rich edit (RichEdit1) and a button (Button1) on a Delphi form. To highlight the differences put the following code in the OnClick event of the button:
procedure TForm1.Button1Click(Sender: TObject);
//Yes, I know...this method could be refactored ;-)
Differences:= TStringComparer.Compare(Edit1.Text, Edit2.Text);
for Diff in Differences do
if Diff.CharStatus = '+' then
RichEdit1.SelText := Diff.Character;
else if Diff.CharStatus = '-' then
It looks like in the image below:
For the full implementation read further down. Note that various optimizations could be added to the code below, but I didn’t implement them. Anyway, I hope this helps. Feedback is welcome! Feel free to find and correct bugs ;-)