encode_number.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package msgpack
  2. import (
  3. "math"
  4. "reflect"
  5. "github.com/vmihailenco/msgpack/v5/msgpcode"
  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(msgpcode.Uint8, n)
  10. }
  11. func (e *Encoder) encodeUint8Cond(n uint8) error {
  12. if e.flags&useCompactIntsFlag != 0 {
  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(msgpcode.Uint16, n)
  20. }
  21. func (e *Encoder) encodeUint16Cond(n uint16) error {
  22. if e.flags&useCompactIntsFlag != 0 {
  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(msgpcode.Uint32, n)
  30. }
  31. func (e *Encoder) encodeUint32Cond(n uint32) error {
  32. if e.flags&useCompactIntsFlag != 0 {
  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(msgpcode.Uint64, n)
  40. }
  41. func (e *Encoder) encodeUint64Cond(n uint64) error {
  42. if e.flags&useCompactIntsFlag != 0 {
  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(msgpcode.Int8, uint8(n))
  50. }
  51. func (e *Encoder) encodeInt8Cond(n int8) error {
  52. if e.flags&useCompactIntsFlag != 0 {
  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(msgpcode.Int16, uint16(n))
  60. }
  61. func (e *Encoder) encodeInt16Cond(n int16) error {
  62. if e.flags&useCompactIntsFlag != 0 {
  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(msgpcode.Int32, uint32(n))
  70. }
  71. func (e *Encoder) encodeInt32Cond(n int32) error {
  72. if e.flags&useCompactIntsFlag != 0 {
  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(msgpcode.Int64, uint64(n))
  80. }
  81. func (e *Encoder) encodeInt64Cond(n int64) error {
  82. if e.flags&useCompactIntsFlag != 0 {
  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(n)
  103. }
  104. // EncodeNumber encodes an int64 in 1, 2, 3, 5, or 9 bytes.
  105. // Type of the 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(msgpcode.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(n)
  123. }
  124. func (e *Encoder) EncodeFloat32(n float32) error {
  125. if e.flags&useCompactFloatsFlag != 0 {
  126. if float32(int64(n)) == n {
  127. return e.EncodeInt(int64(n))
  128. }
  129. }
  130. return e.write4(msgpcode.Float, math.Float32bits(n))
  131. }
  132. func (e *Encoder) EncodeFloat64(n float64) error {
  133. if e.flags&useCompactFloatsFlag != 0 {
  134. // Both NaN and Inf convert to int64(-0x8000000000000000)
  135. // If n is NaN then it never compares true with any other value
  136. // If n is Inf then it doesn't convert from int64 back to +/-Inf
  137. // In both cases the comparison works.
  138. if float64(int64(n)) == n {
  139. return e.EncodeInt(int64(n))
  140. }
  141. }
  142. return e.write8(msgpcode.Double, math.Float64bits(n))
  143. }
  144. func (e *Encoder) write1(code byte, n uint8) error {
  145. e.buf = e.buf[:2]
  146. e.buf[0] = code
  147. e.buf[1] = n
  148. return e.write(e.buf)
  149. }
  150. func (e *Encoder) write2(code byte, n uint16) error {
  151. e.buf = e.buf[:3]
  152. e.buf[0] = code
  153. e.buf[1] = byte(n >> 8)
  154. e.buf[2] = byte(n)
  155. return e.write(e.buf)
  156. }
  157. func (e *Encoder) write4(code byte, n uint32) error {
  158. e.buf = e.buf[:5]
  159. e.buf[0] = code
  160. e.buf[1] = byte(n >> 24)
  161. e.buf[2] = byte(n >> 16)
  162. e.buf[3] = byte(n >> 8)
  163. e.buf[4] = byte(n)
  164. return e.write(e.buf)
  165. }
  166. func (e *Encoder) write8(code byte, n uint64) error {
  167. e.buf = e.buf[:9]
  168. e.buf[0] = code
  169. e.buf[1] = byte(n >> 56)
  170. e.buf[2] = byte(n >> 48)
  171. e.buf[3] = byte(n >> 40)
  172. e.buf[4] = byte(n >> 32)
  173. e.buf[5] = byte(n >> 24)
  174. e.buf[6] = byte(n >> 16)
  175. e.buf[7] = byte(n >> 8)
  176. e.buf[8] = byte(n)
  177. return e.write(e.buf)
  178. }
  179. func encodeUintValue(e *Encoder, v reflect.Value) error {
  180. return e.EncodeUint(v.Uint())
  181. }
  182. func encodeIntValue(e *Encoder, v reflect.Value) error {
  183. return e.EncodeInt(v.Int())
  184. }
  185. func encodeUint8CondValue(e *Encoder, v reflect.Value) error {
  186. return e.encodeUint8Cond(uint8(v.Uint()))
  187. }
  188. func encodeUint16CondValue(e *Encoder, v reflect.Value) error {
  189. return e.encodeUint16Cond(uint16(v.Uint()))
  190. }
  191. func encodeUint32CondValue(e *Encoder, v reflect.Value) error {
  192. return e.encodeUint32Cond(uint32(v.Uint()))
  193. }
  194. func encodeUint64CondValue(e *Encoder, v reflect.Value) error {
  195. return e.encodeUint64Cond(v.Uint())
  196. }
  197. func encodeInt8CondValue(e *Encoder, v reflect.Value) error {
  198. return e.encodeInt8Cond(int8(v.Int()))
  199. }
  200. func encodeInt16CondValue(e *Encoder, v reflect.Value) error {
  201. return e.encodeInt16Cond(int16(v.Int()))
  202. }
  203. func encodeInt32CondValue(e *Encoder, v reflect.Value) error {
  204. return e.encodeInt32Cond(int32(v.Int()))
  205. }
  206. func encodeInt64CondValue(e *Encoder, v reflect.Value) error {
  207. return e.encodeInt64Cond(v.Int())
  208. }
  209. func encodeFloat32Value(e *Encoder, v reflect.Value) error {
  210. return e.EncodeFloat32(float32(v.Float()))
  211. }
  212. func encodeFloat64Value(e *Encoder, v reflect.Value) error {
  213. return e.EncodeFloat64(v.Float())
  214. }