init
This commit is contained in:
15
vendor/github.com/asticode/go-astitools/string/length.go
generated
vendored
Normal file
15
vendor/github.com/asticode/go-astitools/string/length.go
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
package astistring
|
||||
|
||||
// ToLength forces the length of a string
|
||||
func ToLength(i, rpl string, length int) string {
|
||||
if len(i) == length {
|
||||
return i
|
||||
} else if len(i) > length {
|
||||
return i[:length]
|
||||
} else {
|
||||
for idx := 0; idx <= length-len(i); idx++ {
|
||||
i += rpl
|
||||
}
|
||||
return i
|
||||
}
|
||||
}
|
35
vendor/github.com/asticode/go-astitools/string/rand.go
generated
vendored
Normal file
35
vendor/github.com/asticode/go-astitools/string/rand.go
generated
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
package astistring
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
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
|
||||
)
|
||||
|
||||
var src = rand.NewSource(time.Now().UnixNano())
|
||||
|
||||
// RandomString generates a random string
|
||||
// https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang
|
||||
func RandomString(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(b)
|
||||
}
|
Reference in New Issue
Block a user