-
Notifications
You must be signed in to change notification settings - Fork 7
/
converts.go
370 lines (341 loc) · 7.97 KB
/
converts.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package ameda
import (
"fmt"
"math"
"reflect"
"strconv"
"github.com/andeya/ameda/v2/digit"
)
// Zero return zero value.
func Zero[T any]() T {
var z T
return z
}
// ToPtr converts D to *D.
func ToPtr[T any](v T) *T {
return &v
}
// ToPtrWithErr converts D to *D.
func ToPtrWithErr[T any](v T, err error) (*T, error) {
if err != nil {
return nil, err
}
return &v, nil
}
// ToInterface converts any to interface.
func ToInterface(v interface{}) interface{} {
return v
}
// ToInterfaces converts []any to []interface.
func ToInterfaces[T any](a []T) []interface{} {
b := make([]interface{}, len(a))
for i, t := range a {
b[i] = t
}
return b
}
// ToString converts any to ~string.
func ToString[T ~string](v interface{}) T {
switch t := v.(type) {
case string:
return T(t)
default:
return T(fmt.Sprint(v))
}
}
// ToStrings converts []T to []D.
func ToStrings[T any, D ~string](a []T) []D {
b := make([]D, len(a))
for i, t := range a {
b[i] = ToString[D](t)
}
return b
}
// StringToDigit converts ~string to digit.
// If base == 0, the base is implied by the string's prefix:
// base 2 for "0b", base 8 for "0" or "0o", base 16 for "0x",
// and base 10 otherwise. Also, for base == 0 only, underscore
// characters are permitted per the Go integer literal syntax.
// If base is below 0, is 1, or is above 62, an error is returned.
//
func StringToDigit[T ~string, D digit.Digit](v T, base int, bitSize int) (D, error) {
var d *D
var x interface{} = d
switch x.(type) {
case *int, *int8, *int16, *int32, *int64:
r, err := ParseInt(string(v), base, bitSize)
if err != nil {
return 0, err
}
return DigitToDigit[int64, D](r)
case *uint, *uint8, *uint16, *uint32, *uint64:
r, err := ParseUint(string(v), base, bitSize)
if err != nil {
return 0, err
}
return DigitToDigit[uint64, D](r)
case *float32, *float64:
r, err := strconv.ParseFloat(string(v), bitSize)
if err != nil {
return 0, err
}
return DigitToDigit[float64, D](r)
}
switch reflect.TypeOf(x).Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
r, err := ParseInt(string(v), base, bitSize)
if err != nil {
return 0, err
}
return DigitToDigit[int64, D](r)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
r, err := ParseUint(string(v), base, bitSize)
if err != nil {
return 0, err
}
return DigitToDigit[uint64, D](r)
case reflect.Float32, reflect.Float64:
r, err := strconv.ParseFloat(string(v), bitSize)
if err != nil {
return 0, err
}
return DigitToDigit[float64, D](r)
}
return 0, nil
}
func StringsToDigits[T ~string, D digit.Digit](a []T, base int, bitSize int) (b []D, err error) {
b = make([]D, len(a))
for i, t := range a {
b[i], err = StringToDigit[T, D](t, base, bitSize)
if err != nil {
return nil, err
}
}
return b, nil
}
// ToBool converts D to bool.
func ToBool[T ~string | digit.Digit](v T) bool {
zero := new(T)
return v != *zero
}
// ToBools converts []D to []bool.
func ToBools[T ~string | digit.Digit](a []T) []bool {
b := make([]bool, len(a))
for i, t := range a {
b[i] = ToBool(t)
}
return b
}
// BoolToDigit converts bool to digit.
func BoolToDigit[T ~bool, D digit.Digit](v T) D {
if v == true {
return D(1)
}
return D(0)
}
func BoolsToDigits[T ~bool, D digit.Digit](a []T) (b []D) {
b = make([]D, len(a))
for i, t := range a {
b[i] = BoolToDigit[T, D](t)
}
return b
}
func DigitToDigit[T digit.Digit, D digit.Digit](v T) (D, error) {
var d *D
var x interface{} = d
switch x.(type) {
case *int:
r, err := digitToInt(v)
return D(r), err
case *int8:
r, err := digitToInt8[T](v)
return D(r), err
case *int16:
r, err := digitToInt16[T](v)
return D(r), err
case *int32:
r, err := digitToInt32[T](v)
return D(r), err
case *int64:
r, err := digitToInt64[T](v)
return D(r), err
case *uint:
r, err := digitToUint[T](v)
return D(r), err
case *uint8:
r, err := digitToUint8[T](v)
return D(r), err
case *uint16:
r, err := digitToUint16[T](v)
return D(r), err
case *uint32:
r, err := digitToUint32[T](v)
return D(r), err
case *uint64:
r, err := digitToUint64[T](v)
return D(r), err
case *float32:
r, err := digitToFloat32[T](v)
return D(r), err
case *float64:
r := DigitToFloat64[T](v)
return D(r), nil
}
switch reflect.TypeOf(x).Kind() {
case reflect.Int:
r, err := digitToInt(v)
return D(r), err
case reflect.Int8:
r, err := digitToInt8[T](v)
return D(r), err
case reflect.Int16:
r, err := digitToInt16[T](v)
return D(r), err
case reflect.Int32:
r, err := digitToInt32[T](v)
return D(r), err
case reflect.Int64:
r, err := digitToInt64[T](v)
return D(r), err
case reflect.Uint:
r, err := digitToUint[T](v)
return D(r), err
case reflect.Uint8:
r, err := digitToUint8[T](v)
return D(r), err
case reflect.Uint16:
r, err := digitToUint16[T](v)
return D(r), err
case reflect.Uint32:
r, err := digitToUint32[T](v)
return D(r), err
case reflect.Uint64:
r, err := digitToUint64[T](v)
return D(r), err
case reflect.Float32:
r, err := digitToFloat32[T](v)
return D(r), err
case reflect.Float64:
r := DigitToFloat64[T](v)
return D(r), nil
}
return 0, nil
}
// DigitsToDigits creates a copy of the digit slice.
func DigitsToDigits[T digit.Digit, D digit.Digit](a []T) (b []D, err error) {
b = make([]D, len(a))
for i, t := range a {
b[i], err = DigitToDigit[T, D](t)
if err != nil {
return nil, err
}
}
return b, nil
}
// DigitToFloat64 converts digit to float64.
func DigitToFloat64[T digit.Digit](v T) float64 {
return float64(v)
}
// digitToFloat32 converts digit to float32.
func digitToFloat32[T digit.Digit](v T) (float32, error) {
f := float64(v)
if f > math.MaxFloat32 || f < -math.MaxFloat32 {
return 0, errOverflowValue
}
return float32(v), nil
}
// digitToInt converts digit to int.
func digitToInt[T digit.Digit](v T) (int, error) {
f := float64(v)
if f > math.MaxInt || f < math.MinInt {
return 0, errOverflowValue
}
return int(v), nil
}
// digitToInt8 converts digit to int8.
func digitToInt8[T digit.Digit](v T) (int8, error) {
if v > 0 {
if v > math.MaxInt8 {
return 0, errOverflowValue
}
} else {
if float64(v) < math.MinInt8 {
return 0, errOverflowValue
}
}
return int8(v), nil
}
// digitToInt16 converts digit to int16.
func digitToInt16[T digit.Digit](v T) (int16, error) {
f := float64(v)
if f > math.MaxInt16 || f < math.MinInt16 {
return 0, errOverflowValue
}
return int16(v), nil
}
// digitToInt32 converts digit to int32.
func digitToInt32[T digit.Digit](v T) (int32, error) {
f := float64(v)
if f > math.MaxInt32 || f < math.MinInt32 {
return 0, errOverflowValue
}
return int32(v), nil
}
// digitToInt64 converts digit to int64.
func digitToInt64[T digit.Digit](v T) (int64, error) {
f := float64(v)
if f > math.MaxInt64 || f < math.MinInt64 {
return 0, errOverflowValue
}
return int64(v), nil
}
// digitToUint converts digit to uint.
func digitToUint[T digit.Digit](v T) (uint, error) {
if v < 0 {
return 0, errNegativeValue
}
if float64(v) > math.MaxUint {
return 0, errOverflowValue
}
return uint(v), nil
}
// digitToUint8 converts digit to uint8.
func digitToUint8[T digit.Digit](v T) (uint8, error) {
if v < 0 {
return 0, errNegativeValue
}
if float64(v) > math.MaxUint8 {
return 0, errOverflowValue
}
return uint8(v), nil
}
// digitToUint16 converts digit to uint16.
func digitToUint16[T digit.Digit](v T) (uint16, error) {
if v < 0 {
return 0, errNegativeValue
}
if float64(v) > math.MaxUint16 {
return 0, errOverflowValue
}
return uint16(v), nil
}
// digitToUint32 converts digit to uint32.
func digitToUint32[T digit.Digit](v T) (uint32, error) {
if v < 0 {
return 0, errNegativeValue
}
if float64(v) > math.MaxUint32 {
return 0, errOverflowValue
}
return uint32(v), nil
}
// digitToUint64 converts digit to uint64.
func digitToUint64[T digit.Digit](v T) (uint64, error) {
if v < 0 {
return 0, errNegativeValue
}
if float64(v) > math.MaxUint64 {
return 0, errOverflowValue
}
return uint64(v), nil
}