另客网go项目公用的代码库

iter_float.go 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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:
  141. fallthrough
  142. case dotInNumber:
  143. return iter.readFloat32SlowPath()
  144. }
  145. decimalPlaces++
  146. if value > uint64SafeToMultiple10 {
  147. return iter.readFloat32SlowPath()
  148. }
  149. value = (value << 3) + (value << 1) + uint64(ind)
  150. }
  151. }
  152. return iter.readFloat32SlowPath()
  153. }
  154. func (iter *Iterator) readNumberAsString() (ret string) {
  155. strBuf := [16]byte{}
  156. str := strBuf[0:0]
  157. load_loop:
  158. for {
  159. for i := iter.head; i < iter.tail; i++ {
  160. c := iter.buf[i]
  161. switch c {
  162. case '+', '-', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  163. str = append(str, c)
  164. continue
  165. default:
  166. iter.head = i
  167. break load_loop
  168. }
  169. }
  170. if !iter.loadMore() {
  171. break
  172. }
  173. }
  174. if iter.Error != nil && iter.Error != io.EOF {
  175. return
  176. }
  177. if len(str) == 0 {
  178. iter.ReportError("readNumberAsString", "invalid number")
  179. }
  180. return *(*string)(unsafe.Pointer(&str))
  181. }
  182. func (iter *Iterator) readFloat32SlowPath() (ret float32) {
  183. str := iter.readNumberAsString()
  184. if iter.Error != nil && iter.Error != io.EOF {
  185. return
  186. }
  187. errMsg := validateFloat(str)
  188. if errMsg != "" {
  189. iter.ReportError("readFloat32SlowPath", errMsg)
  190. return
  191. }
  192. val, err := strconv.ParseFloat(str, 32)
  193. if err != nil {
  194. iter.Error = err
  195. return
  196. }
  197. return float32(val)
  198. }
  199. // ReadFloat64 read float64
  200. func (iter *Iterator) ReadFloat64() (ret float64) {
  201. c := iter.nextToken()
  202. if c == '-' {
  203. return -iter.readPositiveFloat64()
  204. }
  205. iter.unreadByte()
  206. return iter.readPositiveFloat64()
  207. }
  208. func (iter *Iterator) readPositiveFloat64() (ret float64) {
  209. value := uint64(0)
  210. c := byte(' ')
  211. i := iter.head
  212. // first char
  213. if i == iter.tail {
  214. return iter.readFloat64SlowPath()
  215. }
  216. c = iter.buf[i]
  217. i++
  218. ind := floatDigits[c]
  219. switch ind {
  220. case invalidCharForNumber:
  221. return iter.readFloat64SlowPath()
  222. case endOfNumber:
  223. iter.ReportError("readFloat64", "empty number")
  224. return
  225. case dotInNumber:
  226. iter.ReportError("readFloat64", "leading dot is invalid")
  227. return
  228. case 0:
  229. if i == iter.tail {
  230. return iter.readFloat64SlowPath()
  231. }
  232. c = iter.buf[i]
  233. switch c {
  234. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  235. iter.ReportError("readFloat64", "leading zero is invalid")
  236. return
  237. }
  238. }
  239. value = uint64(ind)
  240. // chars before dot
  241. non_decimal_loop:
  242. for ; i < iter.tail; i++ {
  243. c = iter.buf[i]
  244. ind := floatDigits[c]
  245. switch ind {
  246. case invalidCharForNumber:
  247. return iter.readFloat64SlowPath()
  248. case endOfNumber:
  249. iter.head = i
  250. return float64(value)
  251. case dotInNumber:
  252. break non_decimal_loop
  253. }
  254. if value > uint64SafeToMultiple10 {
  255. return iter.readFloat64SlowPath()
  256. }
  257. value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
  258. }
  259. // chars after dot
  260. if c == '.' {
  261. i++
  262. decimalPlaces := 0
  263. if i == iter.tail {
  264. return iter.readFloat64SlowPath()
  265. }
  266. for ; i < iter.tail; i++ {
  267. c = iter.buf[i]
  268. ind := floatDigits[c]
  269. switch ind {
  270. case endOfNumber:
  271. if decimalPlaces > 0 && decimalPlaces < len(pow10) {
  272. iter.head = i
  273. return float64(value) / float64(pow10[decimalPlaces])
  274. }
  275. // too many decimal places
  276. return iter.readFloat64SlowPath()
  277. case invalidCharForNumber:
  278. fallthrough
  279. case dotInNumber:
  280. return iter.readFloat64SlowPath()
  281. }
  282. decimalPlaces++
  283. if value > uint64SafeToMultiple10 {
  284. return iter.readFloat64SlowPath()
  285. }
  286. value = (value << 3) + (value << 1) + uint64(ind)
  287. }
  288. }
  289. return iter.readFloat64SlowPath()
  290. }
  291. func (iter *Iterator) readFloat64SlowPath() (ret float64) {
  292. str := iter.readNumberAsString()
  293. if iter.Error != nil && iter.Error != io.EOF {
  294. return
  295. }
  296. errMsg := validateFloat(str)
  297. if errMsg != "" {
  298. iter.ReportError("readFloat64SlowPath", errMsg)
  299. return
  300. }
  301. val, err := strconv.ParseFloat(str, 64)
  302. if err != nil {
  303. iter.Error = err
  304. return
  305. }
  306. return val
  307. }
  308. func validateFloat(str string) string {
  309. // strconv.ParseFloat is not validating `1.` or `1.e1`
  310. if len(str) == 0 {
  311. return "empty number"
  312. }
  313. if str[0] == '-' {
  314. return "-- is not valid"
  315. }
  316. dotPos := strings.IndexByte(str, '.')
  317. if dotPos != -1 {
  318. if dotPos == len(str)-1 {
  319. return "dot can not be last character"
  320. }
  321. switch str[dotPos+1] {
  322. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  323. default:
  324. return "missing digit after dot"
  325. }
  326. }
  327. return ""
  328. }
  329. // ReadNumber read json.Number
  330. func (iter *Iterator) ReadNumber() (ret json.Number) {
  331. return json.Number(iter.readNumberAsString())
  332. }