Robust, easy-to-use diff and patch library for comparing and synchronising text with zero dependencies
Different provides extension methods for creating and applying diffs and patches for plain text.
It uses the same robust algorithms and logic as commonly used in git, wrapped in a readable, easy-to-use library.
Get the differences between two texts
string from = "Hello!";
string to = "Hallo?";
List<Diff> diffs = from.Diff(to);
Result
{
{Operation.EQUAL, "H"},
{Operation.DELETE, "e"},
{Operation.INSERT, "a"},
{Operation.EQUAL, "llo"},
{Operation.DELETE, "!"},
{Operation.INSERT, "?"}
}
Details: diff-match-patch#diff_main
Get the differences between two texts, optimised for human-readability
Two unrelated texts can contain many coincidental matches. This can make the diff hard to understand for humans, despite being the optimal result. A semantic diff rewrites the diff to be more legible and easier to understand.
string from = "Computer";
string to = "Mouse";
List<Diff> diffs = from.SemanticDiff(to);
Result
{
{Operation.DELETE, "Computer"},
{Operation.INSERT, "Mouse"},
}
Details: diff-match-patch#diff_cleanupSemantic
Get the differences between two texts, optimised for machine processing
Applying a large number of small diffs can take drastically longer to process in some scenarios than a small number of larger diffs. An efficient diff rewrites the diff to eliminate and combine tiny diffs into larger ones based on an edit cost heuristic. The result is often similar to a semantic diff, but not always.
string from = "Computer";
string to = "Mouse";
List<Diff> diffs = from.EfficientDiff(to);
Result
{
{Operation.DELETE, "Computer"},
{Operation.INSERT, "Mouse"},
}
Details: diff-match-patch#diff_cleanupEfficiency
Measure the Levenshtein distance of a diff in terms of changed characters
The minimum value is 0, meaning the strings are equal. The maximum value is the length of the longer string.
string from = "Hello!";
string to = "Hallo?";
List<Diff> diffs = from.Diff(to);
diffs.Levenshtein();
Result
2 // e -> a, ! -> ?
Details: diff-match-patch#diff_levenshtein
Get an HTML representation of a diff
This can be used to visualise diffs in a human-readable way.
string from = "Hello!";
string to = "Hallo?";
List<Diff> diffs = from.Diff(to);
diffs.Html();
Result
<span>H</span>
<del style="background:#ffe6e6;">e</del> <!-- Red -->
<ins style="background:#e6ffe6;">a</ins> <!-- Green -->
<span>llo</span>
<del style="background:#ffe6e6;">!</del> <!-- Red -->
<ins style="background:#e6ffe6;">?</ins> <!-- Green -->
Details: diff-match-patch#diff_prettyHtml
Get a list of patches given two texts, a text and a diff, or only a diff
Patches are a list of operations to transform one text to another. They provide a way to "apply" diffs.
string from = "Hello!\nWho's there?";
string to = "Hallo?\nWho's there?";
// Given two strings
List<Patch> patches = from.Patch(to);
// Given a string and diffs
List<Diff> diffs = from.Diff(to);
List<Patch> patches = from.Patch(diffs);
// Given diffs
List<Diff> diffs = from.Diff(to);
List<Patch> patches = diffs.Patch();
Result (serialised to JSON)
[
{
"diffs": [
{
"operation": 2,
"text": "H"
},
{
"operation": 0,
"text": "ello!"
},
{
"operation": 1,
"text": "allo?"
},
{
"operation": 2,
"text": "\nWho"
}
],
"start1": 0,
"start2": 0,
"length1": 10,
"length2": 10
}
]
Details: diff-match-patch#patch_make
Apply a list of patches to a string
If the patches cannot be applied, returns null
string from = "Hello!\nWho's there?";
string to = "Hallo?\nWho's there?";
var patch = from.Patch(to);
// Using extension method on string
string? result = from.ApplyPatches(patch);
// Using extension method on List<Patch>
string? result = patch.Apply(from);
Result
"Hallo?\nWho's there?"
Details: diff-match-patch#patch_apply
Get the text representation of a list of patch operations
The format is very similar to the standard GNU diff/patch format. This text can be stored and later converted back to patch objects (see "Text to patch" below).
string from = "Hello!\nWho's there?";
string to = "Hallo?\nWho's there?";
List<Patch> patches = from.Patch(to);
patches.Text();
Result
@@ -1,10 +1,10 @@
H
-ello!
+allo?
%0aWho
Details: diff-match-patch#patch_toText
Transform a text representation of patches to a list of patch operations
Parses and returns a list of patch objects from their textual representation (see "Patch to text" above).
var patchString = @"@@ -1,10 +1,10 @@
H
-ello!
+allo?
%0aWho
";
// Throws ArgumentException on failure
List<Patch> patches = patchString.ParsePatches();
// Safe, returns true or false depending on success
bool success = patchString.TryParsePatches(out List<Patch>? patches);
Result (serialised to JSON)
[
{
"diffs": [
{
"operation": 2,
"text": "H"
},
{
"operation": 0,
"text": "ello!"
},
{
"operation": 1,
"text": "allo?"
},
{
"operation": 2,
"text": "\nWho"
}
],
"start1": 0,
"start2": 0,
"length1": 10,
"length2": 10
}
]
Details: diff-match-patch#patch_fromText
Licensed under Apache 2.0.
Copyright ยฉ 2021 Cloudey IT Ltd
Cloudeyยฎ is a registered trademark of Cloudey IT Ltd. Use of the trademark is NOT GRANTED under the license of this repository or software package.
Copyright 2018 The diff-match-patch Authors.
https://github.com/google/diff-match-patch
Modifications were made to the diff-match-patch library (licensed under Apache 2.0) by the authors of this package (Cloudey)