-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.go
41 lines (35 loc) · 918 Bytes
/
map.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
package enc
import (
"fmt"
"math"
"github.com/shamaton/msgpack/v2/def"
)
// CalcMapLength checks value and returns data size that need.
func (e *Encoder) CalcMapLength(l int) (int, error) {
ret := def.Byte1
if l <= 0x0f {
// do nothing
} else if l <= math.MaxUint16 {
ret += def.Byte2
} else if uint(l) <= math.MaxUint32 {
ret += def.Byte4
} else {
// not supported error
return 0, fmt.Errorf("not support this map length : %d", l)
}
return ret, nil
}
// WriteMapLength sets the contents of l to the buffer.
func (e *Encoder) WriteMapLength(l int, offset int) int {
// format
if l <= 0x0f {
offset = e.setByte1Int(def.FixMap+l, offset)
} else if l <= math.MaxUint16 {
offset = e.setByte1Int(def.Map16, offset)
offset = e.setByte2Int(l, offset)
} else if uint(l) <= math.MaxUint32 {
offset = e.setByte1Int(def.Map32, offset)
offset = e.setByte4Int(l, offset)
}
return offset
}