forked from beanstalkd/go-beanstalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
name.go
51 lines (44 loc) · 1.06 KB
/
name.go
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package beanstalk
import (
"errors"
)
// Characters allowed in a name in the beanstalkd protocol.
const NameChars = `\-+/;.$_()0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`
// NameError indicates that a name was malformed and the specific error
// describing how.
type NameError struct {
Name string
Err error
}
func (e NameError) Error() string {
return e.Err.Error() + ": " + e.Name
}
// Name format errors. The Err field of NameError contains one of these.
var (
ErrEmpty = errors.New("name is empty")
ErrBadChar = errors.New("name has bad char") // contains a character not in NameChars
ErrTooLong = errors.New("name is too long")
)
func checkName(s string) error {
switch {
case len(s) == 0:
return NameError{s, ErrEmpty}
case len(s) >= 200:
return NameError{s, ErrTooLong}
case !containsOnly(s, NameChars):
return NameError{s, ErrBadChar}
}
return nil
}
func containsOnly(s, chars string) bool {
outer:
for _, c := range s {
for _, m := range chars {
if c == m {
continue outer
}
}
return false
}
return true
}