MCPcopy Create free account
hub / github.com/sergi/go-diff / diffLineMode

Method diffLineMode

diffmatchpatch/diff.go:196–251  ·  view source on GitHub ↗

diffLineMode does a quick line-level diff on both []runes, then rediff the parts for greater accuracy. This speedup can produce non-minimal diffs.

(text1, text2 []rune, deadline time.Time)

Source from the content-addressed store, hash-verified

194
195// diffLineMode does a quick line-level diff on both []runes, then rediff the parts for greater accuracy. This speedup can produce non-minimal diffs.
196func (dmp *DiffMatchPatch) diffLineMode(text1, text2 []rune, deadline time.Time) []Diff {
197 // Scan the text on a line-by-line basis first.
198 text1, text2, linearray := dmp.DiffLinesToRunes(string(text1), string(text2))
199
200 diffs := dmp.diffMainRunes(text1, text2, false, deadline)
201
202 // Convert the diff back to original text.
203 diffs = dmp.DiffCharsToLines(diffs, linearray)
204 // Eliminate freak matches (e.g. blank lines)
205 diffs = dmp.DiffCleanupSemantic(diffs)
206
207 // Rediff any replacement blocks, this time character-by-character.
208 // Add a dummy entry at the end.
209 diffs = append(diffs, Diff{DiffEqual, ""})
210
211 pointer := 0
212 countDelete := 0
213 countInsert := 0
214
215 // NOTE: Rune slices are slower than using strings in this case.
216 textDelete := ""
217 textInsert := ""
218
219 for pointer < len(diffs) {
220 switch diffs[pointer].Type {
221 case DiffInsert:
222 countInsert++
223 textInsert += diffs[pointer].Text
224 case DiffDelete:
225 countDelete++
226 textDelete += diffs[pointer].Text
227 case DiffEqual:
228 // Upon reaching an equality, check for prior redundancies.
229 if countDelete >= 1 && countInsert >= 1 {
230 // Delete the offending records and add the merged ones.
231 diffs = splice(diffs, pointer-countDelete-countInsert,
232 countDelete+countInsert)
233
234 pointer = pointer - countDelete - countInsert
235 a := dmp.diffMainRunes([]rune(textDelete), []rune(textInsert), false, deadline)
236 for j := len(a) - 1; j >= 0; j-- {
237 diffs = splice(diffs, pointer, 0, a[j])
238 }
239 pointer = pointer + len(a)
240 }
241
242 countInsert = 0
243 countDelete = 0
244 textDelete = ""
245 textInsert = ""
246 }
247 pointer++
248 }
249
250 return diffs[:len(diffs)-1] // Remove the dummy entry at the end.
251}
252
253// DiffBisect finds the 'middle snake' of a diff, split the problem in two and return the recursively constructed diff.

Callers 1

diffComputeMethod · 0.95

Calls 5

DiffLinesToRunesMethod · 0.95
diffMainRunesMethod · 0.95
DiffCharsToLinesMethod · 0.95
DiffCleanupSemanticMethod · 0.95
spliceFunction · 0.85

Tested by

no test coverage detected