bencode: Remove string allocation when parsing strings

This commit is contained in:
Matt Joiner 2018-06-17 16:21:04 +10:00
parent 2b1068885c
commit c5cc570e42
1 changed files with 6 additions and 1 deletions

View File

@ -10,6 +10,7 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"unsafe"
) )
type Decoder struct { type Decoder struct {
@ -404,7 +405,11 @@ func (d *Decoder) readOneValue() bool {
if b >= '0' && b <= '9' { if b >= '0' && b <= '9' {
start := d.buf.Len() - 1 start := d.buf.Len() - 1
d.readUntil(':') d.readUntil(':')
length, err := strconv.ParseInt(d.buf.String()[start:], 10, 64) s := reflect.StringHeader{
uintptr(unsafe.Pointer(&d.buf.Bytes()[start])),
d.buf.Len() - start,
}
length, err := strconv.ParseInt(*(*string)(unsafe.Pointer(&s)), 10, 64)
checkForIntParseError(err, d.Offset-1) checkForIntParseError(err, d.Offset-1)
d.buf.WriteString(":") d.buf.WriteString(":")