Newer
Older
var src rand.Source
func init() {
src = rand.NewSource(time.Now().UnixNano())
}
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
// RandASCIIString returns a random string consisting of ASCII characters of the given
// length.
func RandASCIIString(n int) string {
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
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(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
return *(*string)(unsafe.Pointer(&b))
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
}
func IntersectSlices(a, b []string) (res []string) {
for _, bb := range b {
if StringInSlice(bb, a) {
res = append(res, bb)
}
}
return
}
func StringInSlice(key string, slice []string) bool {
for _, s := range slice {
if s == key {
return true
}
}
return false
}
func IsSubSet(a, b []string) bool {
for _, aa := range a {
if !StringInSlice(aa, b) {
return false
}
}
return true
}
func IPsAreSubSet(ipsA, ipsB []string) bool {
for _, ipA := range ipsA {
if !ipIsIn(ipA, ipsB) {
return false
}
}
return true
}
func ipIsIn(ip string, ips []string) bool {
ipA := net.ParseIP(ip)
for _, ipp := range ips {
if strings.Contains(ipp, "/") {
_, ipNetB, _ := net.ParseCIDR(ipp)
if ipNetB.Contains(ipA) {
return true
}
} else {
if ip == ipp {
return true
}
}
}
return false
}
// CombineURLPath combines multiple parts of a url
func CombineURLPath(p string, ps ...string) (r string) {
r = p
for _, pp := range ps {
if pp == "" {
continue
}
if r == "" {
r = pp
continue
}
rAppend := r
ppAppend := pp
if strings.HasSuffix(r, "/") {
rAppend = r[:len(r)-1]
}
if strings.HasPrefix(pp, "/") {
ppAppend = pp[1:]
}
r = fmt.Sprintf("%s%c%s", rAppend, '/', ppAppend)
}
return
}