2019-01-31 16:06:36 +00:00
|
|
|
package sanitize
|
|
|
|
|
|
|
|
import "regexp"
|
|
|
|
|
2019-11-17 20:42:41 +00:00
|
|
|
var (
|
|
|
|
reStripName = regexp.MustCompile("[^\\w.-]")
|
|
|
|
reStripData = regexp.MustCompile("[^[:ascii:]]|[[:cntrl:]]")
|
|
|
|
)
|
2019-01-31 16:06:36 +00:00
|
|
|
|
|
|
|
const maxLength = 16
|
|
|
|
|
|
|
|
// Name returns a name with only allowed characters and a reasonable length
|
|
|
|
func Name(s string) string {
|
|
|
|
s = reStripName.ReplaceAllString(s, "")
|
|
|
|
nameLength := maxLength
|
|
|
|
if len(s) <= maxLength {
|
|
|
|
nameLength = len(s)
|
|
|
|
}
|
|
|
|
s = s[:nameLength]
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data returns a string with only allowed characters for client-provided metadata inputs.
|
|
|
|
func Data(s string, maxlen int) string {
|
|
|
|
if len(s) > maxlen {
|
|
|
|
s = s[:maxlen]
|
|
|
|
}
|
|
|
|
return reStripData.ReplaceAllString(s, "")
|
|
|
|
}
|