You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.4 KiB

  1. package msgpack
  2. import (
  3. "math"
  4. "reflect"
  5. "github.com/vmihailenco/msgpack/codes"
  6. )
  7. // EncodeUint encodes an uint64 in 1, 2, 3, 5, or 9 bytes.
  8. func (e *Encoder) EncodeUint(v uint64) error {
  9. if v <= math.MaxInt8 {
  10. return e.w.WriteByte(byte(v))
  11. }
  12. if v <= math.MaxUint8 {
  13. return e.write1(codes.Uint8, v)
  14. }
  15. if v <= math.MaxUint16 {
  16. return e.write2(codes.Uint16, v)
  17. }
  18. if v <= math.MaxUint32 {
  19. return e.write4(codes.Uint32, uint32(v))
  20. }
  21. return e.write8(codes.Uint64, v)
  22. }
  23. // EncodeInt encodes an int64 in 1, 2, 3, 5, or 9 bytes.
  24. func (e *Encoder) EncodeInt(v int64) error {
  25. if v >= 0 {
  26. return e.EncodeUint(uint64(v))
  27. }
  28. if v >= int64(int8(codes.NegFixedNumLow)) {
  29. return e.w.WriteByte(byte(v))
  30. }
  31. if v >= math.MinInt8 {
  32. return e.write1(codes.Int8, uint64(v))
  33. }
  34. if v >= math.MinInt16 {
  35. return e.write2(codes.Int16, uint64(v))
  36. }
  37. if v >= math.MinInt32 {
  38. return e.write4(codes.Int32, uint32(v))
  39. }
  40. return e.write8(codes.Int64, uint64(v))
  41. }
  42. func (e *Encoder) EncodeFloat32(n float32) error {
  43. return e.write4(codes.Float, math.Float32bits(n))
  44. }
  45. func (e *Encoder) EncodeFloat64(n float64) error {
  46. return e.write8(codes.Double, math.Float64bits(n))
  47. }
  48. func (e *Encoder) write1(code codes.Code, n uint64) error {
  49. e.buf = e.buf[:2]
  50. e.buf[0] = byte(code)
  51. e.buf[1] = byte(n)
  52. return e.write(e.buf)
  53. }
  54. func (e *Encoder) write2(code codes.Code, n uint64) error {
  55. e.buf = e.buf[:3]
  56. e.buf[0] = byte(code)
  57. e.buf[1] = byte(n >> 8)
  58. e.buf[2] = byte(n)
  59. return e.write(e.buf)
  60. }
  61. func (e *Encoder) write4(code codes.Code, n uint32) error {
  62. e.buf = e.buf[:5]
  63. e.buf[0] = byte(code)
  64. e.buf[1] = byte(n >> 24)
  65. e.buf[2] = byte(n >> 16)
  66. e.buf[3] = byte(n >> 8)
  67. e.buf[4] = byte(n)
  68. return e.write(e.buf)
  69. }
  70. func (e *Encoder) write8(code codes.Code, n uint64) error {
  71. e.buf = e.buf[:9]
  72. e.buf[0] = byte(code)
  73. e.buf[1] = byte(n >> 56)
  74. e.buf[2] = byte(n >> 48)
  75. e.buf[3] = byte(n >> 40)
  76. e.buf[4] = byte(n >> 32)
  77. e.buf[5] = byte(n >> 24)
  78. e.buf[6] = byte(n >> 16)
  79. e.buf[7] = byte(n >> 8)
  80. e.buf[8] = byte(n)
  81. return e.write(e.buf)
  82. }
  83. func encodeInt64Value(e *Encoder, v reflect.Value) error {
  84. return e.EncodeInt(v.Int())
  85. }
  86. func encodeUint64Value(e *Encoder, v reflect.Value) error {
  87. return e.EncodeUint(v.Uint())
  88. }
  89. func encodeFloat32Value(e *Encoder, v reflect.Value) error {
  90. return e.EncodeFloat32(float32(v.Float()))
  91. }
  92. func encodeFloat64Value(e *Encoder, v reflect.Value) error {
  93. return e.EncodeFloat64(v.Float())
  94. }