Simplify bencode.Decoder.parseStringInterface

This commit is contained in:
Matt Joiner 2021-09-29 09:50:01 +10:00
parent 37455d3c6b
commit a9aec0bb79
1 changed files with 8 additions and 14 deletions

View File

@ -611,27 +611,21 @@ func (d *Decoder) parseIntInterface() (ret interface{}) {
return return
} }
func (d *Decoder) parseStringInterface() (s string) { func (d *Decoder) parseStringInterface() string {
// read the string length first // read the string length first
d.readUntil(':') d.readUntil(':')
length, err := strconv.ParseInt(bytesAsString(d.buf.Bytes()), 10, 64) length, err := strconv.ParseInt(bytesAsString(d.buf.Bytes()), 10, 64)
if err != nil { if err != nil {
panic(&SyntaxError{Offset: d.Offset-1, What: err}) panic(&SyntaxError{Offset: d.Offset - 1, What: err})
} }
d.buf.Reset() d.buf.Reset()
n, err := d.buf.ReadFrom(io.LimitReader(d.r, length)) b := make([]byte, length)
d.Offset += n n, err := io.ReadFull(d.r, b)
switch err { d.Offset += int64(n)
case nil: if err != nil {
s = d.buf.String() panic(&SyntaxError{Offset: d.Offset, What: err})
d.buf.Reset()
return
case io.EOF:
err = io.ErrUnexpectedEOF
default:
err = errors.New("unexpected I/O error: " + err.Error())
} }
panic(&SyntaxError{Offset: d.Offset, What: err}) return bytesAsString(b)
} }
func (d *Decoder) parseDictInterface() interface{} { func (d *Decoder) parseDictInterface() interface{} {