文本对比入门:LCS 与 diff 输出读法
最长公共子序列直觉、增删着色读法、为什么 diff 不猜移动。
Every code review leans on a diff, yet the algorithm behind those red and green lines stays a mystery to most. It is one elegant idea — and knowing it helps you read imperfect diffs correctly.
The longest common subsequence
A diff splits both texts into lines and finds the longest common subsequence — the biggest chain of identical lines that appear in the same order. Everything else is marked added or removed. That is why moved paragraphs can show as delete-plus-add instead of a move: pure LCS does not model motion.
Line diff, then character polish
Line-level diffing stays fast even for thousands of lines. Good tools add a second pass: inside changed line pairs, highlight the exact character or word that changed. The Text Diff Checker shows both — block alignment plus character detail — entirely in your browser.
Formatting noise vs real changes
Indentation differences, capitalization and line-ending styles (CRLF vs LF) bury real edits. That is what the ignore-whitespace and ignore-case options exist for: they change what equality means before the LCS runs, so the diff reports content changes, not formatting churn.
Workflow beyond git
- Compare JSON responses before and after an API change — run both through the JSON Formatter first, then the diff aligns semantically instead of by incidental formatting.
- Review config or policy files copied between environments.
- Clean repeated lines in the inputs first with the Deduplicator so the diff reports real edits, not duplicates.
Because comparison is local, confidential documents and credentials are safe to run through the diff tool — nothing uploads anywhere.
相关指南
文中提到的工具
Text Diff Checker
Compare two texts and see added, removed and unchanged lines with an LCS diff algorithm. Colors, counters and a line-by-line unified view — computed locally.
Open tool →Text Deduplicator
Remove duplicate lines from any list with options for trimming whitespace, ignoring case and dropping empty lines. Shows before/after counts and what was removed.
Open tool →JSON Formatter
Pretty-print JSON with 2/4-space or tab indentation, optional key sorting, and precise error positions when the JSON is broken. Private, instant, browser-only.
Open tool →