-
Notifications
You must be signed in to change notification settings - Fork 58
/
slice_util.go
58 lines (47 loc) · 1.84 KB
/
slice_util.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
// Copyright 2016-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
// +build !safe
package moss
import (
"reflect"
"unsafe"
)
// Uint64SliceToByteSlice gives access to []uint64 as []byte. By
// default, an efficient O(1) implementation of this function is used,
// but which requires the unsafe package. See the "safe" build tag to
// use an O(N) implementation that does not need the unsafe package.
func Uint64SliceToByteSlice(in []uint64) ([]byte, error) {
inHeader := (*reflect.SliceHeader)(unsafe.Pointer(&in))
var out []byte
outHeader := (*reflect.SliceHeader)(unsafe.Pointer(&out))
outHeader.Data = inHeader.Data
outHeader.Len = inHeader.Len * 8
outHeader.Cap = inHeader.Cap * 8
return out, nil
}
// ByteSliceToUint64Slice gives access to []byte as []uint64. By
// default, an efficient O(1) implementation of this function is used,
// but which requires the unsafe package. See the "safe" build tag to
// use an O(N) implementation that does not need the unsafe package.
func ByteSliceToUint64Slice(in []byte) ([]uint64, error) {
inHeader := (*reflect.SliceHeader)(unsafe.Pointer(&in))
var out []uint64
outHeader := (*reflect.SliceHeader)(unsafe.Pointer(&out))
outHeader.Data = inHeader.Data
outHeader.Len = inHeader.Len / 8
outHeader.Cap = outHeader.Len
return out, nil
}
// --------------------------------------------------------------
func endian() string { // See golang-nuts / how-to-tell-endian-ness-of-machine,
var x uint32 = 0x01020304
if *(*byte)(unsafe.Pointer(&x)) == 0x01 {
return "big"
}
return "little"
}