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

Function splice

diffmatchpatch/diff.go:46–80  ·  view source on GitHub ↗

splice removes amount elements from slice at index index, replacing them with elements.

(slice []Diff, index int, amount int, elements ...Diff)

Source from the content-addressed store, hash-verified

44
45// splice removes amount elements from slice at index index, replacing them with elements.
46func splice(slice []Diff, index int, amount int, elements ...Diff) []Diff {
47 if len(elements) == amount {
48 // Easy case: overwrite the relevant items.
49 copy(slice[index:], elements)
50 return slice
51 }
52 if len(elements) < amount {
53 // Fewer new items than old.
54 // Copy in the new items.
55 copy(slice[index:], elements)
56 // Shift the remaining items left.
57 copy(slice[index+len(elements):], slice[index+amount:])
58 // Calculate the new end of the slice.
59 end := len(slice) - amount + len(elements)
60 // Zero stranded elements at end so that they can be garbage collected.
61 tail := slice[end:]
62 for i := range tail {
63 tail[i] = Diff{}
64 }
65 return slice[:end]
66 }
67 // More new items than old.
68 // Make room in slice for new elements.
69 // There's probably an even more efficient way to do this,
70 // but this is simple and clear.
71 need := len(slice) - amount + len(elements)
72 for len(slice) < need {
73 slice = append(slice, Diff{})
74 }
75 // Shift slice elements right to make room for new elements.
76 copy(slice[index+len(elements):], slice[index+amount:])
77 // Copy in new elements.
78 copy(slice[index:], elements)
79 return slice
80}
81
82// DiffMain finds the differences between two texts.
83// If an invalid UTF-8 sequence is encountered, it will be replaced by the Unicode replacement character.

Callers 5

diffLineModeMethod · 0.85
DiffCleanupSemanticMethod · 0.85
DiffCleanupEfficiencyMethod · 0.85
DiffCleanupMergeMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…