/
util.go
294 lines (247 loc) · 5.58 KB
/
util.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2019 Huawei Technologies Co.,Ltd.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
package assist
import (
"crypto/md5"
"encoding/base64"
"encoding/hex"
"encoding/xml"
"fmt"
"gopkg.in/cheggaaa/pb.v2/termutil"
"hash"
"io"
"math"
"math/rand"
"os"
"regexp"
"runtime"
"strconv"
"strings"
"time"
"unsafe"
)
var chineseRegex = regexp.MustCompile("[\u4e00-\u9fa5]")
var noUnicodeRegex = regexp.MustCompile("^[\\w\\s=;:,<>!@#%&-/'~`.+*?\"$^|{}()\\[\\]\\\\]*$")
func HasChinese(val string) bool {
return chineseRegex.MatchString(val)
}
func HasUnicode(val string) bool {
return !noUnicodeRegex.MatchString(val)
}
func GetOS() string {
return runtime.GOOS
}
func GetArch() string {
return runtime.GOARCH
}
func Round(input float64) float64 {
_, t := math.Modf(input)
if t < 0.5 {
return math.Floor(input)
}
return math.Ceil(input)
}
func GetTerminalWidth() (int, error) {
return termutil.TerminalWidth()
}
func IsWindows() bool {
return runtime.GOOS == "windows"
}
func IsMac() bool {
return runtime.GOOS == "darwin"
}
func IsLinux() bool {
return runtime.GOOS == "linux"
}
func GetCpuNumber() int {
return runtime.NumCPU()
}
func StringToInt(value string, def int) int {
ret, err := strconv.Atoi(value)
if err != nil {
ret = def
}
return ret
}
func StringToInt64(value string, def int64) int64 {
ret, err := strconv.ParseInt(value, 10, 64)
if err != nil {
ret = def
}
return ret
}
func StringToFloat64(value string, def float64) float64 {
ret, err := strconv.ParseFloat(value, 64)
if err != nil {
ret = def
}
return ret
}
func StringToIntV2(value string) (int, error) {
return strconv.Atoi(value)
}
func StringToInt64V2(value string) (int64, error) {
return strconv.ParseInt(value, 10, 64)
}
func StringToFloat64V2(value string) (float64, error) {
return strconv.ParseFloat(value, 64)
}
func ParseXml(value []byte, result interface{}) error {
if len(value) == 0 {
return nil
}
return xml.Unmarshal(value, result)
}
func TransToXml(value interface{}) ([]byte, error) {
if value == nil {
return []byte{}, nil
}
return xml.Marshal(value)
}
func Base64Encode(value []byte) string {
return base64.StdEncoding.EncodeToString(value)
}
func Base64Decode(value string) ([]byte, error) {
return base64.StdEncoding.DecodeString(value)
}
func Md5(value []byte) []byte {
m := md5.New()
m.Write(value)
return m.Sum(nil)
}
func GetMd5Writer() io.Writer {
return md5.New()
}
func GetBase64Md5(writer io.Writer) string {
if md5Hash, ok := writer.(hash.Hash); ok {
return Base64Encode(md5Hash.Sum(nil))
}
return ""
}
func GetHexMd5(writer io.Writer) string {
if md5Hash, ok := writer.(hash.Hash); ok {
return Hex(md5Hash.Sum(nil))
}
return ""
}
func Hex(value []byte) string {
return hex.EncodeToString(value)
}
func HexMd5(value []byte) string {
return Hex(Md5(value))
}
func Base64Md5(value []byte) string {
return Base64Encode(Md5(value))
}
func IntToString(value int) string {
return strconv.Itoa(value)
}
func Int64ToString(value int64) string {
return strconv.FormatInt(value, 10)
}
func GetCurrentTimestamp() int64 {
return time.Now().UnixNano() / 1000000
}
func GetUtcNow() time.Time {
return time.Now().UTC()
}
func FormatUtcNow(format string) string {
return time.Now().UTC().Format(format)
}
func FormatUtcToRfc1123(t time.Time) string {
ret := t.UTC().Format(time.RFC1123)
return ret[:strings.LastIndex(ret, "UTC")] + "GMT"
}
func MinFloat64(va, vb float64) float64 {
if va <= vb {
return va
}
return vb
}
func MinInt(va, vb int) int {
if va <= vb {
return va
}
return vb
}
func MaxFloat64(va, vb float64) float64 {
if va <= vb {
return vb
}
return va
}
func MaxInt(va, vb int) int {
if va <= vb {
return vb
}
return va
}
func Str2Timestamp(str string, defaultValue int64) (ts int64, err error) {
basic := "00000101000000"
str = strings.TrimSpace(str)
if str == "*" {
ts = defaultValue
} else {
str += basic[len(str):]
if formatTime, _err := time.Parse("20060102150405", str); _err == nil {
ts = formatTime.Unix()
} else {
err = _err
}
}
return
}
func EnsureDirectory(dir string) (err error) {
stat, err := os.Stat(dir)
if err == nil && !stat.IsDir() {
err = fmt.Errorf("path [%s] is not a directory", dir)
return
}
err = MkdirAll(dir, os.ModePerm)
return
}
func MaybeDeleteBeginningSlash(srckey string) string {
key := srckey
if strings.HasPrefix(srckey, "/") {
key = srckey[1:]
}
return key
}
func StringToBytes(s string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&s))
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h))
}
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func MaybeDeleteTrailingSlash(srckey string) string {
key := srckey
if strings.HasSuffix(srckey, "/") {
key = srckey[:len(srckey)-1]
}
return key
}
func MaybeAddTrailingSlash(srckey string) string {
key := srckey
if srckey != "" && !strings.HasSuffix(srckey, "/") {
key = srckey + "/"
}
return key
}
func SleepByCount(cnt int) {
sleepTime := float64(cnt+2) * rand.Float64()
if sleepTime > 10 {
sleepTime = 10
}
time.Sleep(time.Duration(sleepTime * float64(time.Second)))
}