forked from niubaoshu/gotiny
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
195 lines (171 loc) · 5.22 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package gotiny
import (
"encoding"
"encoding/gob"
"reflect"
"strings"
"unsafe"
)
const (
ptr1Size = 4 << (^uintptr(0) >> 63) // unsafe.Sizeof(uintptr(0)) but an ideal const
)
func float64ToUint64(v unsafe.Pointer) uint64 {
return reverse64Byte(*(*uint64)(v))
}
func uint64ToFloat64(u uint64) float64 {
u = reverse64Byte(u)
return *((*float64)(unsafe.Pointer(&u)))
}
func reverse64Byte(u uint64) uint64 {
u = (u << 32) | (u >> 32)
u = ((u << 16) & 0xFFFF0000FFFF0000) | ((u >> 16) & 0xFFFF0000FFFF)
u = ((u << 8) & 0xFF00FF00FF00FF00) | ((u >> 8) & 0xFF00FF00FF00FF)
return u
}
func float32ToUint32(v unsafe.Pointer) uint32 {
return reverse32Byte(*(*uint32)(v))
}
func uint32ToFloat32(u uint32) float32 {
u = reverse32Byte(u)
return *((*float32)(unsafe.Pointer(&u)))
}
func reverse32Byte(u uint32) uint32 {
u = (u << 16) | (u >> 16)
return ((u << 8) & 0xFF00FF00) | ((u >> 8) & 0xFF00FF)
}
// int -5 -4 -3 -2 -1 0 1 2 3 4 5 6
// uint 9 7 5 3 1 0 2 4 6 8 10 12
func int64ToUint64(v int64) uint64 {
return uint64((v << 1) ^ (v >> 63))
}
// uint 9 7 5 3 1 0 2 4 6 8 10 12
// int -5 -4 -3 -2 -1 0 1 2 3 4 5 6
func uint64ToInt64(u uint64) int64 {
v := int64(u)
return (-(v & 1)) ^ (v>>1)&0x7FFFFFFFFFFFFFFF
}
// int -5 -4 -3 -2 -1 0 1 2 3 4 5 6
// uint 9 7 5 3 1 0 2 4 6 8 10 12
func int32ToUint32(v int32) uint32 {
return uint32((v << 1) ^ (v >> 31))
}
// uint 9 7 5 3 1 0 2 4 6 8 10 12
// int -5 -4 -3 -2 -1 0 1 2 3 4 5 6
func uint32ToInt32(u uint32) int32 {
v := int32(u)
return (-(v & 1)) ^ (v>>1)&0x7FFFFFFF
}
// int -5 -4 -3 -2 -1 0 1 2 3 4 5 6
// uint 9 7 5 3 1 0 2 4 6 8 10 12
func int16ToUint16(v int16) uint16 {
return uint16((v << 1) ^ (v >> 15))
}
// uint 9 7 5 3 1 0 2 4 6 8 10 12
// int -5 -4 -3 -2 -1 0 1 2 3 4 5 6
func uint16ToInt16(u uint16) int16 {
v := int16(u)
return (-(v & 1)) ^ (v>>1)&0x7FFF
}
func isNil(p unsafe.Pointer) bool {
return *(*unsafe.Pointer)(p) == nil
}
type gobInter interface {
gob.GobEncoder
gob.GobDecoder
}
type binInter interface {
encoding.BinaryMarshaler
encoding.BinaryUnmarshaler
}
// GoTinySerializer 只应该由指针来实现该接口 This interface should only be implemented by
// pointers
type GoTinySerializer interface {
// GotinyEncode 编码方法,将对象的序列化结果append到入参数并返回,方法不应该修改入参数值原有的值
//
// Encoding method, append the serialization result of the object to the
// input parameter and return it, the method should not modify the
// original value of the input parameter value
GotinyEncode([]byte) []byte
// GotinyDecode 解码方法,将入参解码到对象里并返回使用的长度。方法从入参的第0个字节开始使用,并且不应该修改入参中的任何数据
//
// The decoding method, which decodes the input parameters into the
// object and returns the length used. The method is used from the 0th
// byte of the input parameter and should not modify any data in the
// input parameter
GotinyDecode([]byte) int
}
func implementOtherSerializer(rt reflect.Type) (encEng encEng, decEng decEng) {
rtNil := reflect.Zero(reflect.PtrTo(rt)).Interface()
if _, ok := rtNil.(GoTinySerializer); ok {
encEng = func(e *Encoder, p unsafe.Pointer) {
e.buf = reflect.NewAt(rt, p).Interface().(GoTinySerializer).GotinyEncode(e.buf)
}
decEng = func(d *Decoder, p unsafe.Pointer) {
d.index += reflect.NewAt(rt, p).Interface().(GoTinySerializer).GotinyDecode(d.buf[d.index:])
}
return
}
if _, ok := rtNil.(binInter); ok {
encEng = func(e *Encoder, p unsafe.Pointer) {
buf, err := reflect.NewAt(rt, p).Interface().(encoding.BinaryMarshaler).MarshalBinary()
if err != nil {
panic(err)
}
e.encLength(len(buf))
e.buf = append(e.buf, buf...)
}
decEng = func(d *Decoder, p unsafe.Pointer) {
length := d.decLength()
start := d.index
d.index += length
if err := reflect.NewAt(rt, p).Interface().(encoding.BinaryUnmarshaler).UnmarshalBinary(d.buf[start:d.index]); err != nil {
panic(err)
}
}
return
}
if _, ok := rtNil.(gobInter); ok {
encEng = func(e *Encoder, p unsafe.Pointer) {
buf, err := reflect.NewAt(rt, p).Interface().(gob.GobEncoder).GobEncode()
if err != nil {
panic(err)
}
e.encLength(len(buf))
e.buf = append(e.buf, buf...)
}
decEng = func(d *Decoder, p unsafe.Pointer) {
length := d.decLength()
start := d.index
d.index += length
if err := reflect.NewAt(rt, p).Interface().(gob.GobDecoder).GobDecode(d.buf[start:d.index]); err != nil {
panic(err)
}
}
}
return
}
// rt.kind is reflect.struct
func getFieldType(rt reflect.Type, baseOff uintptr) (fields []reflect.Type, offs []uintptr) {
for i := 0; i < rt.NumField(); i++ {
field := rt.Field(i)
if ignoreField(field) {
continue
}
ft := field.Type
if ft.Kind() == reflect.Struct {
if _, engine := implementOtherSerializer(ft); engine == nil {
fFields, fOffs := getFieldType(ft, field.Offset+baseOff)
fields = append(fields, fFields...)
offs = append(offs, fOffs...)
continue
}
}
fields = append(fields, ft)
offs = append(offs, field.Offset+baseOff)
}
return
}
func ignoreField(field reflect.StructField) bool {
tinyTag, ok := field.Tag.Lookup("gotiny")
return ok && strings.TrimSpace(tinyTag) == "-"
}