-
Notifications
You must be signed in to change notification settings - Fork 23
/
cast.go
285 lines (270 loc) · 5.63 KB
/
cast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2018 Alexander Poltoratskiy. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package fast
import (
"math"
"strconv"
"strings"
"unicode"
)
const (
uintSize = 32 << (^uint(0) >> 32 & 1)
maxInt = 1<<(uintSize-1) - 1
maxUint = 1<<uintSize - 1
)
func castTo(src, dst interface{}) error {
switch src.(type) {
case []byte:
return castByteVectorTo(src.([]byte), dst)
case string:
return castStringTo(src.(string), dst)
case uint32:
return castUintTo(uint64(src.(uint32)), dst)
case uint64:
return castUintTo(src.(uint64), dst)
case int32:
return castIntTo(int64(src.(int32)), dst)
case int64:
return castIntTo(src.(int64), dst)
case float64:
return castFloatTo(src.(float64), dst)
}
return nil
}
func castByteVectorTo(src []byte, dst interface{}) (err error) {
switch dst.(type) {
case *string:
*dst.(*string) = string(src)
default:
err = ErrD10
}
return
}
func castStringTo(src string, dst interface{}) (err error) {
src = strings.TrimSpace(src)
switch dst.(type) {
case *int:
var tmp int64
tmp, err = strconv.ParseInt(src, 10, 0)
*dst.(*int) = int(tmp)
case *int64:
*dst.(*int64), err = strconv.ParseInt(src, 10, 0)
case *int32:
var tmp int64
tmp, err = strconv.ParseInt(src, 10, 32)
*dst.(*int32) = int32(tmp)
case *int16:
var tmp int64
tmp, err = strconv.ParseInt(src, 10, 16)
*dst.(*int16) = int16(tmp)
case *int8:
var tmp int64
tmp, err = strconv.ParseInt(src, 10, 8)
*dst.(*int8) = int8(tmp)
case *uint:
var tmp uint64
tmp, err = strconv.ParseUint(src, 10, 0)
*dst.(*uint) = uint(tmp)
case *uint64:
*dst.(*uint64), err = strconv.ParseUint(src, 10, 0)
case *uint32:
var tmp uint64
tmp, err = strconv.ParseUint(src, 10, 32)
*dst.(*uint32) = uint32(tmp)
case *uint16:
var tmp uint64
tmp, err = strconv.ParseUint(src, 10, 16)
*dst.(*uint16) = uint16(tmp)
case *uint8:
var tmp uint64
tmp, err = strconv.ParseUint(src, 10, 8)
*dst.(*uint8) = uint8(tmp)
case *float64:
*dst.(*float64), err = strconv.ParseFloat(src, 0)
case *float32:
var tmp float64
tmp, err = strconv.ParseFloat(src, 32)
*dst.(*float32) = float32(tmp)
case *[]byte:
*dst.(*[]byte) = []byte(src)
case *string:
*dst.(*string) = src
}
checkStringErr(err, dst)
return
}
func castUintTo(src uint64, dst interface{}) (err error) {
switch dst.(type) {
case *int:
*dst.(*int) = int(src)
if src > maxInt {
err = ErrR4
}
case *int64:
*dst.(*int64) = int64(src)
if src > math.MaxInt64 {
err = ErrR4
}
case *int32:
*dst.(*int32) = int32(src)
if src > math.MaxInt32 {
err = ErrR4
}
case *int16:
*dst.(*int16) = int16(src)
if src > math.MaxInt16 {
err = ErrR4
}
case *int8:
*dst.(*int8) = int8(src)
if src > math.MaxInt8 {
err = ErrR4
}
case *uint:
*dst.(*uint) = uint(src)
if src > maxUint {
err = ErrR4
}
case *uint64:
*dst.(*uint64) = src
case *uint32:
*dst.(*uint32) = uint32(src)
if src > math.MaxUint32 {
err = ErrR4
}
case *uint16:
*dst.(*uint16) = uint16(src)
if src > math.MaxUint16 {
err = ErrR4
}
case *uint8:
*dst.(*uint8) = uint8(src)
if src > math.MaxUint8 {
err = ErrR4
}
case *float64:
*dst.(*float64) = float64(src)
case *float32:
*dst.(*float32) = float32(src)
case *[]byte:
err = ErrD10
case *string:
*dst.(*string) = strconv.FormatUint(src, 10)
}
return
}
func castIntTo(src int64, dst interface{}) (err error) {
switch dst.(type) {
case *int:
*dst.(*int) = int(src)
if src > maxInt {
err = ErrR4
}
case *int64:
*dst.(*int64) = src
case *int32:
*dst.(*int32) = int32(src)
if src > math.MaxInt32 {
err = ErrR4
}
case *int16:
*dst.(*int16) = int16(src)
if src > math.MaxInt16 {
err = ErrR4
}
case *int8:
*dst.(*int8) = int8(src)
if src > math.MaxInt8 {
err = ErrR4
}
case *uint:
*dst.(*uint) = uint(src)
if uint(src) > maxUint {
err = ErrR4
}
case *uint64:
*dst.(*uint64) = uint64(src)
case *uint32:
*dst.(*uint32) = uint32(src)
if src > math.MaxUint32 {
err = ErrR4
}
case *uint16:
*dst.(*uint16) = uint16(src)
if src > math.MaxUint16 {
err = ErrR4
}
case *uint8:
*dst.(*uint8) = uint8(src)
if src > math.MaxUint8 {
err = ErrR4
}
case *float64:
*dst.(*float64) = float64(src)
case *float32:
*dst.(*float32) = float32(src)
case *[]byte:
err = ErrD10
case *string:
*dst.(*string) = strconv.FormatInt(src, 10)
}
return
}
func castFloatTo(src float64, dst interface{}) (err error) {
switch dst.(type) {
case *int, *int64, *int32, *int16, *int8, *uint, *uint64, *uint32, *uint16, *uint8:
err = checkFloatErr(src)
if err != nil {
return
}
err = castUintTo(uint64(src), dst)
case *float64:
*dst.(*float64) = src
case *float32:
*dst.(*float32) = float32(src)
case *[]byte:
err = ErrD10
case *string:
*dst.(*string) = strconv.FormatFloat(src, 'f', -1, 64)
}
return
}
func castStringToASCII(src interface{}, dst *string) (err error) {
err = castTo(src, dst)
if err != nil {
return
}
for i := 0; i < len(*dst); i++ {
if (*dst)[i] > unicode.MaxASCII {
return ErrR3
}
}
return
}
func checkFloatErr(src float64) error {
if expDecimal(src) > 0 {
return ErrR5
}
return nil
}
func checkStringErr(err error, dst interface{}) {
switch dst.(type) {
case *int, *int64, *int32, *int16, *int8, *uint, *uint64, *uint32, *uint16, *uint8:
if e, ok := err.(*strconv.NumError); ok {
if e.Err == strconv.ErrRange {
err = ErrR4
} else {
err = ErrD11
}
}
case *float32, *float64:
if e, ok := err.(*strconv.NumError); ok {
if e.Err == strconv.ErrRange {
err = ErrR1
} else {
err = ErrD11
}
}
}
}