-
Notifications
You must be signed in to change notification settings - Fork 0
/
bencodeitem.go
51 lines (43 loc) · 948 Bytes
/
bencodeitem.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
package gobencode
import (
"fmt"
"strings"
)
type ItemType byte
const (
EOL ItemType = 0
Integer ItemType = 1
Bytes ItemType = 2
List ItemType = 3
Dict ItemType = 4
)
type BencodeItem struct {
Type ItemType
Value any
}
func (v BencodeItem) String() string {
switch v.Type {
case Integer:
return fmt.Sprintf("%d", v.Value.(int))
case Bytes:
return fmt.Sprintf(`"%s"`, string(v.Value.([]byte)))
case List:
list := v.Value.([]BencodeItem)
items := make([]string, len(list))
for i, v := range list {
items[i] = v.String()
}
return fmt.Sprintf("[%s]", strings.Join(items, ", "))
case Dict:
mp := v.Value.(map[string]BencodeItem)
items := make([]string, len(mp))
i := 0
for key, value := range mp {
items[i] = fmt.Sprintf(`"%s": %s`, key, value.String())
i += 1
}
return fmt.Sprintf("{%s}", strings.Join(items, ", "))
default:
panic(fmt.Sprintf("Wrong item type %d", v.Type))
}
}