iter_float.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. i := iter.head
  73. // first char
  74. if i == iter.tail {
  75. return iter.readFloat32SlowPath()
  76. }
  77. c := iter.buf[i]
  78. i++
  79. ind := floatDigits[c]
  80. switch ind {
  81. case invalidCharForNumber:
  82. return iter.readFloat32SlowPath()
  83. case endOfNumber:
  84. iter.ReportError("readFloat32", "empty number")
  85. return
  86. case dotInNumber:
  87. iter.ReportError("readFloat32", "leading dot is invalid")
  88. return
  89. case 0:
  90. if i == iter.tail {
  91. return iter.readFloat32SlowPath()
  92. }
  93. c = iter.buf[i]
  94. switch c {
  95. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  96. iter.ReportError("readFloat32", "leading zero is invalid")
  97. return
  98. }
  99. }
  100. value := uint64(ind)
  101. // chars before dot
  102. non_decimal_loop:
  103. for ; i < iter.tail; i++ {
  104. c = iter.buf[i]
  105. ind := floatDigits[c]
  106. switch ind {
  107. case invalidCharForNumber:
  108. return iter.readFloat32SlowPath()
  109. case endOfNumber:
  110. iter.head = i
  111. return float32(value)
  112. case dotInNumber:
  113. break non_decimal_loop
  114. }
  115. if value > uint64SafeToMultiple10 {
  116. return iter.readFloat32SlowPath()
  117. }
  118. value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
  119. }
  120. // chars after dot
  121. if c == '.' {
  122. i++
  123. decimalPlaces := 0
  124. if i == iter.tail {
  125. return iter.readFloat32SlowPath()
  126. }
  127. for ; i < iter.tail; i++ {
  128. c = iter.buf[i]
  129. ind := floatDigits[c]
  130. switch ind {
  131. case endOfNumber:
  132. if decimalPlaces > 0 && decimalPlaces < len(pow10) {
  133. iter.head = i
  134. return float32(float64(value) / float64(pow10[decimalPlaces]))
  135. }
  136. // too many decimal places
  137. return iter.readFloat32SlowPath()
  138. case invalidCharForNumber, dotInNumber:
  139. return iter.readFloat32SlowPath()
  140. }
  141. decimalPlaces++
  142. if value > uint64SafeToMultiple10 {
  143. return iter.readFloat32SlowPath()
  144. }
  145. value = (value << 3) + (value << 1) + uint64(ind)
  146. }
  147. }
  148. return iter.readFloat32SlowPath()
  149. }
  150. func (iter *Iterator) readNumberAsString() (ret string) {
  151. strBuf := [16]byte{}
  152. str := strBuf[0:0]
  153. load_loop:
  154. for {
  155. for i := iter.head; i < iter.tail; i++ {
  156. c := iter.buf[i]
  157. switch c {
  158. case '+', '-', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  159. str = append(str, c)
  160. continue
  161. default:
  162. iter.head = i
  163. break load_loop
  164. }
  165. }
  166. if !iter.loadMore() {
  167. break
  168. }
  169. }
  170. if iter.Error != nil && iter.Error != io.EOF {
  171. return
  172. }
  173. if len(str) == 0 {
  174. iter.ReportError("readNumberAsString", "invalid number")
  175. }
  176. return *(*string)(unsafe.Pointer(&str))
  177. }
  178. func (iter *Iterator) readFloat32SlowPath() (ret float32) {
  179. str := iter.readNumberAsString()
  180. if iter.Error != nil && iter.Error != io.EOF {
  181. return
  182. }
  183. errMsg := validateFloat(str)
  184. if errMsg != "" {
  185. iter.ReportError("readFloat32SlowPath", errMsg)
  186. return
  187. }
  188. val, err := strconv.ParseFloat(str, 32)
  189. if err != nil {
  190. iter.Error = err
  191. return
  192. }
  193. return float32(val)
  194. }
  195. // ReadFloat64 read float64
  196. func (iter *Iterator) ReadFloat64() (ret float64) {
  197. c := iter.nextToken()
  198. if c == '-' {
  199. return -iter.readPositiveFloat64()
  200. }
  201. iter.unreadByte()
  202. return iter.readPositiveFloat64()
  203. }
  204. func (iter *Iterator) readPositiveFloat64() (ret float64) {
  205. i := iter.head
  206. // first char
  207. if i == iter.tail {
  208. return iter.readFloat64SlowPath()
  209. }
  210. c := iter.buf[i]
  211. i++
  212. ind := floatDigits[c]
  213. switch ind {
  214. case invalidCharForNumber:
  215. return iter.readFloat64SlowPath()
  216. case endOfNumber:
  217. iter.ReportError("readFloat64", "empty number")
  218. return
  219. case dotInNumber:
  220. iter.ReportError("readFloat64", "leading dot is invalid")
  221. return
  222. case 0:
  223. if i == iter.tail {
  224. return iter.readFloat64SlowPath()
  225. }
  226. c = iter.buf[i]
  227. switch c {
  228. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  229. iter.ReportError("readFloat64", "leading zero is invalid")
  230. return
  231. }
  232. }
  233. value := uint64(ind)
  234. // chars before dot
  235. non_decimal_loop:
  236. for ; i < iter.tail; i++ {
  237. c = iter.buf[i]
  238. ind := floatDigits[c]
  239. switch ind {
  240. case invalidCharForNumber:
  241. return iter.readFloat64SlowPath()
  242. case endOfNumber:
  243. iter.head = i
  244. return float64(value)
  245. case dotInNumber:
  246. break non_decimal_loop
  247. }
  248. if value > uint64SafeToMultiple10 {
  249. return iter.readFloat64SlowPath()
  250. }
  251. value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
  252. }
  253. // chars after dot
  254. if c == '.' {
  255. i++
  256. decimalPlaces := 0
  257. if i == iter.tail {
  258. return iter.readFloat64SlowPath()
  259. }
  260. for ; i < iter.tail; i++ {
  261. c = iter.buf[i]
  262. ind := floatDigits[c]
  263. switch ind {
  264. case endOfNumber:
  265. if decimalPlaces > 0 && decimalPlaces < len(pow10) {
  266. iter.head = i
  267. return float64(value) / float64(pow10[decimalPlaces])
  268. }
  269. // too many decimal places
  270. return iter.readFloat64SlowPath()
  271. case invalidCharForNumber, dotInNumber:
  272. return iter.readFloat64SlowPath()
  273. }
  274. decimalPlaces++
  275. if value > uint64SafeToMultiple10 {
  276. return iter.readFloat64SlowPath()
  277. }
  278. value = (value << 3) + (value << 1) + uint64(ind)
  279. }
  280. }
  281. return iter.readFloat64SlowPath()
  282. }
  283. func (iter *Iterator) readFloat64SlowPath() (ret float64) {
  284. str := iter.readNumberAsString()
  285. if iter.Error != nil && iter.Error != io.EOF {
  286. return
  287. }
  288. errMsg := validateFloat(str)
  289. if errMsg != "" {
  290. iter.ReportError("readFloat64SlowPath", errMsg)
  291. return
  292. }
  293. val, err := strconv.ParseFloat(str, 64)
  294. if err != nil {
  295. iter.Error = err
  296. return
  297. }
  298. return val
  299. }
  300. func validateFloat(str string) string {
  301. // strconv.ParseFloat is not validating `1.` or `1.e1`
  302. if len(str) == 0 {
  303. return "empty number"
  304. }
  305. if str[0] == '-' {
  306. return "-- is not valid"
  307. }
  308. dotPos := strings.IndexByte(str, '.')
  309. if dotPos != -1 {
  310. if dotPos == len(str)-1 {
  311. return "dot can not be last character"
  312. }
  313. switch str[dotPos+1] {
  314. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  315. default:
  316. return "missing digit after dot"
  317. }
  318. }
  319. return ""
  320. }
  321. // ReadNumber read json.Number
  322. func (iter *Iterator) ReadNumber() (ret json.Number) {
  323. return json.Number(iter.readNumberAsString())
  324. }