package utils import ( "crypto/rand" "errors" "fmt" "io" "net/url" "regexp" "strings" "unicode" ) // GenerateRandomAlphanumericString generates a random alphanumeric string of the given length func GenerateRandomAlphanumericString(length int) (string, error) { const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" if length <= 0 { return "", errors.New("length must be a positive integer") } // The algorithm below is adapted from https://stackoverflow.com/a/35615565 const ( letterIdxBits = 6 // 6 bits to represent a letter index letterIdxMask = 1< AB_Cd reLowerToUpper = regexp.MustCompile(`([a-z0-9])([A-Z])`) // aB -> a_B ) func CamelCaseToSnakeCase(s string) string { s = reAcronymBoundary.ReplaceAllString(s, "${1}_${2}") s = reLowerToUpper.ReplaceAllString(s, "${1}_${2}") return strings.ToLower(s) } func CamelCaseToScreamingSnakeCase(s string) string { s = reAcronymBoundary.ReplaceAllString(s, "${1}_${2}") s = reLowerToUpper.ReplaceAllString(s, "${1}_${2}") return strings.ToUpper(s) } // GetFirstCharacter returns the first non-whitespace character of the string, correctly handling Unicode func GetFirstCharacter(str string) string { for _, c := range str { if unicode.IsSpace(c) { continue } return string(c) } // Empty string case return "" }