any_str.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package jsoniter
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. type stringAny struct {
  7. baseAny
  8. val string
  9. }
  10. func (any *stringAny) Get(path ...interface{}) Any {
  11. if len(path) == 0 {
  12. return any
  13. }
  14. return &invalidAny{baseAny{}, fmt.Errorf("GetIndex %v from simple value", path)}
  15. }
  16. func (any *stringAny) Parse() *Iterator {
  17. return nil
  18. }
  19. func (any *stringAny) ValueType() ValueType {
  20. return StringValue
  21. }
  22. func (any *stringAny) MustBeValid() Any {
  23. return any
  24. }
  25. func (any *stringAny) LastError() error {
  26. return nil
  27. }
  28. func (any *stringAny) ToBool() bool {
  29. str := any.ToString()
  30. if str == "0" {
  31. return false
  32. }
  33. for _, c := range str {
  34. switch c {
  35. case ' ', '\n', '\r', '\t':
  36. default:
  37. return true
  38. }
  39. }
  40. return false
  41. }
  42. func (any *stringAny) ToInt() int {
  43. return int(any.ToInt64())
  44. }
  45. func (any *stringAny) ToInt32() int32 {
  46. return int32(any.ToInt64())
  47. }
  48. func (any *stringAny) ToInt64() int64 {
  49. if any.val == "" {
  50. return 0
  51. }
  52. flag := 1
  53. startPos := 0
  54. endPos := 0
  55. if any.val[0] == '+' || any.val[0] == '-' {
  56. startPos = 1
  57. }
  58. if any.val[0] == '-' {
  59. flag = -1
  60. }
  61. for i := startPos; i < len(any.val); i++ {
  62. if any.val[i] >= '0' && any.val[i] <= '9' {
  63. endPos = i + 1
  64. } else {
  65. break
  66. }
  67. }
  68. parsed, _ := strconv.ParseInt(any.val[startPos:endPos], 10, 64)
  69. return int64(flag) * parsed
  70. }
  71. func (any *stringAny) ToUint() uint {
  72. return uint(any.ToUint64())
  73. }
  74. func (any *stringAny) ToUint32() uint32 {
  75. return uint32(any.ToUint64())
  76. }
  77. func (any *stringAny) ToUint64() uint64 {
  78. if any.val == "" {
  79. return 0
  80. }
  81. startPos := 0
  82. endPos := 0
  83. if any.val[0] == '-' {
  84. return 0
  85. }
  86. if any.val[0] == '+' {
  87. startPos = 1
  88. }
  89. for i := startPos; i < len(any.val); i++ {
  90. if any.val[i] >= '0' && any.val[i] <= '9' {
  91. endPos = i + 1
  92. } else {
  93. break
  94. }
  95. }
  96. parsed, _ := strconv.ParseUint(any.val[startPos:endPos], 10, 64)
  97. return parsed
  98. }
  99. func (any *stringAny) ToFloat32() float32 {
  100. return float32(any.ToFloat64())
  101. }
  102. func (any *stringAny) ToFloat64() float64 {
  103. if len(any.val) == 0 {
  104. return 0
  105. }
  106. // first char invalid
  107. if any.val[0] != '+' && any.val[0] != '-' && (any.val[0] > '9' || any.val[0] < '0') {
  108. return 0
  109. }
  110. // extract valid num expression from string
  111. // eg 123true => 123, -12.12xxa => -12.12
  112. endPos := 1
  113. for i := 1; i < len(any.val); i++ {
  114. if any.val[i] == '.' || any.val[i] == 'e' || any.val[i] == 'E' || any.val[i] == '+' || any.val[i] == '-' {
  115. endPos = i + 1
  116. continue
  117. }
  118. // end position is the first char which is not digit
  119. if any.val[i] >= '0' && any.val[i] <= '9' {
  120. endPos = i + 1
  121. } else {
  122. endPos = i
  123. break
  124. }
  125. }
  126. parsed, _ := strconv.ParseFloat(any.val[:endPos], 64)
  127. return parsed
  128. }
  129. func (any *stringAny) ToString() string {
  130. return any.val
  131. }
  132. func (any *stringAny) WriteTo(stream *Stream) {
  133. stream.WriteString(any.val)
  134. }
  135. func (any *stringAny) GetInterface() interface{} {
  136. return any.val
  137. }