Diff Text Lines with a Longest Common Subsequence
Compare two slices of text lines with a longest common subsequence table and emit stable add, remove, and equal operations.
Code
package main
import (
"fmt"
"strings"
)
type Op struct {
Kind byte
Line string
}
func diffLines(left, right []string) []Op {
rows := len(left) + 1
columns := len(right) + 1
table := make([][]int, rows)
for index := range table {
table[index] = make([]int, columns)
}
for i := len(left) - 1; i >= 0; i-- {
for j := len(right) - 1; j >= 0; j-- {
if left[i] == right[j] {
table[i][j] = table[i+1][j+1] + 1
} else if table[i+1][j] >= table[i][j+1] {
table[i][j] = table[i+1][j]
} else {
table[i][j] = table[i][j+1]
}
}
}
var operations []Op
i, j := 0, 0
for i < len(left) && j < len(right) {
switch {
case left[i] == right[j]:
operations = append(operations, Op{' ', left[i]})
i++
j++
case table[i+1][j] >= table[i][j+1]:
operations = append(operations, Op{'-', left[i]})
i++
default:
operations = append(operations, Op{'+', right[j]})
j++
}
}
for ; i < len(left); i++ {
operations = append(operations, Op{'-', left[i]})
}
for ; j < len(right); j++ {
operations = append(operations, Op{'+', right[j]})
}
return operations
}
func render(operations []Op) string {
var builder strings.Builder
for _, operation := range operations {
fmt.Fprintf(&builder, "%c %s\n", operation.Kind, operation.Line)
}
return builder.String()
}
func main() {
left := strings.Split("alpha\nbeta\ngamma\ndelta", "\n")
right := strings.Split("alpha\nbeta changed\ngamma\ndelta\nepsilon", "\n")
operations := diffLines(left, right)
fmt.Print(render(operations))
added, removed := 0, 0
for _, operation := range operations {
switch operation.Kind {
case '+':
added++
case '-':
removed++
}
}
if added != 2 || removed != 1 {
panic("unexpected diff counts")
}
}
How it works
Diff Text Lines with a Longest Common Subsequence keeps everything in the functions diffLines, render and main.
Repetition is confined to for index := range table — the rest of the file is straight-line.
Using it
The main signature reads func diffLines(left, right []string) []Op
The sequence diffLines and render lives in main; running the file triggers it.
Key statements
operations = append(operations, Op{'+', right[j]})
operations = append(operations, Op{' ', left[i]})
operations = append(operations, Op{'-', left[i]})
Interactive Tools for This Topic
Date Difference CalculatorCalculate the number of days, weeks, whole months, and years between two ISO dates, computed in UTC.Diff Ignoring WhitespaceCompare two blocks of text line by line and separate true differences from lines that differ only in spacing, indentation, or line endings.Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.
FAQ
- How do you run Diff Text Lines with a Longest Common Subsequence?
- Nothing to install first — run the file and the program does the rest.
Report a problem with this exampleOpens a pre-filled issue on GitHub.