summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/shazow/ssh-chat/internal/sanitize/sanitize.go
blob: ed532c9711466be6c7b9aeedcf0e89484536530a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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, "")
}