Source file
src/encoding/gob/enc_helpers.go
1
2
3
4
5
6
7 package gob
8
9 import (
10 "reflect"
11 )
12
13 var encArrayHelper = map[reflect.Kind]encHelper{
14 reflect.Bool: encBoolArray,
15 reflect.Complex64: encComplex64Array,
16 reflect.Complex128: encComplex128Array,
17 reflect.Float32: encFloat32Array,
18 reflect.Float64: encFloat64Array,
19 reflect.Int: encIntArray,
20 reflect.Int16: encInt16Array,
21 reflect.Int32: encInt32Array,
22 reflect.Int64: encInt64Array,
23 reflect.Int8: encInt8Array,
24 reflect.String: encStringArray,
25 reflect.Uint: encUintArray,
26 reflect.Uint16: encUint16Array,
27 reflect.Uint32: encUint32Array,
28 reflect.Uint64: encUint64Array,
29 reflect.Uintptr: encUintptrArray,
30 }
31
32 var encSliceHelper = map[reflect.Kind]encHelper{
33 reflect.Bool: encBoolSlice,
34 reflect.Complex64: encComplex64Slice,
35 reflect.Complex128: encComplex128Slice,
36 reflect.Float32: encFloat32Slice,
37 reflect.Float64: encFloat64Slice,
38 reflect.Int: encIntSlice,
39 reflect.Int16: encInt16Slice,
40 reflect.Int32: encInt32Slice,
41 reflect.Int64: encInt64Slice,
42 reflect.Int8: encInt8Slice,
43 reflect.String: encStringSlice,
44 reflect.Uint: encUintSlice,
45 reflect.Uint16: encUint16Slice,
46 reflect.Uint32: encUint32Slice,
47 reflect.Uint64: encUint64Slice,
48 reflect.Uintptr: encUintptrSlice,
49 }
50
51 func encBoolArray(state *encoderState, v reflect.Value) bool {
52
53 if !v.CanAddr() {
54 return false
55 }
56 return encBoolSlice(state, v.Slice(0, v.Len()))
57 }
58
59 func encBoolSlice(state *encoderState, v reflect.Value) bool {
60 slice, ok := v.Interface().([]bool)
61 if !ok {
62
63 return false
64 }
65 for _, x := range slice {
66 if x != false || state.sendZero {
67 if x {
68 state.encodeUint(1)
69 } else {
70 state.encodeUint(0)
71 }
72 }
73 }
74 return true
75 }
76
77 func encComplex64Array(state *encoderState, v reflect.Value) bool {
78
79 if !v.CanAddr() {
80 return false
81 }
82 return encComplex64Slice(state, v.Slice(0, v.Len()))
83 }
84
85 func encComplex64Slice(state *encoderState, v reflect.Value) bool {
86 slice, ok := v.Interface().([]complex64)
87 if !ok {
88
89 return false
90 }
91 for _, x := range slice {
92 if x != 0+0i || state.sendZero {
93 rpart := floatBits(float64(real(x)))
94 ipart := floatBits(float64(imag(x)))
95 state.encodeUint(rpart)
96 state.encodeUint(ipart)
97 }
98 }
99 return true
100 }
101
102 func encComplex128Array(state *encoderState, v reflect.Value) bool {
103
104 if !v.CanAddr() {
105 return false
106 }
107 return encComplex128Slice(state, v.Slice(0, v.Len()))
108 }
109
110 func encComplex128Slice(state *encoderState, v reflect.Value) bool {
111 slice, ok := v.Interface().([]complex128)
112 if !ok {
113
114 return false
115 }
116 for _, x := range slice {
117 if x != 0+0i || state.sendZero {
118 rpart := floatBits(real(x))
119 ipart := floatBits(imag(x))
120 state.encodeUint(rpart)
121 state.encodeUint(ipart)
122 }
123 }
124 return true
125 }
126
127 func encFloat32Array(state *encoderState, v reflect.Value) bool {
128
129 if !v.CanAddr() {
130 return false
131 }
132 return encFloat32Slice(state, v.Slice(0, v.Len()))
133 }
134
135 func encFloat32Slice(state *encoderState, v reflect.Value) bool {
136 slice, ok := v.Interface().([]float32)
137 if !ok {
138
139 return false
140 }
141 for _, x := range slice {
142 if x != 0 || state.sendZero {
143 bits := floatBits(float64(x))
144 state.encodeUint(bits)
145 }
146 }
147 return true
148 }
149
150 func encFloat64Array(state *encoderState, v reflect.Value) bool {
151
152 if !v.CanAddr() {
153 return false
154 }
155 return encFloat64Slice(state, v.Slice(0, v.Len()))
156 }
157
158 func encFloat64Slice(state *encoderState, v reflect.Value) bool {
159 slice, ok := v.Interface().([]float64)
160 if !ok {
161
162 return false
163 }
164 for _, x := range slice {
165 if x != 0 || state.sendZero {
166 bits := floatBits(x)
167 state.encodeUint(bits)
168 }
169 }
170 return true
171 }
172
173 func encIntArray(state *encoderState, v reflect.Value) bool {
174
175 if !v.CanAddr() {
176 return false
177 }
178 return encIntSlice(state, v.Slice(0, v.Len()))
179 }
180
181 func encIntSlice(state *encoderState, v reflect.Value) bool {
182 slice, ok := v.Interface().([]int)
183 if !ok {
184
185 return false
186 }
187 for _, x := range slice {
188 if x != 0 || state.sendZero {
189 state.encodeInt(int64(x))
190 }
191 }
192 return true
193 }
194
195 func encInt16Array(state *encoderState, v reflect.Value) bool {
196
197 if !v.CanAddr() {
198 return false
199 }
200 return encInt16Slice(state, v.Slice(0, v.Len()))
201 }
202
203 func encInt16Slice(state *encoderState, v reflect.Value) bool {
204 slice, ok := v.Interface().([]int16)
205 if !ok {
206
207 return false
208 }
209 for _, x := range slice {
210 if x != 0 || state.sendZero {
211 state.encodeInt(int64(x))
212 }
213 }
214 return true
215 }
216
217 func encInt32Array(state *encoderState, v reflect.Value) bool {
218
219 if !v.CanAddr() {
220 return false
221 }
222 return encInt32Slice(state, v.Slice(0, v.Len()))
223 }
224
225 func encInt32Slice(state *encoderState, v reflect.Value) bool {
226 slice, ok := v.Interface().([]int32)
227 if !ok {
228
229 return false
230 }
231 for _, x := range slice {
232 if x != 0 || state.sendZero {
233 state.encodeInt(int64(x))
234 }
235 }
236 return true
237 }
238
239 func encInt64Array(state *encoderState, v reflect.Value) bool {
240
241 if !v.CanAddr() {
242 return false
243 }
244 return encInt64Slice(state, v.Slice(0, v.Len()))
245 }
246
247 func encInt64Slice(state *encoderState, v reflect.Value) bool {
248 slice, ok := v.Interface().([]int64)
249 if !ok {
250
251 return false
252 }
253 for _, x := range slice {
254 if x != 0 || state.sendZero {
255 state.encodeInt(x)
256 }
257 }
258 return true
259 }
260
261 func encInt8Array(state *encoderState, v reflect.Value) bool {
262
263 if !v.CanAddr() {
264 return false
265 }
266 return encInt8Slice(state, v.Slice(0, v.Len()))
267 }
268
269 func encInt8Slice(state *encoderState, v reflect.Value) bool {
270 slice, ok := v.Interface().([]int8)
271 if !ok {
272
273 return false
274 }
275 for _, x := range slice {
276 if x != 0 || state.sendZero {
277 state.encodeInt(int64(x))
278 }
279 }
280 return true
281 }
282
283 func encStringArray(state *encoderState, v reflect.Value) bool {
284
285 if !v.CanAddr() {
286 return false
287 }
288 return encStringSlice(state, v.Slice(0, v.Len()))
289 }
290
291 func encStringSlice(state *encoderState, v reflect.Value) bool {
292 slice, ok := v.Interface().([]string)
293 if !ok {
294
295 return false
296 }
297 for _, x := range slice {
298 if x != "" || state.sendZero {
299 state.encodeUint(uint64(len(x)))
300 state.b.WriteString(x)
301 }
302 }
303 return true
304 }
305
306 func encUintArray(state *encoderState, v reflect.Value) bool {
307
308 if !v.CanAddr() {
309 return false
310 }
311 return encUintSlice(state, v.Slice(0, v.Len()))
312 }
313
314 func encUintSlice(state *encoderState, v reflect.Value) bool {
315 slice, ok := v.Interface().([]uint)
316 if !ok {
317
318 return false
319 }
320 for _, x := range slice {
321 if x != 0 || state.sendZero {
322 state.encodeUint(uint64(x))
323 }
324 }
325 return true
326 }
327
328 func encUint16Array(state *encoderState, v reflect.Value) bool {
329
330 if !v.CanAddr() {
331 return false
332 }
333 return encUint16Slice(state, v.Slice(0, v.Len()))
334 }
335
336 func encUint16Slice(state *encoderState, v reflect.Value) bool {
337 slice, ok := v.Interface().([]uint16)
338 if !ok {
339
340 return false
341 }
342 for _, x := range slice {
343 if x != 0 || state.sendZero {
344 state.encodeUint(uint64(x))
345 }
346 }
347 return true
348 }
349
350 func encUint32Array(state *encoderState, v reflect.Value) bool {
351
352 if !v.CanAddr() {
353 return false
354 }
355 return encUint32Slice(state, v.Slice(0, v.Len()))
356 }
357
358 func encUint32Slice(state *encoderState, v reflect.Value) bool {
359 slice, ok := v.Interface().([]uint32)
360 if !ok {
361
362 return false
363 }
364 for _, x := range slice {
365 if x != 0 || state.sendZero {
366 state.encodeUint(uint64(x))
367 }
368 }
369 return true
370 }
371
372 func encUint64Array(state *encoderState, v reflect.Value) bool {
373
374 if !v.CanAddr() {
375 return false
376 }
377 return encUint64Slice(state, v.Slice(0, v.Len()))
378 }
379
380 func encUint64Slice(state *encoderState, v reflect.Value) bool {
381 slice, ok := v.Interface().([]uint64)
382 if !ok {
383
384 return false
385 }
386 for _, x := range slice {
387 if x != 0 || state.sendZero {
388 state.encodeUint(x)
389 }
390 }
391 return true
392 }
393
394 func encUintptrArray(state *encoderState, v reflect.Value) bool {
395
396 if !v.CanAddr() {
397 return false
398 }
399 return encUintptrSlice(state, v.Slice(0, v.Len()))
400 }
401
402 func encUintptrSlice(state *encoderState, v reflect.Value) bool {
403 slice, ok := v.Interface().([]uintptr)
404 if !ok {
405
406 return false
407 }
408 for _, x := range slice {
409 if x != 0 || state.sendZero {
410 state.encodeUint(uint64(x))
411 }
412 }
413 return true
414 }
415
View as plain text