This repository has been archived by the owner on Aug 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
part_string.go
120 lines (102 loc) · 2.9 KB
/
part_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
package magicconch
import (
"math/rand"
"strconv"
"strings"
)
// StringPtr is an alias to PtrToString
var StringPtr = PtrToString
// StringEnsurePrefix returns a string, which is the original string
// if it has the prefix, or the prefix will be added.
func StringEnsurePrefix(s string, cut string) string {
if strings.HasPrefix(s, cut) {
return s
}
return cut + s
}
// StringEnsureSuffix returns a string, which is the original string
// if it has the suffix, or the suffix will be appended.
func StringEnsureSuffix(s string, cut string) string {
if strings.HasSuffix(s, cut) {
return s
}
return s + cut
}
// StringInSlice determines whether a string is in a slice or not.
func StringInSlice(str string, slice []string) bool {
for _, s := range slice {
if s == str {
return true
}
}
return false
}
// StringToInt converts a string to an int, errors are ignored
func StringToInt(s string) int {
i, _ := strconv.Atoi(s)
return i
}
// StringToInt8 converts a string to an int8, errors are ignored
func StringToInt8(s string) int8 {
i, _ := strconv.ParseInt(s, 10, 8)
return int8(i)
}
// StringToInt16 converts a string to an int16, errors are ignored
func StringToInt16(s string) int16 {
i, _ := strconv.ParseInt(s, 10, 16)
return int16(i)
}
// StringToInt32 converts a string to an int32, errors are ignored
func StringToInt32(s string) int32 {
i, _ := strconv.ParseInt(s, 10, 32)
return int32(i)
}
// StringToInt64 converts a string to an int64, errors are ignored
func StringToInt64(s string) int64 {
i, _ := strconv.ParseInt(s, 10, 64)
return i
}
// StringToUint converts a string to an uint, errors are ignored
func StringToUint(s string) uint {
i, _ := strconv.Atoi(s)
if i < 0 {
return 0
}
return uint(i)
}
// StringToUint8 converts a string to an uint8, errors are ignored
func StringToUint8(s string) uint8 {
i, _ := strconv.ParseUint(s, 10, 8)
return uint8(i)
}
// StringToUint16 converts a string to an uint16, errors are ignored
func StringToUint16(s string) uint16 {
i, _ := strconv.ParseUint(s, 10, 16)
return uint16(i)
}
// StringToUint32 converts a string to an uint32, errors are ignored
func StringToUint32(s string) uint32 {
i, _ := strconv.ParseUint(s, 10, 32)
return uint32(i)
}
// StringToUint64 converts a string to an uint64, errors are ignored
func StringToUint64(s string) uint64 {
i, _ := strconv.ParseUint(s, 10, 64)
return i
}
const (
// CharsetDefault is the charset for StringRand
CharsetDefault = "abcdefghijklmnopqrstuvwxyzsABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
)
// StringRandWithCharset generates random string with specific length and charset.
func StringRandWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[rand.Intn(len(charset))]
}
return string(b)
}
// StringRand generates random string with specific length.
func StringRand(length int) string {
return StringRandWithCharset(length, CharsetDefault)
}