Introduction

edit

Meziantou.Framework.TextDiff computes the differences between two texts. It splits both texts into chunks (lines, words, characters, or your own unit), matches the chunks they have in common, and tells you, chunk by chunk, what was kept, deleted and inserted.

What you can do with it

  • Compare two texts line by line, word by word, character by character or grapheme by grapheme, or with your own chunks, such as sentences. See Chunking.
  • Ignore what does not matter: casing, accents, the whitespace around each chunk, and \r\n versus \n line terminators, with ordinal or culture-aware rules. See Comparison options.
  • Refine the changes: find the changed lines, then the changed words inside them, then the changed characters inside those words. See Hierarchical diffs.
  • Find moved blocks: a block deleted at one place and inserted at another is tagged as a move. See Detecting moves.
  • Choose the algorithm that suits your texts: Myers, Patience, Histogram or Hunt-Szymanski. See Algorithms.
  • Rebuild either text from the result, even when an option made two different chunks compare equal. See Reading the result.
  • Show the result your way: write it as a unified diff, or as HTML or anything else from the entries. See Displaying a diff.
  • Read and apply patches in the unified format of diff -u and git diff. See Patches.
  • Merge two versions of a text changed from a common base, with conflict markers. See Merging.

How it works

A diff is a list of entries. Each entry is one chunk of the texts with an operation:

OperationMeaning
EqualThe chunk is present in both texts.
DeleteThe chunk is only present in the old text.
InsertThe chunk is only present in the new text.

Read the entries in order: the Equal and Delete entries give the old text, the Equal and Insert entries give the new text. A change is a Delete followed by an Insert, so a moved block of text appears as a deletion at its old place and an insertion at its new place; move detection links the two.

Namespace

Every type is in the Meziantou.Framework namespace: TextDiff, TextDiffOptions, TextChunker, TextDiffResult, TextDiffEntry, the hierarchy types, UnifiedDiff and TextMerge.

Next steps