2021-08-16 00:29:36 +02:00
|
|
|
package entity
|
|
|
|
|
2021-09-03 19:02:26 +02:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-03-30 20:36:25 +02:00
|
|
|
ClipStringType = 64
|
2021-09-03 19:02:26 +02:00
|
|
|
)
|
2021-08-16 00:29:36 +02:00
|
|
|
|
2021-09-03 19:02:26 +02:00
|
|
|
// ToASCII removes all non-ascii characters from a string and returns it.
|
|
|
|
func ToASCII(s string) string {
|
|
|
|
result := make([]rune, 0, len(s))
|
|
|
|
|
|
|
|
for _, r := range s {
|
|
|
|
if r <= 127 {
|
|
|
|
result = append(result, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(result)
|
|
|
|
}
|
|
|
|
|
2022-03-30 20:36:25 +02:00
|
|
|
// Clip shortens a string to the given number of characters, and removes all leading and trailing white space.
|
|
|
|
func Clip(s string, maxLen int) string {
|
2021-09-03 19:02:26 +02:00
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
l := len(s)
|
|
|
|
|
|
|
|
if l <= maxLen {
|
|
|
|
return s
|
|
|
|
} else {
|
2022-01-03 12:09:00 +01:00
|
|
|
return s[:maxLen]
|
2021-09-03 19:02:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 20:36:25 +02:00
|
|
|
// SanitizeStringType omits invalid runes, ensures a maximum length of 32 characters, and returns the result.
|
|
|
|
func SanitizeStringType(s string) string {
|
|
|
|
return Clip(ToASCII(s), ClipStringType)
|
2021-09-03 19:02:26 +02:00
|
|
|
}
|
2021-11-21 14:05:07 +01:00
|
|
|
|
2022-03-30 20:36:25 +02:00
|
|
|
// SanitizeStringTypeLower converts a type string to lowercase, omits invalid runes, and shortens it if needed.
|
|
|
|
func SanitizeStringTypeLower(s string) string {
|
|
|
|
return SanitizeStringType(strings.ToLower(s))
|
2021-12-09 07:00:39 +01:00
|
|
|
}
|
|
|
|
|
2021-11-21 14:05:07 +01:00
|
|
|
// TypeString returns an entity type string for logging.
|
|
|
|
func TypeString(entityType string) string {
|
|
|
|
if entityType == "" {
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
return entityType
|
|
|
|
}
|