public class Diff extends java.lang.Object
change
objects which form an
edit script. The objects compared are traditionally lines
of text from two files. Comparison options such as "ignore
whitespace" are implemented by modifying the equals
and hashcode
methods for the objects compared.
The basic algorithm is described in:
"An O(ND) Difference Algorithm and its Variations", Eugene Myers,
Algorithmica Vol. 1 No. 2, 1986, p 251.
This class outputs different results from GNU diff 1.15 on some inputs. Our results are actually better (smaller change list, smaller total size of changes), but it would be nice to know why. Perhaps there is a memory overwrite bug in GNU diff 1.15.
Modifier and Type | Class and Description |
---|---|
static class |
Diff.Change
The result of comparison is an "edit script": a chain of change objects.
|
(package private) class |
Diff.FileData
Data on one input file being compared.
|
(package private) static class |
Diff.ForwardScript |
(package private) static class |
Diff.ReverseScript
Scan the tables of which lines are inserted and deleted, producing an edit script in reverse order.
|
static interface |
Diff.ScriptBuilder
Script builder.
|
Modifier and Type | Field and Description |
---|---|
private int[] |
bdiag |
private int |
bdiagoff |
private int |
cost |
private int |
equivMax
1 more than the maximum equivalence value used for this or its
sibling file.
|
private int[] |
fdiag |
private int |
fdiagoff |
private Diff.FileData[] |
filevec |
static Diff.ScriptBuilder |
forwardScript
Standard Forward ScriptBuilder.
|
static Diff.ScriptBuilder |
reverseScript
Standard Reverse ScriptBuilder.
|
private int[] |
xvec |
private int[] |
yvec |
Constructor and Description |
---|
Diff(java.lang.Object[] a,
java.lang.Object[] b)
Prepare to find differences between two arrays.
|
Modifier and Type | Method and Description |
---|---|
private void |
compareseq(int xoff,
int xlim,
int yoff,
int ylim)
Compare in detail contiguous subsequences of the two files
which are known, as a whole, to match each other.
|
private int |
diag(int xoff,
int xlim,
int yoff,
int ylim)
Find the midpoint of the shortest edit script for a specified
portion of the two files.
|
Diff.Change |
diff(Diff.ScriptBuilder bld)
Get the results of comparison as an edit script.
|
Diff.Change |
diff2(boolean reverse)
Report the differences of two files.
|
private void |
discardConfusingLines()
Discard lines from one file that have no matches in the other file.
|
private void |
shiftBoundaries()
Adjust inserts/deletes of blank lines to join changes as much as possible.
|
private int equivMax
private int[] xvec
private int[] yvec
private int[] fdiag
private int[] bdiag
private int fdiagoff
private int bdiagoff
private final Diff.FileData[] filevec
private int cost
public static final Diff.ScriptBuilder forwardScript
public static final Diff.ScriptBuilder reverseScript
public Diff(java.lang.Object[] a, java.lang.Object[] b)
equals
. The original Object arrays
are no longer needed for computing the differences. They will
be needed again later to print the results of the comparison as
an edit script, if desired.a
- first arrayb
- second arrayprivate int diag(int xoff, int xlim, int yoff, int ylim)
xoff
- xoffxlim
- xlimyoff
- yoffylim
- ylimprivate void compareseq(int xoff, int xlim, int yoff, int ylim)
xoff
- xoffxlim
- xlimyoff
- yoffylim
- ylimprivate void discardConfusingLines()
private void shiftBoundaries()
public final Diff.Change diff2(boolean reverse)
reverse
- if true
use reverseScript
else use forwardScript
public Diff.Change diff(Diff.ScriptBuilder bld)
bld
- an object to build the script from change flags