feature_stream_int.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package jsoniter
  2. var DIGITS []uint32
  3. func init() {
  4. DIGITS = make([]uint32, 1000)
  5. for i := uint32(0); i < 1000; i++ {
  6. DIGITS[i] = (((i / 100) + '0') << 16) + ((((i / 10) % 10) + '0') << 8) + i%10 + '0'
  7. if i < 10 {
  8. DIGITS[i] += 2 << 24
  9. } else if i < 100 {
  10. DIGITS[i] += 1 << 24
  11. }
  12. }
  13. }
  14. func writeFirstBuf(buf []byte, v uint32, n int) int {
  15. start := v >> 24
  16. if start == 0 {
  17. buf[n] = byte(v >> 16)
  18. n++
  19. buf[n] = byte(v >> 8)
  20. n++
  21. } else if start == 1 {
  22. buf[n] = byte(v >> 8)
  23. n++
  24. }
  25. buf[n] = byte(v)
  26. n++
  27. return n
  28. }
  29. func writeBuf(buf []byte, v uint32, n int) {
  30. buf[n] = byte(v >> 16)
  31. buf[n+1] = byte(v >> 8)
  32. buf[n+2] = byte(v)
  33. }
  34. func (stream *Stream) WriteUint8(val uint8) {
  35. stream.ensure(3)
  36. stream.n = writeFirstBuf(stream.buf, DIGITS[val], stream.n)
  37. }
  38. func (stream *Stream) WriteInt8(nval int8) {
  39. stream.ensure(4)
  40. n := stream.n
  41. var val uint8
  42. if nval < 0 {
  43. val = uint8(-nval)
  44. stream.buf[n] = '-'
  45. n++
  46. } else {
  47. val = uint8(nval)
  48. }
  49. stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
  50. }
  51. func (stream *Stream) WriteUint16(val uint16) {
  52. stream.ensure(5)
  53. q1 := val / 1000
  54. if q1 == 0 {
  55. stream.n = writeFirstBuf(stream.buf, DIGITS[val], stream.n)
  56. return
  57. }
  58. r1 := val - q1*1000
  59. n := writeFirstBuf(stream.buf, DIGITS[q1], stream.n)
  60. writeBuf(stream.buf, DIGITS[r1], n)
  61. stream.n = n + 3
  62. return
  63. }
  64. func (stream *Stream) WriteInt16(nval int16) {
  65. stream.ensure(6)
  66. n := stream.n
  67. var val uint16
  68. if nval < 0 {
  69. val = uint16(-nval)
  70. stream.buf[n] = '-'
  71. n++
  72. } else {
  73. val = uint16(nval)
  74. }
  75. q1 := val / 1000
  76. if q1 == 0 {
  77. stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
  78. return
  79. }
  80. r1 := val - q1*1000
  81. n = writeFirstBuf(stream.buf, DIGITS[q1], n)
  82. writeBuf(stream.buf, DIGITS[r1], n)
  83. stream.n = n + 3
  84. return
  85. }
  86. func (stream *Stream) WriteUint32(val uint32) {
  87. stream.ensure(10)
  88. n := stream.n
  89. q1 := val / 1000
  90. if q1 == 0 {
  91. stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
  92. return
  93. }
  94. r1 := val - q1*1000
  95. q2 := q1 / 1000
  96. if q2 == 0 {
  97. n := writeFirstBuf(stream.buf, DIGITS[q1], n)
  98. writeBuf(stream.buf, DIGITS[r1], n)
  99. stream.n = n + 3
  100. return
  101. }
  102. r2 := q1 - q2*1000
  103. q3 := q2 / 1000
  104. if q3 == 0 {
  105. n = writeFirstBuf(stream.buf, DIGITS[q2], n)
  106. } else {
  107. r3 := q2 - q3*1000
  108. stream.buf[n] = byte(q3 + '0')
  109. n++
  110. writeBuf(stream.buf, DIGITS[r3], n)
  111. n += 3
  112. }
  113. writeBuf(stream.buf, DIGITS[r2], n)
  114. writeBuf(stream.buf, DIGITS[r1], n+3)
  115. stream.n = n + 6
  116. }
  117. func (stream *Stream) WriteInt32(nval int32) {
  118. stream.ensure(11)
  119. n := stream.n
  120. var val uint32
  121. if nval < 0 {
  122. val = uint32(-nval)
  123. stream.buf[n] = '-'
  124. n++
  125. } else {
  126. val = uint32(nval)
  127. }
  128. q1 := val / 1000
  129. if q1 == 0 {
  130. stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
  131. return
  132. }
  133. r1 := val - q1*1000
  134. q2 := q1 / 1000
  135. if q2 == 0 {
  136. n := writeFirstBuf(stream.buf, DIGITS[q1], n)
  137. writeBuf(stream.buf, DIGITS[r1], n)
  138. stream.n = n + 3
  139. return
  140. }
  141. r2 := q1 - q2*1000
  142. q3 := q2 / 1000
  143. if q3 == 0 {
  144. n = writeFirstBuf(stream.buf, DIGITS[q2], n)
  145. } else {
  146. r3 := q2 - q3*1000
  147. stream.buf[n] = byte(q3 + '0')
  148. n++
  149. writeBuf(stream.buf, DIGITS[r3], n)
  150. n += 3
  151. }
  152. writeBuf(stream.buf, DIGITS[r2], n)
  153. writeBuf(stream.buf, DIGITS[r1], n+3)
  154. stream.n = n + 6
  155. }
  156. func (stream *Stream) WriteUint64(val uint64) {
  157. stream.ensure(20)
  158. n := stream.n
  159. q1 := val / 1000
  160. if q1 == 0 {
  161. stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
  162. return
  163. }
  164. r1 := val - q1*1000
  165. q2 := q1 / 1000
  166. if q2 == 0 {
  167. n := writeFirstBuf(stream.buf, DIGITS[q1], n)
  168. writeBuf(stream.buf, DIGITS[r1], n)
  169. stream.n = n + 3
  170. return
  171. }
  172. r2 := q1 - q2*1000
  173. q3 := q2 / 1000
  174. if q3 == 0 {
  175. n = writeFirstBuf(stream.buf, DIGITS[q2], n)
  176. writeBuf(stream.buf, DIGITS[r2], n)
  177. writeBuf(stream.buf, DIGITS[r1], n+3)
  178. stream.n = n + 6
  179. return
  180. }
  181. r3 := q2 - q3*1000
  182. q4 := q3 / 1000
  183. if q4 == 0 {
  184. n = writeFirstBuf(stream.buf, DIGITS[q3], n)
  185. writeBuf(stream.buf, DIGITS[r3], n)
  186. writeBuf(stream.buf, DIGITS[r2], n+3)
  187. writeBuf(stream.buf, DIGITS[r1], n+6)
  188. stream.n = n + 9
  189. return
  190. }
  191. r4 := q3 - q4*1000
  192. q5 := q4 / 1000
  193. if q5 == 0 {
  194. n = writeFirstBuf(stream.buf, DIGITS[q4], n)
  195. writeBuf(stream.buf, DIGITS[r4], n)
  196. writeBuf(stream.buf, DIGITS[r3], n+3)
  197. writeBuf(stream.buf, DIGITS[r2], n+6)
  198. writeBuf(stream.buf, DIGITS[r1], n+9)
  199. stream.n = n + 12
  200. return
  201. }
  202. r5 := q4 - q5*1000
  203. q6 := q5 / 1000
  204. if q6 == 0 {
  205. n = writeFirstBuf(stream.buf, DIGITS[q5], n)
  206. } else {
  207. n = writeFirstBuf(stream.buf, DIGITS[q6], n)
  208. r6 := q5 - q6*1000
  209. writeBuf(stream.buf, DIGITS[r6], n)
  210. n += 3
  211. }
  212. writeBuf(stream.buf, DIGITS[r5], n)
  213. writeBuf(stream.buf, DIGITS[r4], n+3)
  214. writeBuf(stream.buf, DIGITS[r3], n+6)
  215. writeBuf(stream.buf, DIGITS[r2], n+9)
  216. writeBuf(stream.buf, DIGITS[r1], n+12)
  217. stream.n = n + 15
  218. }
  219. func (stream *Stream) WriteInt64(nval int64) {
  220. stream.ensure(20)
  221. n := stream.n
  222. var val uint64
  223. if nval < 0 {
  224. val = uint64(-nval)
  225. stream.buf[n] = '-'
  226. n++
  227. } else {
  228. val = uint64(nval)
  229. }
  230. q1 := val / 1000
  231. if q1 == 0 {
  232. stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
  233. return
  234. }
  235. r1 := val - q1*1000
  236. q2 := q1 / 1000
  237. if q2 == 0 {
  238. n := writeFirstBuf(stream.buf, DIGITS[q1], n)
  239. writeBuf(stream.buf, DIGITS[r1], n)
  240. stream.n = n + 3
  241. return
  242. }
  243. r2 := q1 - q2*1000
  244. q3 := q2 / 1000
  245. if q3 == 0 {
  246. n = writeFirstBuf(stream.buf, DIGITS[q2], n)
  247. writeBuf(stream.buf, DIGITS[r2], n)
  248. writeBuf(stream.buf, DIGITS[r1], n+3)
  249. stream.n = n + 6
  250. return
  251. }
  252. r3 := q2 - q3*1000
  253. q4 := q3 / 1000
  254. if q4 == 0 {
  255. n = writeFirstBuf(stream.buf, DIGITS[q3], n)
  256. writeBuf(stream.buf, DIGITS[r3], n)
  257. writeBuf(stream.buf, DIGITS[r2], n+3)
  258. writeBuf(stream.buf, DIGITS[r1], n+6)
  259. stream.n = n + 9
  260. return
  261. }
  262. r4 := q3 - q4*1000
  263. q5 := q4 / 1000
  264. if q5 == 0 {
  265. n = writeFirstBuf(stream.buf, DIGITS[q4], n)
  266. writeBuf(stream.buf, DIGITS[r4], n)
  267. writeBuf(stream.buf, DIGITS[r3], n+3)
  268. writeBuf(stream.buf, DIGITS[r2], n+6)
  269. writeBuf(stream.buf, DIGITS[r1], n+9)
  270. stream.n = n + 12
  271. return
  272. }
  273. r5 := q4 - q5*1000
  274. q6 := q5 / 1000
  275. if q6 == 0 {
  276. n = writeFirstBuf(stream.buf, DIGITS[q5], n)
  277. } else {
  278. stream.buf[n] = byte(q6 + '0')
  279. n++
  280. r6 := q5 - q6*1000
  281. writeBuf(stream.buf, DIGITS[r6], n)
  282. n += 3
  283. }
  284. writeBuf(stream.buf, DIGITS[r5], n)
  285. writeBuf(stream.buf, DIGITS[r4], n+3)
  286. writeBuf(stream.buf, DIGITS[r3], n+6)
  287. writeBuf(stream.buf, DIGITS[r2], n+9)
  288. writeBuf(stream.buf, DIGITS[r1], n+12)
  289. stream.n = n + 15
  290. }
  291. func (stream *Stream) WriteInt(val int) {
  292. stream.WriteInt64(int64(val))
  293. }
  294. func (stream *Stream) WriteUint(val uint) {
  295. stream.WriteUint64(uint64(val))
  296. }