This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexcept.go
68 lines (63 loc) · 1.48 KB
/
except.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
67
68
package slices
import (
"unsafe"
)
type exceptFn func(sptr, optr unsafe.Pointer) interface{}
// Except returns a slice with the elements of slice that are not in other.
// When the slices different, slice is returned.
func Except(slice, other interface{}) interface{} {
fn, ok := exceptOf(slice, other)
if fn == nil {
panic("slice is not a supported slice type")
}
if !ok {
panic("other is not the same type as slice")
}
sptr := noescape(ptrOf(slice))
optr := noescape(ptrOf(other))
return fn(sptr, optr)
}
func exceptOf(slice, other interface{}) (exceptFn, bool) {
switch slice.(type) {
case []string:
_, ok := other.([]string)
return stringExcept, ok
case []int:
_, ok := other.([]int)
return intExcept, ok
case []int8:
_, ok := other.([]int8)
return int8Except, ok
case []int16:
_, ok := other.([]int16)
return int16Except, ok
case []int32:
_, ok := other.([]int32)
return int32Except, ok
case []int64:
_, ok := other.([]int64)
return int64Except, ok
case []uint:
_, ok := other.([]uint)
return uintExcept, ok
case []uint8:
_, ok := other.([]uint8)
return uint8Except, ok
case []uint16:
_, ok := other.([]uint16)
return uint16Except, ok
case []uint32:
_, ok := other.([]uint32)
return uint32Except, ok
case []uint64:
_, ok := other.([]uint64)
return uint64Except, ok
case []float32:
_, ok := other.([]float32)
return float32Except, ok
case []float64:
_, ok := other.([]float64)
return float64Except, ok
}
return nil, false
}