TextDiff.Sharp
1.0.3
dotnet add package TextDiff.Sharp --version 1.0.3
NuGet\Install-Package TextDiff.Sharp -Version 1.0.3
<PackageReference Include="TextDiff.Sharp" Version="1.0.3" />
paket add TextDiff.Sharp --version 1.0.3
#r "nuget: TextDiff.Sharp, 1.0.3"
// Install TextDiff.Sharp as a Cake Addin #addin nuget:?package=TextDiff.Sharp&version=1.0.3 // Install TextDiff.Sharp as a Cake Tool #tool nuget:?package=TextDiff.Sharp&version=1.0.3
TextDiff.Sharp
TextDiff.Sharp is a powerful and efficient C# library specifically designed for applying diffs to original text documents. If you have a diff file and an original document, TextDiff.Sharp allows you to seamlessly patch the diff onto the original, producing the updated document. This makes it an ideal tool for applications that need to update documents based on diff files, such as version control systems, code editors, or any text manipulation utilities that rely on diff operations.
Key Features
- Apply Diffs to Originals: Precisely patch diff files onto original documents to generate updated versions.
- Robust Parsing: Accurately parse diff files, handling various diff formats and edge cases.
- High Performance: Optimized for efficiency, suitable for large documents and complex diffs.
- Easy Integration: Simple API that can be easily integrated into your C# projects.
Installation
TextDiff.Sharp can be seamlessly integrated into your C# project. You can include it using source files or via NuGet.
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
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
, andline3
. - The diff indicates that
line2
should be replaced withline2_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
withnew_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 | 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.