PatchSharp 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package PatchSharp --version 1.0.3
                    
NuGet\Install-Package PatchSharp -Version 1.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PatchSharp" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PatchSharp" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="PatchSharp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PatchSharp --version 1.0.3
                    
#r "nuget: PatchSharp, 1.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package PatchSharp@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PatchSharp&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=PatchSharp&version=1.0.3
                    
Install as a Cake Tool

PatchSharp

CI NuGet NuGet Downloads codecov

A C# library for applying text diffs and replacements with built-in fuzzy matching. Supports OpenAI's V4A patch format and Anthropic-style str_replace operations.

Installation

dotnet add package PatchSharp

Targets netstandard2.1, net8.0, and net10.0.

Usage

All methods are on the static class PatchSharp.ApplyPatch.

Apply a V4A diff

using PatchSharp;

var original = "line one\nline two\nline three\n";
var diff = @"*** original.txt
 line one
-line two
+line 2
 line three
";

var result = ApplyPatch.Apply(original, diff);
// "line one\nline 2\nline three\n"

Create a new file from a diff

var diff = @"*** new-file.txt
+hello
+world
";

var result = ApplyPatch.Create(diff);
// "hello\nworld\n"

Find and replace with fuzzy matching

var input = "function hello() {\n  return 'world';\n}\n";

var result = ApplyPatch.StrReplace(
    input,
    oldStr: "  return 'world';",
    newStr: "  return 'hello world';");

Replace all occurrences:

var result = ApplyPatch.StrReplace(input, "old", "new", allowMulti: true);

Use a regex pattern:

var result = ApplyPatch.StrReplace(input, @"v\d+\.\d+", "v2.0", useRegex: true);

Error handling

All methods throw PatchApplyException on failure:

try
{
    ApplyPatch.Apply(input, diff);
}
catch (PatchApplyException ex)
{
    Console.WriteLine(ex.Message);
    Console.WriteLine(ex.LineNumber); // line in the diff where the error occurred
    Console.WriteLine(ex.Fuzz);      // fuzzy match level used (0 = exact)
    Console.WriteLine(ex.Context);   // surrounding context for diagnostics
}

Fuzzy matching

When an exact match isn't found, PatchSharp tries progressively looser matching:

Fuzz level Strategy
0 Exact match
1 Ignore trailing whitespace
100 Ignore leading and trailing whitespace
1000 Unicode normalization (smart quotes → ASCII quotes, em-dashes → hyphens, etc.)

The lowest fuzz level that produces a match wins.

V4A diff format

*** path/to/file.txt           ← file header
@@ context line                ← anchor (jump to this line)
 unchanged line                ← context (space prefix)
-removed line                  ← deletion
+added line                    ← insertion
*** End of File                ← anchor to end of file

Wrapped in optional *** Begin Patch / *** End Patch markers.

Origin

PatchSharp was extracted from Instruct UI, an AI-powered platform that generates production-ready Blazor UI code from text descriptions and screenshots. These patching and replacement algorithms are battle-tested across thousands of AI-generated code edits in Instruct UI's multi-agent workflow, where reliable diff application is critical for compiling and rendering live previews.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.4 53 3/20/2026
1.0.3 32 3/19/2026
1.0.2 46 3/18/2026
1.0.1 34 3/18/2026
1.0.0 31 3/18/2026