| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
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.
TextDiff.Sharp can be seamlessly integrated into your C# project. You can include it using source files or via NuGet.
Install-Package TextDiff.Sharp
# or
dotnet add package TextDiff.SharpTo start using TextDiff.Sharp for applying diffs to original documents, follow the example below.
using TextDiff;
// Original document
string originalText = @"line1
line2
line3";
// Diff to be applied (standard unified diff format: 1-char prefix)
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:
To gain a deeper understanding of how TextDiff.Sharp applies diffs to original documents, you can examine the test files included in the repository:
[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:
TextDiff.Sharp processes diffs line by line, matching context lines and applying additions and deletions accordingly:
The library ensures that the diff is applied accurately, even in cases where the diff contains complex changes, special characters, or whitespace variations.
TextDiff.Sharp can be combined with DiffPlex to create a complete diff generation and application workflow:
using DiffPlex;
using DiffPlex.DiffBuilder;
using DiffPlex.DiffBuilder.Model;
using TextDiff;
// Generate unified diff using DiffPlex
var diffBuilder = new InlineDiffBuilder(new Differ());
var diffResult = diffBuilder.BuildDiffModel(oldText, newText);
// Convert DiffPlex result to unified diff format
var unifiedDiff = ConvertToUnifiedDiff(diffResult);
// Apply the diff using TextDiff.Sharp
var textDiffer = new TextDiffer();
var result = textDiffer.Process(oldText, unifiedDiff);
string patchedText = result.Text;
// Helper method to convert DiffPlex output to unified diff
static string ConvertToUnifiedDiff(DiffPaneModel diffModel)
{
var lines = new List<string>();
foreach (var line in diffModel.Lines)
{
switch (line.Type)
{
case ChangeType.Unchanged:
lines.Add($" {line.Text}");
break;
case ChangeType.Deleted:
lines.Add($"-{line.Text}");
break;
case ChangeType.Inserted:
lines.Add($"+{line.Text}");
break;
}
}
return string.Join(Environment.NewLine, lines);
}This combination allows you to:
TextDiff.Sharp supports the DiffX extensible diff format, which provides structured multi-file diffs with metadata:
using TextDiff;
// DiffX content with structured sections
var diffX = @"#diffx: encoding=utf-8, version=1.0
#.change:
#..file:
#...meta: format=json, length=30
{""path"": ""/src/main.py""}
#...diff:
def hello():
- print('Hello')
+ print('Hello, World!')";
var differ = new TextDiffer();
// Auto-detect format (works with both DiffX and unified diff)
var result = differ.ProcessDiffX(document, diffX);
// Or specify target file in multi-file DiffX
var result = differ.ProcessDiffX(document, diffX, "/src/main.py");
// Extract all file entries for batch processing
var entries = differ.ExtractDiffXEntries(diffX);
foreach (var entry in entries)
{
var doc = LoadDocument(entry.Path);
var result = differ.Process(doc, entry.DiffContent);
SaveDocument(entry.Path, result.Text);
}
// Check format before processing
if (TextDiffer.IsDiffX(content))
{
// Handle as DiffX
}| Method | Description |
|---|---|
| Process() | Standard synchronous processing for unified diff |
| ProcessAsync() | Async processing with cancellation and progress |
| ProcessStreamsAsync() | Streaming for very large files |
| ProcessOptimized() | Memory-optimized synchronous processing |
| ProcessDiffX() | Auto-detect DiffX or unified diff format |
Each processing method resets internal matcher state. For concurrent processing, use separate TextDiffer instances:
// Safe: separate instances for parallel work
await Task.WhenAll(
Task.Run(() => new TextDiffer().Process(doc1, diff1)),
Task.Run(() => new TextDiffer().Process(doc2, diff2))
);var result = differ.Process(document, diff);
Console.WriteLine($"Added: {result.Changes.AddedLines}");
Console.WriteLine($"Deleted: {result.Changes.DeletedLines}");
Console.WriteLine($"Changed: {result.Changes.ChangedLines}");This project is licensed under the MIT License. See the LICENSE file for details.
| Back | FazBrowse Home | New Git URL |