feature_any_int64.go 2.7 KB

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