intern.go
4.5 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package msgpack
import (
"encoding/binary"
"errors"
"fmt"
"math"
"reflect"
"github.com/vmihailenco/msgpack/v5/codes"
)
var internStringExtID int8 = -128
var errUnexpectedCode = errors.New("msgpack: unexpected code")
func encodeInternInterfaceValue(e *Encoder, v reflect.Value) error {
if v.IsNil() {
return e.EncodeNil()
}
v = v.Elem()
if v.Kind() == reflect.String {
return encodeInternStringValue(e, v)
}
return e.EncodeValue(v)
}
func encodeInternStringValue(e *Encoder, v reflect.Value) error {
s := v.String()
if s != "" {
if idx, ok := e.intern[s]; ok {
return e.internStringIndex(idx)
}
if e.intern == nil {
e.intern = make(map[string]int)
}
idx := len(e.intern)
e.intern[s] = idx
}
return e.EncodeString(s)
}
func (e *Encoder) internStringIndex(idx int) error {
if idx < math.MaxUint8 {
if err := e.writeCode(codes.FixExt1); err != nil {
return err
}
if err := e.w.WriteByte(byte(internStringExtID)); err != nil {
return err
}
return e.w.WriteByte(byte(idx))
}
if idx < math.MaxUint16 {
if err := e.writeCode(codes.FixExt2); err != nil {
return err
}
if err := e.w.WriteByte(byte(internStringExtID)); err != nil {
return err
}
if err := e.w.WriteByte(byte(idx >> 8)); err != nil {
return err
}
return e.w.WriteByte(byte(idx))
}
if int64(idx) < math.MaxUint32 {
if err := e.writeCode(codes.FixExt4); err != nil {
return err
}
if err := e.w.WriteByte(byte(internStringExtID)); err != nil {
return err
}
if err := e.w.WriteByte(byte(idx >> 24)); err != nil {
return err
}
if err := e.w.WriteByte(byte(idx >> 16)); err != nil {
return err
}
if err := e.w.WriteByte(byte(idx >> 8)); err != nil {
return err
}
return e.w.WriteByte(byte(idx))
}
return fmt.Errorf("msgpack: intern string index=%d is too large", idx)
}
//------------------------------------------------------------------------------
func decodeInternInterfaceValue(d *Decoder, v reflect.Value) error {
c, err := d.readCode()
if err != nil {
return err
}
s, err := d.internString(c)
if err == nil {
v.Set(reflect.ValueOf(s))
return nil
}
if err != nil && err != errUnexpectedCode {
return err
}
if err := d.s.UnreadByte(); err != nil {
return err
}
return decodeInterfaceValue(d, v)
}
func decodeInternStringValue(d *Decoder, v reflect.Value) error {
c, err := d.readCode()
if err != nil {
return err
}
s, err := d.internString(c)
if err != nil {
if err == errUnexpectedCode {
return fmt.Errorf("msgpack: invalid code=%x decoding intern string", c)
}
return err
}
v.SetString(s)
return nil
}
func (d *Decoder) internString(c codes.Code) (string, error) {
if codes.IsFixedString(c) {
n := int(c & codes.FixedStrMask)
return d.internStringWithLen(n)
}
switch c {
case codes.FixExt1, codes.FixExt2, codes.FixExt4:
typeID, length, err := d.extHeader(c)
if err != nil {
return "", err
}
if typeID != internStringExtID {
err := fmt.Errorf("msgpack: got ext type=%d, wanted %d",
typeID, internStringExtID)
return "", err
}
idx, err := d.internStringIndex(length)
if err != nil {
return "", err
}
return d.internStringAtIndex(idx)
case codes.Str8, codes.Bin8:
n, err := d.uint8()
if err != nil {
return "", err
}
return d.internStringWithLen(int(n))
case codes.Str16, codes.Bin16:
n, err := d.uint16()
if err != nil {
return "", err
}
return d.internStringWithLen(int(n))
case codes.Str32, codes.Bin32:
n, err := d.uint32()
if err != nil {
return "", err
}
return d.internStringWithLen(int(n))
}
return "", errUnexpectedCode
}
func (d *Decoder) internStringIndex(length int) (int, error) {
switch length {
case 1:
c, err := d.s.ReadByte()
if err != nil {
return 0, err
}
return int(c), nil
case 2:
b, err := d.readN(2)
if err != nil {
return 0, err
}
n := binary.BigEndian.Uint16(b)
return int(n), nil
case 4:
b, err := d.readN(4)
if err != nil {
return 0, err
}
n := binary.BigEndian.Uint32(b)
return int(n), nil
}
err := fmt.Errorf("msgpack: unsupported intern string index length=%d", length)
return 0, err
}
func (d *Decoder) internStringAtIndex(idx int) (string, error) {
if idx >= len(d.intern) {
err := fmt.Errorf("msgpack: intern string with index=%d does not exist", idx)
return "", err
}
return d.intern[idx], nil
}
func (d *Decoder) internStringWithLen(n int) (string, error) {
if n <= 0 {
return "", nil
}
s, err := d.stringWithLen(n)
if err != nil {
return "", err
}
d.intern = append(d.intern, s)
return s, nil
}