splice removes amount elements from slice at index index, replacing them with elements.
(slice []Diff, index int, amount int, elements ...Diff)
| 44 | |
| 45 | // splice removes amount elements from slice at index index, replacing them with elements. |
| 46 | func 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. |
no outgoing calls
no test coverage detected
searching dependent graphs…