encode_number.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package msgpack
  2. import (
  3. "math"
  4. "reflect"
  5. "github.com/vmihailenco/msgpack/codes"
  6. )
  7. // EncodeUint8 encodes an uint8 in 2 bytes preserving type of the number.
  8. func (e *Encoder) EncodeUint8(n uint8) error {
  9. return e.write1(codes.Uint8, n)
  10. }
  11. func (e *Encoder) encodeUint8Cond(n uint8) error {
  12. if e.useCompact {
  13. return e.EncodeUint(uint64(n))
  14. }
  15. return e.EncodeUint8(n)
  16. }
  17. // EncodeUint16 encodes an uint16 in 3 bytes preserving type of the number.
  18. func (e *Encoder) EncodeUint16(n uint16) error {
  19. return e.write2(codes.Uint16, n)
  20. }
  21. func (e *Encoder) encodeUint16Cond(n uint16) error {
  22. if e.useCompact {
  23. return e.EncodeUint(uint64(n))
  24. }
  25. return e.EncodeUint16(n)
  26. }
  27. // EncodeUint32 encodes an uint16 in 5 bytes preserving type of the number.
  28. func (e *Encoder) EncodeUint32(n uint32) error {
  29. return e.write4(codes.Uint32, n)
  30. }
  31. func (e *Encoder) encodeUint32Cond(n uint32) error {
  32. if e.useCompact {
  33. return e.EncodeUint(uint64(n))
  34. }
  35. return e.EncodeUint32(n)
  36. }
  37. // EncodeUint64 encodes an uint16 in 9 bytes preserving type of the number.
  38. func (e *Encoder) EncodeUint64(n uint64) error {
  39. return e.write8(codes.Uint64, n)
  40. }
  41. func (e *Encoder) encodeUint64Cond(n uint64) error {
  42. if e.useCompact {
  43. return e.EncodeUint(n)
  44. }
  45. return e.EncodeUint64(n)
  46. }
  47. // EncodeInt8 encodes an int8 in 2 bytes preserving type of the number.
  48. func (e *Encoder) EncodeInt8(n int8) error {
  49. return e.write1(codes.Int8, uint8(n))
  50. }
  51. func (e *Encoder) encodeInt8Cond(n int8) error {
  52. if e.useCompact {
  53. return e.EncodeInt(int64(n))
  54. }
  55. return e.EncodeInt8(n)
  56. }
  57. // EncodeInt16 encodes an int16 in 3 bytes preserving type of the number.
  58. func (e *Encoder) EncodeInt16(n int16) error {
  59. return e.write2(codes.Int16, uint16(n))
  60. }
  61. func (e *Encoder) encodeInt16Cond(n int16) error {
  62. if e.useCompact {
  63. return e.EncodeInt(int64(n))
  64. }
  65. return e.EncodeInt16(n)
  66. }
  67. // EncodeInt32 encodes an int32 in 5 bytes preserving type of the number.
  68. func (e *Encoder) EncodeInt32(n int32) error {
  69. return e.write4(codes.Int32, uint32(n))
  70. }
  71. func (e *Encoder) encodeInt32Cond(n int32) error {
  72. if e.useCompact {
  73. return e.EncodeInt(int64(n))
  74. }
  75. return e.EncodeInt32(n)
  76. }
  77. // EncodeInt64 encodes an int64 in 9 bytes preserving type of the number.
  78. func (e *Encoder) EncodeInt64(n int64) error {
  79. return e.write8(codes.Int64, uint64(n))
  80. }
  81. func (e *Encoder) encodeInt64Cond(n int64) error {
  82. if e.useCompact {
  83. return e.EncodeInt(n)
  84. }
  85. return e.EncodeInt64(n)
  86. }
  87. // EncodeUnsignedNumber encodes an uint64 in 1, 2, 3, 5, or 9 bytes.
  88. // Type of the number is lost during encoding.
  89. func (e *Encoder) EncodeUint(n uint64) error {
  90. if n <= math.MaxInt8 {
  91. return e.w.WriteByte(byte(n))
  92. }
  93. if n <= math.MaxUint8 {
  94. return e.EncodeUint8(uint8(n))
  95. }
  96. if n <= math.MaxUint16 {
  97. return e.EncodeUint16(uint16(n))
  98. }
  99. if n <= math.MaxUint32 {
  100. return e.EncodeUint32(uint32(n))
  101. }
  102. return e.EncodeUint64(uint64(n))
  103. }
  104. // EncodeNumber encodes an int64 in 1, 2, 3, 5, or 9 bytes.
  105. // Type of number is lost during encoding.
  106. func (e *Encoder) EncodeInt(n int64) error {
  107. if n >= 0 {
  108. return e.EncodeUint(uint64(n))
  109. }
  110. if n >= int64(int8(codes.NegFixedNumLow)) {
  111. return e.w.WriteByte(byte(n))
  112. }
  113. if n >= math.MinInt8 {
  114. return e.EncodeInt8(int8(n))
  115. }
  116. if n >= math.MinInt16 {
  117. return e.EncodeInt16(int16(n))
  118. }
  119. if n >= math.MinInt32 {
  120. return e.EncodeInt32(int32(n))
  121. }
  122. return e.EncodeInt64(int64(n))
  123. }
  124. func (e *Encoder) EncodeFloat32(n float32) error {
  125. return e.write4(codes.Float, math.Float32bits(n))
  126. }
  127. func (e *Encoder) EncodeFloat64(n float64) error {
  128. return e.write8(codes.Double, math.Float64bits(n))
  129. }
  130. func (e *Encoder) write1(code codes.Code, n uint8) error {
  131. e.buf = e.buf[:2]
  132. e.buf[0] = byte(code)
  133. e.buf[1] = byte(n)
  134. return e.write(e.buf)
  135. }
  136. func (e *Encoder) write2(code codes.Code, n uint16) error {
  137. e.buf = e.buf[:3]
  138. e.buf[0] = byte(code)
  139. e.buf[1] = byte(n >> 8)
  140. e.buf[2] = byte(n)
  141. return e.write(e.buf)
  142. }
  143. func (e *Encoder) write4(code codes.Code, n uint32) error {
  144. e.buf = e.buf[:5]
  145. e.buf[0] = byte(code)
  146. e.buf[1] = byte(n >> 24)
  147. e.buf[2] = byte(n >> 16)
  148. e.buf[3] = byte(n >> 8)
  149. e.buf[4] = byte(n)
  150. return e.write(e.buf)
  151. }
  152. func (e *Encoder) write8(code codes.Code, n uint64) error {
  153. e.buf = e.buf[:9]
  154. e.buf[0] = byte(code)
  155. e.buf[1] = byte(n >> 56)
  156. e.buf[2] = byte(n >> 48)
  157. e.buf[3] = byte(n >> 40)
  158. e.buf[4] = byte(n >> 32)
  159. e.buf[5] = byte(n >> 24)
  160. e.buf[6] = byte(n >> 16)
  161. e.buf[7] = byte(n >> 8)
  162. e.buf[8] = byte(n)
  163. return e.write(e.buf)
  164. }
  165. func encodeUint8CondValue(e *Encoder, v reflect.Value) error {
  166. return e.encodeUint8Cond(uint8(v.Uint()))
  167. }
  168. func encodeUint16CondValue(e *Encoder, v reflect.Value) error {
  169. return e.encodeUint16Cond(uint16(v.Uint()))
  170. }
  171. func encodeUint32CondValue(e *Encoder, v reflect.Value) error {
  172. return e.encodeUint32Cond(uint32(v.Uint()))
  173. }
  174. func encodeUint64CondValue(e *Encoder, v reflect.Value) error {
  175. return e.encodeUint64Cond(v.Uint())
  176. }
  177. func encodeInt8CondValue(e *Encoder, v reflect.Value) error {
  178. return e.encodeInt8Cond(int8(v.Int()))
  179. }
  180. func encodeInt16CondValue(e *Encoder, v reflect.Value) error {
  181. return e.encodeInt16Cond(int16(v.Int()))
  182. }
  183. func encodeInt32CondValue(e *Encoder, v reflect.Value) error {
  184. return e.encodeInt32Cond(int32(v.Int()))
  185. }
  186. func encodeInt64CondValue(e *Encoder, v reflect.Value) error {
  187. return e.encodeInt64Cond(v.Int())
  188. }
  189. func encodeFloat32Value(e *Encoder, v reflect.Value) error {
  190. return e.EncodeFloat32(float32(v.Float()))
  191. }
  192. func encodeFloat64Value(e *Encoder, v reflect.Value) error {
  193. return e.EncodeFloat64(v.Float())
  194. }