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