-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
innerlist.go
91 lines (70 loc) · 1.79 KB
/
innerlist.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package httpsfv
import (
"errors"
"strings"
)
// ErrInvalidInnerListFormat is returned when an inner list format is invalid.
var ErrInvalidInnerListFormat = errors.New("invalid inner list format")
// InnerList represents an inner list as defined in
// https://httpwg.org/specs/rfc8941.html#inner-list.
type InnerList struct {
Items []Item
Params *Params
}
func (il InnerList) member() {
}
// marshalSFV serializes as defined in
// https://httpwg.org/specs/rfc8941.html#ser-innerlist.
func (il InnerList) marshalSFV(b *strings.Builder) error {
if err := b.WriteByte('('); err != nil {
return err
}
l := len(il.Items)
for i := 0; i < l; i++ {
if err := il.Items[i].marshalSFV(b); err != nil {
return err
}
if i != l-1 {
if err := b.WriteByte(' '); err != nil {
return err
}
}
}
if err := b.WriteByte(')'); err != nil {
return err
}
return il.Params.marshalSFV(b)
}
// parseInnerList parses as defined in
// https://httpwg.org/specs/rfc8941.html#parse-item-or-list.
func parseInnerList(s *scanner) (InnerList, error) {
if s.eof() || s.data[s.off] != '(' {
return InnerList{}, &UnmarshalError{s.off, ErrInvalidInnerListFormat}
}
s.off++
il := InnerList{nil, nil}
for !s.eof() {
s.scanWhileSp()
if s.eof() {
return InnerList{}, &UnmarshalError{s.off, ErrInvalidInnerListFormat}
}
if s.data[s.off] == ')' {
s.off++
p, err := parseParams(s)
if err != nil {
return InnerList{}, err
}
il.Params = p
return il, nil
}
i, err := parseItem(s)
if err != nil {
return InnerList{}, err
}
if s.eof() || (s.data[s.off] != ')' && s.data[s.off] != ' ') {
return InnerList{}, &UnmarshalError{s.off, ErrInvalidInnerListFormat}
}
il.Items = append(il.Items, i)
}
return InnerList{}, &UnmarshalError{s.off, ErrInvalidInnerListFormat}
}