Diff : Algorithm.Diff Namespace
Diff

Computes the differences between two arbitrary lists.

public class Diff : IDiff


Remarks

Use this class to compare two arbitrary lists of data. Normally the lists will be of characters, to perform a character-level comparison, or of strings, to perform a line-by-line comparison, of two strings.

Any object type may be contained in the lists, and comparisons among objects in the lists will be made with the System.Object.Equals method of the objects. If the Equals method is overridden, the the System.Object.GetHashCode method must also be overridden so that any two objects considered equal always have the same hash code.

A user-supplied IComparer and IHashCodeProvider may be supplied to control how equality between elements in the list is determined.

The differences between the lists are computed when the Diff object is constructed. The differences themselves are represented as Algorithm.Diff.Diff+Hunk objects. To access the hunks, use a for-each over the Diff object. Each hunk represents a range of corresponding elements in the two lists.

C# Example
using Algorithm.Diff;

// Performs a line-by-line, case-sensitive, whitespace-ignoring comparison.
diff = new Diff(file1.Split('\n'), file2.Split('\n'), true, false);

// Compares the contents of two ILists, using the objects' own Equals() methods
ArrayList list1 = ...;
ArrayList list2 = ...;
diff = new Diff(list1, list2)

// Compares the contents of two ILists, using custom comparison and hash code generation functions
diff = new Diff(list1, list2, comparer, hashcoder)

foreach (Diff.Hunk hunk in diff) {
	Console.WriteLine(hunk);
}
Members

See Also: Inherited members from object.

Constructors
Compares two lists using a user-supplied comparer and hash code provider.
Performs a line-by-line comparison of two files.
Compares two lists of strings, i.e. performs a line-by-line comparison of two documents.
Properties
Left [read-only]
IList . The list used as the left document.
Right [read-only]
IList . The list used as the right document.
Methods
CreatePatch () : Algorithm.Diff.Patch
Creates a Algorithm.Diff.Patch object for the diff.
Member Details
Diff Constructor
public Diff (IList left, IList right, IComparer comparer, IHashCodeProvider hashcoder)

Compares two lists using a user-supplied comparer and hash code provider.

Parameters
left
The first list of objects. The objects may be of any time, not just characters or strings.
right
The second list of objects. The objects may be of any time, not just characters or strings.To be added.
comparer
Null to use the objects' own System.Object.Equals method to determine equality among objects in the lists, or a IComparer object to use to perform equality tests.
hashcoder
Null to use the objects' own System.Object.GetHashCode method to determine hash codes for the objects in the lists, or a IHashCodeProvider object to use to provide the hash codes.
Remarks

The Diff object will be initialized as the comparison between the two lists, using the comparison and hash code provider objects if supplied.

Both the comparison and hash code provider objects are optional, and if one is provided it is not strictly necessary to provide the other. However, it must be the case that when two objects are considered equal (either by their own Equals method or by the user-given comparer), then they must have the same hash code. It is generally the case that when a comparer object is provided, a hash code provider must be given as well.

Comparer objects are supposed to return -1, 0, or 1 depending on whether the first object is "less" than the second, equal, or greater. The Diff class is not concerned with the ordering of objects, so a comparer used here can return either 1 or -1 when the two objects being compared are not equal.


Diff Constructor
public Diff (string leftFile, string rightFile, bool caseSensitive, bool compareWhitespace)

Performs a line-by-line comparison of two files.

Parameters
leftFile
The path to the left file.
rightFile
The path to the right file.
caseSensitive
Whether to treat changes in case as differences.
compareWhitespace
Whether to treat changes in whitespace as differences.
Remarks
The Diff object is initialized as a comparison between the two documents. Comparisons are made using the current culture settings.

Diff Constructor
public Diff (string[] left, string[] right, bool caseSensitive, bool compareWhitespace)

Compares two lists of strings, i.e. performs a line-by-line comparison of two documents.

Parameters
left
The lines of the first document.
right
The lines of the second document.
caseSensitive
Whether to perform a case-sensitive comparison.
compareWhitespace
Whether to compare changes in whitespace.
Remarks
The Diff object is initialized as a comparison between the two documents. Comparisons are made using the current culture settings.

CreatePatch
public Patch CreatePatch ()

Creates a Algorithm.Diff.Patch object for the diff.

Returns
A patch object for the diff.
Remarks
A Patch can generate more compact representations of diffs.

Left
public IList Left { get; }

The list used as the left document.

Value
The left document.
Remarks
None.

Right
public IList Right { get; }

The list used as the right document.

Value
The right document.
Remarks
None.