2019-12-07 11:18:22 +00:00
|
|
|
package utils
|
|
|
|
|
2020-03-06 01:38:02 +00:00
|
|
|
import (
|
|
|
|
"math/rand"
|
2020-12-03 05:23:52 +00:00
|
|
|
"strings"
|
2020-03-06 01:38:02 +00:00
|
|
|
"time"
|
2020-05-21 02:20:55 +00:00
|
|
|
"unicode"
|
2020-03-06 01:38:02 +00:00
|
|
|
)
|
|
|
|
|
2020-05-21 02:20:55 +00:00
|
|
|
// IsStringAlphaNumeric returns false if any rune in the string is not alpha-numeric.
|
|
|
|
func IsStringAlphaNumeric(input string) bool {
|
|
|
|
for _, r := range input {
|
|
|
|
if !unicode.IsLetter(r) && !unicode.IsNumber(r) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-05 04:18:31 +00:00
|
|
|
// IsStringInSlice checks if a single string is in a slice of strings.
|
|
|
|
func IsStringInSlice(a string, slice []string) (inSlice bool) {
|
|
|
|
for _, b := range slice {
|
2019-12-07 11:18:22 +00:00
|
|
|
if b == a {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-12-07 11:18:22 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-03-06 01:38:02 +00:00
|
|
|
|
2021-03-05 04:18:31 +00:00
|
|
|
// IsStringInSliceFold checks if a single string is in a slice of strings but uses strings.EqualFold to compare them.
|
|
|
|
func IsStringInSliceFold(a string, slice []string) (inSlice bool) {
|
|
|
|
for _, b := range slice {
|
|
|
|
if strings.EqualFold(b, a) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-16 01:30:03 +00:00
|
|
|
// IsStringInSliceContains checks if a single string is in an array of strings.
|
|
|
|
func IsStringInSliceContains(a string, list []string) (inSlice bool) {
|
|
|
|
for _, b := range list {
|
|
|
|
if strings.Contains(a, b) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:03:38 +00: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 01:38:02 +00:00
|
|
|
func SliceString(s string, d int) (array []string) {
|
|
|
|
n := len(s)
|
|
|
|
q := n / d
|
|
|
|
r := n % d
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-03-06 01:38:02 +00:00
|
|
|
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:])
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-03-06 01:38:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-04 22:06:05 +00:00
|
|
|
func isStringSlicesDifferent(a, b []string, method func(s string, b []string) bool) (different bool) {
|
2021-02-02 01:01:46 +00:00
|
|
|
if len(a) != len(b) {
|
|
|
|
return true
|
2020-05-04 19:39:25 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-02-02 01:01:46 +00:00
|
|
|
for _, s := range a {
|
2021-05-04 22:06:05 +00:00
|
|
|
if !method(s, b) {
|
2020-05-04 19:39:25 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-05-04 19:39:25 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-04 22:06:05 +00:00
|
|
|
// IsStringSlicesDifferent checks two slices of strings and on the first occurrence of a string item not existing in the
|
|
|
|
// other slice returns true, otherwise returns false.
|
|
|
|
func IsStringSlicesDifferent(a, b []string) (different bool) {
|
|
|
|
return isStringSlicesDifferent(a, b, IsStringInSlice)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsStringSlicesDifferentFold checks two slices of strings and on the first occurrence of a string item not existing in
|
|
|
|
// the other slice (case insensitive) returns true, otherwise returns false.
|
|
|
|
func IsStringSlicesDifferentFold(a, b []string) (different bool) {
|
|
|
|
return isStringSlicesDifferent(a, b, IsStringInSliceFold)
|
|
|
|
}
|
|
|
|
|
2020-05-04 19:39:25 +00:00
|
|
|
// StringSlicesDelta takes a before and after []string and compares them returning a added and removed []string.
|
|
|
|
func StringSlicesDelta(before, after []string) (added, removed []string) {
|
|
|
|
for _, s := range before {
|
|
|
|
if !IsStringInSlice(s, after) {
|
|
|
|
removed = append(removed, s)
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-05-04 19:39:25 +00:00
|
|
|
for _, s := range after {
|
|
|
|
if !IsStringInSlice(s, before) {
|
|
|
|
added = append(added, s)
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-05-04 19:39:25 +00:00
|
|
|
return added, removed
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// RandomString generate a random string of n characters.
|
2020-03-06 01:38:02 +00:00
|
|
|
func RandomString(n int, characters []rune) (randomString string) {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-03-06 01:38:02 +00:00
|
|
|
b := make([]rune, n)
|
|
|
|
for i := range b {
|
2020-09-04 03:20:17 +00:00
|
|
|
b[i] = characters[rand.Intn(len(characters))] //nolint:gosec // Likely isn't necessary to use the more expensive crypto/rand for this utility func.
|
2020-03-06 01:38:02 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-03-06 01:38:02 +00:00
|
|
|
return string(b)
|
|
|
|
}
|