aboutsummaryrefslogtreecommitdiff
path: root/bencode
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-06-12 05:45:36 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-06-12 05:45:36 +0200
commitfaf43e21a4561b17a5433a3b8990d314ab515711 (patch)
tree35fbe12751efae9783305f531ef2dc35417d7f2a /bencode
parentcf9e66821412aaf7461410928659c1b0296bc5c2 (diff)
Cleanup
Diffstat (limited to 'bencode')
-rw-r--r--bencode/bencode.go35
1 files changed, 15 insertions, 20 deletions
diff --git a/bencode/bencode.go b/bencode/bencode.go
index f1c0245..6c362fa 100644
--- a/bencode/bencode.go
+++ b/bencode/bencode.go
@@ -140,7 +140,7 @@ func (d *decodeState) unmarshalField(v reflect.Value) error {
case 'l':
d.unmarshalList(v)
case 'e':
- panic("end")
+ panic("unexpected end")
default:
d.unmarshalString(v)
}
@@ -148,11 +148,10 @@ func (d *decodeState) unmarshalField(v reflect.Value) error {
}
func (d *decodeState) unmarshalDict(v reflect.Value) {
- if d.data[d.off] == 'd' {
- d.off++
- } else {
- panic("not dict")
+ if d.data[d.off] != 'd' {
+ panic("not a dict")
}
+ d.off++
for d.data[d.off] != 'e' {
key, n := parseString(d.data[d.off:])
@@ -172,11 +171,10 @@ func (d *decodeState) unmarshalDict(v reflect.Value) {
}
func (d *decodeState) unmarshalList(v reflect.Value) {
- if d.data[d.off] == 'l' {
- d.off++
- } else {
- panic("not list")
+ if d.data[d.off] != 'l' {
+ panic("not a list")
}
+ d.off++
for i := 0; d.data[d.off] != 'e'; i++ {
if i >= v.Cap() {
@@ -210,13 +208,13 @@ func (d *decodeState) unmarshalString(v reflect.Value) {
func parseString(data []byte) (string, int) {
i := bytes.IndexByte(data, ':')
if i < 0 {
- panic("not string")
+ panic("not a string")
}
- n, err := strconv.Atoi(string(data[:i]))
+ size, err := strconv.Atoi(string(data[:i]))
if err != nil {
- return "", 0
+ panic(err)
}
- end := i + 1 + n
+ end := size + i + 1
return string(data[i+1 : end]), end
}
@@ -233,16 +231,13 @@ func (d *decodeState) unmarshalInt(v reflect.Value) {
}
func parseInt(data []byte) (int64, int) {
- off := 0
- if data[off] == 'i' {
- off++
- } else {
- panic("not int")
+ if data[0] != 'i' {
+ panic("not an int")
}
end := bytes.IndexByte(data, 'e')
- i, err := strconv.Atoi(string(data[off:end]))
+ i, err := strconv.Atoi(string(data[1:end]))
if err != nil {
- return 0, end + 1
+ panic(err)
}
return int64(i), end + 1
}