-
Notifications
You must be signed in to change notification settings - Fork 7
/
numbers.go
187 lines (147 loc) · 4.05 KB
/
numbers.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
package gostrutils
import (
"regexp"
"strconv"
"strings"
)
var (
intRegex = regexp.MustCompile("^[0-9]+$")
signedIntRegex = regexp.MustCompile("^-?[0-9]+$")
floatRegex = regexp.MustCompile(`^[0-9]+\.[0-9]+$`)
signedFloatRegex = regexp.MustCompile(`^-?[0-9]+\.[0-9]+$`)
)
// StrToInt64 convert a string to int64
func StrToInt64(str string, def int64) int64 {
result, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return def
}
return result
}
// StrToUInt64 convert string to uint64
func StrToUInt64(str string, def uint64) uint64 {
result, err := strconv.ParseUint(str, 10, 64)
if err != nil {
return def
}
return result
}
// UInt64Join takes a list of uint64 and join them with a separator
// based on https://play.golang.org/p/KpdkVS1B4s
func UInt64Join(list []uint64, sep string) string {
length := len(list)
if length == 0 {
return ""
}
if length == 1 {
return strconv.FormatUint(list[0], 10)
}
s := ""
for i, item := range list {
s += strconv.FormatUint(item, 10)
if i < length-1 {
s += sep
}
}
return s
}
// Int64Join takes a list of int64 and join them with a separator
// based on https://play.golang.org/p/KpdkVS1B4s
func Int64Join(list []int64, sep string) string {
length := len(list)
if length == 0 {
return ""
}
if length == 1 {
return strconv.FormatInt(list[0], 10)
}
s := ""
for i, item := range list {
s += strconv.FormatInt(item, 10)
if i < length-1 {
s += sep
}
}
return s
}
// Uin64Split get a string with separator and convert it to a slice of uint64
func Uin64Split(data, sep string) []uint64 {
fields := strings.Split(data, sep)
result := make([]uint64, len(fields))
for i, elem := range fields {
result[i], _ = strconv.ParseUint(elem, 10, 64)
}
return result
}
// In64Split get a string with separator and convert it to a slice of int64
func In64Split(data, sep string) []int64 {
fields := strings.Split(data, sep)
result := make([]int64, len(fields))
for i, elem := range fields {
result[i], _ = strconv.ParseInt(elem, 10, 64)
}
return result
}
// ToFloat32Default convert string to float32 without errors. If error returns, defaultValue is set instead.
func ToFloat32Default(field string, defaultValue float32) float32 {
result, err := strconv.ParseFloat(field, 32)
if err != nil {
return defaultValue
}
return float32(result)
}
// ToFloat32 convert string to float32 without errors!
func ToFloat32(field string) float32 {
return ToFloat32Default(field, 0.0)
}
// ToFloat6Default convert string to float64 without errors. If error returns, default is set instead.
func ToFloat6Default(field string, defaultValue float64) float64 {
result, err := strconv.ParseFloat(field, 64)
if err != nil {
return defaultValue
}
return result
}
// ToFloat64 convert string to float64 without errors!
func ToFloat64(field string) float64 {
return ToFloat6Default(field, 0.0)
}
// IsUInteger returns true if a string is unsigned integer
func IsUInteger(txt string) bool {
return intRegex.MatchString(txt)
}
// IsInteger returns true if a string is an integer
func IsInteger(txt string) bool {
return signedIntRegex.MatchString(txt)
}
// IsUFloat returns true if a string is unsigned floating point
func IsUFloat(txt string) bool {
return floatRegex.MatchString(txt)
}
// IsFloat returns true if a given text is a floating point
func IsFloat(txt string) bool {
return signedFloatRegex.MatchString(txt)
}
// IsUNumber returns true if a given string is unsigned integer or float
func IsUNumber(txt string) bool {
return IsUInteger(txt) || IsUFloat(txt)
}
// IsNumber returns true if a given string is integer or float
func IsNumber(txt string) bool {
return IsInteger(txt) || IsFloat(txt)
}
// IsInRange takes an integer range and look at the string that contains only
// numbers tom make sure it is inside the range
func IsInRange(min, max int64, src string) bool {
if min > max { // no error so nothing to look for
return false
}
if src == "" {
return false
}
dest, err := strconv.ParseInt(src, 10, 64)
if err != nil {
return false
}
return dest >= min && dest <= max
}