2024-01-17 14:26:33 +00:00
|
|
|
package textutil
|
|
|
|
|
|
|
|
import (
|
2024-07-17 12:33:49 +00:00
|
|
|
"regexp"
|
2024-01-17 14:26:33 +00:00
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
)
|
|
|
|
|
|
|
|
// We leave the full range of unicode letters in tact, but remove all "special" characters,
|
|
|
|
// including spaces and dots, which are not supported in e.g. experiment names or YAML keys.
|
|
|
|
func NormalizeString(name string) string {
|
|
|
|
name = strings.ToLower(name)
|
2024-07-17 12:33:49 +00:00
|
|
|
s := strings.Map(replaceNonAlphanumeric, name)
|
|
|
|
|
|
|
|
// replacing multiple underscores with a single one
|
|
|
|
re := regexp.MustCompile(`_+`)
|
|
|
|
s = re.ReplaceAllString(s, "_")
|
|
|
|
|
|
|
|
// removing leading and trailing underscores
|
|
|
|
return strings.Trim(s, "_")
|
2024-01-17 14:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func replaceNonAlphanumeric(r rune) rune {
|
|
|
|
if unicode.IsLetter(r) || unicode.IsDigit(r) {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
return '_'
|
|
|
|
}
|