diff options
author | Rutger Broekhoff | 2023-12-29 21:31:53 +0100 |
---|---|---|
committer | Rutger Broekhoff | 2023-12-29 21:31:53 +0100 |
commit | 404aeae4545d2426c089a5f8d5e82dae56f5212b (patch) | |
tree | 2d84e00af272b39fc04f3795ae06bc48970e57b5 /vendor/github.com/json-iterator/go/any_str.go | |
parent | 209d8b0187ed025dec9ac149ebcced3462877bff (diff) | |
download | gitolfs3-404aeae4545d2426c089a5f8d5e82dae56f5212b.tar.gz gitolfs3-404aeae4545d2426c089a5f8d5e82dae56f5212b.zip |
Make Nix builds work
Diffstat (limited to 'vendor/github.com/json-iterator/go/any_str.go')
-rw-r--r-- | vendor/github.com/json-iterator/go/any_str.go | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/vendor/github.com/json-iterator/go/any_str.go b/vendor/github.com/json-iterator/go/any_str.go new file mode 100644 index 0000000..1f12f66 --- /dev/null +++ b/vendor/github.com/json-iterator/go/any_str.go | |||
@@ -0,0 +1,166 @@ | |||
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 | 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 | |||
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 | |||
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 | |||
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 | } | ||