gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

decode_number.go (5737B)


      1 package msgpack
      2 
      3 import (
      4 	"fmt"
      5 	"math"
      6 	"reflect"
      7 
      8 	"github.com/vmihailenco/msgpack/v5/msgpcode"
      9 )
     10 
     11 func (d *Decoder) skipN(n int) error {
     12 	_, err := d.readN(n)
     13 	return err
     14 }
     15 
     16 func (d *Decoder) uint8() (uint8, error) {
     17 	c, err := d.readCode()
     18 	if err != nil {
     19 		return 0, err
     20 	}
     21 	return c, nil
     22 }
     23 
     24 func (d *Decoder) int8() (int8, error) {
     25 	n, err := d.uint8()
     26 	return int8(n), err
     27 }
     28 
     29 func (d *Decoder) uint16() (uint16, error) {
     30 	b, err := d.readN(2)
     31 	if err != nil {
     32 		return 0, err
     33 	}
     34 	return (uint16(b[0]) << 8) | uint16(b[1]), nil
     35 }
     36 
     37 func (d *Decoder) int16() (int16, error) {
     38 	n, err := d.uint16()
     39 	return int16(n), err
     40 }
     41 
     42 func (d *Decoder) uint32() (uint32, error) {
     43 	b, err := d.readN(4)
     44 	if err != nil {
     45 		return 0, err
     46 	}
     47 	n := (uint32(b[0]) << 24) |
     48 		(uint32(b[1]) << 16) |
     49 		(uint32(b[2]) << 8) |
     50 		uint32(b[3])
     51 	return n, nil
     52 }
     53 
     54 func (d *Decoder) int32() (int32, error) {
     55 	n, err := d.uint32()
     56 	return int32(n), err
     57 }
     58 
     59 func (d *Decoder) uint64() (uint64, error) {
     60 	b, err := d.readN(8)
     61 	if err != nil {
     62 		return 0, err
     63 	}
     64 	n := (uint64(b[0]) << 56) |
     65 		(uint64(b[1]) << 48) |
     66 		(uint64(b[2]) << 40) |
     67 		(uint64(b[3]) << 32) |
     68 		(uint64(b[4]) << 24) |
     69 		(uint64(b[5]) << 16) |
     70 		(uint64(b[6]) << 8) |
     71 		uint64(b[7])
     72 	return n, nil
     73 }
     74 
     75 func (d *Decoder) int64() (int64, error) {
     76 	n, err := d.uint64()
     77 	return int64(n), err
     78 }
     79 
     80 // DecodeUint64 decodes msgpack int8/16/32/64 and uint8/16/32/64
     81 // into Go uint64.
     82 func (d *Decoder) DecodeUint64() (uint64, error) {
     83 	c, err := d.readCode()
     84 	if err != nil {
     85 		return 0, err
     86 	}
     87 	return d.uint(c)
     88 }
     89 
     90 func (d *Decoder) uint(c byte) (uint64, error) {
     91 	if c == msgpcode.Nil {
     92 		return 0, nil
     93 	}
     94 	if msgpcode.IsFixedNum(c) {
     95 		return uint64(int8(c)), nil
     96 	}
     97 	switch c {
     98 	case msgpcode.Uint8:
     99 		n, err := d.uint8()
    100 		return uint64(n), err
    101 	case msgpcode.Int8:
    102 		n, err := d.int8()
    103 		return uint64(n), err
    104 	case msgpcode.Uint16:
    105 		n, err := d.uint16()
    106 		return uint64(n), err
    107 	case msgpcode.Int16:
    108 		n, err := d.int16()
    109 		return uint64(n), err
    110 	case msgpcode.Uint32:
    111 		n, err := d.uint32()
    112 		return uint64(n), err
    113 	case msgpcode.Int32:
    114 		n, err := d.int32()
    115 		return uint64(n), err
    116 	case msgpcode.Uint64, msgpcode.Int64:
    117 		return d.uint64()
    118 	}
    119 	return 0, fmt.Errorf("msgpack: invalid code=%x decoding uint64", c)
    120 }
    121 
    122 // DecodeInt64 decodes msgpack int8/16/32/64 and uint8/16/32/64
    123 // into Go int64.
    124 func (d *Decoder) DecodeInt64() (int64, error) {
    125 	c, err := d.readCode()
    126 	if err != nil {
    127 		return 0, err
    128 	}
    129 	return d.int(c)
    130 }
    131 
    132 func (d *Decoder) int(c byte) (int64, error) {
    133 	if c == msgpcode.Nil {
    134 		return 0, nil
    135 	}
    136 	if msgpcode.IsFixedNum(c) {
    137 		return int64(int8(c)), nil
    138 	}
    139 	switch c {
    140 	case msgpcode.Uint8:
    141 		n, err := d.uint8()
    142 		return int64(n), err
    143 	case msgpcode.Int8:
    144 		n, err := d.uint8()
    145 		return int64(int8(n)), err
    146 	case msgpcode.Uint16:
    147 		n, err := d.uint16()
    148 		return int64(n), err
    149 	case msgpcode.Int16:
    150 		n, err := d.uint16()
    151 		return int64(int16(n)), err
    152 	case msgpcode.Uint32:
    153 		n, err := d.uint32()
    154 		return int64(n), err
    155 	case msgpcode.Int32:
    156 		n, err := d.uint32()
    157 		return int64(int32(n)), err
    158 	case msgpcode.Uint64, msgpcode.Int64:
    159 		n, err := d.uint64()
    160 		return int64(n), err
    161 	}
    162 	return 0, fmt.Errorf("msgpack: invalid code=%x decoding int64", c)
    163 }
    164 
    165 func (d *Decoder) DecodeFloat32() (float32, error) {
    166 	c, err := d.readCode()
    167 	if err != nil {
    168 		return 0, err
    169 	}
    170 	return d.float32(c)
    171 }
    172 
    173 func (d *Decoder) float32(c byte) (float32, error) {
    174 	if c == msgpcode.Float {
    175 		n, err := d.uint32()
    176 		if err != nil {
    177 			return 0, err
    178 		}
    179 		return math.Float32frombits(n), nil
    180 	}
    181 
    182 	n, err := d.int(c)
    183 	if err != nil {
    184 		return 0, fmt.Errorf("msgpack: invalid code=%x decoding float32", c)
    185 	}
    186 	return float32(n), nil
    187 }
    188 
    189 // DecodeFloat64 decodes msgpack float32/64 into Go float64.
    190 func (d *Decoder) DecodeFloat64() (float64, error) {
    191 	c, err := d.readCode()
    192 	if err != nil {
    193 		return 0, err
    194 	}
    195 	return d.float64(c)
    196 }
    197 
    198 func (d *Decoder) float64(c byte) (float64, error) {
    199 	switch c {
    200 	case msgpcode.Float:
    201 		n, err := d.float32(c)
    202 		if err != nil {
    203 			return 0, err
    204 		}
    205 		return float64(n), nil
    206 	case msgpcode.Double:
    207 		n, err := d.uint64()
    208 		if err != nil {
    209 			return 0, err
    210 		}
    211 		return math.Float64frombits(n), nil
    212 	}
    213 
    214 	n, err := d.int(c)
    215 	if err != nil {
    216 		return 0, fmt.Errorf("msgpack: invalid code=%x decoding float32", c)
    217 	}
    218 	return float64(n), nil
    219 }
    220 
    221 func (d *Decoder) DecodeUint() (uint, error) {
    222 	n, err := d.DecodeUint64()
    223 	return uint(n), err
    224 }
    225 
    226 func (d *Decoder) DecodeUint8() (uint8, error) {
    227 	n, err := d.DecodeUint64()
    228 	return uint8(n), err
    229 }
    230 
    231 func (d *Decoder) DecodeUint16() (uint16, error) {
    232 	n, err := d.DecodeUint64()
    233 	return uint16(n), err
    234 }
    235 
    236 func (d *Decoder) DecodeUint32() (uint32, error) {
    237 	n, err := d.DecodeUint64()
    238 	return uint32(n), err
    239 }
    240 
    241 func (d *Decoder) DecodeInt() (int, error) {
    242 	n, err := d.DecodeInt64()
    243 	return int(n), err
    244 }
    245 
    246 func (d *Decoder) DecodeInt8() (int8, error) {
    247 	n, err := d.DecodeInt64()
    248 	return int8(n), err
    249 }
    250 
    251 func (d *Decoder) DecodeInt16() (int16, error) {
    252 	n, err := d.DecodeInt64()
    253 	return int16(n), err
    254 }
    255 
    256 func (d *Decoder) DecodeInt32() (int32, error) {
    257 	n, err := d.DecodeInt64()
    258 	return int32(n), err
    259 }
    260 
    261 func decodeFloat32Value(d *Decoder, v reflect.Value) error {
    262 	f, err := d.DecodeFloat32()
    263 	if err != nil {
    264 		return err
    265 	}
    266 	v.SetFloat(float64(f))
    267 	return nil
    268 }
    269 
    270 func decodeFloat64Value(d *Decoder, v reflect.Value) error {
    271 	f, err := d.DecodeFloat64()
    272 	if err != nil {
    273 		return err
    274 	}
    275 	v.SetFloat(f)
    276 	return nil
    277 }
    278 
    279 func decodeInt64Value(d *Decoder, v reflect.Value) error {
    280 	n, err := d.DecodeInt64()
    281 	if err != nil {
    282 		return err
    283 	}
    284 	v.SetInt(n)
    285 	return nil
    286 }
    287 
    288 func decodeUint64Value(d *Decoder, v reflect.Value) error {
    289 	n, err := d.DecodeUint64()
    290 	if err != nil {
    291 		return err
    292 	}
    293 	v.SetUint(n)
    294 	return nil
    295 }