OneDiff is a free, open-source .NET text diff and document comparison library from Recursive AI, licensed under MIT or Apache-2.0 (NuGet · GitHub).
dotnet add package RecursiveAI.OneDiff --version 1.0.1Diff Match Patch (DMP) is Google’s character-level diff library, ported to many languages.
In a .NET 10 console project, replace Program.cs and run dotnet run.
using OneDiff; const string before = "Status\nColor: red.\nDone\n"; const string after = "Status\nColor: blue.\nDone\n"; var unified = Diff.Unified(before, after, "a/status.txt", "b/status.txt"); var dmp = Diff.PatchText(before, after); Console.WriteLine("Unified:"); Console.Write(unified); Console.WriteLine("Diff Match Patch:"); Console.Write(dmp); Console.WriteLine($"Unified round trip: {Diff.ApplyUnified(before, unified).Text == after}"); Console.WriteLine($"DMP round trip: {Diff.ApplyPatchText(before, dmp).Text == after}");
--- a/status.txt +++ b/status.txt @@ -1,3 +1,3 @@ Status -Color: red. +Color: blue. Done
@@ -11,11 +11,12 @@ or: -red +blue .%0ADo
In this example, offsets count characters, and %0A is an encoded newline.
Unified round trip: True DMP round trip: True
How to read the two patch formats
In the GNU unified format, --- and +++ name the old and new files. The hunk header @@ -1,3 +1,3 @@ gives the starting line and line count on each side. A space marks a context line; - and + mark the whole line being removed or added. The unchanged lines help identify where the change belongs.
In DMP patch text, there are no file headers. @@ -11,11 +11,12 @@ describes text offsets and lengths, not lines. Only red and blue are marked as edits. Apply DMP patches in order: later offsets refer to the text after earlier patches have been applied.
Choose by what receives the patch
Send changes for review or apply them with Git
Unified diff. File names and line context identify the edit. Git apply reads patch files and can check whether they apply without changing the file.
Exchange edits to a long paragraph or single-line value
DMP. It records character edits with encoded text context, so changing one word need not replace the full line. Include the document identifier separately.
Apply to text that may have drifted
Either, with result checks. OneDiff’s unified apply searches for matching lines and can drop outer context lines. Its DMP apply can match near the expected offset even when the text differs.
The unified output above applied to status.txt with Git 2.53.0. The DMP output applied to the original string with Python’s diff-match-patch 20241021. That sample is ASCII. For text with emoji, agree on offset units: OneDiff uses UTF-16 code units by default; set DmpOffsetUnit.CodePoint on the patcher when exchanging with the Python port, which counts Unicode code points. See the pinned offset and encoding rules.
If the patch must apply only to the exact base, check a version ID or content hash first. If you allow drift, check every hunk or patch outcome before committing the result. A match near the expected position is not proof that the edit landed in the intended paragraph, especially when text repeats.
See the OneDiff getting-started guide and the patch format and apply reference.