Skip to content
This repository has been archived by the owner on May 8, 2019. It is now read-only.

Commit

Permalink
improved AppendExtension and ReadUint64Bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
dchenk committed May 20, 2018
1 parent 54f6081 commit 083f73b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 63 deletions.
69 changes: 34 additions & 35 deletions msgp/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,65 +377,64 @@ func (m *Reader) ReadExtension(e Extension) error {

}

// AppendExtension appends a MessagePack extension to the provided slice
// AppendExtension appends a MessagePack extension to slice b.
func AppendExtension(b []byte, e Extension) ([]byte, error) {
l := e.Len()
var o []byte
var n int
switch l {
case 0:
o, n = ensure(b, 3)
o[n] = mext8
o[n+1] = 0
o[n+2] = byte(e.ExtensionType())
return o[:n+3], nil
b, n = ensure(b, 3)
b[n] = mext8
b[n+1] = 0
b[n+2] = byte(e.ExtensionType())
return b[:n+3], nil
case 1:
o, n = ensure(b, 3)
o[n] = mfixext1
o[n+1] = byte(e.ExtensionType())
b, n = ensure(b, 3)
b[n] = mfixext1
b[n+1] = byte(e.ExtensionType())
n += 2
case 2:
o, n = ensure(b, 4)
o[n] = mfixext2
o[n+1] = byte(e.ExtensionType())
b, n = ensure(b, 4)
b[n] = mfixext2
b[n+1] = byte(e.ExtensionType())
n += 2
case 4:
o, n = ensure(b, 6)
o[n] = mfixext4
o[n+1] = byte(e.ExtensionType())
b, n = ensure(b, 6)
b[n] = mfixext4
b[n+1] = byte(e.ExtensionType())
n += 2
case 8:
o, n = ensure(b, 10)
o[n] = mfixext8
o[n+1] = byte(e.ExtensionType())
b, n = ensure(b, 10)
b[n] = mfixext8
b[n+1] = byte(e.ExtensionType())
n += 2
case 16:
o, n = ensure(b, 18)
o[n] = mfixext16
o[n+1] = byte(e.ExtensionType())
b, n = ensure(b, 18)
b[n] = mfixext16
b[n+1] = byte(e.ExtensionType())
n += 2
}
switch {
case l < math.MaxUint8:
o, n = ensure(b, l+3)
o[n] = mext8
o[n+1] = byte(uint8(l))
o[n+2] = byte(e.ExtensionType())
b, n = ensure(b, l+3)
b[n] = mext8
b[n+1] = byte(uint8(l))
b[n+2] = byte(e.ExtensionType())
n += 3
case l < math.MaxUint16:
o, n = ensure(b, l+4)
o[n] = mext16
big.PutUint16(o[n+1:], uint16(l))
o[n+3] = byte(e.ExtensionType())
b, n = ensure(b, l+4)
b[n] = mext16
big.PutUint16(b[n+1:], uint16(l))
b[n+3] = byte(e.ExtensionType())
n += 4
default:
o, n = ensure(b, l+6)
o[n] = mext32
big.PutUint32(o[n+1:], uint32(l))
o[n+5] = byte(e.ExtensionType())
b, n = ensure(b, l+6)
b[n] = mext32
big.PutUint32(b[n+1:], uint32(l))
b[n+5] = byte(e.ExtensionType())
n += 6
}
return o, e.MarshalBinaryTo(o[n:])
return b, e.MarshalBinaryTo(b[n:])
}

// ReadExtensionBytes reads an extension from b into e and returns any remaining bytes.
Expand Down
43 changes: 15 additions & 28 deletions msgp/read_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,62 +384,49 @@ func ReadIntBytes(b []byte) (int, []byte, error) {
return int(i), b, err
}

// ReadUint64Bytes tries to read a uint64 from b and return the value and the remaining bytes.
// Possible errors include ErrShortBytes (too few bytes) and TypeError{} (not a uint).
func ReadUint64Bytes(b []byte) (u uint64, o []byte, err error) {
// ReadUint64Bytes reads a uint64 from b and returns the value and any remaining bytes.
// Possible errors include ErrShortBytes and TypeError.
func ReadUint64Bytes(b []byte) (uint64, []byte, error) {

l := len(b)
if l < 1 {
return 0, nil, ErrShortBytes
}

lead := b[0]
if isfixint(lead) {
u = uint64(rfixint(lead))
o = b[1:]
return
return uint64(rfixint(lead)), b[1:], nil
}

switch lead {
case muint8:
if l < 2 {
err = ErrShortBytes
return
return 0, b, ErrShortBytes
}
u = uint64(getMuint8(b))
o = b[2:]
return
return uint64(getMuint8(b)), b[2:], nil

case muint16:
if l < 3 {
err = ErrShortBytes
return
return 0, b, ErrShortBytes
}
u = uint64(getMuint16(b))
o = b[3:]
return
return uint64(getMuint16(b)), b[3:], nil

case muint32:
if l < 5 {
err = ErrShortBytes
return
return 0, b, ErrShortBytes
}
u = uint64(getMuint32(b))
o = b[5:]
return
return uint64(getMuint32(b)), b[5:], nil

case muint64:
if l < 9 {
err = ErrShortBytes
return
return 0, b, ErrShortBytes
}
u = getMuint64(b)
o = b[9:]
return
return getMuint64(b), b[9:], nil

default:
err = badPrefix(UintType, lead)
return
return 0, b, badPrefix(UintType, lead)
}

}

// ReadUint32Bytes tries to read a uint32 from b and return the value and the remaining bytes.
Expand Down

0 comments on commit 083f73b

Please sign in to comment.