-
Notifications
You must be signed in to change notification settings - Fork 1
/
to.int.go
142 lines (132 loc) · 3.91 KB
/
to.int.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
package cast
import (
"fmt"
"math"
"reflect"
"strconv"
"strings"
"github.com/bdlm/errors/v2"
"golang.org/x/exp/constraints"
)
// toInt casts an interface to an int type.
func toInt[TTo constraints.Integer](from reflect.Value, ops Ops) (TTo, error) {
fromVal := reflect.Indirect(from)
if !fromVal.IsValid() || !fromVal.CanInterface() {
return TTo(0), errors.Errorf("unable to cast %#.10v of type %T to %T", from, from, TTo(0))
}
errDetail := errors.Errorf("unable to cast %#.10v of type %T to %T", from.Interface(), from.Interface(), TTo(0))
to := reflect.Indirect(reflect.ValueOf(new(TTo)))
unsigned := false
switch to.Interface().(type) {
case uint, uint8, uint16, uint32, uint64, uintptr:
unsigned = true
}
//fmt.Printf("\n\nto: %v (%T); from: %v (%T)\n\n", to, to.Interface(), from, from.Interface())
switch typ := from.Interface().(type) {
case nil:
return TTo(0), nil
case bool:
if typ {
return TTo(1), nil
}
return TTo(0), nil
case int:
if unsigned && typ < 0 {
return 0, errors.WrapE(ErrorSignedToUnsigned, errDetail)
}
return TTo(from.Interface().(int)), nil
case int64:
if unsigned && typ < 0 {
return 0, errors.WrapE(ErrorSignedToUnsigned, errDetail)
}
return TTo(from.Interface().(int64)), nil
case int32:
if unsigned && typ < 0 {
return 0, errors.WrapE(ErrorSignedToUnsigned, errDetail)
}
return TTo(from.Interface().(int32)), nil
case int16:
if unsigned && typ < 0 {
return 0, errors.WrapE(ErrorSignedToUnsigned, errDetail)
}
return TTo(from.Interface().(int16)), nil
case int8:
if unsigned && typ < 0 {
return 0, errors.WrapE(ErrorSignedToUnsigned, errDetail)
}
return TTo(from.Interface().(int8)), nil
case float64:
if unsigned && typ < 0 {
return 0, errors.WrapE(ErrorSignedToUnsigned, errDetail)
}
v, e := strToInt[TTo](to, To[string](from.Interface()))
return TTo(v), e
case float32:
if unsigned && typ < 0 {
return 0, errors.WrapE(ErrorSignedToUnsigned, errDetail)
}
v, e := strToInt[TTo](to, To[string](from.Interface()))
return TTo(v), e
case uint:
return TTo(from.Interface().(uint)), nil
case uintptr:
return TTo(from.Interface().(uintptr)), nil
case uint64:
return TTo(from.Interface().(uint64)), nil
case uint32:
return TTo(from.Interface().(uint32)), nil
case uint16:
return TTo(from.Interface().(uint16)), nil
case uint8:
return TTo(from.Interface().(uint8)), nil
case fmt.Stringer:
return strToInt[TTo](to, typ.String())
case string:
return strToInt[TTo](to, typ)
//case complex128:
//case complex64:
default:
return toInt[TTo](reflect.ValueOf(fmt.Sprintf("%#.10v", from.Interface())), ops)
}
}
// strToInt converts a string to an integer type.
func strToInt[TTo constraints.Integer](to reflect.Value, from string) (TTo, error) {
errDetail := errors.Errorf("unable to cast %#.10v of type %T to %T", from, from, TTo(0))
var e, err error
var val float64
if val, e = strconv.ParseFloat(from, 64); e != nil {
err = e
from = strings.ReplaceAll(
strings.Split(
strings.Trim(from, "\r\n\t "),
".",
)[0],
",", "",
)
if val, e = strconv.ParseFloat(from, 64); e != nil {
err = errors.WrapE(err, e)
_, e := strconv.ParseComplex(from, 64)
if e != nil {
err = errors.WrapE(err, e)
} else {
err = errors.Wrap(err, "unable to cast complex to int")
val = 0
}
} else {
err = nil
}
}
//fmt.Printf("to: %T, from: %v (%v), val: %d (%T)\n", TTo(0), from, from.Type().Kind(), int(math.Floor(val)), int(math.Floor(val)))
if err != nil {
return 0, errors.WrapE(err, errDetail)
}
if val >= 0 {
return TTo(math.Floor(val)), nil
}
// Negative to uint error.
ref := reflect.ValueOf(TTo(0))
if ref.Kind() == reflect.Uint || ref.Kind() == reflect.Uint8 || ref.Kind() == reflect.Uint16 || ref.Kind() == reflect.Uint32 || ref.Kind() == reflect.Uint64 || ref.Kind() == reflect.Uintptr {
return 0, errors.WrapE(ErrorSignedToUnsigned, errDetail)
}
return TTo(math.Ceil(val)), nil
}