-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.go
148 lines (129 loc) · 3.54 KB
/
diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>.
// Package diff implements coloured diffs on a word per word basis.
package diff
const (
diffNone = iota
diffAdded
diffRemoved
)
type diff struct {
ignoreWhitespace bool
}
type component struct {
value string
status int
}
type path struct {
newPos int
comp []component
}
func clonePath(p *path) *path {
newp := path{
newPos: p.newPos,
comp: make([]component, len(p.comp)),
}
copy(newp.comp, p.comp)
return &newp
}
func (d *diff) extractCommon(basePath *path, new, old []string, diagonalPath int) int {
newPos := basePath.newPos
oldPos := newPos - diagonalPath
for newPos+1 < len(new) && oldPos+1 < len(old) && d.equals(new[newPos+1], old[oldPos+1]) {
newPos++
oldPos++
d.pushComponent(&basePath.comp, new[newPos], diffNone)
}
basePath.newPos = newPos
return oldPos
}
func (d *diff) equals(a, b string) bool {
if d.ignoreWhitespace &&
allWhitespace(a) &&
allWhitespace(b) {
return true
}
return a == b
}
func (d *diff) pushComponent(comp *[]component, value string, status int) {
if len(*comp) > 0 {
last := (*comp)[len(*comp)-1]
if last.status == status {
(*comp)[len(*comp)-1] = component{
value: last.value + value,
status: status,
}
return
}
}
*comp = append(*comp, component{
value: value,
status: status,
})
}
func (d *diff) diff(a, b string) []component {
if a == b {
return []component{{value: b}}
}
if b == "" {
return []component{{value: a, status: diffRemoved}}
}
if a == "" {
return []component{{value: b, status: diffAdded}}
}
old := tokenize(a)
new := tokenize(b)
maxEditLength := len(old) + len(new)
bestPath := map[int]*path{
0: &path{newPos: -1},
}
oldPos := d.extractCommon(bestPath[0], new, old, 0)
if bestPath[0].newPos+1 >= len(new) && oldPos+1 >= len(old) {
return bestPath[0].comp
}
for editLength := 1; editLength <= maxEditLength; editLength++ {
for diagonalPath := -1 * editLength; diagonalPath <= editLength; diagonalPath += 2 {
addPath := bestPath[diagonalPath-1]
removePath := bestPath[diagonalPath+1]
oldPos = 0
if removePath != nil {
oldPos = removePath.newPos
}
oldPos -= diagonalPath
if addPath != nil {
delete(bestPath, diagonalPath-1)
}
canAdd := addPath != nil && addPath.newPos+1 < len(new)
canRemove := removePath != nil && 0 <= oldPos && oldPos < len(old)
if !canAdd && !canRemove {
delete(bestPath, diagonalPath)
continue
}
var basePath *path
if !canAdd || (canRemove && addPath.newPos < removePath.newPos) {
basePath = clonePath(removePath)
d.pushComponent(&basePath.comp, old[oldPos], diffRemoved)
} else {
basePath = clonePath(addPath)
basePath.newPos++
d.pushComponent(&basePath.comp, new[basePath.newPos], diffAdded)
}
oldPos = d.extractCommon(basePath, new, old, diagonalPath)
if basePath.newPos+1 >= len(new) && oldPos+1 >= len(old) {
return basePath.comp
}
bestPath[diagonalPath] = basePath
}
}
return nil
}