PatchSharp 1.0.1
See the version list below for details.
dotnet add package PatchSharp --version 1.0.1
NuGet\Install-Package PatchSharp -Version 1.0.1
<PackageReference Include="PatchSharp" Version="1.0.1" />
<PackageVersion Include="PatchSharp" Version="1.0.1" />
<PackageReference Include="PatchSharp" />
paket add PatchSharp --version 1.0.1
#r "nuget: PatchSharp, 1.0.1"
#:package PatchSharp@1.0.1
#addin nuget:?package=PatchSharp&version=1.0.1
#tool nuget:?package=PatchSharp&version=1.0.1
PatchSharp
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.
License
MIT
| Product | Versions 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. |
-
.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.