feature_iter_float.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package jsoniter
  2. import (
  3. "io"
  4. "math/big"
  5. "strconv"
  6. "unsafe"
  7. )
  8. var floatDigits []int8
  9. const invalidCharForNumber = int8(-1)
  10. const endOfNumber = int8(-2)
  11. const dotInNumber = int8(-3)
  12. func init() {
  13. floatDigits = make([]int8, 256)
  14. for i := 0; i < len(floatDigits); i++ {
  15. floatDigits[i] = invalidCharForNumber
  16. }
  17. for i := int8('0'); i <= int8('9'); i++ {
  18. floatDigits[i] = i - int8('0')
  19. }
  20. floatDigits[','] = endOfNumber
  21. floatDigits[']'] = endOfNumber
  22. floatDigits['}'] = endOfNumber
  23. floatDigits[' '] = endOfNumber
  24. floatDigits['\t'] = endOfNumber
  25. floatDigits['\n'] = endOfNumber
  26. floatDigits['.'] = dotInNumber
  27. }
  28. func (iter *Iterator) ReadBigFloat() (ret *big.Float) {
  29. str := iter.readNumberAsString()
  30. if iter.Error != nil && iter.Error != io.EOF {
  31. return nil
  32. }
  33. prec := 64
  34. if len(str) > prec {
  35. prec = len(str)
  36. }
  37. val, _, err := big.ParseFloat(str, 10, uint(prec), big.ToZero)
  38. if err != nil {
  39. iter.Error = err
  40. return nil
  41. }
  42. return val
  43. }
  44. func (iter *Iterator) ReadBigInt() (ret *big.Int) {
  45. str := iter.readNumberAsString()
  46. if iter.Error != nil && iter.Error != io.EOF {
  47. return nil
  48. }
  49. ret = big.NewInt(0)
  50. var success bool
  51. ret, success = ret.SetString(str, 10)
  52. if !success {
  53. iter.reportError("ReadBigInt", "invalid big int")
  54. return nil
  55. }
  56. return ret
  57. }
  58. func (iter *Iterator) ReadFloat32() (ret float32) {
  59. c := iter.nextToken()
  60. if c == '-' {
  61. return -iter.readPositiveFloat32()
  62. } else {
  63. iter.unreadByte()
  64. return iter.readPositiveFloat32()
  65. }
  66. }
  67. func (iter *Iterator) readPositiveFloat32() (ret float32) {
  68. value := uint64(0)
  69. c := byte(' ')
  70. i := iter.head
  71. non_decimal_loop:
  72. for ; i < iter.tail; i++ {
  73. c = iter.buf[i]
  74. ind := floatDigits[c]
  75. switch ind {
  76. case invalidCharForNumber:
  77. return iter.readFloat32SlowPath()
  78. case endOfNumber:
  79. iter.head = i
  80. return float32(value)
  81. case dotInNumber:
  82. break non_decimal_loop
  83. }
  84. if value > uint64SafeToMultiple10 {
  85. return iter.readFloat32SlowPath()
  86. }
  87. value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
  88. }
  89. if c == '.' {
  90. i++
  91. decimalPlaces := 0
  92. for ; i < iter.tail; i++ {
  93. c = iter.buf[i]
  94. ind := floatDigits[c]
  95. switch ind {
  96. case endOfNumber:
  97. if decimalPlaces > 0 && decimalPlaces < len(POW10) {
  98. iter.head = i
  99. return float32(float64(value) / float64(POW10[decimalPlaces]))
  100. }
  101. // too many decimal places
  102. return iter.readFloat32SlowPath()
  103. case invalidCharForNumber:
  104. fallthrough
  105. case dotInNumber:
  106. return iter.readFloat32SlowPath()
  107. }
  108. decimalPlaces++
  109. if value > uint64SafeToMultiple10 {
  110. return iter.readFloat32SlowPath()
  111. }
  112. value = (value << 3) + (value << 1) + uint64(ind)
  113. }
  114. }
  115. return iter.readFloat32SlowPath()
  116. }
  117. func (iter *Iterator) readNumberAsString() (ret string) {
  118. strBuf := [16]byte{}
  119. str := strBuf[0:0]
  120. load_loop:
  121. for {
  122. for i := iter.head; i < iter.tail; i++ {
  123. c := iter.buf[i]
  124. switch c {
  125. case '-', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  126. str = append(str, c)
  127. continue
  128. default:
  129. iter.head = i
  130. break load_loop
  131. }
  132. }
  133. if !iter.loadMore() {
  134. break
  135. }
  136. }
  137. if iter.Error != nil && iter.Error != io.EOF {
  138. return
  139. }
  140. if len(str) == 0 {
  141. iter.reportError("readNumberAsString", "invalid number")
  142. }
  143. return *(*string)(unsafe.Pointer(&str))
  144. }
  145. func (iter *Iterator) readFloat32SlowPath() (ret float32) {
  146. str := iter.readNumberAsString()
  147. if iter.Error != nil && iter.Error != io.EOF {
  148. return
  149. }
  150. val, err := strconv.ParseFloat(str, 32)
  151. if err != nil {
  152. iter.Error = err
  153. return
  154. }
  155. return float32(val)
  156. }
  157. func (iter *Iterator) ReadFloat64() (ret float64) {
  158. c := iter.nextToken()
  159. if c == '-' {
  160. return -iter.readPositiveFloat64()
  161. } else {
  162. iter.unreadByte()
  163. return iter.readPositiveFloat64()
  164. }
  165. }
  166. func (iter *Iterator) readPositiveFloat64() (ret float64) {
  167. value := uint64(0)
  168. c := byte(' ')
  169. i := iter.head
  170. non_decimal_loop:
  171. for ; i < iter.tail; i++ {
  172. c = iter.buf[i]
  173. ind := floatDigits[c]
  174. switch ind {
  175. case invalidCharForNumber:
  176. return iter.readFloat64SlowPath()
  177. case endOfNumber:
  178. iter.head = i
  179. return float64(value)
  180. case dotInNumber:
  181. break non_decimal_loop
  182. }
  183. if value > uint64SafeToMultiple10 {
  184. return iter.readFloat64SlowPath()
  185. }
  186. value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
  187. }
  188. if c == '.' {
  189. i++
  190. decimalPlaces := 0
  191. for ; i < iter.tail; i++ {
  192. c = iter.buf[i]
  193. ind := floatDigits[c]
  194. switch ind {
  195. case endOfNumber:
  196. if decimalPlaces > 0 && decimalPlaces < len(POW10) {
  197. iter.head = i
  198. return float64(value) / float64(POW10[decimalPlaces])
  199. }
  200. // too many decimal places
  201. return iter.readFloat64SlowPath()
  202. case invalidCharForNumber:
  203. fallthrough
  204. case dotInNumber:
  205. return iter.readFloat64SlowPath()
  206. }
  207. decimalPlaces++
  208. if value > uint64SafeToMultiple10 {
  209. return iter.readFloat64SlowPath()
  210. }
  211. value = (value << 3) + (value << 1) + uint64(ind)
  212. }
  213. }
  214. return iter.readFloat64SlowPath()
  215. }
  216. func (iter *Iterator) readFloat64SlowPath() (ret float64) {
  217. str := iter.readNumberAsString()
  218. if iter.Error != nil && iter.Error != io.EOF {
  219. return
  220. }
  221. val, err := strconv.ParseFloat(str, 64)
  222. if err != nil {
  223. iter.Error = err
  224. return
  225. }
  226. return val
  227. }