How Text Comparison Works
A diff answers one question: what has to change to turn the first text into the second? This guide explains how diff tools find the answer, how to read their output, and how to prepare two versions so the result shows the changes you care about instead of noise.
What a Diff Shows
Most diff tools, including the LeedyBox Text Diff, compare texts line by line. Every line of the result is one of three kinds: unchanged (in both versions), removed (only in the original) or added (only in the new version). There is no separate "changed" kind, so a line where one word was edited appears twice, once as removed and once as added:
Dear customer,
- Your order ships on Monday.
+ Your order ships on Tuesday.
Kind regards
The marks follow a long-standing convention: - for removed, + for added and a space for unchanged lines. Colours, usually red and green, repeat the same information.
How the Lines Are Matched
A diff tool looks for the longest sequence of lines that appear in both texts in the same order, the longest common subsequence. Those lines are kept as unchanged; everything else is removed or added. If the original is A, B, C, D and the new text is A, C, B, D, the longest common sequence has three lines, for example A, B, D, so C is shown as moved by being removed in one place and added in another.
Often several answers are equally long, and tools pick between them differently. That is why the same pair of files can produce slightly different diffs in two programs. Git uses the Myers algorithm by default and offers patience and histogram as alternatives, which often give more readable results for code where many lines are just a closing bracket.
Finding the longest common sequence takes work that grows with the number of lines in one text times the number in the other. The LeedyBox tool first sets aside identical lines at the start and end, then compares the rest, and stops with a message when that product exceeds 4,000,000, for example about 2,000 against 2,000 differing lines.
Reading Unified Diffs From Git and Code Reviews
Version control shows only the changed parts of a file, in the unified diff format:
--- a/config.yml
+++ b/config.yml
@@ -12,4 +12,5 @@
server:
- port: 8080
+ port: 8081
+ timeout: 30
database:
The --- and +++ lines name the old and new file. The hunk header @@ -12,4 +12,5 @@
means: this block starts at line 12 and spans 4 lines in the old file, and starts at line 12 and
spans 5 lines in the new one. A few unchanged lines around each change, three by default, give
context. A tool that shows the full text, like the LeedyBox one, has no hunk headers because every
line is visible; for that reason its output cannot be applied as a patch.
Preparing Texts for a Useful Comparison
A diff compares characters, not meaning. Small invisible differences make lines differ, and they can bury the real changes:
- Spaces and tabs at the end of lines, or tabs in one version and spaces in the other. Remove trailing spaces and use the same indentation in both.
- Invisible characters such as a non-breaking space or a zero-width space, common in text copied from web pages and word processors.
- Unicode forms. An accented letter like é can be stored as one character or as e plus a combining accent. Text from macOS file names or some PDFs often uses the second form.
- Smart quotes and dashes. Word processors replace " with “ ” and - with –, so a pasted copy no longer matches the plain original.
- Line endings. Windows ends lines with CR LF and macOS and Linux with LF. Browsers turn every line break in a text box into LF, so this difference does not show in the LeedyBox tool, but it does in tools that compare files directly.
- Formatting of data and code. Re-indented JSON or keys in another order make nearly every line differ. Format both versions the same way first, for example with sorted keys and the same indentation.
Comparing Prose
In ordinary documents a whole paragraph is often one long line, so a single edited word marks the
entire paragraph as removed and added. Two habits help. Before comparing, put each sentence on its
own line, so only the changed sentence stands out. Or use a tool that compares words within lines,
such as git diff --word-diff or the track-changes comparison built into word
processors.
Text copied from a PDF has its own trap: lines break where the page layout broke them, and words can be hyphenated at the end of a line. Join the lines of each paragraph again before comparing it with a source document.
What a Line Diff Cannot Tell You
- Moves. A paragraph moved to another place shows up as removed in one spot
and added in another. Some tools, such as git with
--color-moved, mark moved blocks separately. - Meaning. Two texts that say the same thing in different words differ completely, and a one-character change, such as a missing "not", looks small but can reverse a sentence.
- Binary content. Word documents, PDFs and images are not plain text. Extract their text first, or use a comparison feature of the program that made them.
Privacy When Comparing Documents
Contracts, configuration files and logs often contain names, keys or passwords. Some online comparison services save pasted text on their servers, sometimes under a shareable link. The LeedyBox Text Diff runs in your browser and does not send or store the text, but it is still worth removing secrets before pasting a configuration file anywhere.
Frequently Asked Questions
Why is a whole paragraph marked as changed when I edited one word?
The paragraph is one line, and line diffs compare whole lines. Split the text into sentences or shorter lines before comparing, or use a word-level comparison.
Can I ignore differences in spaces?
The LeedyBox tool compares lines exactly. Clean up both texts first, for example by removing
trailing spaces and converting tabs to spaces; git offers options such as
--ignore-space-change for files.
How do I compare two Word documents?
Word has a built-in Compare function that also shows formatting changes. To compare only the wording, copy the text of both documents into a text comparison tool.
What is a three-way merge?
When two people edit the same original, a merge tool compares each version with the common original. Changes in different places are combined automatically; changes to the same lines are reported as conflicts that a person has to resolve.