2019-12-07 18:18:22 +07:00
|
|
|
package utils
|
|
|
|
|
2020-03-06 08:38:02 +07:00
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// IsStringInSlice checks if a single string is in an array of strings.
|
2020-03-06 08:38:02 +07:00
|
|
|
func IsStringInSlice(a string, list []string) (inSlice bool) {
|
2019-12-07 18:18:22 +07:00
|
|
|
for _, b := range list {
|
|
|
|
if b == a {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-03-06 08:38:02 +07:00
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// SliceString splits a string s into an array with each item being a max of int d
|
|
|
|
// d = denominator, n = numerator, q = quotient, r = remainder.
|
2020-03-06 08:38:02 +07:00
|
|
|
func SliceString(s string, d int) (array []string) {
|
|
|
|
n := len(s)
|
|
|
|
q := n / d
|
|
|
|
r := n % d
|
|
|
|
for i := 0; i < q; i++ {
|
|
|
|
array = append(array, s[i*d:i*d+d])
|
|
|
|
if i+1 == q && r != 0 {
|
|
|
|
array = append(array, s[i*d+d:])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// RandomString generate a random string of n characters.
|
2020-03-06 08:38:02 +07:00
|
|
|
func RandomString(n int, characters []rune) (randomString string) {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
b := make([]rune, n)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = characters[rand.Intn(len(characters))]
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|