-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.go
60 lines (48 loc) · 1.17 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package enkodo
import (
"bytes"
"fmt"
"io"
"unsafe"
)
const notEnoughBytesLayout = "not enough bytes available to decode <%T>, needed %d and has an available %d"
func getStringBytes(str *string) *[]byte {
return ((*[]byte)(unsafe.Pointer(str)))
}
func getStringFromBytes(bs []byte) string {
return *((*string)(unsafe.Pointer(&bs)))
}
// Marshal will encode a value
func Marshal(v Encodee) (bs []byte, err error) {
return MarshalAppend(v, nil)
}
// MarshalAppend will encode a value to a provided slice
func MarshalAppend(v Encodee, buffer []byte) (bs []byte, err error) {
enc := newEncoder(nil)
enc.bs = buffer
if err = enc.Encode(v); err != nil {
return
}
bs = enc.bs
return
}
// Unmarshal will decode a value
func Unmarshal(bs []byte, v Decodee) (err error) {
dec := newDecoder(bytes.NewReader(bs))
return dec.Decode(v)
}
func newNotEnoughBytesError(target interface{}, needed, remaining int) (err error) {
err = fmt.Errorf(notEnoughBytesLayout, target, needed, remaining)
return
}
type reader interface {
io.Reader
io.ByteReader
}
func expandSlice(bs *[]byte, sz int) {
if *bs != nil && cap(*bs) >= sz {
*bs = (*bs)[:sz]
return
}
*bs = make([]byte, sz)
}