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

Method diffLineMode

diffmatchpatch/diff.go:194–249  ·  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

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