feature_any_float.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package jsoniter
  2. import (
  3. "io"
  4. "strconv"
  5. "unsafe"
  6. )
  7. type float64LazyAny struct {
  8. baseAny
  9. cfg *frozenConfig
  10. buf []byte
  11. err error
  12. cache float64
  13. }
  14. func (any *float64LazyAny) ValueType() ValueType {
  15. return Number
  16. }
  17. func (any *float64LazyAny) fillCache() {
  18. if any.err != nil {
  19. return
  20. }
  21. iter := any.cfg.BorrowIterator(any.buf)
  22. defer any.cfg.ReturnIterator(iter)
  23. any.cache = iter.ReadFloat64()
  24. if iter.Error != io.EOF {
  25. iter.reportError("floatLazyAny", "there are bytes left")
  26. }
  27. any.err = iter.Error
  28. }
  29. func (any *float64LazyAny) LastError() error {
  30. return any.err
  31. }
  32. func (any *float64LazyAny) ToBool() bool {
  33. return any.ToFloat64() != 0
  34. }
  35. func (any *float64LazyAny) ToInt() int {
  36. any.fillCache()
  37. return int(any.cache)
  38. }
  39. func (any *float64LazyAny) ToInt32() int32 {
  40. any.fillCache()
  41. return int32(any.cache)
  42. }
  43. func (any *float64LazyAny) ToInt64() int64 {
  44. any.fillCache()
  45. return int64(any.cache)
  46. }
  47. func (any *float64LazyAny) ToUint() uint {
  48. any.fillCache()
  49. return uint(any.cache)
  50. }
  51. func (any *float64LazyAny) ToUint32() uint32 {
  52. any.fillCache()
  53. return uint32(any.cache)
  54. }
  55. func (any *float64LazyAny) ToUint64() uint64 {
  56. any.fillCache()
  57. return uint64(any.cache)
  58. }
  59. func (any *float64LazyAny) ToFloat32() float32 {
  60. any.fillCache()
  61. return float32(any.cache)
  62. }
  63. func (any *float64LazyAny) ToFloat64() float64 {
  64. any.fillCache()
  65. return any.cache
  66. }
  67. func (any *float64LazyAny) ToString() string {
  68. return *(*string)(unsafe.Pointer(&any.buf))
  69. }
  70. func (any *float64LazyAny) WriteTo(stream *Stream) {
  71. stream.Write(any.buf)
  72. }
  73. func (any *float64LazyAny) GetInterface() interface{} {
  74. any.fillCache()
  75. return any.cache
  76. }
  77. type floatAny struct {
  78. baseAny
  79. val float64
  80. }
  81. func (any *floatAny) Parse() *Iterator {
  82. return nil
  83. }
  84. func (any *floatAny) ValueType() ValueType {
  85. return Number
  86. }
  87. func (any *floatAny) LastError() error {
  88. return nil
  89. }
  90. func (any *floatAny) ToBool() bool {
  91. return any.ToFloat64() != 0
  92. }
  93. func (any *floatAny) ToInt() int {
  94. return int(any.val)
  95. }
  96. func (any *floatAny) ToInt32() int32 {
  97. return int32(any.val)
  98. }
  99. func (any *floatAny) ToInt64() int64 {
  100. return int64(any.val)
  101. }
  102. func (any *floatAny) ToUint() uint {
  103. return uint(any.val)
  104. }
  105. func (any *floatAny) ToUint32() uint32 {
  106. return uint32(any.val)
  107. }
  108. func (any *floatAny) ToUint64() uint64 {
  109. return uint64(any.val)
  110. }
  111. func (any *floatAny) ToFloat32() float32 {
  112. return float32(any.val)
  113. }
  114. func (any *floatAny) ToFloat64() float64 {
  115. return any.val
  116. }
  117. func (any *floatAny) ToString() string {
  118. return strconv.FormatFloat(any.val, 'E', -1, 64)
  119. }
  120. func (any *floatAny) WriteTo(stream *Stream) {
  121. stream.WriteFloat64(any.val)
  122. }
  123. func (any *floatAny) GetInterface() interface{} {
  124. return any.val
  125. }