feature_stream_int.go 8.1 KB

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