-
Notifications
You must be signed in to change notification settings - Fork 13
/
helpers.go
102 lines (94 loc) · 2.15 KB
/
helpers.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
package core
import (
"fmt"
"strconv"
)
// return the position of the first none space, or -1 if no white space exists
func SkipSpaces(data []byte) int {
for index, b := range data {
if b != ' ' {
return index
}
}
return -1
}
// Since these templates are possibly long-lived, let's free up any space
// which was accumulated while we grew these arrays
func TrimStrings(values []string) []string {
if len(values) == cap(values) {
return values
}
trimmed := make([]string, len(values))
copy(trimmed, values)
return trimmed
}
// Since these templates are possibly long-lived, let's free up any space
// which was accumulated while we grew these arrays
func TrimValues(values []Value) []Value {
if len(values) == cap(values) {
return values
}
trimmed := make([]Value, len(values))
copy(trimmed, values)
return trimmed
}
// Convert arbitrary data to []byte
func ToBytes(data interface{}) []byte {
switch typed := data.(type) {
case []byte:
return typed
case string:
return []byte(typed)
case bool:
return []byte(strconv.FormatBool(typed))
case float64:
return []byte(strconv.FormatFloat(typed, 'g', -1, 64))
case uint64:
return []byte(strconv.FormatUint(typed, 10))
case uint:
return []byte(strconv.FormatUint(uint64(typed), 10))
case int:
return []byte(strconv.Itoa(typed))
case fmt.Stringer:
return []byte(typed.String())
}
return []byte(fmt.Sprintf("%v", data))
}
// Convert arbitrary data to string
func ToString(data interface{}) string {
switch typed := data.(type) {
case string:
return typed
case fmt.Stringer:
return typed.String()
default:
return string(ToBytes(data))
}
}
// Convert arbitrary data to string
func ToInt(data interface{}) (int, bool) {
switch typed := data.(type) {
case int:
return typed, true
case int32:
return int(typed), true
case int64:
return int(typed), true
case uint:
return int(typed), true
case float64:
return int(typed), true
case string:
return stringToInt(typed)
case []byte:
return stringToInt(string(typed))
default:
return 0, false
}
}
func stringToInt(s string) (int, bool) {
if n, err := strconv.Atoi(s); err == nil {
return n, true
}
return 0, false
}