Skip to content

Commit

Permalink
fix: fix Copy documentation and implementation
Browse files Browse the repository at this point in the history
Copy should not create slices bigger than original. Fixed that.

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Oct 17, 2022
1 parent 521f737 commit b3b6db8
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion slices/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,16 @@ func Contains[T any](s []T, fn func(T) bool) bool {
return IndexFunc(s, fn) >= 0
}

// Copy returns a slice of V with the last n elements removed.
// Copy copies first n elements. If n is greater than the length of the slice, it will copy the whole slice.
func Copy[S ~[]V, V any](s S, n int) S {
if s == nil {
return nil
}

if n > len(s) {
n = len(s)
}

result := make([]V, n)
copy(result, s)

Expand Down

0 comments on commit b3b6db8

Please sign in to comment.