-
Notifications
You must be signed in to change notification settings - Fork 56
/
accepts.go
47 lines (39 loc) · 933 Bytes
/
accepts.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
package acceptHeaders
import (
"sort"
"strings"
)
type Accepts []acceptItem
func (accepts Accepts) Len() int {
return len(accepts)
}
func (accepts Accepts) Swap(i, j int) {
accepts[i], accepts[j] = accepts[j], accepts[i]
}
func (accepts Accepts) Less(i, j int) bool {
return accepts[i].quality > accepts[j].quality
}
// make sure `accepts` is sorted
func (accepts Accepts) GetPreferredValue(availables []string) (index int, value string, ok bool) {
for _, accept := range accepts {
for i, avail := range availables {
if accept.value == avail {
return i, avail, true
}
}
}
return
}
func ParseAccepts(input string) Accepts {
entries := strings.Split(input, ",")
entryCount := len(entries)
if entryCount == 0 {
return nil
}
accepts := make(Accepts, entryCount)
for i := 0; i < entryCount; i++ {
accepts[i] = parseAcceptItem(strings.TrimSpace(entries[i]))
}
sort.Sort(accepts)
return accepts
}