container

package
v0.9.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2021 License: GPL-3.0 Imports: 4 Imported by: 23

Documentation

Overview

Package container gives you a []byte slice on steroids, allowing for quick data appending, prepending and fetching as well as transparent error transportation.

A Container is basically a [][]byte slice that just appends new []byte slices and only copies things around when necessary.

Byte slices added to the Container are not changed or appended, to not corrupt any other data that may be before and after the given slice. If interested, consider the following example to understand why this is important:

package main

import (
	"fmt"
)

func main() {
	a := []byte{0, 1,2,3,4,5,6,7,8,9}
	fmt.Printf("a: %+v\n", a)
	fmt.Printf("\nmaking changes...\n(we are not changing a directly)\n\n")
	b := a[2:6]
	c := append(b, 10, 11)
	fmt.Printf("b: %+v\n", b)
	fmt.Printf("c: %+v\n", c)
	fmt.Printf("a: %+v\n", a)
}

run it here: https://play.golang.org/p/xu1BXT3QYeE

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

type Container struct {
	// contains filtered or unexported fields
}

Container is []byte sclie on steroids, allowing for quick data appending, prepending and fetching as well as transparent error transportation. (Error transportation requires use of varints for data)

func New

func New(data ...[]byte) *Container

New creates a new container with an optional initial []byte slice. Data will NOT be copied.

func NewContainer

func NewContainer(data ...[]byte) *Container

NewContainer is DEPRECATED, please use New(), it's the same thing.

func (*Container) Append

func (c *Container) Append(data []byte)

Append appends the given data. Data will NOT be copied.

func (*Container) AppendAsBlock

func (c *Container) AppendAsBlock(data []byte)

AppendAsBlock appends the length of the data and the data itself. Data will NOT be copied.

func (*Container) AppendContainer added in v0.4.1

func (c *Container) AppendContainer(data *Container)

AppendContainer appends another Container. Data will NOT be copied.

func (*Container) AppendContainerAsBlock added in v0.4.1

func (c *Container) AppendContainerAsBlock(data *Container)

AppendContainerAsBlock appends another Container (length and data). Data will NOT be copied.

func (*Container) AppendInt added in v0.4.1

func (c *Container) AppendInt(n int)

AppendInt appends an int (varint encoded).

func (*Container) AppendNumber

func (c *Container) AppendNumber(n uint64)

AppendNumber appends a number (varint encoded).

func (*Container) CheckError

func (c *Container) CheckError()

CheckError checks if there is an error in the data. If so, it will parse the error and delete the data.

func (*Container) CompileData

func (c *Container) CompileData() []byte

CompileData concatenates all bytes held by the container and returns it as one single []byte slice. Data will NOT be copied and is NOT consumed.

func (*Container) ErrString

func (c *Container) ErrString() string

ErrString returns the error as a string.

func (*Container) Error

func (c *Container) Error() error

Error returns the error.

func (*Container) Get

func (c *Container) Get(n int) ([]byte, error)

Get returns the given amount of bytes. Data MAY be copied and IS consumed.

func (*Container) GetAsContainer added in v0.4.1

func (c *Container) GetAsContainer(n int) (*Container, error)

GetAsContainer returns the given amount of bytes in a new container. Data will NOT be copied and IS consumed.

func (*Container) GetMax

func (c *Container) GetMax(n int) []byte

GetMax returns as much as possible, but the given amount of bytes at maximum. Data MAY be copied and IS consumed.

func (*Container) GetNextBlock

func (c *Container) GetNextBlock() ([]byte, error)

GetNextBlock returns the next block of data defined by a varint. Data MAY be copied and IS consumed.

func (*Container) GetNextBlockAsContainer added in v0.4.1

func (c *Container) GetNextBlockAsContainer() (*Container, error)

GetNextBlockAsContainer returns the next block of data as a Container defined by a varint. Data will NOT be copied and IS consumed.

func (*Container) GetNextN16

func (c *Container) GetNextN16() (uint16, error)

GetNextN16 parses and returns a varint of type uint16.

func (*Container) GetNextN32

func (c *Container) GetNextN32() (uint32, error)

GetNextN32 parses and returns a varint of type uint32.

func (*Container) GetNextN64

func (c *Container) GetNextN64() (uint64, error)

GetNextN64 parses and returns a varint of type uint64.

func (*Container) GetNextN8

func (c *Container) GetNextN8() (uint8, error)

GetNextN8 parses and returns a varint of type uint8.

func (*Container) HasError

func (c *Container) HasError() bool

HasError returns wether or not the container is holding an error.

func (*Container) Length

func (c *Container) Length() (length int)

Length returns the full length of all bytes held by the container.

func (*Container) MarshalJSON added in v0.4.1

func (c *Container) MarshalJSON() ([]byte, error)

MarshalJSON serializes the container as a JSON byte array.

func (*Container) Prepend

func (c *Container) Prepend(data []byte)

Prepend prepends data. Data will NOT be copied.

func (*Container) PrependLength

func (c *Container) PrependLength()

PrependLength prepends the current full length of all bytes in the container.

func (*Container) Replace

func (c *Container) Replace(data []byte)

Replace replaces all held data with a new data slice. Data will NOT be copied.

func (*Container) SetError

func (c *Container) SetError(err error)

SetError sets an error.

func (*Container) UnmarshalJSON added in v0.4.1

func (c *Container) UnmarshalJSON(data []byte) error

UnmarshalJSON unserializes a container from a JSON byte array.

func (*Container) WriteAllTo added in v0.4.1

func (c *Container) WriteAllTo(writer io.Writer) error

WriteAllTo writes all the data to the given io.Writer. Data IS NOT copied (but may be by writer) and IS NOT consumed.

func (*Container) WriteToSlice

func (c *Container) WriteToSlice(slice []byte) (n int, containerEmptied bool)

WriteToSlice copies data to the give slice until it is full, or the container is empty. It returns the bytes written and if the container is now empty. Data IS copied and IS consumed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL