From 28317a168326e4ec3d348980c0d8843a881c01fe Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 19 Jan 2022 20:41:46 +0800 Subject: [PATCH] feat: remove ConvertSlice func --- README.md | 1 - README_zh-CN.md | 1 - slice/slice.go | 21 --------------------- slice/slice_test.go | 14 +------------- 4 files changed, 1 insertion(+), 36 deletions(-) diff --git a/README.md b/README.md index 9aa3765..3dfee18 100644 --- a/README.md +++ b/README.md @@ -434,7 +434,6 @@ func main() { func Contain(slice interface{}, value interface{}) bool //check if the value is in the slice or not func ContainSubSlice(slice interface{}, subslice interface{}) bool //check if the slice contain subslice or not func Chunk(slice []interface{}, size int) [][]interface{} //creates an slice of elements split into groups the length of `size` -func ConvertSlice(originalSlice interface{}, newSliceType reflect.Type) interface{} //convert originalSlice to newSliceType func Difference(slice1, slice2 interface{}) interface{} //creates an slice of whose element not included in the other given slice func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error) //delete the element of slice from start index to end index - 1 func Drop(slice interface{}, n int) interface{} //creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0 diff --git a/README_zh-CN.md b/README_zh-CN.md index fdc9a70..b9a653b 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -434,7 +434,6 @@ func main() { func Contain(slice interface{}, value interface{}) bool //判断slice是否包含value func ContainSubSlice(slice interface{}, subslice interface{}) bool //判断slice是否包含subslice func Chunk(slice []interface{}, size int) [][]interface{} //均分slice -func ConvertSlice(originalSlice interface{}, newSliceType reflect.Type) interface{} //将originalSlice转换为 newSliceType func Difference(slice1, slice2 interface{}) interface{} //返回切片,其元素在slice1中,不在slice2中 func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error) //删除切片中start到end位置的值 func Drop(slice interface{}, n int) interface{} //创建一个新切片,当n大于0时删除原切片前n个元素,当n小于0时删除原切片后n个元素 diff --git a/slice/slice.go b/slice/slice.go index 3af5f37..ea9e1f8 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -12,7 +12,6 @@ import ( "reflect" "sort" "strings" - "unsafe" ) // Contain check if the value is in the iterable type or not @@ -427,26 +426,6 @@ func IntSlice(slice interface{}) []int { return out } -// ConvertSlice convert original slice to new data type element of slice. -func ConvertSlice(originalSlice interface{}, newSliceType reflect.Type) interface{} { - sv := sliceValue(originalSlice) - if newSliceType.Kind() != reflect.Slice { - panic(fmt.Sprintf("Invalid newSliceType(non-slice type of type %T)", newSliceType)) - } - - newSlice := reflect.New(newSliceType) - - hdr := (*reflect.SliceHeader)(unsafe.Pointer(newSlice.Pointer())) - - var newElemSize = int(sv.Type().Elem().Size()) / int(newSliceType.Elem().Size()) - - hdr.Cap = sv.Cap() * newElemSize - hdr.Len = sv.Len() * newElemSize - hdr.Data = sv.Pointer() - - return newSlice.Elem().Interface() -} - // DeleteByIndex delete the element of slice from start index to end index - 1. // Delete i: s = append(s[:i], s[i+1:]...) // Delete i to j: s = append(s[:i], s[j:]...) diff --git a/slice/slice_test.go b/slice/slice_test.go index cb8763b..ea452d8 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -74,19 +74,6 @@ func TestChunk(t *testing.T) { assert.Equal(r5, Chunk(InterfaceSlice(arr), 5)) } -func TestConvertSlice(t *testing.T) { - //t1 := []string{"1","2"} - //aInt, _ := strconv.ParseInt("1", 10, 64) - //bInt, _ := strconv.ParseInt("2", 10, 64) - //expected :=[]int64{aInt, bInt} - // - //a := ConvertSlice(t1, reflect.TypeOf(expected)) - //if !reflect.DeepEqual(a, expected) { - // utils.LogFailedTestInfo(t, "ConvertSlice", t1, expected, a) - // t.FailNow() - //} -} - func TestEvery(t *testing.T) { nums := []int{1, 2, 3, 5} isEven := func(i, num int) bool { @@ -430,6 +417,7 @@ func TestIntersection(t *testing.T) { for i := 0; i < len(res); i++ { assert.Equal(res[i], expected[i]) } + assert.IsNil(Intersection()) } func TestReverseSlice(t *testing.T) {