Skip to content

Commit

Permalink
Merge pull request #185 from mailgun/medge/slice-remove
Browse files Browse the repository at this point in the history
slice.Remove added as a safer alternative to built-in slices.Delete
  • Loading branch information
MatthewEdge authored Dec 12, 2023
2 parents 8fb0fc9 + 8fae945 commit d6898a2
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
23 changes: 23 additions & 0 deletions slice/remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package slice

// Remove removes the given index range from the given slice, preserving element order.
// It is the safer version of slices.Delete() as it will
// not panic on invalid slice ranges. Nil and empty slices are also safe.
// Remove modifies the contents of the given slice and performs no allocations
// or copies.
func Remove[T comparable](slice []T, i, j int) []T {
// Nothing to do for emtpy slices or starting indecies outside range
if len(slice) == 0 || i > len(slice) {
return slice
}
// Prevent invalid slice indexing
if i < 0 || j > len(slice) {
return slice
}
// Removing the last element is a simple re-slice
if i == len(slice)-1 && j >= len(slice) {
return slice[:i]
}
// Note: this modifies the slice
return append(slice[:i], slice[j:]...)
}
108 changes: 108 additions & 0 deletions slice/remove_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package slice_test

import (
"fmt"
"testing"

"github.com/mailgun/holster/v4/slice"
"github.com/stretchr/testify/assert"
)

func ExampleRemove() {
s := []string{"1", "2", "3"}
i := []int{1, 2, 3}

fmt.Println(slice.Remove(s, 0, 2))
fmt.Println(slice.Remove(i, 1, 2))
// Output:
// [3]
// [1 3]
}

func TestRemove(t *testing.T) {
for _, test := range []struct {
name string
i int
j int
inSlice []int
outSlice []int
}{{
name: "empty slice",
inSlice: []int{},
outSlice: []int{},
}, {
name: "nil check",
i: 1,
j: 3,
inSlice: nil,
outSlice: nil,
}, {
name: "start index out of range should not panic or modify the slice",
i: -2,
j: 2,
inSlice: []int{2},
outSlice: []int{2},
}, {
name: "end index out of range should not panic or modify the slice",
i: 0,
j: 5,
inSlice: []int{2},
outSlice: []int{2},
}, {
name: "invalid start and end index should not panic or modify the slice",
i: 4,
j: 7,
inSlice: []int{2},
outSlice: []int{2},
}, {
name: "single value remove",
i: 0,
j: 1,
inSlice: []int{1, 2, 3},
outSlice: []int{2, 3},
}, {
name: "middle value removed",
i: 1,
j: 2,
inSlice: []int{1, 2, 3},
outSlice: []int{1, 3},
}, {
name: "last value removed",
i: 2,
j: 3,
inSlice: []int{1, 2, 3},
outSlice: []int{1, 2},
}, {
name: "multi-value remove",
i: 1,
j: 3,
inSlice: []int{1, 2, 3},
outSlice: []int{1},
}, {
name: "end of slice gut check for invalid ending index",
i: 2,
j: 5, // This index being out of bounds shouldn't matter
inSlice: []int{1, 2, 3},
outSlice: []int{1, 2},
}} {
t.Run(test.name, func(t *testing.T) {
got := slice.Remove(test.inSlice, test.i, test.j)
for i := range test.outSlice {
assert.Equal(t, test.outSlice[i], got[i])
}
})
}
}

var out []string

func BenchmarkRemove(b *testing.B) {
o := []string{}
in := []string{"localhost", "mg.example.com", "testlabs.io", "localhost", "m.example.com.br", "localhost", "localhost"}

b.ResetTimer()
for i := 0; i < b.N; i++ {
o = slice.Remove(in, 2, 4)
}
out = o
}

0 comments on commit d6898a2

Please sign in to comment.