mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-04-13 07:37:17 +03:00
For #285 Turns out there were some bugs in Set, and I was using it incorrectly too. The query syntax is a little awkward but couldn't find a nicer easy to parse format that worked with quoted string values.
29 lines
648 B
Go
29 lines
648 B
Go
package sanitize
|
|
|
|
import "regexp"
|
|
|
|
var reStripName = regexp.MustCompile("[^\\w.-]")
|
|
|
|
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
|
|
}
|
|
|
|
var reStripData = regexp.MustCompile("[^[:ascii:]]|[[:cntrl:]]")
|
|
|
|
// 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, "")
|
|
}
|