iter_int.go 8.5 KB

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