-
Notifications
You must be signed in to change notification settings - Fork 16
/
raw.go
37 lines (30 loc) · 939 Bytes
/
raw.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
/*
Package raw provides utilities for mapping raw Go structs to byte slices.
*/
package raw
import (
"unsafe"
)
// String represents an offset and pointer to a string in a byte slice.
type String struct {
Offset uint16
Length uint16
}
// Encode writes a string to a byte slice and updates the offset/length.
func (s *String) Encode(str string, value *[]byte) {
s.Offset = uint16(len(*value))
s.Length = uint16(len(str))
*value = append(*value, []byte(str)...)
}
// Bytes returns a byte slice pointing to the string's contents.
func (s *String) Bytes(value []byte) []byte {
return (*[0xFFFF]byte)(unsafe.Pointer(&value[s.Offset]))[:s.Length]
}
// String returns a Go string of the string value from an encoded byte slice.
func (s *String) String(value []byte) string {
return string(s.Bytes(value))
}
// Time is a marker type for time.Time.
type Time int64
// Duration is a marker type for time.Duration.
type Duration int64