-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey.go
33 lines (28 loc) · 1.27 KB
/
key.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
package netstring
// Key is the byte value provided to the Encoder Encode*() functions to determine whether
// the encoded netstring is a standard netstring or a "keyed" netstring. Valid values are:
// NoKey (or 0) for a standard netstring or an isalpha() value ('a'-'z' or 'A'-'Z') for a
// "keyed" netstring. All other values are invalid. Key is also the type returned by the
// Decoder functions. Use Key.Assess() to determine the validity and type of a key.
type Key byte
// NoKey is the special "key" provided to the Encoder.Encode*() functions to indicate that
// a standard netstring should be encoded.
const NoKey Key = 0
func (k Key) String() string {
return string(k)
}
// Assess determines whether the Key 'k' is valid or not and whether it implies a standard
// or "keyed" netstring. NoKey.Assess() returns keyed=false and err=nil which is to say
// that Assess treats NoKey as valid but it signifies a standard netstring.
//
// Assess returnes keyed=true and err=nil if 'k' is in the range 'a'-'z' or 'A'-'Z'
// inclusive which is to say that it matches the isalpha() C90 library function.
func (k Key) Assess() (keyed bool, err error) {
if k == NoKey {
return false, nil
}
if (k >= 'a' && k <= 'z') || (k >= 'A' && k <= 'Z') {
return true, nil
}
return false, ErrInvalidKey
}