Recursive AI

Apply diff-match-patch text in C#

OneDiff is a free, open-source diff library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Apply a text patch that records inserted and deleted characters plus their surrounding context.

Terminal
dotnet add package RecursiveAI.OneDiff

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneDiff;
 
var patch = "@@ -1,3 +1,3 @@\n c\n-a\n+u\n t\n";
var result = Diff.ApplyPatchText("cat", patch);
Console.WriteLine(result.Text);
foreach (var part in result.Patches)
    Console.WriteLine(part.Outcome);
Text
cut
Applied

Unzip the example, then dotnet run.

The full program

The patch text is embedded in the program; no file is needed. Unplaceable patches return outcomes; malformed patch text throws PatchFormatException.

C#
using OneDiff;
 
var patch = "@@ -1,3 +1,3 @@\n c\n-a\n+u\n t\n";
var result = Diff.ApplyPatchText("cat", patch);
Console.WriteLine(result.Text);
foreach (var part in result.Patches)
    Console.WriteLine(part.Outcome);
var strict = new OneDiff.Patching.DmpPatcher
{
    MatchThreshold = 0
};
var rejected = Diff.ApplyPatchText("zzz", patch, strict);
Console.WriteLine($"Unmatched input: {rejected.Text}");
foreach (var part in rejected.Patches)
    Console.WriteLine(part.Outcome);

dotnet run prints:

Text
cut
Applied
Unmatched input: zzz
NotFound

Variations

Create the patch text before applying it. This is a complete replacement program.

C#
using OneDiff;
 
var patch = Diff.PatchText("cat", "cut");
Console.Write(patch);
Console.WriteLine(
    Diff.ApplyPatchText("cat", patch).Text);
Text
@@ -1,3 +1,3 @@
 c
-a
+u
 t
cut

The short answer

Pass the source text and the patch string (diff-match-patch’s text format) to Diff.ApplyPatchText. Read Text for the resulting string and inspect every entry in Patches before saving it. The example changes cat to cut; the returned outcome is Applied.

How do you apply diff-match-patch text in C#?

The patch string in the snippet is the entire input, including its final line break. Its header describes the old and new ranges. Context lines start with a space; deletion and insertion lines start with - and +. In this format the line contents are URI-encoded UTF-8, with spaces left as spaces. It is not interchangeable with an ordinary line-based unified patch even though the hunk headers look similar. This is the patch text format of Google’s diff-match-patch library. OneDiff follows the format described in that project’s wiki, as documented in its patch guide.

The top snippet applies an existing patch string. The variation generates that text with Diff.PatchText, prints it, and applies it to the old string. This is useful when both producer and consumer are in your application: retain the old version used to produce the patch and test the round trip before transporting it. The format uses rolling context, so later patch starts refer to the text after earlier patches have applied.

Application is fuzzy by default. A match can succeed even when the surrounding text has drifted. That is useful for applying edits to a nearby version, but it is not a guarantee that the entire original document matches. For small patches in particular, don’t treat Applied as a document identity check. Compare the source version separately when your workflow requires one exact base document.

The full program demonstrates stricter behavior by setting MatchThreshold = 0 on a DmpPatcher. The documented meaning is a perfect match at the expected place. Applying the same patch to zzz then returns NotFound, leaving the string unchanged. Without the strict setting this tiny example can match fuzzily, so checking only whether an exception was thrown would miss the distinction.

A patch that cannot be placed is reported in the result, not thrown as a conflict. Failed patches leave their locations unchanged, while other patches may still apply. This allows a partial result. Decide whether your application accepts partial edits before writing Text to disk; otherwise retain the original and present the outcomes for review. Malformed patch text is different: the parsing path throws PatchFormatException.

Offsets default to UTF-16 code units, as in the JavaScript port. The Python package counts Unicode code points instead. When exchanging patches with a producer that counts Unicode code points, choose DmpOffsetUnit.CodePoint consistently for making and applying patches, especially for text outside the Basic Multilingual Plane. The offset setting concerns placement; it does not turn one patch format into another. Use the same format and offset convention on both ends of the exchange.

All onediff recipes