-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplice.go
66 lines (65 loc) · 2.13 KB
/
splice.go
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
// Package splice is an implementation of JavaScript's array.splice function for []T.
package splice
// Splice modifies the source slice by deleting delete amount of items starting with index and replacing them with
// optional item(s). It returns all the elements that have been removed, nil for zero removed items.
//
// Splice emulates JavaScript's array.splice function for type T
//
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
//
// start: The index at which to start changing the slice.
//
// If greater than the length of the slice, start will be set to the length of the slice.
// If negative, it will begin that many elements from the end of the slice.
//
// delete:
//
// An integer indicating the number of elements in the slice to remove from start.
// It can't be optional in Go, so 0 will act as its absence.
//
// item(s):
//
// The elements to add to the slice, beginning from start.
// If you do not specify any elements, splice() will only remove elements from the slice.
//
// Differences from the JavaScript version:
//
// * Only item(s) arguments may be omitted. The one integer argument call that only trims the source from start
// to the end of the source can be emulated using Splice(source, index, len(source)).
//
// * No support for undefined elements or indices.
func Splice[T any](source *[]T, start int, delete int, item ...T) (removed []T) {
if start > len(*source) {
start = len(*source)
}
if start < 0 {
start = len(*source) + start
}
if start < 0 {
start = 0
}
if delete < 0 {
delete = 0
}
if delete > 0 {
for i := 0; i < delete; i++ {
if i+start < len(*source) {
removed = append(removed, (*source)[i+start])
}
}
}
delete = len(removed) // Adjust to actual delete count
grow := len(item) - delete
switch {
case grow > 0: // So we grow
*source = append(*source, make([]T, grow)...)
copy((*source)[start+delete+grow:], (*source)[start+delete:])
case grow < 0: // So we shrink
from := start + len(item)
to := start + delete
copy((*source)[from:], (*source)[to:])
*source = (*source)[:len(*source)+grow]
}
copy((*source)[start:], item)
return
}