Expose container.Gather

This commit is contained in:
Daniel 2022-07-22 14:42:06 +02:00
parent 456a235b8b
commit 5db95ac362
2 changed files with 14 additions and 12 deletions

View file

@ -127,7 +127,7 @@ func (c *Container) CompileData() []byte {
// Get returns the given amount of bytes. Data MAY be copied and IS consumed.
func (c *Container) Get(n int) ([]byte, error) {
buf := c.gather(n)
buf := c.Gather(n)
if len(buf) < n {
return nil, errors.New("container: not enough data to return")
}
@ -138,14 +138,14 @@ func (c *Container) Get(n int) ([]byte, error) {
// GetAll returns all data. Data MAY be copied and IS consumed.
func (c *Container) GetAll() []byte {
// TODO: Improve.
buf := c.gather(c.Length())
buf := c.Gather(c.Length())
c.skip(len(buf))
return buf
}
// GetAsContainer returns the given amount of bytes in a new container. Data will NOT be copied and IS consumed.
func (c *Container) GetAsContainer(n int) (*Container, error) {
newC := c.gatherAsContainer(n)
newC := c.GatherAsContainer(n)
if newC == nil {
return nil, errors.New("container: not enough data to return")
}
@ -155,7 +155,7 @@ func (c *Container) GetAsContainer(n int) (*Container, error) {
// GetMax returns as much as possible, but the given amount of bytes at maximum. Data MAY be copied and IS consumed.
func (c *Container) GetMax(n int) []byte {
buf := c.gather(n)
buf := c.Gather(n)
c.skip(len(buf))
return buf
}
@ -233,7 +233,8 @@ func (c *Container) PrependLength() {
c.Prepend(varint.Pack64(uint64(c.Length())))
}
func (c *Container) gather(n int) []byte {
// Gather returns the given amount of bytes. Data MAY be copied and IS NOT consumed.
func (c *Container) Gather(n int) []byte {
// Check requested length.
if n <= 0 {
return nil
@ -260,7 +261,8 @@ func (c *Container) gather(n int) []byte {
return slice[:n]
}
func (c *Container) gatherAsContainer(n int) (newC *Container) {
// GatherAsContainer returns the given amount of bytes in a new container. Data will NOT be copied and IS NOT consumed.
func (c *Container) GatherAsContainer(n int) (newC *Container) {
// Check requested length.
if n < 0 {
return nil
@ -323,7 +325,7 @@ func (c *Container) GetNextBlockAsContainer() (*Container, error) {
// GetNextN8 parses and returns a varint of type uint8.
func (c *Container) GetNextN8() (uint8, error) {
buf := c.gather(2)
buf := c.Gather(2)
num, n, err := varint.Unpack8(buf)
if err != nil {
return 0, err
@ -334,7 +336,7 @@ func (c *Container) GetNextN8() (uint8, error) {
// GetNextN16 parses and returns a varint of type uint16.
func (c *Container) GetNextN16() (uint16, error) {
buf := c.gather(3)
buf := c.Gather(3)
num, n, err := varint.Unpack16(buf)
if err != nil {
return 0, err
@ -345,7 +347,7 @@ func (c *Container) GetNextN16() (uint16, error) {
// GetNextN32 parses and returns a varint of type uint32.
func (c *Container) GetNextN32() (uint32, error) {
buf := c.gather(5)
buf := c.Gather(5)
num, n, err := varint.Unpack32(buf)
if err != nil {
return 0, err
@ -356,7 +358,7 @@ func (c *Container) GetNextN32() (uint32, error) {
// GetNextN64 parses and returns a varint of type uint64.
func (c *Container) GetNextN64() (uint64, error) {
buf := c.gather(10)
buf := c.Gather(10)
num, n, err := varint.Unpack64(buf)
if err != nil {
return 0, err

View file

@ -66,9 +66,9 @@ func TestContainerDataHandling(t *testing.T) {
}
c8.clean()
c9 := c8.gatherAsContainer(len(testData))
c9 := c8.GatherAsContainer(len(testData))
c10 := c9.gatherAsContainer(len(testData) - 1)
c10 := c9.GatherAsContainer(len(testData) - 1)
c10.Append(testData[len(testData)-1:])
compareMany(t, testData, c1.CompileData(), c2.CompileData(), c3.CompileData(), d4, d5, c6.CompileData(), c7.CompileData(), c8.CompileData(), c9.CompileData(), c10.CompileData())