Cloudey.Different 1.0.1

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

// Install Cloudey.Different as a Cake Tool
#tool nuget:?package=Cloudey.Different&version=1.0.1                

Cloudey

🔀 Different

Robust, easy-to-use diff and patch library for comparing and synchronising text with zero dependencies


GitHub Nuget Nuget

What is this

Different provides extension methods for creating and applying diffs and patches for plain text.
It uses the same robust algorithms and logic as commonly seen used with git repositories, wrapped in a readable, easy-to-use library.

How to use

🔀 Diff

Get the differences between two texts

string from = "Hello!";
string to = "Hallo?";

List<Diff> diffs = from.Diff(to);

Result

{
    {Operation.EQUAL, "H"},
    {Operation.DELETE, "e"},
    {Operation.INSERT, "a"},
    {Operation.EQUAL, "llo"},
    {Operation.DELETE, "!"},
    {Operation.INSERT, "?"}
}

Details: diff-match-patch#diff_main

✨ Semantic diff

Get the differences between two texts, optimised for human-readability
Two unrelated texts can contain many coincidental matches. This can make the diff hard to understand for humans, despite being the optimal result. A semantic diff rewrites the diff to be more legible and easier to understand.

string from = "Computer";
string to = "Mouse";

List<Diff> diffs = from.SemanticDiff(to);

Result

{
    {Operation.DELETE, "Computer"},
    {Operation.INSERT, "Mouse"},
}

Details: diff-match-patch#diff_cleanupSemantic

⏱ Efficient diff

Get the differences between two texts, optimised for machine processing
Applying a large number of small diffs can take drastically longer to process in some scenarios than a small number of larger diffs. An efficient diff rewrites the diff to eliminate and combine tiny diffs into larger ones based on an edit cost heuristic. The result is often similar to a semantic diff, but not always.

string from = "Computer";
string to = "Mouse";

List<Diff> diffs = from.EfficientDiff(to);

Result

{
    {Operation.DELETE, "Computer"},
    {Operation.INSERT, "Mouse"},
}

Details: diff-match-patch#diff_cleanupEfficiency

📐 Levenshtein distance

Measure the Levenshtein distance of a diff in terms of changed characters
The minimum value is 0, meaning the strings are equal. The maximum value is the length of the longer string.

string from = "Hello!";
string to = "Hallo?";

List<Diff> diffs = from.Diff(to);

diffs.Levenshtein();

Result

2 // e -> a, ! -> ?

Details: diff-match-patch#diff_levenshtein

🔡 Visualise diffs as HTML

Get an HTML representation of a diff
This can be used to visualise diffs in a human-readable way.

string from = "Hello!";
string to = "Hallo?";

List<Diff> diffs = from.Diff(to);

diffs.Html();

Result

<span>H</span>
<del style="background:#ffe6e6;">e</del> 
<ins style="background:#e6ffe6;">a</ins> 
<span>llo</span>
<del style="background:#ffe6e6;">!</del> 
<ins style="background:#e6ffe6;">?</ins> 

Details: diff-match-patch#diff_prettyHtml

🩹 Patch

Get a list of patches given two texts, a text and a diff, or only a diff
Patches are a list of operations to transform one text to another. They provide a way to "apply" diffs.

string from = "Hello!\nWho's there?";
string to = "Hallo?\nWho's there?";

// Given two strings
List<Patch> patches = from.Patch(to);

// Given a string and diffs
List<Diff> diffs = from.Diff(to);
List<Patch> patches = from.Patch(diffs);

// Given diffs
List<Diff> diffs = from.Diff(to);
List<Patch> patches = diffs.Patch();

Result (serialised to JSON)

[
  {
    "diffs": [
      {
        "operation": 2,
        "text": "H"
      },
      {
        "operation": 0,
        "text": "ello!"
      },
      {
        "operation": 1,
        "text": "allo?"
      },
      {
        "operation": 2,
        "text": "\nWho"
      }
    ],
    "start1": 0,
    "start2": 0,
    "length1": 10,
    "length2": 10
  }
]

Details: diff-match-patch#patch_make

🧩 Apply patch

Apply a list of patches to a string
If the patches cannot be applied, returns null

string from = "Hello!\nWho's there?";
string to = "Hallo?\nWho's there?";

var patch = from.Patch(to);

// Using extension method on string
string? result = from.ApplyPatches(patch);

// Using extension method on List<Patch>
string? result = patch.Apply(from);

Result

"Hallo?\nWho's there?"

Details: diff-match-patch#patch_apply

📃 Patch to text

Get the text representation of a list of patch operations
The format is very similar to the standard GNU diff/patch format. This text can be stored and later converted back to patch objects (see "Text to patch" below).

string from = "Hello!\nWho's there?";
string to = "Hallo?\nWho's there?";

List<Patch> patches = from.Patch(to);

patches.Text();

Result

@@ -1,10 +1,10 @@
 H
-ello!
+allo?
 %0aWho

Details: diff-match-patch#patch_toText

📜 Text to patch

Transform a text representation of patches to a list of patch operations
Parses and returns a list of patch objects from their textual representation (see "Patch to text" above).

var patchString = @"@@ -1,10 +1,10 @@
 H
-ello!
+allo?
 %0aWho
";

// Throws ArgumentException on failure
List<Patch> patches = patchString.ParsePatches();

// Safe, returns true or false depending on success
bool success = patchString.TryParsePatches(out List<Patch>? patches);

Result (serialised to JSON)

[
  {
    "diffs": [
      {
        "operation": 2,
        "text": "H"
      },
      {
        "operation": 0,
        "text": "ello!"
      },
      {
        "operation": 1,
        "text": "allo?"
      },
      {
        "operation": 2,
        "text": "\nWho"
      }
    ],
    "start1": 0,
    "start2": 0,
    "length1": 10,
    "length2": 10
  }
]

Details: diff-match-patch#patch_fromText

License

Licensed under Apache 2.0.
Copyright © 2021 Cloudey IT Ltd
Cloudey® is a registered trademark of Cloudey IT Ltd. Use of the trademark is NOT GRANTED under the license of this repository or software package.

####For diff-match-patch library contained within repository and distributed code:

Copyright 2018 The diff-match-patch Authors.
https://github.com/google/diff-match-patch

Modifications were made to the diff-match-patch library (licensed under Apache 2.0) by the authors of this package (Cloudey)

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.0.1 485 2/6/2022
1.0.0 426 2/5/2022