TextDiff.Sharp
1.0.2
See the version list below for details.
dotnet add package TextDiff.Sharp --version 1.0.2
NuGet\Install-Package TextDiff.Sharp -Version 1.0.2
<PackageReference Include="TextDiff.Sharp" Version="1.0.2" />
paket add TextDiff.Sharp --version 1.0.2
#r "nuget: TextDiff.Sharp, 1.0.2"
// Install TextDiff.Sharp as a Cake Addin #addin nuget:?package=TextDiff.Sharp&version=1.0.2 // Install TextDiff.Sharp as a Cake Tool #tool nuget:?package=TextDiff.Sharp&version=1.0.2
TextDiff.Sharp
TextDiff.Sharp is a robust and efficient C# library designed for parsing, analyzing, and applying textual diffs to documents. Whether you're building a version control system, a text editor with diff capabilities, or any application that requires detailed text comparison and manipulation, TextDiff.Sharp provides the tools you need to handle complex diff operations seamlessly.
Table of Contents
Features
- Diff Parsing: Efficiently parses diff texts into manageable blocks.
- Change Tracking: Accurately tracks added, deleted, and changed lines.
- Line Matching: Intelligent line matching with similarity scoring to handle ambiguous cases.
- Whitespace Handling: Robust handling of whitespace differences to ensure meaningful comparisons.
- String Similarity: Utilizes Levenshtein Distance for precise string similarity calculations.
- Comprehensive Testing: Includes a suite of unit tests to ensure reliability and correctness.
- Extensible Architecture: Designed with extensibility in mind, allowing for easy integration and customization.
Installation
TextDiff.Sharp can be easily integrated into your C# project. You can add the source files directly or include it as a project reference.
Using Source Files
- Clone or download the repository.
- Add the
TextDiff.Sharp
project or individual source files to your solution. - Add a reference to the
TextDiff.Sharp
project in your application.
Using NuGet
Note: If TextDiff.Sharp is published to NuGet, you can install it using the following command:
Install-Package TextDiff.Sharp
Replace the package name with the actual NuGet package name if available.
Getting Started
Basic Usage
Here's a simple example of how to use TextDiff.Sharp to apply a diff to a document:
using TextDiff;
using System;
class Program
{
static void Main(string[] args)
{
var document = @"line1
line2
line3
line4";
var diff = @" line1
- line2
- line3
+ new_line2
+ new_line3
line4";
var processor = new DiffProcessor();
var result = processor.Process(document, diff);
Console.WriteLine("Updated Document:");
Console.WriteLine(result.Text);
Console.WriteLine("\nChange Summary:");
Console.WriteLine($"Added Lines: {result.Changes.AddedLines}");
Console.WriteLine($"Deleted Lines: {result.Changes.DeletedLines}");
Console.WriteLine($"Changed Lines: {result.Changes.ChangedLines}");
}
}
Output:
Updated Document:
line1
new_line2
new_line3
line4
Change Summary:
Added Lines: 2
Deleted Lines: 2
Changed Lines: 0
Advanced Usage
For more complex scenarios involving multiple diff blocks, whitespace handling, and similarity scoring, refer to the Examples section below.
API Reference
DiffProcessor
Namespace: TextDiff
The main class responsible for processing diffs and applying them to documents.
- Methods:
Process(string documentText, string diffText)
: Applies the given diff to the document and returns the result.
DiffParser
Namespace: TextDiff
Parses diff texts into structured DiffBlock
objects.
- Methods:
Parse(string diffText)
: Parses the diff text and returns a list ofDiffBlock
instances.
DiffBlock
Namespace: TextDiff
Represents a block of differences, including leading, trailing, target, and insert lines.
Properties:
IReadOnlyList<string> TargetLines
IReadOnlyList<string> LeadingLines
IReadOnlyList<string> TrailingLines
IReadOnlyList<string> InsertLines
Methods:
AddTargetLine(DiffLine line)
AddLeadingLine(DiffLine line)
AddTrailingLine(DiffLine line)
AddInsertLine(DiffLine line)
GetCommonIndentation()
ChangeStats
Namespace: TextDiff
Tracks statistics about changes, including added, deleted, and changed lines.
- Methods:
UpdateStats(DiffBlock block)
ToResult()
: Converts the statistics to aDocumentChangeResult
.
DocumentChange
Namespace: TextDiff
Represents a single change to be applied to the document.
- Properties:
int LineNumber
IReadOnlyList<string> LinesToRemove
IReadOnlyList<string> LinesToInsert
DocumentChangeResult
Namespace: TextDiff
Holds the summary of changes after processing a diff.
- Properties:
int DeletedLines
int ChangedLines
int AddedLines
StringSimilarityCalculator
Namespace: TextDiff
Provides functionality to calculate the similarity between two strings using Levenshtein Distance.
- Methods:
Calculate(string str1, string str2)
: Returns a similarity score between 0.0 and 1.0.
Examples
Applying a Simple Diff
var document = @"line1
line2
line3
line4";
var diff = @" line1
- line2
- line3
+ new_line2
+ new_line3
line4";
var processor = new DiffProcessor();
var result = processor.Process(document, diff);
Console.WriteLine(result.Text);
Output:
line1
new_line2
new_line3
line4
Handling Additions Only
var document = @"line1
line2
line3";
var diff = @" line1
+ line1.5
line2
line3";
var result = processor.Process(document, diff);
Console.WriteLine(result.Text);
Output:
line1
line1.5
line2
line3
Handling Deletions Only
var document = @"line1
line2
line3";
var diff = @" line1
- line2
line3";
var result = processor.Process(document, diff);
Console.WriteLine(result.Text);
Output:
line1
line3
Complex Changes with Multiple Diff Blocks
var document = @"header1
line1
line2
line3
line4
footer1
footer2";
var diff = @" header1
- line1
+ new_line1
line2
- line3
+ new_line3
+ added_line3.1
line4
footer1
- footer2
+ footer2_modified";
var result = processor.Process(document, diff);
Console.WriteLine(result.Text);
Output:
header1
new_line1
line2
new_line3
added_line3.1
line4
footer1
footer2_modified
Testing
TextDiff.Sharp includes a comprehensive suite of unit tests to ensure functionality and reliability. The tests are located in the TextDiff.Tests
project and cover various scenarios, including simple additions and deletions, complex multi-line changes, whitespace handling, and more.
To run the tests:
- Open the solution in your preferred IDE (e.g., Visual Studio).
- Build the solution to restore all dependencies.
- Navigate to the Test Explorer.
- Run all tests to ensure everything is working as expected.
Contributing
Contributions are welcome! If you'd like to improve TextDiff.Sharp, please follow these guidelines:
- Fork the Repository: Click the "Fork" button at the top-right corner of the repository page.
- Clone the Fork: Clone your forked repository to your local machine.
- Create a Branch: Create a new branch for your feature or bugfix.
git checkout -b feature/YourFeatureName
- Make Changes: Implement your changes and ensure they align with the existing code style.
- Run Tests: Ensure all tests pass and add new tests for your changes if necessary.
- Commit and Push: Commit your changes and push them to your fork.
git commit -m "Add feature XYZ" git push origin feature/YourFeatureName
- Create a Pull Request: Navigate to the original repository and create a pull request from your fork.
Please ensure your code adheres to the project's coding standards and that all tests pass before submitting a pull request.
License
This project is licensed under the MIT License.
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 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. 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.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.