-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtoken.go
More file actions
284 lines (240 loc) · 5.96 KB
/
token.go
File metadata and controls
284 lines (240 loc) · 5.96 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package diff
import (
"context"
"strconv"
"strings"
)
// Type of token
type Type int
const (
// Newline type
Newline Type = iota
// Space type
Space
// ChunkStart type
ChunkStart
// ChunkEnd type
ChunkEnd
// SameSymbol type
SameSymbol
// SameLine type
SameLine
// AddSymbol type
AddSymbol
// AddLine type
AddLine
// DelSymbol typ
DelSymbol
// DelLine type
DelLine
// SameWords type
SameWords
// AddWords type
AddWords
// DelWords type
DelWords
// EmptyLine type
EmptyLine
)
// Token is a lazily-rendered piece of diff output.
type Token interface {
Type() Type
Build(sb *strings.Builder)
}
// Text renders a token into a string via its Build method.
func Text(t Token) string {
var sb strings.Builder
t.Build(&sb)
return sb.String()
}
// SegToken carries a single strSeg tagged with a Type.
type SegToken struct {
T Type
s strSeg
}
// Type of the token.
func (x SegToken) Type() Type { return x.T }
// Build writes the segment into sb.
func (x SegToken) Build(sb *strings.Builder) { x.s.Build(sb) }
// ConcatToken holds a run of strSegs sharing one Type. Produced by mergeRuns
// so that merging is zero-copy: parts are concatenated only at Build time.
type ConcatToken struct {
T Type
parts []strSeg
}
// Type of the token.
func (x ConcatToken) Type() Type { return x.T }
// Build writes each part into sb in order.
func (x ConcatToken) Build(sb *strings.Builder) {
for _, p := range x.parts {
p.Build(sb)
}
}
// DelGutter renders the deleted-line gutter: "<N pad Xl> <Yl+1 spaces>-".
type DelGutter struct{ N, Xl, Yl int }
// Type of the token.
func (DelGutter) Type() Type { return DelSymbol }
// Build writes the gutter into sb.
func (g DelGutter) Build(sb *strings.Builder) {
appendPad(sb, g.N, g.Xl)
sb.WriteByte(' ')
for i := 0; i <= g.Yl; i++ {
sb.WriteByte(' ')
}
sb.WriteByte('-')
}
// AddGutter renders the added-line gutter: "<Xl+1 spaces><N pad Yl> +".
type AddGutter struct{ N, Xl, Yl int }
// Type of the token.
func (AddGutter) Type() Type { return AddSymbol }
// Build writes the gutter into sb.
func (g AddGutter) Build(sb *strings.Builder) {
for i := 0; i <= g.Xl; i++ {
sb.WriteByte(' ')
}
appendPad(sb, g.N, g.Yl)
sb.WriteByte(' ')
sb.WriteByte('+')
}
// SameGutter renders the unchanged-line gutter: "<A pad Xl> <B pad Yl> ".
type SameGutter struct{ A, B, Xl, Yl int }
// Type of the token.
func (SameGutter) Type() Type { return SameSymbol }
// Build writes the gutter into sb.
func (g SameGutter) Build(sb *strings.Builder) {
appendPad(sb, g.A, g.Xl)
sb.WriteByte(' ')
appendPad(sb, g.B, g.Yl)
sb.WriteByte(' ')
sb.WriteByte(' ')
}
// digits returns the number of base-10 digits of n for n >= 0.
func digits(n int) int {
d := 1
for n >= 10 {
d++
n /= 10
}
return d
}
// appendPad writes n in base 10, zero-padded to at least width digits.
func appendPad(sb *strings.Builder, n, width int) {
for i := digits(n); i < width; i++ {
sb.WriteByte('0')
}
sb.WriteString(strconv.Itoa(n))
}
// TokenizeText text block a and b into diff tokens.
func TokenizeText(ctx context.Context, x, y string) []Token {
xls := splitLines(x)
yls := splitLines(y)
xi, yi := internLines(xls, yls)
matches := histogramDiff(ctx, xi, yi)
xl := digits(len(xls))
yl := digits(len(yls))
ts := []Token{}
i, j, mi := 0, 0, 0
for i < len(xls) || j < len(yls) {
xStop, yStop := len(xls), len(yls)
hasMatch := false
if mi < len(matches) {
xStop = matches[mi].xStart
yStop = matches[mi].yStart
hasMatch = true
}
if i < xStop {
ts = append(ts,
DelGutter{N: i + 1, Xl: xl, Yl: yl},
SegToken{T: Space, s: segSpace},
SegToken{T: DelLine, s: xls[i]},
SegToken{T: Newline, s: segNewline})
i++
} else if j < yStop {
ts = append(ts,
AddGutter{N: j + 1, Xl: xl, Yl: yl},
SegToken{T: Space, s: segSpace},
SegToken{T: AddLine, s: yls[j]},
SegToken{T: Newline, s: segNewline})
j++
} else if hasMatch {
m := matches[mi]
for k := 0; k < m.length; k++ {
ts = append(ts,
SameGutter{A: m.xStart + k + 1, B: m.yStart + k + 1, Xl: xl, Yl: yl},
SegToken{T: Space, s: segSpace},
SegToken{T: SameLine, s: xls[m.xStart+k]},
SegToken{T: Newline, s: segNewline})
}
i = m.xStart + m.length
j = m.yStart + m.length
mi++
}
}
return ts
}
// TokenizeLine two different lines
func TokenizeLine(ctx context.Context, x, y string) ([]Token, []Token) {
split := Split
val := ctx.Value(SplitKey)
if val != nil {
split = val.(func(string) []string)
}
xs := split(x)
ys := split(y)
xi, yi := intern(xs, ys)
matches := myersDiff(ctx, xi, yi)
xTokens := []Token{}
yTokens := []Token{}
i, j, mi := 0, 0, 0
for i < len(xs) || j < len(ys) {
xStop, yStop := len(xs), len(ys)
hasMatch := false
if mi < len(matches) {
xStop = matches[mi].xStart
yStop = matches[mi].yStart
hasMatch = true
}
if i < xStop {
xTokens = append(xTokens, SegToken{T: DelWords, s: seg(xs[i])})
i++
} else if j < yStop {
yTokens = append(yTokens, SegToken{T: AddWords, s: seg(ys[j])})
j++
} else if hasMatch {
m := matches[mi]
for k := 0; k < m.length; k++ {
xTokens = append(xTokens, SegToken{T: SameWords, s: seg(xs[m.xStart+k])})
yTokens = append(yTokens, SegToken{T: SameWords, s: seg(ys[m.yStart+k])})
}
i = m.xStart + m.length
j = m.yStart + m.length
mi++
}
}
return mergeRuns(xTokens), mergeRuns(yTokens)
}
// mergeRuns collapses each maximal run of same-Type tokens into a ConcatToken
// that holds the run's strSegs. Concatenation is deferred until Build time.
func mergeRuns(ts []Token) []Token {
if len(ts) < 2 {
return ts
}
out := make([]Token, 0, len(ts))
for i := 0; i < len(ts); {
start := i
for i+1 < len(ts) && ts[i+1].Type() == ts[start].Type() {
i++
}
if i == start {
out = append(out, ts[start])
} else {
parts := make([]strSeg, 0, i-start+1)
for k := start; k <= i; k++ {
parts = append(parts, ts[k].(SegToken).s)
}
out = append(out, ConcatToken{T: ts[start].Type(), parts: parts})
}
i++
}
return out
}