Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pre-allocate decoding errors #690

Merged
merged 2 commits into from
Jul 5, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions real_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"math"
)

var errInvalidArrayLength = PacketDecodingError{"invalid array length"}
var errInvalidByteSliceLength = PacketDecodingError{"invalid byteslice length"}
var errInvalidStringLength = PacketDecodingError{"invalid string length"}
var errInvalidSubsetSize = PacketDecodingError{"invalid subset size"}

type realDecoder struct {
raw []byte
off int
Expand Down Expand Up @@ -64,7 +69,7 @@ func (rd *realDecoder) getArrayLength() (int, error) {
rd.off = len(rd.raw)
return -1, ErrInsufficientData
} else if tmp > 2*math.MaxUint16 {
return -1, PacketDecodingError{"invalid array length"}
return -1, errInvalidArrayLength
}
return tmp, nil
}
Expand All @@ -82,7 +87,7 @@ func (rd *realDecoder) getBytes() ([]byte, error) {

switch {
case n < -1:
return nil, PacketDecodingError{"invalid byteslice length"}
return nil, errInvalidByteSliceLength
case n == -1:
return nil, nil
case n == 0:
Expand All @@ -108,7 +113,7 @@ func (rd *realDecoder) getString() (string, error) {

switch {
case n < -1:
return "", PacketDecodingError{"invalid string length"}
return "", errInvalidStringLength
case n == -1:
return "", nil
case n == 0:
Expand Down Expand Up @@ -141,7 +146,7 @@ func (rd *realDecoder) getInt32Array() ([]int32, error) {
}

if n < 0 {
return nil, PacketDecodingError{"invalid array length"}
return nil, errInvalidArrayLength
}

ret := make([]int32, n)
Expand Down Expand Up @@ -170,7 +175,7 @@ func (rd *realDecoder) getInt64Array() ([]int64, error) {
}

if n < 0 {
return nil, PacketDecodingError{"invalid array length"}
return nil, errInvalidArrayLength
}

ret := make([]int64, n)
Expand All @@ -194,7 +199,7 @@ func (rd *realDecoder) getStringArray() ([]string, error) {
}

if n < 0 {
return nil, PacketDecodingError{"invalid array length"}
return nil, errInvalidArrayLength
}

ret := make([]string, n)
Expand All @@ -216,7 +221,7 @@ func (rd *realDecoder) remaining() int {

func (rd *realDecoder) getSubset(length int) (packetDecoder, error) {
if length < 0 {
return nil, PacketDecodingError{"invalid subset size"}
return nil, errInvalidSubsetSize
} else if length > rd.remaining() {
rd.off = len(rd.raw)
return nil, ErrInsufficientData
Expand Down