-
Notifications
You must be signed in to change notification settings - Fork 36
/
tools.go
170 lines (150 loc) · 2.82 KB
/
tools.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
package utiles
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math/big"
"reflect"
"strconv"
"strings"
)
func StringToInt(s string) int {
if i, err := strconv.Atoi(s); err == nil {
return i
}
return 0
}
func IntInSlice(a int, list []int) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func AddHex(s string) string {
if strings.HasPrefix(s, "0x") {
return s
}
return "0x" + s
}
func U256(v string) *big.Int {
v = strings.TrimPrefix(v, "0x")
bn := new(big.Int)
n, _ := bn.SetString(v, 16)
return n
}
func HexToBytes(s string) []byte {
s = strings.TrimPrefix(s, "0x")
c := make([]byte, hex.DecodedLen(len(s)))
_, _ = hex.Decode(c, []byte(s))
return c
}
func BytesToHex(b []byte) string {
c := make([]byte, hex.EncodedLen(len(b)))
hex.Encode(c, b)
return string(c)
}
func IntToHex(i interface{}) string {
return fmt.Sprintf("%x", i)
}
func UniqueSlice(s []string) (list []string) {
keys := make(map[string]bool)
for _, entry := range s {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return
}
func ReverseBytes(a []byte) []byte {
for i := len(a)/2 - 1; i >= 0; i-- {
opp := len(a) - 1 - i
a[i], a[opp] = a[opp], a[i]
}
return a
}
func ToString(i interface{}) string {
var val string
switch i := i.(type) {
case string:
val = i
case []byte:
val = string(i)
default:
b, _ := json.Marshal(i)
val = string(b)
}
return val
}
func TrimHex(s string) string {
return strings.TrimPrefix(s, "0x")
}
func BytesToBnHex(value []byte) string {
var h string
for _, b := range value {
h += fmt.Sprintf("%d", int(b))
}
return h
}
func Debug(i interface{}) {
var val string
switch i := i.(type) {
case string:
val = i
case []byte:
val = string(i)
case error:
val = i.Error()
default:
b, _ := json.MarshalIndent(i, "", " ")
val = string(b)
}
fmt.Println(val)
}
func IsASCII(b []byte) bool {
for _, c := range b {
if c > 127 || (c < 32 && !IntInSlice(int(c), []int{9, 10, 13})) {
return false
}
}
return true
}
func SliceIndex(a string, list []string) int {
for index, b := range list {
if b == a {
return index
}
}
return -1
}
func TrueOrElse(expect bool, a, b string) string {
if expect {
return a
}
return b
}
func U8Encode(i int) string {
bs := make([]byte, 1)
bs[0] = byte(i)
return BytesToHex(bs)
}
func IsNil(a interface{}) bool {
if a == nil {
return true
}
switch reflect.TypeOf(a).Kind() {
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice, reflect.Interface, reflect.Func:
return reflect.ValueOf(a).IsNil()
}
return false
}
// GetEnumValue get enum single key && value
func GetEnumValue(e map[string]interface{}) (string, interface{}, error) {
for key, v := range e {
return key, v, nil
}
return "", "", errors.New("empty enum")
}