feature_any_string.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package jsoniter
  2. import (
  3. "io"
  4. "strconv"
  5. )
  6. type stringLazyAny struct {
  7. baseAny
  8. buf []byte
  9. iter *Iterator
  10. err error
  11. cache string
  12. }
  13. func (any *stringLazyAny) ValueType() ValueType {
  14. return String
  15. }
  16. func (any *stringLazyAny) Parse() *Iterator {
  17. iter := any.iter
  18. if iter == nil {
  19. iter = NewIterator()
  20. any.iter = iter
  21. }
  22. iter.ResetBytes(any.buf)
  23. return iter
  24. }
  25. func (any *stringLazyAny) fillCache() {
  26. if any.err != nil {
  27. return
  28. }
  29. iter := any.Parse()
  30. any.cache = iter.ReadString()
  31. if iter.Error != io.EOF {
  32. iter.reportError("stringLazyAny", "there are bytes left")
  33. }
  34. any.err = iter.Error
  35. }
  36. func (any *stringLazyAny) LastError() error {
  37. return any.err
  38. }
  39. func (any *stringLazyAny) ToBool() bool {
  40. str := any.ToString()
  41. if str == "false" {
  42. return false
  43. }
  44. for _, c := range str {
  45. switch c {
  46. case ' ', '\n', '\r', '\t':
  47. default:
  48. return true
  49. }
  50. }
  51. return false
  52. }
  53. func (any *stringLazyAny) ToInt() int {
  54. iter := any.Parse()
  55. iter.head++
  56. val := iter.ReadInt()
  57. any.err = iter.Error
  58. return val
  59. }
  60. func (any *stringLazyAny) ToInt32() int32 {
  61. iter := any.Parse()
  62. iter.head++
  63. val := iter.ReadInt32()
  64. any.err = iter.Error
  65. return val
  66. }
  67. func (any *stringLazyAny) ToInt64() int64 {
  68. iter := any.Parse()
  69. iter.head++
  70. val := iter.ReadInt64()
  71. any.err = iter.Error
  72. return val
  73. }
  74. func (any *stringLazyAny) ToUint() uint {
  75. iter := any.Parse()
  76. iter.head++
  77. val := iter.ReadUint()
  78. any.err = iter.Error
  79. return val
  80. }
  81. func (any *stringLazyAny) ToUint32() uint32 {
  82. iter := any.Parse()
  83. iter.head++
  84. val := iter.ReadUint32()
  85. any.err = iter.Error
  86. return val
  87. }
  88. func (any *stringLazyAny) ToUint64() uint64 {
  89. iter := any.Parse()
  90. iter.head++
  91. val := iter.ReadUint64()
  92. any.err = iter.Error
  93. return val
  94. }
  95. func (any *stringLazyAny) ToFloat32() float32 {
  96. iter := any.Parse()
  97. iter.head++
  98. val := iter.ReadFloat32()
  99. any.err = iter.Error
  100. return val
  101. }
  102. func (any *stringLazyAny) ToFloat64() float64 {
  103. iter := any.Parse()
  104. iter.head++
  105. val := iter.ReadFloat64()
  106. any.err = iter.Error
  107. return val
  108. }
  109. func (any *stringLazyAny) ToString() string {
  110. any.fillCache()
  111. return any.cache
  112. }
  113. func (any *stringLazyAny) WriteTo(stream *Stream) {
  114. stream.Write(any.buf)
  115. }
  116. func (any *stringLazyAny) GetInterface() interface{} {
  117. any.fillCache()
  118. return any.cache
  119. }
  120. type stringAny struct {
  121. baseAny
  122. err error
  123. val string
  124. }
  125. func (any *stringAny) Parse() *Iterator {
  126. return nil
  127. }
  128. func (any *stringAny) ValueType() ValueType {
  129. return String
  130. }
  131. func (any *stringAny) LastError() error {
  132. return any.err
  133. }
  134. func (any *stringAny) ToBool() bool {
  135. str := any.ToString()
  136. if str == "false" {
  137. return false
  138. }
  139. for _, c := range str {
  140. switch c {
  141. case ' ', '\n', '\r', '\t':
  142. default:
  143. return true
  144. }
  145. }
  146. return false
  147. }
  148. func (any *stringAny) ToInt() int {
  149. parsed, err := strconv.ParseInt(any.val, 10, 64)
  150. any.err = err
  151. return int(parsed)
  152. }
  153. func (any *stringAny) ToInt32() int32 {
  154. parsed, err := strconv.ParseInt(any.val, 10, 32)
  155. any.err = err
  156. return int32(parsed)
  157. }
  158. func (any *stringAny) ToInt64() int64 {
  159. parsed, err := strconv.ParseInt(any.val, 10, 64)
  160. any.err = err
  161. return parsed
  162. }
  163. func (any *stringAny) ToUint() uint {
  164. parsed, err := strconv.ParseUint(any.val, 10, 64)
  165. any.err = err
  166. return uint(parsed)
  167. }
  168. func (any *stringAny) ToUint32() uint32 {
  169. parsed, err := strconv.ParseUint(any.val, 10, 32)
  170. any.err = err
  171. return uint32(parsed)
  172. }
  173. func (any *stringAny) ToUint64() uint64 {
  174. parsed, err := strconv.ParseUint(any.val, 10, 64)
  175. any.err = err
  176. return parsed
  177. }
  178. func (any *stringAny) ToFloat32() float32 {
  179. parsed, err := strconv.ParseFloat(any.val, 32)
  180. any.err = err
  181. return float32(parsed)
  182. }
  183. func (any *stringAny) ToFloat64() float64 {
  184. parsed, err := strconv.ParseFloat(any.val, 64)
  185. any.err = err
  186. return parsed
  187. }
  188. func (any *stringAny) ToString() string {
  189. return any.val
  190. }
  191. func (any *stringAny) WriteTo(stream *Stream) {
  192. stream.WriteString(any.val)
  193. }
  194. func (any *stringAny) GetInterface() interface{} {
  195. return any.val
  196. }