7 "github.com/vmihailenco/msgpack/codes"
10 // EncodeUint8 encodes an uint8 in 2 bytes preserving type of the number.
11 func (e *Encoder) EncodeUint8(n uint8) error {
12 return e.write1(codes.Uint8, n)
15 func (e *Encoder) encodeUint8Cond(n uint8) error {
17 return e.EncodeUint(uint64(n))
19 return e.EncodeUint8(n)
22 // EncodeUint16 encodes an uint16 in 3 bytes preserving type of the number.
23 func (e *Encoder) EncodeUint16(n uint16) error {
24 return e.write2(codes.Uint16, n)
27 func (e *Encoder) encodeUint16Cond(n uint16) error {
29 return e.EncodeUint(uint64(n))
31 return e.EncodeUint16(n)
34 // EncodeUint32 encodes an uint16 in 5 bytes preserving type of the number.
35 func (e *Encoder) EncodeUint32(n uint32) error {
36 return e.write4(codes.Uint32, n)
39 func (e *Encoder) encodeUint32Cond(n uint32) error {
41 return e.EncodeUint(uint64(n))
43 return e.EncodeUint32(n)
46 // EncodeUint64 encodes an uint16 in 9 bytes preserving type of the number.
47 func (e *Encoder) EncodeUint64(n uint64) error {
48 return e.write8(codes.Uint64, n)
51 func (e *Encoder) encodeUint64Cond(n uint64) error {
53 return e.EncodeUint(n)
55 return e.EncodeUint64(n)
58 // EncodeInt8 encodes an int8 in 2 bytes preserving type of the number.
59 func (e *Encoder) EncodeInt8(n int8) error {
60 return e.write1(codes.Int8, uint8(n))
63 func (e *Encoder) encodeInt8Cond(n int8) error {
65 return e.EncodeInt(int64(n))
67 return e.EncodeInt8(n)
70 // EncodeInt16 encodes an int16 in 3 bytes preserving type of the number.
71 func (e *Encoder) EncodeInt16(n int16) error {
72 return e.write2(codes.Int16, uint16(n))
75 func (e *Encoder) encodeInt16Cond(n int16) error {
77 return e.EncodeInt(int64(n))
79 return e.EncodeInt16(n)
82 // EncodeInt32 encodes an int32 in 5 bytes preserving type of the number.
83 func (e *Encoder) EncodeInt32(n int32) error {
84 return e.write4(codes.Int32, uint32(n))
87 func (e *Encoder) encodeInt32Cond(n int32) error {
89 return e.EncodeInt(int64(n))
91 return e.EncodeInt32(n)
94 // EncodeInt64 encodes an int64 in 9 bytes preserving type of the number.
95 func (e *Encoder) EncodeInt64(n int64) error {
96 return e.write8(codes.Int64, uint64(n))
99 func (e *Encoder) encodeInt64Cond(n int64) error {
101 return e.EncodeInt(n)
103 return e.EncodeInt64(n)
106 // EncodeUnsignedNumber encodes an uint64 in 1, 2, 3, 5, or 9 bytes.
107 // Type of the number is lost during encoding.
108 func (e *Encoder) EncodeUint(n uint64) error {
109 if n <= math.MaxInt8 {
110 return e.w.WriteByte(byte(n))
112 if n <= math.MaxUint8 {
113 return e.EncodeUint8(uint8(n))
115 if n <= math.MaxUint16 {
116 return e.EncodeUint16(uint16(n))
118 if n <= math.MaxUint32 {
119 return e.EncodeUint32(uint32(n))
121 return e.EncodeUint64(uint64(n))
124 // EncodeNumber encodes an int64 in 1, 2, 3, 5, or 9 bytes.
125 // Type of number is lost during encoding.
126 func (e *Encoder) EncodeInt(n int64) error {
128 return e.EncodeUint(uint64(n))
130 if n >= int64(int8(codes.NegFixedNumLow)) {
131 return e.w.WriteByte(byte(n))
133 if n >= math.MinInt8 {
134 return e.EncodeInt8(int8(n))
136 if n >= math.MinInt16 {
137 return e.EncodeInt16(int16(n))
139 if n >= math.MinInt32 {
140 return e.EncodeInt32(int32(n))
142 return e.EncodeInt64(int64(n))
145 func (e *Encoder) EncodeFloat32(n float32) error {
146 return e.write4(codes.Float, math.Float32bits(n))
149 func (e *Encoder) EncodeFloat64(n float64) error {
150 return e.write8(codes.Double, math.Float64bits(n))
153 func (e *Encoder) write1(code codes.Code, n uint8) error {
155 e.buf[0] = byte(code)
157 return e.write(e.buf)
160 func (e *Encoder) write2(code codes.Code, n uint16) error {
162 e.buf[0] = byte(code)
163 e.buf[1] = byte(n >> 8)
165 return e.write(e.buf)
168 func (e *Encoder) write4(code codes.Code, n uint32) error {
170 e.buf[0] = byte(code)
171 e.buf[1] = byte(n >> 24)
172 e.buf[2] = byte(n >> 16)
173 e.buf[3] = byte(n >> 8)
175 return e.write(e.buf)
178 func (e *Encoder) write8(code codes.Code, n uint64) error {
180 e.buf[0] = byte(code)
181 e.buf[1] = byte(n >> 56)
182 e.buf[2] = byte(n >> 48)
183 e.buf[3] = byte(n >> 40)
184 e.buf[4] = byte(n >> 32)
185 e.buf[5] = byte(n >> 24)
186 e.buf[6] = byte(n >> 16)
187 e.buf[7] = byte(n >> 8)
189 return e.write(e.buf)
192 func encodeUint8CondValue(e *Encoder, v reflect.Value) error {
193 return e.encodeUint8Cond(uint8(v.Uint()))
196 func encodeUint16CondValue(e *Encoder, v reflect.Value) error {
197 return e.encodeUint16Cond(uint16(v.Uint()))
200 func encodeUint32CondValue(e *Encoder, v reflect.Value) error {
201 return e.encodeUint32Cond(uint32(v.Uint()))
204 func encodeUint64CondValue(e *Encoder, v reflect.Value) error {
205 return e.encodeUint64Cond(v.Uint())
208 func encodeInt8CondValue(e *Encoder, v reflect.Value) error {
209 return e.encodeInt8Cond(int8(v.Int()))
212 func encodeInt16CondValue(e *Encoder, v reflect.Value) error {
213 return e.encodeInt16Cond(int16(v.Int()))
216 func encodeInt32CondValue(e *Encoder, v reflect.Value) error {
217 return e.encodeInt32Cond(int32(v.Int()))
220 func encodeInt64CondValue(e *Encoder, v reflect.Value) error {
221 return e.encodeInt64Cond(v.Int())
224 func encodeFloat32Value(e *Encoder, v reflect.Value) error {
225 return e.EncodeFloat32(float32(v.Float()))
228 func encodeFloat64Value(e *Encoder, v reflect.Value) error {
229 return e.EncodeFloat64(v.Float())