-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
216 lines (167 loc) · 3.86 KB
/
writer.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package protocolbytes
import "unicode/utf8"
type Buffer []byte
// Read a int8 from the buffer
func (w *Buffer) WInt8(i int8) {
p := w.growSlice(1)
(*w)[p] = byte(i)
}
// Write a int16 as a byte array
func (w *Buffer) WInt16(i int16) {
p := w.growSlice(2)
(*w)[p] = byte(i >> 8)
(*w)[p+1] = byte(i)
}
// Write a int32 as a byte array
func (w *Buffer) WInt32(i int32) {
p := w.growSlice(4)
(*w)[p] = byte(i >> 24)
(*w)[p+1] = byte(i >> 16)
(*w)[p+2] = byte(i >> 8)
(*w)[p+3] = byte(i)
}
// Write a int64 as a byte array
func (w *Buffer) WInt64(i int64) {
p := w.growSlice(8)
for n := 0; n < 8; n++ {
(*w)[p+n] = byte(i >> (56 - 8*n))
}
}
// Write a uint8 as a byte array
func (w *Buffer) WUInt8(i uint8) {
p := w.growSlice(1)
(*w)[p] = byte(i)
}
// Write a uint16 as a byte array
func (w *Buffer) WUInt16(i uint16) {
p := w.growSlice(2)
(*w)[p] = byte(i >> 8)
(*w)[p+1] = byte(i)
}
// Write a uint32 as a byte array
func (w *Buffer) WUInt32(i uint32) {
p := w.growSlice(4)
(*w)[p] = byte(i >> 24)
(*w)[p+1] = byte(i >> 16)
(*w)[p+2] = byte(i >> 8)
(*w)[p+3] = byte(i)
}
// Write a uint64 as a byte array
func (w *Buffer) WUInt64(i uint64) {
p := w.growSlice(8)
for n := 0; n < 8; n++ {
(*w)[p+n] = byte(i >> (56 - 8*n))
}
}
// Write a string as a UTF-8 encoded string prefixed with its length
// the length is the beginning byte of the string
// If the string is not UTF use WString
func (w *Buffer) WUTF(s string) {
// Each rune can take up to utf8.UTFMax bytes.
requiredCapacity := len(s)*utf8.UTFMax + 1
p := w.growSlice(requiredCapacity)
(*w)[p] = byte(len(s))
writePos := p + 1
for _, r := range s {
var buf [utf8.UTFMax]byte
n := utf8.EncodeRune(buf[:], r)
// Grow the buffer further if needed
if len(*w) < writePos+n {
w.grow(n)
}
copy((*w)[writePos:], buf[:n])
writePos += n
}
// Update the slice length to match the actual written content
*w = (*w)[:writePos]
}
// Write a string as a byte array prefixed with its length
// If the string is UTF-8 use WUTF
func (w *Buffer) WString(s string) {
p := w.growSlice(len(s) + 1)
(*w)[p] = byte(len(s))
for i, r := range s {
(*w)[p+i+1] = byte(r)
}
}
// Write a byte array prefixed with its length
func (w *Buffer) WBytes(b []byte) {
p := w.growSlice(len(b) + 1)
(*w)[p] = byte(len(b))
for i, v := range b {
(*w)[p+i+1] = v
}
}
// Write a boolean as a byte
func (w *Buffer) WBool(b bool) {
p := w.growSlice(1)
if b {
(*w)[p] = 1
} else {
(*w)[p] = 0
}
}
func (w *Buffer) WVarInt(i int32) {
p := w.growSlice(5)
for {
if (i & ^0x7f) == 0 {
(*w)[p] = byte(i)
return
}
(*w)[p] = byte(i&0x7f | 0x80)
i >>= 7
p++
}
}
func (w *Buffer) WVarLong(i int64) {
p := w.growSlice(10)
for {
if (i & ^0x7f) == 0 {
(*w)[p] = byte(i)
return
}
(*w)[p] = byte(i&0x7f | 0x80)
i >>= 7
p++
}
}
// Check if the slice is reslicebale, if so return the missing capacity
// and true, otherwise return 0 and false
func (w *Buffer) checkForReeslice(n int) (int, bool) {
c := cap((*w))
l := len((*w))
if c < l+n {
return l + n - c, true
}
return 0, false
}
// Grow expands the buffer by n bytes and returns the updated buffer.
// If the buffer is nil, it initializes it with a capacity of 64 bytes.
// If more capacity is needed, it doubles the capacity or ensures it can hold len + n bytes.
// Finally, it reslices the buffer to the new length.
func (w *Buffer) grow(n int) Buffer {
if *w == nil {
// Preallocate 64 bytes
var buf [64]byte
*w = buf[:0:64]
}
l := len(*w)
if missingCap, ok := w.checkForReeslice(n); ok {
newCap := cap(*w) * 2
if newCap < l+n {
newCap = l + missingCap + n
}
ns := make([]byte, l, newCap)
copy(ns, *w)
*w = ns
}
*w = (*w)[:l+n]
return *w
}
// Grow the slice by n bytes and return the previous length
// as index to start writing
func (w *Buffer) growSlice(n int) int {
l := len(*w)
*w = w.grow(n)
return l
}