-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStr.go
153 lines (128 loc) · 3.42 KB
/
Str.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
package gohelper
import (
"math/rand"
"reflect"
"regexp"
"strings"
"time"
"unicode"
"unsafe"
)
const lowercs = "abcdefghijklmnopqrstuvwxyz"
const uppercs = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const numbers = "0123456789"
const symbols = `~!@#$%^&*()_+{}|:"<>?-=[]\;',./`
const letterIdxBits = 6
const letterIdxMask = 2<<letterIdxBits - 1
const letterIdxMax = 63 / letterIdxBits
type Option struct {
IncludeNumber bool
IncludeUppercase bool
IncludeSymbol bool
}
// RandStr generates a random string of length n.
//
// n: the length of the string to be generated.
// string: the randomly generated string.
//
// @see https://github.com/tamboto2000/random
func RandStr(n int) string {
return gen(n, Option{})
}
// RandStrWithOption generates a random string of length n with custom options.
//
// n: the length of the string to be generated.
// opt: the options to include in the generated string.
// string: the randomly generated string.
func RandStrWithOption(n int, opt Option) string {
return gen(n, opt)
}
func Empty(val any) bool {
if val == nil {
return true
}
v := reflect.ValueOf(val)
switch v.Kind() {
case reflect.String, reflect.Array:
return v.Len() == 0
case reflect.Slice:
return v.Len() == 0 || (v.Cap() != 0 && v.IsNil())
case reflect.Map:
return v.Len() == 0 || v.IsNil()
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Ptr:
return v.IsNil()
}
return reflect.DeepEqual(val, reflect.Zero(v.Type()).Interface())
}
// Trim trims the specified characters from both ends of a string.
//
// Parameters:
// - s: the string to be trimmed.
// - char: the characters to be trimmed.
// Return type:
// - string: the trimmed string.
func Trim(s string, char string) string {
return TrimLeft(TrimRight(s, char), char)
}
// TrimLeft removes leading characters from a string.
//
// Parameters:
// - s: the string to trim.
// - char: the characters to remove from the start of the string.
// Return type:
// - string: the trimmed string.
func TrimLeft(s string, char string) string {
if char == "" {
return strings.TrimLeftFunc(s, unicode.IsSpace)
}
r, _ := regexp.Compile("^[" + char + "]+")
return r.ReplaceAllString(s, "")
}
// TrimRight removes trailing characters from a string.
//
// Parameters:
// - s: the string to be trimmed.
// - char: the characters to be trimmed.
// Return type:
// - string: the trimmed string.
func TrimRight(s string, char string) string {
if char == "" {
return strings.TrimRightFunc(s, unicode.IsSpace)
}
r, _ := regexp.Compile("[" + char + "]+$")
return r.ReplaceAllString(s, "")
}
func gen(n int, opt Option) string {
letters := lowercs
if opt.IncludeNumber {
letters += numbers
}
if opt.IncludeUppercase {
letters +=uppercs
}
if opt.IncludeSymbol {
letters += symbols
}
src := rand.NewSource(time.Now().UnixNano())
byt := make([]byte, n)
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letters) {
byt[i] = letters[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return *(*string)(unsafe.Pointer(&byt))
}