-
Notifications
You must be signed in to change notification settings - Fork 1
/
yencline.go
54 lines (41 loc) · 1.07 KB
/
yencline.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
package yenc
import (
"bytes"
"fmt"
"strings"
)
//Header is a map type for reading a serialized yenc header.
type Header map[string]string
//String implements the stringer interface, printing the proper yenc header
func (y *Header) String() string {
s := make([]string, 0, 5)
for k, v := range *y {
s = append(s, fmt.Sprintf("%s=%s", k, v))
}
return strings.Join(s, " ")
}
//Put creates a string entry for a k, v pair
func (y *Header) Put(k, v string) {
(*y)[k] = v
}
//Get returns the string for a key
func (y *Header) Get(k string) string {
return (*y)[k]
}
//ReadYENCHeader accepts a byte slice and returns a YENCHeader or any error
//encountered while decoding, and the header length so that the consumer can
//ignore the appropriate bytes.
func ReadYENCHeader(bs []byte) (*Header, int) {
i := bytes.IndexByte(bs, '\n') + 1
s := string((bs)[:i])
s = strings.TrimSpace(s)
y := &Header{}
ss := strings.Split(s, " ")
for _, kvString := range ss {
kvPair := strings.Split(kvString, "=")
if len(kvPair) == 2 {
y.Put(kvPair[0], kvPair[1])
}
}
return y, i
}