-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
138 lines (121 loc) · 2.73 KB
/
string.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
package item
import (
"bytes"
"math/rand"
"reflect"
"sync"
"time"
"unsafe"
)
var buffPool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
// 获取随机字符串
var randStringLowBase = []byte("0123456789abcdefghijklmnopqrstuvwxyz")
func GetRandomString(cnum int) string {
bs := randStringLowBase
result := buffPool.Get().(*bytes.Buffer)
defer func() {
result.Reset()
buffPool.Put(result)
}()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < cnum; i++ {
result.WriteByte(bs[r.Intn(len(bs))])
}
return result.String()
}
var randStringNumber = []byte("1234567890")
// GetRandomNumString 获取数字字符串
func GetRandomNumString(cnum int) string {
bs := randStringNumber
result := buffPool.Get().(*bytes.Buffer)
defer func() {
result.Reset()
buffPool.Put(result)
}()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < cnum; i++ {
result.WriteByte(bs[r.Intn(len(bs))])
}
return result.String()
}
// GetRandomNumInt 获取数字字符串
func GetRandomNumInt(cnum int) int {
bs := randStringNumber
var result int
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < cnum; i++ {
result += result*10 + r.Intn(len(bs))
}
return result
}
var randStringUpBase = []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYX")
func GetRandomStringUp(cnum int) string {
bs := randStringUpBase
result := buffPool.Get().(*bytes.Buffer)
defer func() {
result.Reset()
buffPool.Put(result)
}()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < cnum; i++ {
result.WriteByte(bs[r.Intn(len(bs))])
}
return result.String()
}
func CamelToCase(str string) string {
result := buffPool.Get().(*bytes.Buffer)
defer func() {
result.Reset()
buffPool.Put(result)
}()
for i, p := range StringToBytesUnsafe(str) {
if p > 64 && p < 91 {
p = p + 32
if i != 0 {
result.WriteByte('_')
}
}
result.WriteByte(p)
}
return result.String()
}
func CaseToCamel(str string) string {
result := buffPool.Get().(*bytes.Buffer)
defer func() {
result.Reset()
buffPool.Put(result)
}()
ucFlag := true
for _, p := range StringToBytesUnsafe(str) {
if p == '_' {
ucFlag = true
continue
}
if p > 96 && p < 123 {
if ucFlag {
p = p - 32
ucFlag = false
}
}
result.WriteByte(p)
}
return result.String()
}
// StringToBytesUnsafe 快速转换 但是不能用于并发场景
func StringToBytesUnsafe(s string) []byte {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := reflect.SliceHeader{
Data: sh.Data,
Len: sh.Len,
Cap: sh.Len,
}
return *(*[]byte)(unsafe.Pointer(&bh))
}
// BytesToString 快速转换 但是不能用于并发场景
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}