-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtoolkit.go
More file actions
81 lines (73 loc) · 1.71 KB
/
Copy pathtoolkit.go
File metadata and controls
81 lines (73 loc) · 1.71 KB
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
package toolkit
import (
"encoding/hex"
"encoding/json"
"reflect"
)
// ToolKit is the set of tool plugins.
// exported methods and fields can be inject into vm.
type ToolKit struct {
}
// NewToolKit create a new ToolKit instant as a plugin to vm
func NewToolKit() *ToolKit {
return &ToolKit{}
}
// RandStr generate a random string with specific length
func (t *ToolKit) RandStr(l uint) string {
return randomString(l)
}
// RandInt generate a random int in specific range
func (t *ToolKit) RandInt(min, max int) int {
return randomInt(min, max)
}
// String convert to string
func (t *ToolKit) String(input interface{}, offsets ...int) string {
v := reflect.ValueOf(input)
switch v.Kind() {
case reflect.Ptr:
return t.String(v.Elem().Interface(), offsets...)
case reflect.Slice:
bs := transferToBts(input)
l := len(bs)
start, end := 0, l
if len(offsets) > 0 && offsets[0] <= l {
start = offsets[0]
}
if len(offsets) > 1 && offsets[1] <= l+1 {
start = offsets[1]
}
return string(bs[start:end])
case reflect.Array:
bs := transferToBts(input)
l := v.Type().Len()
start, end := 0, l
if len(offsets) > 0 && offsets[0] <= l {
start = offsets[0]
}
if len(offsets) > 1 && offsets[1] <= l+1 {
start = offsets[1]
}
ss := make([]byte, 0, end-start)
for i := start; i < end; i++ {
ss = append(ss, bs[i])
}
return string(ss)
}
return ""
}
func transferToBts(input interface{}) []byte {
bs, ok := input.([]byte)
if !ok {
bs = []byte{}
jsData, _ := json.Marshal(input)
err := json.Unmarshal(jsData, &bs)
if err != nil {
return []byte{}
}
}
return bs
}
// Hex encode string as hex string
func (t *ToolKit) Hex(input string) string {
return hex.EncodeToString([]byte(input))
}