Diff.pas
Object pascal class to compare two string arrays (resp. text files) in the
way of the classical unix standard ``diff´´ program.
Version 0.4a - always find the most current version at
http://flocke.vssd.de/prog/code/pascal/pasdiff/
Copyright (C) 2005, 2006 Volker Siebert, Germany
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
---------------------------------------------------------------------------
Delphi versions: 5, 6, 7, 2005 and 2006.
TTextComparer is a class to compare to string arrays passed as TString
objects line by line and return a set of differences.
Sample usage:
+-------------------------------------------------------------------------
| Diff := TTextComparer.Create;
| try
| Diff.Compare(Strings1, Strings2);
| Label1.Caption := Format('There are %d differences', [Diff.Count]);
| // Process Diff.Diff[x].(Left|Right).(Start.Stop)
| finally
| Diff.Free;
| end;
+-------------------------------------------------------------------------
Sample output:
Consider these two text files (line numbers are zero based):
Left file: Right file:
+---+----------+ +---+----------+
| 0 | One | | 0 | One |
| 1 | Two | | 1 | Three |
| 2 | Five | | 2 | Four |
| 3 | Six | | 3 | Five |
| 4 | Eight | | 4 | Seven |
| 5 | Nine | | 5 | Nine |
| 6 | Ten | | 6 | Twelve |
| 7 | Eleven | | 7 | Thirteen |
| 8 | Twelve | | 8 | Fourteen |
| 9 | Fourteen | +---+----------+
+---+----------+
Comparing them will result in the following four differences:
Left Right
No. Start Stop Start Stop Explanation
==== ====== ====== ====== ====== ===================================
0 1 2 1 3 Changed "Two" <> "Three|Four"
1 3 5 4 5 Changed "Six|Eight" <> "Seven"
2 6 8 6 6 Deleted "Ten|Eleven"
3 9 9 7 8 Inserted "Thirteen"
TTextComparer uses a hash on all lines to be able to compare them fast.
If you provide your own compare function (e.g. not case sensitive), then
you also have to provide your own hash function.
The only thing the hash function must ensure is that if two strings match
by your custom compare function
MyOwnCompareFunction(s1, s2) = true
then both strings must have the same hash using your custom hash function
MyOwnHashFunction(s1) = MyOwnHashFunction(s2).
Note: it will also work if you just a provide 0 for every hash value, but
it will slow down the algorithm *VERY* much.
The containted class TStandardTextComparer extends TTextComparer by three
options that control the comparisons.
Use "IgnoreCase" to do what the name says: ignore the letter case when
comparing strings.
The two options "IgnoreSpaces" and "SpacesAsOne" are mutually exclusive,
with "IgnoreSpaces" having precedence over "SpacesAsOne".
"IgnoreSpaces" does what the name says: ignore whitespaces. Thus, the
following three lines are considered equal:
This is a sample application
This is a sample application
Thisisasampleapplication
Especially line 3 is normally not what you want ;)
"SpacesAsOne" is a little bit more sophisticated: it removes leading and
trailing spaces and reduces all blocks of whitespace to a single space
character. This, the first two lines of the example above would be
considered equal but the third not!
Also take a look at the included sample application, a few lines of code
often say more than 1,000 words ;) |