-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
46 lines (44 loc) · 935 Bytes
/
utils.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
package bloomfilter
import (
"encoding/binary"
"math"
)
// GetBytes ...
func GetBytes(arg interface{}) []byte {
switch v := arg.(type) {
case int32:
var buf [4]byte
binary.LittleEndian.PutUint32(buf[:], uint32(v))
return buf[:]
case uint32:
var buf [4]byte
binary.LittleEndian.PutUint32(buf[:], v)
return buf[:]
case int64:
var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], uint64(v))
return buf[:]
case uint64:
var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], v)
return buf[:]
case int:
if v >= math.MinInt32 && v <= math.MaxInt32 {
var buf [4]byte
binary.LittleEndian.PutUint32(buf[:], uint32(v))
return buf[:]
} else if v >= math.MinInt64 && v <= math.MaxInt64 {
var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], uint64(v))
return buf[:]
} else {
return []byte{}
}
case string:
return []byte(v)
case []byte:
return v
default:
return []byte{}
}
}