DimonSmart.StringDiff 1.24725.1329

Prefix Reserved
dotnet add package DimonSmart.StringDiff --version 1.24725.1329                
NuGet\Install-Package DimonSmart.StringDiff -Version 1.24725.1329                
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="DimonSmart.StringDiff" Version="1.24725.1329" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DimonSmart.StringDiff --version 1.24725.1329                
#r "nuget: DimonSmart.StringDiff, 1.24725.1329"                
#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.
// Install DimonSmart.StringDiff as a Cake Addin
#addin nuget:?package=DimonSmart.StringDiff&version=1.24725.1329

// Install DimonSmart.StringDiff as a Cake Tool
#tool nuget:?package=DimonSmart.StringDiff&version=1.24725.1329                

DimonSmart.StringDiff

Introduction

DimonSmart.StringDiff is a library for computing differences between strings, and creating difference models. Support for Markdown formatting.

TRY online demo

Installation

You can install the DimonSmart.StringDiff package via NuGet Package Manager:

Install-Package DimonSmart.StringDiff

Or by using the .NET CLI:

dotnet add package DimonSmart.StringDiff

Usage

Below is a guide on how to use the DimonSmart.StringDiff library to compute and visualize differences between strings.

Basic Example

using DimonSmart.StringDiff;

// Create an instance of StringDiff with default options
var stringDiff = new StringDiff();

// Compute the differences between two strings
var sourceText = "Hello, world!";
var targetText = "Hello, brave new world!";
var textDiff = stringDiff.ComputeDiff(sourceText, targetText);

// Display the differences
foreach (var edit in textDiff.Edits)
{
  Console.WriteLine($"StartPosition: {edit.StartPosition}, DeletedLength: {edit.DeletedLength}, InsertedText: {edit.InsertedText}");
}

Custom Options

You can customize the behavior of the diff algorithm by providing StringDiffOptions.

using DimonSmart.StringDiff;

// Create custom options
var options = new StringDiffOptions(10, DefaultWordBoundaryDetector.Instance);

// Create an instance of StringDiff with custom options
var stringDiff = new StringDiff(options);

// Compute the differences between two strings
var sourceText = "123-456-7890";
var targetText = "123-456-0000";
var textDiff = stringDiff.ComputeDiff(sourceText, targetText);

// Display the differences
foreach (var edit in textDiff.Edits)
{
    Console.WriteLine($"StartPosition: {edit.StartPosition}, DeletedLength: {edit.DeletedLength}, InsertedText: {edit.InsertedText}");
}

Visualizing Changes

To visualize changes, you can use the StringReconstructor class. This class helps to reconstruct the target string from the source string and a list of edits, applying formatting to the changes.

Example

using DimonSmart.StringDiff;

// Compute the differences
var stringDiff = new StringDiff();
var sourceText = "Hello, world!";
var targetText = "Hello, brave new world!";
var textDiff = stringDiff.ComputeDiff(sourceText, targetText);

// Reconstruct the target text with visual markers for changes
var reconstructor = StringReconstructor.Instance;
var visualizedText = reconstructor.Reconstruct(textDiff.Edits, sourceText);

Console.WriteLine(visualizedText);

Markdown Support

You can also use the MarkdownStringReconstructor class to format the changes using Markdown syntax, which is useful for applications that support Markdown rendering.

using DimonSmart.StringDiff;

// Compute the differences
var stringDiff = new StringDiff();
var sourceText = "Hello, world!";
var targetText = "Hello, brave new world!";
var textDiff = stringDiff.ComputeDiff(sourceText, targetText);

// Reconstruct the target text with Markdown formatting for changes
var markdownReconstructor = MarkdownStringReconstructor.Instance;
var visualizedMarkdownText = markdownReconstructor.Reconstruct(textDiff.Edits, sourceText);

Console.WriteLine(visualizedMarkdownText);

API Reference

StringDiff Class

The StringDiff class provides methods for computing differences between strings.

Constructors
  • StringDiff(): Initializes a new instance of the StringDiff class with default options.
  • StringDiff(StringDiffOptions options): Initializes a new instance of the StringDiff class with custom options.
Methods
  • TextDiff ComputeDiff(string sourceText, string targetText): Computes the differences between the source and target texts and returns a TextDiff object.

StringDiffOptions Record

The StringDiffOptions record allows customization of the diff algorithm.

Parameters
  • MinCommonLength (int): Forces the differ to report longer changes. For example, if set to 10 (phone number length), the differ will prefer not to report single-letter changes.
  • WordBoundaryDetector (IWordBoundaryDetector?): The word boundary detector to use for detecting word boundaries. If null, the default word boundary detection will be used.

TextDiff Class

The TextDiff class represents the result of a diff operation.

Properties
  • string SourceText: The source text.
  • string TargetText: The target text.
  • List<TextEdit> Edits: A list of TextEdit objects representing the differences between the source and target texts.

TextEdit Class

The TextEdit class represents a single text edit.

Properties
  • int Offset: The offset position in the source text where the edit occurs.
  • int Length: The length of the text to be replaced.
  • string Replacement: The replacement text.

IWordBoundaryDetector Interface

The IWordBoundaryDetector interface defines a method for detecting word boundaries in a string.

Methods
  • HashSet<int> Detect(string s): Detects word boundaries in the specified string and returns a set of boundary indices.

DefaultWordBoundaryDetector Class

The DefaultWordBoundaryDetector class provides a default implementation of IWordBoundaryDetector using regular expressions.

Constructors
  • DefaultWordBoundaryDetector(): Initializes a new instance with the default word boundary regex pattern (\b).
  • DefaultWordBoundaryDetector(string regexPattern): Initializes a new instance with the specified regex pattern.

StringReconstructor Class

The StringReconstructor class helps to reconstruct the target string from the source string and a list of text edits. This class also provides methods for formatting the changes.

Methods
  • string Reconstruct(IReadOnlyCollection<TextEdit> edits, string source): Reconstructs the target string from the source string and a list of text edits.
Overridable Methods
  • protected virtual string FormatInsertedText(string text): Formats the inserted text. Default implementation returns the text as is.
  • protected virtual string FormatDeletedText(string text): Formats the deleted text. Default implementation returns an empty string.
  • protected virtual string FormatModifiedText(string oldText, string newText): Formats the modified text. Default implementation returns the new text.

MarkdownStringReconstructor Class

The MarkdownStringReconstructor class extends the StringReconstructor class to provide Markdown formatting for changes.

Overridable Methods
  • protected override string FormatInsertedText(string text): Formats the inserted text using Markdown bold syntax (**text**).
  • protected override string FormatDeletedText(string text): Formats the deleted text using Markdown strikethrough syntax (~~text~~).
  • protected override string FormatModifiedText(string oldText, string newText): Formats the modified text using both Markdown strikethrough and bold syntax (~~oldText~~**newText**).

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue on GitHub.

License

This project is licensed under the 0BSD License, the most permissive of all licenses. See the LICENSE file for details.

Acknowledgements

Special thanks to the contributors and the community for their valuable input and feedback.


DimonSmart.StringDiff is a powerful tool for computing and visualizing differences between text. It's ideal for use cases such as highlighting changes in edit history or comparing different versions of documents.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.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.24725.1329 147 7/25/2024
1.24718.1510 91 7/18/2024
1.24709.2242 91 7/9/2024
1.24708.2155 90 7/8/2024
1.24708.1956 104 7/8/2024
1.24708.1543 104 7/8/2024