feature_iter_int.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package jsoniter
  2. import (
  3. "strconv"
  4. )
  5. var intDigits []int8
  6. const uint32SafeToMultiply10 = uint32(0xffffffff)/10 - 1
  7. const uint64SafeToMultiple10 = uint64(0xffffffffffffffff)/10 - 1
  8. const int64Max = uint64(0x7fffffffffffffff)
  9. const int32Max = uint32(0x7fffffff)
  10. const int16Max = uint32(0x7fff)
  11. const uint16Max = uint32(0xffff)
  12. const int8Max = uint32(0x7fff)
  13. const uint8Max = uint32(0xffff)
  14. func init() {
  15. intDigits = make([]int8, 256)
  16. for i := 0; i < len(intDigits); i++ {
  17. intDigits[i] = invalidCharForNumber
  18. }
  19. for i := int8('0'); i <= int8('9'); i++ {
  20. intDigits[i] = i - int8('0')
  21. }
  22. }
  23. func (iter *Iterator) ReadUint() uint {
  24. return uint(iter.ReadUint64())
  25. }
  26. func (iter *Iterator) ReadInt() int {
  27. return int(iter.ReadInt64())
  28. }
  29. func (iter *Iterator) ReadInt8() (ret int8) {
  30. c := iter.nextToken()
  31. if c == '-' {
  32. val := iter.readUint32(iter.readByte())
  33. if val > int8Max+1 {
  34. iter.reportError("ReadInt8", "overflow: "+strconv.FormatInt(int64(val), 10))
  35. return
  36. }
  37. return -int8(val)
  38. } else {
  39. val := iter.readUint32(c)
  40. if val > int8Max {
  41. iter.reportError("ReadInt8", "overflow: "+strconv.FormatInt(int64(val), 10))
  42. return
  43. }
  44. return int8(val)
  45. }
  46. }
  47. func (iter *Iterator) ReadUint8() (ret uint8) {
  48. val := iter.readUint32(iter.nextToken())
  49. if val > uint8Max {
  50. iter.reportError("ReadUint8", "overflow: "+strconv.FormatInt(int64(val), 10))
  51. return
  52. }
  53. return uint8(val)
  54. }
  55. func (iter *Iterator) ReadInt16() (ret int16) {
  56. c := iter.nextToken()
  57. if c == '-' {
  58. val := iter.readUint32(iter.readByte())
  59. if val > int16Max+1 {
  60. iter.reportError("ReadInt16", "overflow: "+strconv.FormatInt(int64(val), 10))
  61. return
  62. }
  63. return -int16(val)
  64. } else {
  65. val := iter.readUint32(c)
  66. if val > int16Max {
  67. iter.reportError("ReadInt16", "overflow: "+strconv.FormatInt(int64(val), 10))
  68. return
  69. }
  70. return int16(val)
  71. }
  72. }
  73. func (iter *Iterator) ReadUint16() (ret uint16) {
  74. val := iter.readUint32(iter.nextToken())
  75. if val > uint16Max {
  76. iter.reportError("ReadUint16", "overflow: "+strconv.FormatInt(int64(val), 10))
  77. return
  78. }
  79. return uint16(val)
  80. }
  81. func (iter *Iterator) ReadInt32() (ret int32) {
  82. c := iter.nextToken()
  83. if c == '-' {
  84. val := iter.readUint32(iter.readByte())
  85. if val > int32Max+1 {
  86. iter.reportError("ReadInt32", "overflow: "+strconv.FormatInt(int64(val), 10))
  87. return
  88. }
  89. return -int32(val)
  90. } else {
  91. val := iter.readUint32(c)
  92. if val > int32Max {
  93. iter.reportError("ReadInt32", "overflow: "+strconv.FormatInt(int64(val), 10))
  94. return
  95. }
  96. return int32(val)
  97. }
  98. }
  99. func (iter *Iterator) ReadUint32() (ret uint32) {
  100. return iter.readUint32(iter.nextToken())
  101. }
  102. func (iter *Iterator) readUint32(c byte) (ret uint32) {
  103. ind := intDigits[c]
  104. if ind == 0 {
  105. return 0 // single zero
  106. }
  107. if ind == invalidCharForNumber {
  108. iter.reportError("readUint32", "unexpected character: "+string([]byte{byte(ind)}))
  109. return
  110. }
  111. value := uint32(ind)
  112. if iter.tail-iter.head > 10 {
  113. i := iter.head
  114. ind2 := intDigits[iter.buf[i]]
  115. if ind2 == invalidCharForNumber {
  116. iter.head = i
  117. return value
  118. }
  119. i++
  120. ind3 := intDigits[iter.buf[i]]
  121. if ind3 == invalidCharForNumber {
  122. iter.head = i
  123. return value*10 + uint32(ind2)
  124. }
  125. //iter.head = i + 1
  126. //value = value * 100 + uint32(ind2) * 10 + uint32(ind3)
  127. i++
  128. ind4 := intDigits[iter.buf[i]]
  129. if ind4 == invalidCharForNumber {
  130. iter.head = i
  131. return value*100 + uint32(ind2)*10 + uint32(ind3)
  132. }
  133. i++
  134. ind5 := intDigits[iter.buf[i]]
  135. if ind5 == invalidCharForNumber {
  136. iter.head = i
  137. return value*1000 + uint32(ind2)*100 + uint32(ind3)*10 + uint32(ind4)
  138. }
  139. i++
  140. ind6 := intDigits[iter.buf[i]]
  141. if ind6 == invalidCharForNumber {
  142. iter.head = i
  143. return value*10000 + uint32(ind2)*1000 + uint32(ind3)*100 + uint32(ind4)*10 + uint32(ind5)
  144. }
  145. i++
  146. ind7 := intDigits[iter.buf[i]]
  147. if ind7 == invalidCharForNumber {
  148. iter.head = i
  149. return value*100000 + uint32(ind2)*10000 + uint32(ind3)*1000 + uint32(ind4)*100 + uint32(ind5)*10 + uint32(ind6)
  150. }
  151. i++
  152. ind8 := intDigits[iter.buf[i]]
  153. if ind8 == invalidCharForNumber {
  154. iter.head = i
  155. return value*1000000 + uint32(ind2)*100000 + uint32(ind3)*10000 + uint32(ind4)*1000 + uint32(ind5)*100 + uint32(ind6)*10 + uint32(ind7)
  156. }
  157. i++
  158. ind9 := intDigits[iter.buf[i]]
  159. value = value*10000000 + uint32(ind2)*1000000 + uint32(ind3)*100000 + uint32(ind4)*10000 + uint32(ind5)*1000 + uint32(ind6)*100 + uint32(ind7)*10 + uint32(ind8)
  160. iter.head = i
  161. if ind9 == invalidCharForNumber {
  162. return value
  163. }
  164. }
  165. for {
  166. for i := iter.head; i < iter.tail; i++ {
  167. ind = intDigits[iter.buf[i]]
  168. if ind == invalidCharForNumber {
  169. iter.head = i
  170. return value
  171. }
  172. if value > uint32SafeToMultiply10 {
  173. value2 := (value << 3) + (value << 1) + uint32(ind)
  174. if value2 < value {
  175. iter.reportError("readUint32", "overflow")
  176. return
  177. } else {
  178. value = value2
  179. continue
  180. }
  181. }
  182. value = (value << 3) + (value << 1) + uint32(ind)
  183. }
  184. if !iter.loadMore() {
  185. return value
  186. }
  187. }
  188. }
  189. func (iter *Iterator) ReadInt64() (ret int64) {
  190. c := iter.nextToken()
  191. if c == '-' {
  192. val := iter.readUint64(iter.readByte())
  193. if val > int64Max+1 {
  194. iter.reportError("ReadInt64", "overflow: "+strconv.FormatUint(uint64(val), 10))
  195. return
  196. }
  197. return -int64(val)
  198. } else {
  199. val := iter.readUint64(c)
  200. if val > int64Max {
  201. iter.reportError("ReadInt64", "overflow: "+strconv.FormatUint(uint64(val), 10))
  202. return
  203. }
  204. return int64(val)
  205. }
  206. }
  207. func (iter *Iterator) ReadUint64() uint64 {
  208. return iter.readUint64(iter.nextToken())
  209. }
  210. func (iter *Iterator) readUint64(c byte) (ret uint64) {
  211. ind := intDigits[c]
  212. if ind == 0 {
  213. return 0 // single zero
  214. }
  215. if ind == invalidCharForNumber {
  216. iter.reportError("readUint64", "unexpected character: "+string([]byte{byte(ind)}))
  217. return
  218. }
  219. value := uint64(ind)
  220. for {
  221. for i := iter.head; i < iter.tail; i++ {
  222. ind = intDigits[iter.buf[i]]
  223. if ind == invalidCharForNumber {
  224. iter.head = i
  225. return value
  226. }
  227. if value > uint64SafeToMultiple10 {
  228. value2 := (value << 3) + (value << 1) + uint64(ind)
  229. if value2 < value {
  230. iter.reportError("readUint64", "overflow")
  231. return
  232. } else {
  233. value = value2
  234. continue
  235. }
  236. }
  237. value = (value << 3) + (value << 1) + uint64(ind)
  238. }
  239. if !iter.loadMore() {
  240. return value
  241. }
  242. }
  243. }