-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
98 lines (85 loc) · 2.51 KB
/
encode.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
92
93
94
95
96
97
98
// This Package implements a simple bencode encode/decode
// bencode(https://en.wikipedia.org/wiki/Bencode)
// Lexicographic order(https://en.wikipedia.org/wiki/Lexicographic_order)
package bencode
import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"
)
type encode struct {
output []byte
}
func Encode(input interface{}) (s []byte, err error) {
encode := &encode{}
defer func() {
if r := recover(); r != nil {
if e, ok := r.(bencodeErorr); ok {
err = e.error
}
}
}()
encode.next(input)
return encode.output, nil
}
func (encode *encode) next(input interface{}) {
value := reflect.ValueOf(input)
kind := value.Kind()
switch kind {
case reflect.Slice, reflect.Array:
// string alias
if value.Type().String() == "[]uint8" {
encode.encodeString(string(value.Bytes()))
} else {
encode.encodeSlice(value)
}
case reflect.Map:
encode.encodeDirectory(value)
case reflect.String:
encode.encodeString(value.String())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16,
reflect.Uint32, reflect.Uint64:
encode.encodeNumeric(strconv.FormatInt(value.Int(), 10))
default:
panic(bencodeErorr{error: fmt.Errorf("Invalid type: can't support %s", kind)})
}
}
// Someone need convert WTF string(non-standard ASCII) if necessary.
func (encode *encode) encodeString(s string) {
var sb strings.Builder
l := strconv.Itoa(len(s))
sb.WriteString(l)
sb.WriteByte(StringDelim)
sb.WriteString(s)
encode.output = append(encode.output, sb.String()...)
}
// Should sort directory
func (encode *encode) encodeDirectory(value reflect.Value) {
encode.output = append(encode.output, DirectroyStart)
keys := value.MapKeys()
sort.Slice(keys, func(a, b int) bool {
return keys[a].String() < keys[b].String()
})
for _, k := range keys {
encode.encodeString(k.String())
encode.next(value.MapIndex(k).Interface())
}
encode.output = append(encode.output, EndOfType)
}
func (encode *encode) encodeSlice(value reflect.Value) {
encode.output = append(encode.output, SliceStart)
for i := 0; i < value.Len(); i++ {
encode.next(value.Index(i).Interface())
}
encode.output = append(encode.output, EndOfType)
}
func (encode *encode) encodeNumeric(s string) {
content := make([]byte, 0, 2+len(s))
content = append(content, NumericStart)
content = append(content, s...)
content = append(content, EndOfType)
encode.output = append(encode.output, content...)
}