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
|
package msgpack
import (
"math"
"reflect"
"github.com/vmihailenco/msgpack/v4/codes"
)
// EncodeUint8 encodes an uint8 in 2 bytes preserving type of the number.
func (e *Encoder) EncodeUint8(n uint8) error {
return e.write1(codes.Uint8, n)
}
func (e *Encoder) encodeUint8Cond(n uint8) error {
if e.useCompact {
return e.EncodeUint(uint64(n))
}
return e.EncodeUint8(n)
}
// EncodeUint16 encodes an uint16 in 3 bytes preserving type of the number.
func (e *Encoder) EncodeUint16(n uint16) error {
return e.write2(codes.Uint16, n)
}
func (e *Encoder) encodeUint16Cond(n uint16) error {
if e.useCompact {
return e.EncodeUint(uint64(n))
}
return e.EncodeUint16(n)
}
// EncodeUint32 encodes an uint16 in 5 bytes preserving type of the number.
func (e *Encoder) EncodeUint32(n uint32) error {
return e.write4(codes.Uint32, n)
}
func (e *Encoder) encodeUint32Cond(n uint32) error {
if e.useCompact {
return e.EncodeUint(uint64(n))
}
return e.EncodeUint32(n)
}
// EncodeUint64 encodes an uint16 in 9 bytes preserving type of the number.
func (e *Encoder) EncodeUint64(n uint64) error {
return e.write8(codes.Uint64, n)
}
func (e *Encoder) encodeUint64Cond(n uint64) error {
if e.useCompact {
return e.EncodeUint(n)
}
return e.EncodeUint64(n)
}
// EncodeInt8 encodes an int8 in 2 bytes preserving type of the number.
func (e *Encoder) EncodeInt8(n int8) error {
return e.write1(codes.Int8, uint8(n))
}
func (e *Encoder) encodeInt8Cond(n int8) error {
if e.useCompact {
return e.EncodeInt(int64(n))
}
return e.EncodeInt8(n)
}
// EncodeInt16 encodes an int16 in 3 bytes preserving type of the number.
func (e *Encoder) EncodeInt16(n int16) error {
return e.write2(codes.Int16, uint16(n))
}
func (e *Encoder) encodeInt16Cond(n int16) error {
if e.useCompact {
return e.EncodeInt(int64(n))
}
return e.EncodeInt16(n)
}
// EncodeInt32 encodes an int32 in 5 bytes preserving type of the number.
func (e *Encoder) EncodeInt32(n int32) error {
return e.write4(codes.Int32, uint32(n))
}
func (e *Encoder) encodeInt32Cond(n int32) error {
if e.useCompact {
return e.EncodeInt(int64(n))
}
return e.EncodeInt32(n)
}
// EncodeInt64 encodes an int64 in 9 bytes preserving type of the number.
func (e *Encoder) EncodeInt64(n int64) error {
return e.write8(codes.Int64, uint64(n))
}
func (e *Encoder) encodeInt64Cond(n int64) error {
if e.useCompact {
return e.EncodeInt(n)
}
return e.EncodeInt64(n)
}
// EncodeUnsignedNumber encodes an uint64 in 1, 2, 3, 5, or 9 bytes.
// Type of the number is lost during encoding.
func (e *Encoder) EncodeUint(n uint64) error {
if n <= math.MaxInt8 {
return e.w.WriteByte(byte(n))
}
if n <= math.MaxUint8 {
return e.EncodeUint8(uint8(n))
}
if n <= math.MaxUint16 {
return e.EncodeUint16(uint16(n))
}
if n <= math.MaxUint32 {
return e.EncodeUint32(uint32(n))
}
return e.EncodeUint64(n)
}
// EncodeNumber encodes an int64 in 1, 2, 3, 5, or 9 bytes.
// Type of the number is lost during encoding.
func (e *Encoder) EncodeInt(n int64) error {
if n >= 0 {
return e.EncodeUint(uint64(n))
}
if n >= int64(int8(codes.NegFixedNumLow)) {
return e.w.WriteByte(byte(n))
}
if n >= math.MinInt8 {
return e.EncodeInt8(int8(n))
}
if n >= math.MinInt16 {
return e.EncodeInt16(int16(n))
}
if n >= math.MinInt32 {
return e.EncodeInt32(int32(n))
}
return e.EncodeInt64(n)
}
func (e *Encoder) EncodeFloat32(n float32) error {
return e.write4(codes.Float, math.Float32bits(n))
}
func (e *Encoder) EncodeFloat64(n float64) error {
return e.write8(codes.Double, math.Float64bits(n))
}
func (e *Encoder) write1(code codes.Code, n uint8) error {
e.buf = e.buf[:2]
e.buf[0] = byte(code)
e.buf[1] = n
return e.write(e.buf)
}
func (e *Encoder) write2(code codes.Code, n uint16) error {
e.buf = e.buf[:3]
e.buf[0] = byte(code)
e.buf[1] = byte(n >> 8)
e.buf[2] = byte(n)
return e.write(e.buf)
}
func (e *Encoder) write4(code codes.Code, n uint32) error {
e.buf = e.buf[:5]
e.buf[0] = byte(code)
e.buf[1] = byte(n >> 24)
e.buf[2] = byte(n >> 16)
e.buf[3] = byte(n >> 8)
e.buf[4] = byte(n)
return e.write(e.buf)
}
func (e *Encoder) write8(code codes.Code, n uint64) error {
e.buf = e.buf[:9]
e.buf[0] = byte(code)
e.buf[1] = byte(n >> 56)
e.buf[2] = byte(n >> 48)
e.buf[3] = byte(n >> 40)
e.buf[4] = byte(n >> 32)
e.buf[5] = byte(n >> 24)
e.buf[6] = byte(n >> 16)
e.buf[7] = byte(n >> 8)
e.buf[8] = byte(n)
return e.write(e.buf)
}
func encodeUint8CondValue(e *Encoder, v reflect.Value) error {
return e.encodeUint8Cond(uint8(v.Uint()))
}
func encodeUint16CondValue(e *Encoder, v reflect.Value) error {
return e.encodeUint16Cond(uint16(v.Uint()))
}
func encodeUint32CondValue(e *Encoder, v reflect.Value) error {
return e.encodeUint32Cond(uint32(v.Uint()))
}
func encodeUint64CondValue(e *Encoder, v reflect.Value) error {
return e.encodeUint64Cond(v.Uint())
}
func encodeInt8CondValue(e *Encoder, v reflect.Value) error {
return e.encodeInt8Cond(int8(v.Int()))
}
func encodeInt16CondValue(e *Encoder, v reflect.Value) error {
return e.encodeInt16Cond(int16(v.Int()))
}
func encodeInt32CondValue(e *Encoder, v reflect.Value) error {
return e.encodeInt32Cond(int32(v.Int()))
}
func encodeInt64CondValue(e *Encoder, v reflect.Value) error {
return e.encodeInt64Cond(v.Int())
}
func encodeFloat32Value(e *Encoder, v reflect.Value) error {
return e.EncodeFloat32(float32(v.Float()))
}
func encodeFloat64Value(e *Encoder, v reflect.Value) error {
return e.EncodeFloat64(v.Float())
}
|