56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package utils
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// FirstCharacterToUppercase converts the first character of a string to uppercase
|
|
func FirstCharacterToUppercase(s string) string {
|
|
if len(s) == 0 {
|
|
return s
|
|
}
|
|
|
|
if len(s) == 1 {
|
|
return strings.ToUpper(s)
|
|
}
|
|
|
|
return strings.ToUpper(s[:1]) + strings.ToLower(s[1:])
|
|
}
|
|
|
|
// GetSHA256SumOfString returns the sha256 sum of s
|
|
func GetSHA256SumOfString(s string) string {
|
|
h := sha256.New()
|
|
h.Write([]byte(s))
|
|
return fmt.Sprintf("%x", h.Sum(nil))
|
|
}
|
|
|
|
// RemoveDoubledStrings removes all duplicate occurrences of target in s
|
|
func RemoveDoubledStrings(s string, target string) string {
|
|
doubledTarget := fmt.Sprintf("%s%s", target, target)
|
|
|
|
if !strings.Contains(s, doubledTarget) {
|
|
return s
|
|
}
|
|
|
|
return RemoveDoubledStrings(
|
|
strings.Replace(s, doubledTarget, target, -1),
|
|
target,
|
|
)
|
|
}
|
|
|
|
// SanitizeStringWithWhitelist cleans s by all substrings which do not occur in whitelist
|
|
func SanitizeStringWithWhitelist(s string, whitelist map[string]string) string {
|
|
sanitizedString := ""
|
|
|
|
for _, char := range s {
|
|
whitelistedChar, charIsWhitelisted := whitelist[string(char)]
|
|
if charIsWhitelisted {
|
|
sanitizedString = sanitizedString + whitelistedChar
|
|
}
|
|
}
|
|
|
|
return sanitizedString
|
|
}
|