iter_int.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package jsoniter
  2. import (
  3. "math"
  4. "strconv"
  5. )
  6. var intDigits []int8
  7. const uint32SafeToMultiply10 = uint32(0xffffffff)/10 - 1
  8. const uint64SafeToMultiple10 = uint64(0xffffffffffffffff)/10 - 1
  9. const maxFloat64 = 1<<53 - 1
  10. func init() {
  11. intDigits = make([]int8, 256)
  12. for i := 0; i < len(intDigits); i++ {
  13. intDigits[i] = invalidCharForNumber
  14. }
  15. for i := int8('0'); i <= int8('9'); i++ {
  16. intDigits[i] = i - int8('0')
  17. }
  18. }
  19. // ReadUint read uint
  20. func (iter *Iterator) ReadUint() uint {
  21. if strconv.IntSize == 32 {
  22. return uint(iter.ReadUint32())
  23. }
  24. return uint(iter.ReadUint64())
  25. }
  26. // ReadInt read int
  27. func (iter *Iterator) ReadInt() int {
  28. if strconv.IntSize == 32 {
  29. return int(iter.ReadInt32())
  30. }
  31. return int(iter.ReadInt64())
  32. }
  33. // ReadInt8 read int8
  34. func (iter *Iterator) ReadInt8() (ret int8) {
  35. c := iter.nextToken()
  36. if c == '-' {
  37. val := iter.readUint32(iter.readByte())
  38. if val > math.MaxInt8+1 {
  39. iter.ReportError("ReadInt8", "overflow: "+strconv.FormatInt(int64(val), 10))
  40. return
  41. }
  42. return -int8(val)
  43. }
  44. val := iter.readUint32(c)
  45. if val > math.MaxInt8 {
  46. iter.ReportError("ReadInt8", "overflow: "+strconv.FormatInt(int64(val), 10))
  47. return
  48. }
  49. return int8(val)
  50. }
  51. // ReadUint8 read uint8
  52. func (iter *Iterator) ReadUint8() (ret uint8) {
  53. val := iter.readUint32(iter.nextToken())
  54. if val > math.MaxUint8 {
  55. iter.ReportError("ReadUint8", "overflow: "+strconv.FormatInt(int64(val), 10))
  56. return
  57. }
  58. return uint8(val)
  59. }
  60. // ReadInt16 read int16
  61. func (iter *Iterator) ReadInt16() (ret int16) {
  62. c := iter.nextToken()
  63. if c == '-' {
  64. val := iter.readUint32(iter.readByte())
  65. if val > math.MaxInt16+1 {
  66. iter.ReportError("ReadInt16", "overflow: "+strconv.FormatInt(int64(val), 10))
  67. return
  68. }
  69. return -int16(val)
  70. }
  71. val := iter.readUint32(c)
  72. if val > math.MaxInt16 {
  73. iter.ReportError("ReadInt16", "overflow: "+strconv.FormatInt(int64(val), 10))
  74. return
  75. }
  76. return int16(val)
  77. }
  78. // ReadUint16 read uint16
  79. func (iter *Iterator) ReadUint16() (ret uint16) {
  80. val := iter.readUint32(iter.nextToken())
  81. if val > math.MaxUint16 {
  82. iter.ReportError("ReadUint16", "overflow: "+strconv.FormatInt(int64(val), 10))
  83. return
  84. }
  85. return uint16(val)
  86. }
  87. // ReadInt32 read int32
  88. func (iter *Iterator) ReadInt32() (ret int32) {
  89. c := iter.nextToken()
  90. if c == '-' {
  91. val := iter.readUint32(iter.readByte())
  92. if val > math.MaxInt32+1 {
  93. iter.ReportError("ReadInt32", "overflow: "+strconv.FormatInt(int64(val), 10))
  94. return
  95. }
  96. return -int32(val)
  97. }
  98. val := iter.readUint32(c)
  99. if val > math.MaxInt32 {
  100. iter.ReportError("ReadInt32", "overflow: "+strconv.FormatInt(int64(val), 10))
  101. return
  102. }
  103. return int32(val)
  104. }
  105. // ReadUint32 read uint32
  106. func (iter *Iterator) ReadUint32() (ret uint32) {
  107. return iter.readUint32(iter.nextToken())
  108. }
  109. func (iter *Iterator) readUint32(c byte) (ret uint32) {
  110. ind := intDigits[c]
  111. if ind == 0 {
  112. iter.assertInteger()
  113. return 0 // single zero
  114. }
  115. if ind == invalidCharForNumber {
  116. iter.ReportError("readUint32", "unexpected character: "+string([]byte{byte(ind)}))
  117. return
  118. }
  119. value := uint32(ind)
  120. if iter.tail-iter.head > 10 {
  121. i := iter.head
  122. ind2 := intDigits[iter.buf[i]]
  123. if ind2 == invalidCharForNumber {
  124. iter.head = i
  125. iter.assertInteger()
  126. return value
  127. }
  128. i++
  129. ind3 := intDigits[iter.buf[i]]
  130. if ind3 == invalidCharForNumber {
  131. iter.head = i
  132. iter.assertInteger()
  133. return value*10 + uint32(ind2)
  134. }
  135. //iter.head = i + 1
  136. //value = value * 100 + uint32(ind2) * 10 + uint32(ind3)
  137. i++
  138. ind4 := intDigits[iter.buf[i]]
  139. if ind4 == invalidCharForNumber {
  140. iter.head = i
  141. iter.assertInteger()
  142. return value*100 + uint32(ind2)*10 + uint32(ind3)
  143. }
  144. i++
  145. ind5 := intDigits[iter.buf[i]]
  146. if ind5 == invalidCharForNumber {
  147. iter.head = i
  148. iter.assertInteger()
  149. return value*1000 + uint32(ind2)*100 + uint32(ind3)*10 + uint32(ind4)
  150. }
  151. i++
  152. ind6 := intDigits[iter.buf[i]]
  153. if ind6 == invalidCharForNumber {
  154. iter.head = i
  155. iter.assertInteger()
  156. return value*10000 + uint32(ind2)*1000 + uint32(ind3)*100 + uint32(ind4)*10 + uint32(ind5)
  157. }
  158. i++
  159. ind7 := intDigits[iter.buf[i]]
  160. if ind7 == invalidCharForNumber {
  161. iter.head = i
  162. iter.assertInteger()
  163. return value*100000 + uint32(ind2)*10000 + uint32(ind3)*1000 + uint32(ind4)*100 + uint32(ind5)*10 + uint32(ind6)
  164. }
  165. i++
  166. ind8 := intDigits[iter.buf[i]]
  167. if ind8 == invalidCharForNumber {
  168. iter.head = i
  169. iter.assertInteger()
  170. return value*1000000 + uint32(ind2)*100000 + uint32(ind3)*10000 + uint32(ind4)*1000 + uint32(ind5)*100 + uint32(ind6)*10 + uint32(ind7)
  171. }
  172. i++
  173. ind9 := intDigits[iter.buf[i]]
  174. value = value*10000000 + uint32(ind2)*1000000 + uint32(ind3)*100000 + uint32(ind4)*10000 + uint32(ind5)*1000 + uint32(ind6)*100 + uint32(ind7)*10 + uint32(ind8)
  175. iter.head = i
  176. if ind9 == invalidCharForNumber {
  177. iter.assertInteger()
  178. return value
  179. }
  180. }
  181. for {
  182. for i := iter.head; i < iter.tail; i++ {
  183. ind = intDigits[iter.buf[i]]
  184. if ind == invalidCharForNumber {
  185. iter.head = i
  186. iter.assertInteger()
  187. return value
  188. }
  189. if value > uint32SafeToMultiply10 {
  190. value2 := (value << 3) + (value << 1) + uint32(ind)
  191. if value2 < value {
  192. iter.ReportError("readUint32", "overflow")
  193. return
  194. }
  195. value = value2
  196. continue
  197. }
  198. value = (value << 3) + (value << 1) + uint32(ind)
  199. }
  200. if !iter.loadMore() {
  201. iter.assertInteger()
  202. return value
  203. }
  204. }
  205. }
  206. // ReadInt64 read int64
  207. func (iter *Iterator) ReadInt64() (ret int64) {
  208. c := iter.nextToken()
  209. if c == '-' {
  210. val := iter.readUint64(iter.readByte())
  211. if val > math.MaxInt64+1 {
  212. iter.ReportError("ReadInt64", "overflow: "+strconv.FormatUint(uint64(val), 10))
  213. return
  214. }
  215. return -int64(val)
  216. }
  217. val := iter.readUint64(c)
  218. if val > math.MaxInt64 {
  219. iter.ReportError("ReadInt64", "overflow: "+strconv.FormatUint(uint64(val), 10))
  220. return
  221. }
  222. return int64(val)
  223. }
  224. // ReadUint64 read uint64
  225. func (iter *Iterator) ReadUint64() uint64 {
  226. return iter.readUint64(iter.nextToken())
  227. }
  228. func (iter *Iterator) readUint64(c byte) (ret uint64) {
  229. ind := intDigits[c]
  230. if ind == 0 {
  231. iter.assertInteger()
  232. return 0 // single zero
  233. }
  234. if ind == invalidCharForNumber {
  235. iter.ReportError("readUint64", "unexpected character: "+string([]byte{byte(ind)}))
  236. return
  237. }
  238. value := uint64(ind)
  239. if iter.tail-iter.head > 10 {
  240. i := iter.head
  241. ind2 := intDigits[iter.buf[i]]
  242. if ind2 == invalidCharForNumber {
  243. iter.head = i
  244. iter.assertInteger()
  245. return value
  246. }
  247. i++
  248. ind3 := intDigits[iter.buf[i]]
  249. if ind3 == invalidCharForNumber {
  250. iter.head = i
  251. iter.assertInteger()
  252. return value*10 + uint64(ind2)
  253. }
  254. //iter.head = i + 1
  255. //value = value * 100 + uint32(ind2) * 10 + uint32(ind3)
  256. i++
  257. ind4 := intDigits[iter.buf[i]]
  258. if ind4 == invalidCharForNumber {
  259. iter.head = i
  260. iter.assertInteger()
  261. return value*100 + uint64(ind2)*10 + uint64(ind3)
  262. }
  263. i++
  264. ind5 := intDigits[iter.buf[i]]
  265. if ind5 == invalidCharForNumber {
  266. iter.head = i
  267. iter.assertInteger()
  268. return value*1000 + uint64(ind2)*100 + uint64(ind3)*10 + uint64(ind4)
  269. }
  270. i++
  271. ind6 := intDigits[iter.buf[i]]
  272. if ind6 == invalidCharForNumber {
  273. iter.head = i
  274. iter.assertInteger()
  275. return value*10000 + uint64(ind2)*1000 + uint64(ind3)*100 + uint64(ind4)*10 + uint64(ind5)
  276. }
  277. i++
  278. ind7 := intDigits[iter.buf[i]]
  279. if ind7 == invalidCharForNumber {
  280. iter.head = i
  281. iter.assertInteger()
  282. return value*100000 + uint64(ind2)*10000 + uint64(ind3)*1000 + uint64(ind4)*100 + uint64(ind5)*10 + uint64(ind6)
  283. }
  284. i++
  285. ind8 := intDigits[iter.buf[i]]
  286. if ind8 == invalidCharForNumber {
  287. iter.head = i
  288. iter.assertInteger()
  289. return value*1000000 + uint64(ind2)*100000 + uint64(ind3)*10000 + uint64(ind4)*1000 + uint64(ind5)*100 + uint64(ind6)*10 + uint64(ind7)
  290. }
  291. i++
  292. ind9 := intDigits[iter.buf[i]]
  293. value = value*10000000 + uint64(ind2)*1000000 + uint64(ind3)*100000 + uint64(ind4)*10000 + uint64(ind5)*1000 + uint64(ind6)*100 + uint64(ind7)*10 + uint64(ind8)
  294. iter.head = i
  295. if ind9 == invalidCharForNumber {
  296. iter.assertInteger()
  297. return value
  298. }
  299. }
  300. for {
  301. for i := iter.head; i < iter.tail; i++ {
  302. ind = intDigits[iter.buf[i]]
  303. if ind == invalidCharForNumber {
  304. iter.head = i
  305. iter.assertInteger()
  306. return value
  307. }
  308. if value > uint64SafeToMultiple10 {
  309. value2 := (value << 3) + (value << 1) + uint64(ind)
  310. if value2 < value {
  311. iter.ReportError("readUint64", "overflow")
  312. return
  313. }
  314. value = value2
  315. continue
  316. }
  317. value = (value << 3) + (value << 1) + uint64(ind)
  318. }
  319. if !iter.loadMore() {
  320. iter.assertInteger()
  321. return value
  322. }
  323. }
  324. }
  325. func (iter *Iterator) assertInteger() {
  326. if iter.head < iter.tail && iter.buf[iter.head] == '.' {
  327. iter.ReportError("assertInteger", "can not decode float as int")
  328. }
  329. }