-
Notifications
You must be signed in to change notification settings - Fork 23
/
encbase.go
107 lines (99 loc) · 4.08 KB
/
encbase.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
package gotiny
import (
"time"
"unsafe"
)
func (e *Encoder) encBool(v bool) {
if e.boolBit == 0 {
e.boolPos = len(e.buf)
e.buf = append(e.buf, 0)
e.boolBit = 1
}
if v {
e.buf[e.boolPos] |= e.boolBit
}
e.boolBit <<= 1
}
func (e *Encoder) encUint64(v uint64) {
switch {
case v < 1<<7:
e.buf = append(e.buf, byte(v))
case v < 1<<14:
e.buf = append(e.buf, byte(v)|0x80, byte(v>>7))
case v < 1<<21:
e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14))
case v < 1<<28:
e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14)|0x80, byte(v>>21))
case v < 1<<35:
e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14)|0x80, byte(v>>21)|0x80, byte(v>>28))
case v < 1<<42:
e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14)|0x80, byte(v>>21)|0x80, byte(v>>28)|0x80, byte(v>>35))
case v < 1<<49:
e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14)|0x80, byte(v>>21)|0x80, byte(v>>28)|0x80, byte(v>>35)|0x80, byte(v>>42))
case v < 1<<56:
e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14)|0x80, byte(v>>21)|0x80, byte(v>>28)|0x80, byte(v>>35)|0x80, byte(v>>42)|0x80, byte(v>>49))
default:
e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14)|0x80, byte(v>>21)|0x80, byte(v>>28)|0x80, byte(v>>35)|0x80, byte(v>>42)|0x80, byte(v>>49)|0x80, byte(v>>56))
}
}
func (e *Encoder) encUint16(v uint16) {
if v < 1<<7 {
e.buf = append(e.buf, byte(v))
} else if v < 1<<14 {
e.buf = append(e.buf, byte(v)|0x80, byte(v>>7))
} else {
e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14))
}
}
func (e *Encoder) encUint32(v uint32) {
switch {
case v < 1<<7:
e.buf = append(e.buf, byte(v))
case v < 1<<14:
e.buf = append(e.buf, byte(v)|0x80, byte(v>>7))
case v < 1<<21:
e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14))
case v < 1<<28:
e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14)|0x80, byte(v>>21))
default:
e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14)|0x80, byte(v>>21)|0x80, byte(v>>28))
}
}
func (e *Encoder) encLength(v int) { e.encUint32(uint32(v)) }
func (e *Encoder) encString(s string) { e.encUint32(uint32(len(s))); e.buf = append(e.buf, s...) }
func (e *Encoder) encIsNotNil(v bool) { e.encBool(v) }
func encIgnore(*Encoder, unsafe.Pointer) {}
func encBool(e *Encoder, p unsafe.Pointer) { e.encBool(*(*bool)(p)) }
func encInt(e *Encoder, p unsafe.Pointer) { e.encUint64(int64ToUint64(int64(*(*int)(p)))) }
func encInt8(e *Encoder, p unsafe.Pointer) { e.buf = append(e.buf, *(*uint8)(p)) }
func encInt16(e *Encoder, p unsafe.Pointer) { e.encUint16(int16ToUint16(*(*int16)(p))) }
func encInt32(e *Encoder, p unsafe.Pointer) { e.encUint32(int32ToUint32(*(*int32)(p))) }
func encInt64(e *Encoder, p unsafe.Pointer) { e.encUint64(int64ToUint64(*(*int64)(p))) }
func encUint8(e *Encoder, p unsafe.Pointer) { e.buf = append(e.buf, *(*uint8)(p)) }
func encUint16(e *Encoder, p unsafe.Pointer) { e.encUint16(*(*uint16)(p)) }
func encUint32(e *Encoder, p unsafe.Pointer) { e.encUint32(*(*uint32)(p)) }
func encUint64(e *Encoder, p unsafe.Pointer) { e.encUint64(uint64(*(*uint64)(p))) }
func encUint(e *Encoder, p unsafe.Pointer) { e.encUint64(uint64(*(*uint)(p))) }
func encUintptr(e *Encoder, p unsafe.Pointer) { e.encUint64(uint64(*(*uintptr)(p))) }
func encFloat32(e *Encoder, p unsafe.Pointer) { e.encUint32(float32ToUint32(p)) }
func encFloat64(e *Encoder, p unsafe.Pointer) { e.encUint64(float64ToUint64(p)) }
func encString(e *Encoder, p unsafe.Pointer) {
s := *(*string)(p)
e.encUint32(uint32(len(s)))
e.buf = append(e.buf, s...)
}
func encTime(e *Encoder, p unsafe.Pointer) { e.encUint64(uint64((*time.Time)(p).UnixNano())) }
func encComplex64(e *Encoder, p unsafe.Pointer) { e.encUint64(*(*uint64)(p)) }
func encComplex128(e *Encoder, p unsafe.Pointer) {
e.encUint64(*(*uint64)(p))
e.encUint64(*(*uint64)(unsafe.Add(p, 8)))
}
func encBytes(e *Encoder, p unsafe.Pointer) {
isNotNil := !isNil(p)
e.encIsNotNil(isNotNil)
if isNotNil {
buf := *(*[]byte)(p)
e.encLength(len(buf))
e.buf = append(e.buf, buf...)
}
}