-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathboolean.go
49 lines (37 loc) · 958 Bytes
/
boolean.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
package httpsfv
import (
"errors"
"io"
)
// ErrInvalidBooleanFormat is returned when a boolean format is invalid.
var ErrInvalidBooleanFormat = errors.New("invalid boolean format")
// marshalBoolean serializes as defined in
// https://httpwg.org/specs/rfc8941.html#ser-boolean.
func marshalBoolean(bd io.StringWriter, b bool) error {
if b {
_, err := bd.WriteString("?1")
return err
}
_, err := bd.WriteString("?0")
return err
}
// parseBoolean parses as defined in
// https://httpwg.org/specs/rfc8941.html#parse-boolean.
func parseBoolean(s *scanner) (bool, error) {
if s.eof() || s.data[s.off] != '?' {
return false, &UnmarshalError{s.off, ErrInvalidBooleanFormat}
}
s.off++
if s.eof() {
return false, &UnmarshalError{s.off, ErrInvalidBooleanFormat}
}
switch s.data[s.off] {
case '0':
s.off++
return false, nil
case '1':
s.off++
return true, nil
}
return false, &UnmarshalError{s.off, ErrInvalidBooleanFormat}
}