Add Prepend* and HoldsData functions, remove error carrying

This commit is contained in:
Daniel 2021-06-24 10:59:55 +02:00
parent 52b0683882
commit adedde1310
2 changed files with 38 additions and 33 deletions

View file

@ -44,11 +44,21 @@ func (c *Container) Append(data []byte) {
c.compartments = append(c.compartments, data)
}
// PrependNumber prepends a number (varint encoded).
func (c *Container) PrependNumber(n uint64) {
c.Prepend(varint.Pack64(n))
}
// AppendNumber appends a number (varint encoded).
func (c *Container) AppendNumber(n uint64) {
c.compartments = append(c.compartments, varint.Pack64(n))
}
// PrependInt prepends an int (varint encoded).
func (c *Container) PrependInt(n int) {
c.Prepend(varint.Pack64(uint64(n)))
}
// AppendInt appends an int (varint encoded).
func (c *Container) AppendInt(n int) {
c.compartments = append(c.compartments, varint.Pack64(uint64(n)))
@ -60,6 +70,12 @@ func (c *Container) AppendAsBlock(data []byte) {
c.Append(data)
}
// PrependAsBlock prepends the length of the data and the data itself. Data will NOT be copied.
func (c *Container) PrependAsBlock(data []byte) {
c.Prepend(data)
c.PrependNumber(uint64(len(data)))
}
// AppendContainer appends another Container. Data will NOT be copied.
func (c *Container) AppendContainer(data *Container) {
c.compartments = append(c.compartments, data.compartments...)
@ -71,6 +87,16 @@ func (c *Container) AppendContainerAsBlock(data *Container) {
c.compartments = append(c.compartments, data.compartments...)
}
// HoldsData returns true if the Container holds any data.
func (c *Container) HoldsData() bool {
for i := c.offset; i < len(c.compartments); i++ {
if len(c.compartments[i]) > 0 {
return true
}
}
return false
}
// Length returns the full length of all bytes held by the container.
func (c *Container) Length() (length int) {
for i := c.offset; i < len(c.compartments); i++ {
@ -109,6 +135,14 @@ func (c *Container) Get(n int) ([]byte, error) {
return buf, nil
}
// GetAll returns all data. Data MAY be copied and IS consumed.
func (c *Container) GetAll() []byte {
// TODO: Improve.
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) {
new := c.gatherAsContainer(n)
@ -198,6 +232,9 @@ func (c *Container) checkOffset() {
// Error Handling
/*
DEPRECATING... like.... NOW.
// SetError sets an error.
func (c *Container) SetError(err error) {
c.err = err
@ -227,6 +264,7 @@ func (c *Container) Error() error {
func (c *Container) ErrString() string {
return c.err.Error()
}
*/
// Block Handling

View file

@ -2,7 +2,6 @@ package container
import (
"bytes"
"errors"
"testing"
"github.com/safing/portbase/utils"
@ -82,38 +81,6 @@ func compareMany(t *testing.T, reference []byte, other ...[]byte) {
}
}
func TestContainerErrorHandling(t *testing.T) {
c1 := New(nil)
if c1.HasError() {
t.Error("should not have error")
}
c1.SetError(errors.New("test error"))
if !c1.HasError() {
t.Error("should have error")
}
c2 := New(append([]byte{0}, []byte("test error")...))
if c2.HasError() {
t.Error("should not have error")
}
c2.CheckError()
if !c2.HasError() {
t.Error("should have error")
}
if c2.Error().Error() != "test error" {
t.Errorf("error message mismatch, was %s", c2.Error())
}
}
func TestDataFetching(t *testing.T) {
c1 := New(utils.DuplicateBytes(testData))
data := c1.GetMax(1)