blob: 1f12f6612de98255335e5dccf6200355ef27363f [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001package jsoniter
2
3import (
4 "fmt"
5 "strconv"
6)
7
8type stringAny struct {
9 baseAny
10 val string
11}
12
13func (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
20func (any *stringAny) Parse() *Iterator {
21 return nil
22}
23
24func (any *stringAny) ValueType() ValueType {
25 return StringValue
26}
27
28func (any *stringAny) MustBeValid() Any {
29 return any
30}
31
32func (any *stringAny) LastError() error {
33 return nil
34}
35
36func (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
51func (any *stringAny) ToInt() int {
52 return int(any.ToInt64())
53
54}
55
56func (any *stringAny) ToInt32() int32 {
57 return int32(any.ToInt64())
58}
59
60func (any *stringAny) ToInt64() int64 {
61 if any.val == "" {
62 return 0
63 }
64
65 flag := 1
66 startPos := 0
67 if any.val[0] == '+' || any.val[0] == '-' {
68 startPos = 1
69 }
70
71 if any.val[0] == '-' {
72 flag = -1
73 }
74
75 endPos := startPos
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
87func (any *stringAny) ToUint() uint {
88 return uint(any.ToUint64())
89}
90
91func (any *stringAny) ToUint32() uint32 {
92 return uint32(any.ToUint64())
93}
94
95func (any *stringAny) ToUint64() uint64 {
96 if any.val == "" {
97 return 0
98 }
99
100 startPos := 0
101
102 if any.val[0] == '-' {
103 return 0
104 }
105 if any.val[0] == '+' {
106 startPos = 1
107 }
108
109 endPos := startPos
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
121func (any *stringAny) ToFloat32() float32 {
122 return float32(any.ToFloat64())
123}
124
125func (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
156func (any *stringAny) ToString() string {
157 return any.val
158}
159
160func (any *stringAny) WriteTo(stream *Stream) {
161 stream.WriteString(any.val)
162}
163
164func (any *stringAny) GetInterface() interface{} {
165 return any.val
166}