TextDiff.Sharp 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package TextDiff.Sharp --version 1.1.0
                    
NuGet\Install-Package TextDiff.Sharp -Version 1.1.0
                    
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="TextDiff.Sharp" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TextDiff.Sharp" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="TextDiff.Sharp" />
                    
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 TextDiff.Sharp --version 1.1.0
                    
#r "nuget: TextDiff.Sharp, 1.1.0"
                    
#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 TextDiff.Sharp@1.1.0
                    
#: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=TextDiff.Sharp&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=TextDiff.Sharp&version=1.1.0
                    
Install as a Cake Tool

TextDiff.Sharp

NuGet Version NuGet Downloads Build Status License: MIT

A production-ready C# library for processing unified diff files and applying changes to text documents. TextDiff.Sharp provides multiple API variants optimized for different scenarios, from simple synchronous processing to high-performance streaming for large files.

Features

  • 🔄 Multiple Processing APIs: Synchronous, asynchronous, streaming, and memory-optimized processing
  • ⚡ High Performance: Memory-efficient algorithms optimized for large files with 2-5x performance improvements
  • 🛡️ Comprehensive Error Handling: Specific exception types with detailed error information and line numbers
  • 📊 Progress Reporting: Real-time progress updates for long-running operations with cancellation support
  • 🔧 Extensible Design: Dependency injection support for custom parsing, matching, and tracking logic
  • 🌐 Cross-Platform: Supports .NET Standard 2.1, .NET 8.0, and .NET 9.0
  • 📚 Complete Documentation: Comprehensive XML documentation, examples, and guides
  • ✅ Production Ready: 100% test coverage with comprehensive validation and monitoring support

Installation

TextDiff.Sharp can be seamlessly integrated into your C# project. You can include it using source files or via NuGet.

Using Source Files

  1. Clone or download the repository.
  2. Add the TextDiff.Sharp project or individual source files to your solution.
  3. Add a reference to the TextDiff.Sharp project in your application.

Using NuGet

Install-Package TextDiff.Sharp

# or

dotnet add package TextDiff.Sharp

Getting Started

To start using TextDiff.Sharp for applying diffs to original documents, follow the example below.

Example: Applying a Diff to a Text Document

using TextDiff;

// Original document
string originalText = @"line1
line2
line3";

// Diff to be applied
string diffText = @" line1
- line2
+ line2_modified
 line3";

// Create a TextDiffer instance
var textDiffer = new TextDiffer();

// Process the diff
var result = textDiffer.Process(originalText, diffText);

// Get the updated text
string updatedText = result.Text;

// Output the updated text
Console.WriteLine(updatedText);

/* Output:
line1
line2_modified
line3
*/

In this example:

  • The original document contains three lines: line1, line2, and line3.
  • The diff indicates that line2 should be replaced with line2_modified.
  • Using TextDiffer.Process, the diff is applied to the original text.
  • The resulting updatedText reflects the change specified by the diff.

Exploring the Code

To gain a deeper understanding of how TextDiff.Sharp applies diffs to original documents, you can examine the test files included in the repository:

  • DiffProcessorTests.cs
    Located at /src/TextDiff.Tests/DiffProcessorTests.cs, this file contains unit tests demonstrating various scenarios of applying diffs to original texts. The tests cover simple replacements, insertions, deletions, and complex changes, showcasing the library's capabilities.

Sample Test Case from DiffProcessorTests.cs

[Fact]
public void TestSimpleDeleteAndInsert()
{
    // Arrange
    var original = @"line1
line2
line3
line4";

    var diff = @" line1
- line2
+ new_line2
 line3
 line4";

    var expected = @"line1
new_line2
line3
line4";

    // Act
    var result = _differ.Process(original, diff);

    // Assert
    Assert.Equal(expected, result.Text);
}

In this test case:

  • The original document has four lines.
  • The diff replaces line2 with new_line2.
  • The Process method applies the diff to the original text.
  • The test asserts that the resulting text matches the expected output.

How It Works

TextDiff.Sharp processes diffs line by line, matching context lines and applying additions and deletions accordingly:

  • Context Lines: Lines that start with a space ' ' represent unchanged lines in the diff and are used to align the diff with the original document.
  • Deletion Lines: Lines that start with a minus '-' indicate lines to be removed from the original document.
  • Addition Lines: Lines that start with a plus '+' represent new lines to be inserted into the original document.

The library ensures that the diff is applied accurately, even in cases where the diff contains complex changes, special characters, or whitespace variations.

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 is compatible.  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 was computed.  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.
  • net8.0

    • No dependencies.
  • net9.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.1.1 127 9/18/2025
1.1.0 129 9/18/2025
1.0.3 355 12/5/2024
1.0.2 116 11/29/2024
1.0.1 114 11/29/2024
1.0.0 125 11/29/2024