Exemple #1
0
func SliceDelete(slice []interface{}, index int, count int) (result []interface{}) {
	switch {
	case count < 0:
		panic(errs.Format("Negative count %d not allowed", count))
	case count == 0:
		return slice
	}

	length := len(slice)
	errs.PanicIfErrIndexOutOfBounds("SliceDelete", index, length)

	if index+count > length {
		count = length - index
	}

	return append(slice[:index], slice[index+count:]...)
}
Exemple #2
0
func SliceInsert(slice []interface{}, index int, count int, value interface{}) (result []interface{}) {
	switch {
	case count < 0:
		panic(errs.Format("Negative count %d not allowed", count))
	case count == 0:
		return slice
	}

	length := len(slice)
	errs.PanicIfErrIndexOutOfBounds("SliceInsert", index, length)

	result = make([]interface{}, length+count)
	copy(result, slice[:index])
	copy(result[index+count:], slice[index:])
	for i := index; i < index+count; i++ {
		result[i] = value
	}

	return result
}