iter_float.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "io"
  5. "math/big"
  6. "strconv"
  7. "strings"
  8. "unsafe"
  9. )
  10. var floatDigits []int8
  11. const invalidCharForNumber = int8(-1)
  12. const endOfNumber = int8(-2)
  13. const dotInNumber = int8(-3)
  14. func init() {
  15. floatDigits = make([]int8, 256)
  16. for i := 0; i < len(floatDigits); i++ {
  17. floatDigits[i] = invalidCharForNumber
  18. }
  19. for i := int8('0'); i <= int8('9'); i++ {
  20. floatDigits[i] = i - int8('0')
  21. }
  22. floatDigits[','] = endOfNumber
  23. floatDigits[']'] = endOfNumber
  24. floatDigits['}'] = endOfNumber
  25. floatDigits[' '] = endOfNumber
  26. floatDigits['\t'] = endOfNumber
  27. floatDigits['\n'] = endOfNumber
  28. floatDigits['.'] = dotInNumber
  29. }
  30. // ReadBigFloat read big.Float
  31. func (iter *Iterator) ReadBigFloat() (ret *big.Float) {
  32. str := iter.readNumberAsString()
  33. if iter.Error != nil && iter.Error != io.EOF {
  34. return nil
  35. }
  36. prec := 64
  37. if len(str) > prec {
  38. prec = len(str)
  39. }
  40. val, _, err := big.ParseFloat(str, 10, uint(prec), big.ToZero)
  41. if err != nil {
  42. iter.Error = err
  43. return nil
  44. }
  45. return val
  46. }
  47. // ReadBigInt read big.Int
  48. func (iter *Iterator) ReadBigInt() (ret *big.Int) {
  49. str := iter.readNumberAsString()
  50. if iter.Error != nil && iter.Error != io.EOF {
  51. return nil
  52. }
  53. ret = big.NewInt(0)
  54. var success bool
  55. ret, success = ret.SetString(str, 10)
  56. if !success {
  57. iter.ReportError("ReadBigInt", "invalid big int")
  58. return nil
  59. }
  60. return ret
  61. }
  62. //ReadFloat32 read float32
  63. func (iter *Iterator) ReadFloat32() (ret float32) {
  64. c := iter.nextToken()
  65. if c == '-' {
  66. return -iter.readPositiveFloat32()
  67. }
  68. iter.unreadByte()
  69. return iter.readPositiveFloat32()
  70. }
  71. func (iter *Iterator) readPositiveFloat32() (ret float32) {
  72. value := uint64(0)
  73. c := byte(' ')
  74. i := iter.head
  75. // first char
  76. if i == iter.tail {
  77. return iter.readFloat32SlowPath()
  78. }
  79. c = iter.buf[i]
  80. i++
  81. ind := floatDigits[c]
  82. switch ind {
  83. case invalidCharForNumber:
  84. return iter.readFloat32SlowPath()
  85. case endOfNumber:
  86. iter.ReportError("readFloat32", "empty number")
  87. return
  88. case dotInNumber:
  89. iter.ReportError("readFloat32", "leading dot is invalid")
  90. return
  91. case 0:
  92. if i == iter.tail {
  93. return iter.readFloat32SlowPath()
  94. }
  95. c = iter.buf[i]
  96. switch c {
  97. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  98. iter.ReportError("readFloat32", "leading zero is invalid")
  99. return
  100. }
  101. }
  102. value = uint64(ind)
  103. // chars before dot
  104. non_decimal_loop:
  105. for ; i < iter.tail; i++ {
  106. c = iter.buf[i]
  107. ind := floatDigits[c]
  108. switch ind {
  109. case invalidCharForNumber:
  110. return iter.readFloat32SlowPath()
  111. case endOfNumber:
  112. iter.head = i
  113. return float32(value)
  114. case dotInNumber:
  115. break non_decimal_loop
  116. }
  117. if value > uint64SafeToMultiple10 {
  118. return iter.readFloat32SlowPath()
  119. }
  120. value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
  121. }
  122. // chars after dot
  123. if c == '.' {
  124. i++
  125. decimalPlaces := 0
  126. if i == iter.tail {
  127. return iter.readFloat32SlowPath()
  128. }
  129. for ; i < iter.tail; i++ {
  130. c = iter.buf[i]
  131. ind := floatDigits[c]
  132. switch ind {
  133. case endOfNumber:
  134. if decimalPlaces > 0 && decimalPlaces < len(pow10) {
  135. iter.head = i
  136. return float32(float64(value) / float64(pow10[decimalPlaces]))
  137. }
  138. // too many decimal places
  139. return iter.readFloat32SlowPath()
  140. case invalidCharForNumber, dotInNumber:
  141. return iter.readFloat32SlowPath()
  142. }
  143. decimalPlaces++
  144. if value > uint64SafeToMultiple10 {
  145. return iter.readFloat32SlowPath()
  146. }
  147. value = (value << 3) + (value << 1) + uint64(ind)
  148. }
  149. }
  150. return iter.readFloat32SlowPath()
  151. }
  152. func (iter *Iterator) readNumberAsString() (ret string) {
  153. strBuf := [16]byte{}
  154. str := strBuf[0:0]
  155. load_loop:
  156. for {
  157. for i := iter.head; i < iter.tail; i++ {
  158. c := iter.buf[i]
  159. switch c {
  160. case '+', '-', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  161. str = append(str, c)
  162. continue
  163. default:
  164. iter.head = i
  165. break load_loop
  166. }
  167. }
  168. if !iter.loadMore() {
  169. break
  170. }
  171. }
  172. if iter.Error != nil && iter.Error != io.EOF {
  173. return
  174. }
  175. if len(str) == 0 {
  176. iter.ReportError("readNumberAsString", "invalid number")
  177. }
  178. return *(*string)(unsafe.Pointer(&str))
  179. }
  180. func (iter *Iterator) readFloat32SlowPath() (ret float32) {
  181. str := iter.readNumberAsString()
  182. if iter.Error != nil && iter.Error != io.EOF {
  183. return
  184. }
  185. errMsg := validateFloat(str)
  186. if errMsg != "" {
  187. iter.ReportError("readFloat32SlowPath", errMsg)
  188. return
  189. }
  190. val, err := strconv.ParseFloat(str, 32)
  191. if err != nil {
  192. iter.Error = err
  193. return
  194. }
  195. return float32(val)
  196. }
  197. // ReadFloat64 read float64
  198. func (iter *Iterator) ReadFloat64() (ret float64) {
  199. c := iter.nextToken()
  200. if c == '-' {
  201. return -iter.readPositiveFloat64()
  202. }
  203. iter.unreadByte()
  204. return iter.readPositiveFloat64()
  205. }
  206. func (iter *Iterator) readPositiveFloat64() (ret float64) {
  207. value := uint64(0)
  208. c := byte(' ')
  209. i := iter.head
  210. // first char
  211. if i == iter.tail {
  212. return iter.readFloat64SlowPath()
  213. }
  214. c = iter.buf[i]
  215. i++
  216. ind := floatDigits[c]
  217. switch ind {
  218. case invalidCharForNumber:
  219. return iter.readFloat64SlowPath()
  220. case endOfNumber:
  221. iter.ReportError("readFloat64", "empty number")
  222. return
  223. case dotInNumber:
  224. iter.ReportError("readFloat64", "leading dot is invalid")
  225. return
  226. case 0:
  227. if i == iter.tail {
  228. return iter.readFloat64SlowPath()
  229. }
  230. c = iter.buf[i]
  231. switch c {
  232. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  233. iter.ReportError("readFloat64", "leading zero is invalid")
  234. return
  235. }
  236. }
  237. value = uint64(ind)
  238. // chars before dot
  239. non_decimal_loop:
  240. for ; i < iter.tail; i++ {
  241. c = iter.buf[i]
  242. ind := floatDigits[c]
  243. switch ind {
  244. case invalidCharForNumber:
  245. return iter.readFloat64SlowPath()
  246. case endOfNumber:
  247. iter.head = i
  248. return float64(value)
  249. case dotInNumber:
  250. break non_decimal_loop
  251. }
  252. if value > uint64SafeToMultiple10 {
  253. return iter.readFloat64SlowPath()
  254. }
  255. value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
  256. }
  257. // chars after dot
  258. if c == '.' {
  259. i++
  260. decimalPlaces := 0
  261. if i == iter.tail {
  262. return iter.readFloat64SlowPath()
  263. }
  264. for ; i < iter.tail; i++ {
  265. c = iter.buf[i]
  266. ind := floatDigits[c]
  267. switch ind {
  268. case endOfNumber:
  269. if decimalPlaces > 0 && decimalPlaces < len(pow10) {
  270. iter.head = i
  271. return float64(value) / float64(pow10[decimalPlaces])
  272. }
  273. // too many decimal places
  274. return iter.readFloat64SlowPath()
  275. case invalidCharForNumber, dotInNumber:
  276. return iter.readFloat64SlowPath()
  277. }
  278. decimalPlaces++
  279. if value > uint64SafeToMultiple10 {
  280. return iter.readFloat64SlowPath()
  281. }
  282. value = (value << 3) + (value << 1) + uint64(ind)
  283. }
  284. }
  285. return iter.readFloat64SlowPath()
  286. }
  287. func (iter *Iterator) readFloat64SlowPath() (ret float64) {
  288. str := iter.readNumberAsString()
  289. if iter.Error != nil && iter.Error != io.EOF {
  290. return
  291. }
  292. errMsg := validateFloat(str)
  293. if errMsg != "" {
  294. iter.ReportError("readFloat64SlowPath", errMsg)
  295. return
  296. }
  297. val, err := strconv.ParseFloat(str, 64)
  298. if err != nil {
  299. iter.Error = err
  300. return
  301. }
  302. return val
  303. }
  304. func validateFloat(str string) string {
  305. // strconv.ParseFloat is not validating `1.` or `1.e1`
  306. if len(str) == 0 {
  307. return "empty number"
  308. }
  309. if str[0] == '-' {
  310. return "-- is not valid"
  311. }
  312. dotPos := strings.IndexByte(str, '.')
  313. if dotPos != -1 {
  314. if dotPos == len(str)-1 {
  315. return "dot can not be last character"
  316. }
  317. switch str[dotPos+1] {
  318. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  319. default:
  320. return "missing digit after dot"
  321. }
  322. }
  323. return ""
  324. }
  325. // ReadNumber read json.Number
  326. func (iter *Iterator) ReadNumber() (ret json.Number) {
  327. return json.Number(iter.readNumberAsString())
  328. }