encode_number.go (6290B)
1 package msgpack 2 3 import ( 4 "math" 5 "reflect" 6 7 "github.com/vmihailenco/msgpack/v5/msgpcode" 8 ) 9 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(msgpcode.Uint8, n) 13 } 14 15 func (e *Encoder) encodeUint8Cond(n uint8) error { 16 if e.flags&useCompactIntsFlag != 0 { 17 return e.EncodeUint(uint64(n)) 18 } 19 return e.EncodeUint8(n) 20 } 21 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(msgpcode.Uint16, n) 25 } 26 27 func (e *Encoder) encodeUint16Cond(n uint16) error { 28 if e.flags&useCompactIntsFlag != 0 { 29 return e.EncodeUint(uint64(n)) 30 } 31 return e.EncodeUint16(n) 32 } 33 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(msgpcode.Uint32, n) 37 } 38 39 func (e *Encoder) encodeUint32Cond(n uint32) error { 40 if e.flags&useCompactIntsFlag != 0 { 41 return e.EncodeUint(uint64(n)) 42 } 43 return e.EncodeUint32(n) 44 } 45 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(msgpcode.Uint64, n) 49 } 50 51 func (e *Encoder) encodeUint64Cond(n uint64) error { 52 if e.flags&useCompactIntsFlag != 0 { 53 return e.EncodeUint(n) 54 } 55 return e.EncodeUint64(n) 56 } 57 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(msgpcode.Int8, uint8(n)) 61 } 62 63 func (e *Encoder) encodeInt8Cond(n int8) error { 64 if e.flags&useCompactIntsFlag != 0 { 65 return e.EncodeInt(int64(n)) 66 } 67 return e.EncodeInt8(n) 68 } 69 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(msgpcode.Int16, uint16(n)) 73 } 74 75 func (e *Encoder) encodeInt16Cond(n int16) error { 76 if e.flags&useCompactIntsFlag != 0 { 77 return e.EncodeInt(int64(n)) 78 } 79 return e.EncodeInt16(n) 80 } 81 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(msgpcode.Int32, uint32(n)) 85 } 86 87 func (e *Encoder) encodeInt32Cond(n int32) error { 88 if e.flags&useCompactIntsFlag != 0 { 89 return e.EncodeInt(int64(n)) 90 } 91 return e.EncodeInt32(n) 92 } 93 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(msgpcode.Int64, uint64(n)) 97 } 98 99 func (e *Encoder) encodeInt64Cond(n int64) error { 100 if e.flags&useCompactIntsFlag != 0 { 101 return e.EncodeInt(n) 102 } 103 return e.EncodeInt64(n) 104 } 105 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)) 111 } 112 if n <= math.MaxUint8 { 113 return e.EncodeUint8(uint8(n)) 114 } 115 if n <= math.MaxUint16 { 116 return e.EncodeUint16(uint16(n)) 117 } 118 if n <= math.MaxUint32 { 119 return e.EncodeUint32(uint32(n)) 120 } 121 return e.EncodeUint64(n) 122 } 123 124 // EncodeNumber encodes an int64 in 1, 2, 3, 5, or 9 bytes. 125 // Type of the number is lost during encoding. 126 func (e *Encoder) EncodeInt(n int64) error { 127 if n >= 0 { 128 return e.EncodeUint(uint64(n)) 129 } 130 if n >= int64(int8(msgpcode.NegFixedNumLow)) { 131 return e.w.WriteByte(byte(n)) 132 } 133 if n >= math.MinInt8 { 134 return e.EncodeInt8(int8(n)) 135 } 136 if n >= math.MinInt16 { 137 return e.EncodeInt16(int16(n)) 138 } 139 if n >= math.MinInt32 { 140 return e.EncodeInt32(int32(n)) 141 } 142 return e.EncodeInt64(n) 143 } 144 145 func (e *Encoder) EncodeFloat32(n float32) error { 146 if e.flags&useCompactFloatsFlag != 0 { 147 if float32(int64(n)) == n { 148 return e.EncodeInt(int64(n)) 149 } 150 } 151 return e.write4(msgpcode.Float, math.Float32bits(n)) 152 } 153 154 func (e *Encoder) EncodeFloat64(n float64) error { 155 if e.flags&useCompactFloatsFlag != 0 { 156 // Both NaN and Inf convert to int64(-0x8000000000000000) 157 // If n is NaN then it never compares true with any other value 158 // If n is Inf then it doesn't convert from int64 back to +/-Inf 159 // In both cases the comparison works. 160 if float64(int64(n)) == n { 161 return e.EncodeInt(int64(n)) 162 } 163 } 164 return e.write8(msgpcode.Double, math.Float64bits(n)) 165 } 166 167 func (e *Encoder) write1(code byte, n uint8) error { 168 e.buf = e.buf[:2] 169 e.buf[0] = code 170 e.buf[1] = n 171 return e.write(e.buf) 172 } 173 174 func (e *Encoder) write2(code byte, n uint16) error { 175 e.buf = e.buf[:3] 176 e.buf[0] = code 177 e.buf[1] = byte(n >> 8) 178 e.buf[2] = byte(n) 179 return e.write(e.buf) 180 } 181 182 func (e *Encoder) write4(code byte, n uint32) error { 183 e.buf = e.buf[:5] 184 e.buf[0] = code 185 e.buf[1] = byte(n >> 24) 186 e.buf[2] = byte(n >> 16) 187 e.buf[3] = byte(n >> 8) 188 e.buf[4] = byte(n) 189 return e.write(e.buf) 190 } 191 192 func (e *Encoder) write8(code byte, n uint64) error { 193 e.buf = e.buf[:9] 194 e.buf[0] = code 195 e.buf[1] = byte(n >> 56) 196 e.buf[2] = byte(n >> 48) 197 e.buf[3] = byte(n >> 40) 198 e.buf[4] = byte(n >> 32) 199 e.buf[5] = byte(n >> 24) 200 e.buf[6] = byte(n >> 16) 201 e.buf[7] = byte(n >> 8) 202 e.buf[8] = byte(n) 203 return e.write(e.buf) 204 } 205 206 func encodeUintValue(e *Encoder, v reflect.Value) error { 207 return e.EncodeUint(v.Uint()) 208 } 209 210 func encodeIntValue(e *Encoder, v reflect.Value) error { 211 return e.EncodeInt(v.Int()) 212 } 213 214 func encodeUint8CondValue(e *Encoder, v reflect.Value) error { 215 return e.encodeUint8Cond(uint8(v.Uint())) 216 } 217 218 func encodeUint16CondValue(e *Encoder, v reflect.Value) error { 219 return e.encodeUint16Cond(uint16(v.Uint())) 220 } 221 222 func encodeUint32CondValue(e *Encoder, v reflect.Value) error { 223 return e.encodeUint32Cond(uint32(v.Uint())) 224 } 225 226 func encodeUint64CondValue(e *Encoder, v reflect.Value) error { 227 return e.encodeUint64Cond(v.Uint()) 228 } 229 230 func encodeInt8CondValue(e *Encoder, v reflect.Value) error { 231 return e.encodeInt8Cond(int8(v.Int())) 232 } 233 234 func encodeInt16CondValue(e *Encoder, v reflect.Value) error { 235 return e.encodeInt16Cond(int16(v.Int())) 236 } 237 238 func encodeInt32CondValue(e *Encoder, v reflect.Value) error { 239 return e.encodeInt32Cond(int32(v.Int())) 240 } 241 242 func encodeInt64CondValue(e *Encoder, v reflect.Value) error { 243 return e.encodeInt64Cond(v.Int()) 244 } 245 246 func encodeFloat32Value(e *Encoder, v reflect.Value) error { 247 return e.EncodeFloat32(float32(v.Float())) 248 } 249 250 func encodeFloat64Value(e *Encoder, v reflect.Value) error { 251 return e.EncodeFloat64(v.Float()) 252 }