bites

package module
v0.0.0-...-6b91579 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2017 License: MIT Imports: 6 Imported by: 2

README

bites

Coverage GoDoc Go Report

A Go library that deals with byte slices, mainly to assist with the marshaling and unmarshaling of binary data.

Documentation

Overview

Package bites implements a way to easily pack and unpack any primitive type into a byte slice.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorInvalidRune

type ErrorInvalidRune rune

func (ErrorInvalidRune) Error

func (err ErrorInvalidRune) Error() string

type Get

type Get []byte

Bites' purpose is to give byte slices some useful methods. The Get methods snip things off the front, and return the remainder of the slice. The Expect methods do a Get, and then compare it to the provided value. If there is not enough space for a Get, or if the Expect does not match, the method will return nil. The int methods are big-endian by default, but they have Little-Endian versions too. The float and complex methods put them in the form of IEE754 binary representation.

func (Get) Clone

func (b Get) Clone() Get

Make an exact copy of b and return it. This will allocate.

func (Get) Error

func (b Get) Error() bool

Return true if the slice is nil. This is poorly named, use More instead.

func (Get) ExpectBool

func (b Get) ExpectBool(bools ...bool) Get

Try to read a set of bools, and see if it matches. See GetBool.

func (Get) ExpectByte

func (b Get) ExpectByte(byt byte) Get

Read one byte, compare it to byt. If it does not match, panic with ErrorExpectByte.

func (Get) ExpectComplex128

func (b Get) ExpectComplex128(expect complex128) Get

func (Get) ExpectComplex64

func (b Get) ExpectComplex64(expect complex64) Get

func (Get) ExpectFloat32

func (b Get) ExpectFloat32(expect float32) Get

func (Get) ExpectFloat64

func (b Get) ExpectFloat64(expect float64) Get

func (Get) ExpectInt16

func (b Get) ExpectInt16(expect int16) Get

func (Get) ExpectInt16LE

func (b Get) ExpectInt16LE(expect int16) Get

func (Get) ExpectInt32

func (b Get) ExpectInt32(expect int32) Get

func (Get) ExpectInt32LE

func (b Get) ExpectInt32LE(expect int32) Get

func (Get) ExpectInt64

func (b Get) ExpectInt64(expect int64) Get

func (Get) ExpectInt64LE

func (b Get) ExpectInt64LE(expect int64) Get

func (Get) ExpectInt8

func (b Get) ExpectInt8(expect int8) Get

func (Get) ExpectRune

func (b Get) ExpectRune(r rune) Get

Read one rune, compare it to byt. If it does not match, panic with ErrorExpectRune.

func (Get) ExpectSlice

func (b Get) ExpectSlice(byt []byte) Get

Try to read a slice as long as byt, and see if it matches. If it does not, panic with ErrorExpectSlice. If there is not enough to read the whole slice, panic with ErrSliceEOF.

func (Get) ExpectString

func (b Get) ExpectString(s string) Get

Try to read a string as long as s, and see if it matches. If it does not, panic with ErrorExpectString. If it panics with this, it also allocates, but otherwise it does not. If there is not enough to read the whole slice, panic with ErrSliceEOF.

func (Get) ExpectUint16

func (b Get) ExpectUint16(expect uint16) Get

func (Get) ExpectUint16LE

func (b Get) ExpectUint16LE(expect uint16) Get

func (Get) ExpectUint32

func (b Get) ExpectUint32(expect uint32) Get

func (Get) ExpectUint32LE

func (b Get) ExpectUint32LE(expect uint32) Get

func (Get) ExpectUint64

func (b Get) ExpectUint64(expect uint64) Get

func (Get) ExpectUint64LE

func (b Get) ExpectUint64LE(expect uint64) Get

func (Get) ExpectUint8

func (b Get) ExpectUint8(expect uint8) Get

func (Get) ExpectVarInt

func (b Get) ExpectVarInt(expect int64, size *int) Get

func (Get) ExpectVarUint

func (b Get) ExpectVarUint(expect uint64, size *int) Get

func (Get) GetBool

func (b Get) GetBool(bools ...*bool) Get

Get a list of bools. The bools are interpreted as though they were written by PutBool. If a bool is nil, that bit is skipped.

func (Get) GetByte

func (b Get) GetByte(byt *byte) Get

Get one byte, and return the rest.

func (Get) GetComplex128

func (b Get) GetComplex128(f *complex128) Get

func (Get) GetComplex64

func (b Get) GetComplex64(f *complex64) Get

func (Get) GetFloat32

func (b Get) GetFloat32(f *float32) Get

func (Get) GetFloat64

func (b Get) GetFloat64(f *float64) Get

func (Get) GetInt16

func (b Get) GetInt16(ip *int16) Get

func (Get) GetInt16LE

func (b Get) GetInt16LE(ip *int16) Get

func (Get) GetInt32

func (b Get) GetInt32(ip *int32) Get

func (Get) GetInt32LE

func (b Get) GetInt32LE(ip *int32) Get

func (Get) GetInt64

func (b Get) GetInt64(ip *int64) Get

func (Get) GetInt64LE

func (b Get) GetInt64LE(ip *int64) Get

func (Get) GetInt8

func (b Get) GetInt8(i *int8) Get

func (Get) GetRune

func (b Get) GetRune(r *rune, s *int) Get

Get the first UTF8 rune in b. If the rune is not valid UTF8 or b is empty, it returns nil.

func (Get) GetSlice

func (b Get) GetSlice(slice *[]byte, size int) Get

Get a slice of the given size.

func (Get) GetSliceCopy

func (b Get) GetSliceCopy(slice []byte) Get

Copy b to slice, and return what's left of b. If there's not enough in b to fill the slice, return nil.

func (Get) GetString

func (b Get) GetString(str *string, size int) Get

Get a string of the given size. This allocates. If there's not enough in b to read the full string, return nil.

func (Get) GetUint16

func (b Get) GetUint16(ip *uint16) Get

func (Get) GetUint16LE

func (b Get) GetUint16LE(ip *uint16) Get

func (Get) GetUint32

func (b Get) GetUint32(ip *uint32) Get

func (Get) GetUint32LE

func (b Get) GetUint32LE(ip *uint32) Get

func (Get) GetUint64

func (b Get) GetUint64(ip *uint64) Get

func (Get) GetUint64LE

func (b Get) GetUint64LE(ip *uint64) Get

func (Get) GetUint8

func (b Get) GetUint8(i *uint8) Get

func (Get) GetVar

func (b Get) GetVar(i *int) Get

func (Get) GetVarInt

func (b Get) GetVarInt(i *int64, size *int) Get

func (Get) GetVarUint

func (b Get) GetVarUint(i *uint64, size *int) Get

func (Get) Last

func (b Get) Last(s int) Get

Return a slice containing the last s bytes.

func (Get) Len

func (b Get) Len() int

Return the length of the slice.

func (Get) More

func (b Get) More() bool

Return true if there is more data to get.

func (Get) NewReader

func (b Get) NewReader() *Reader

A Reader returned by this will allow you to Read everything in the given byte slice.

func (Get) Put

func (b Get) Put() Put

func (Get) Skip

func (b Get) Skip(s int) Get

Return a slice without the first s bytes.

func (Get) Snip

func (b Get) Snip(s int) Get

Return a slice with the last s bytes snipped off.

func (Get) Space

func (b Get) Space(expect int) bool

Returns true if the length of the slice is at least expect.

func (Get) Split

func (b Get) Split(s int) (Get, Get)

Split the slice into the first s bytes and the rest.

func (Get) String

func (b Get) String() string

Return the slice as a string (allocates).

type Put

type Put []byte

func New

func New() Put

Returns an empty slice as a Put.

func (Put) Capacity

func (b Put) Capacity(s int) Put

Returns b with at least s capacity left.

func (Put) Clone

func (b Put) Clone() Put

Make an exact copy of b and return it. This will allocate.

func (Put) Extend

func (b Put) Extend(s int) Put

Extend b by s, return the complete, extended, slice. The extension may contain garbage. An allocation (new backing array) will occur if s is larger than cap-len.

func (Put) Get

func (b Put) Get() Get

Return the slice as a Get.

func (Put) Last

func (b Put) Last(s int) Put

Return a slice containing the last s bytes.

func (Put) NewWriter

func (b Put) NewWriter() *Writer

The byte slice provided here is merely as an option to re-use an existing slice. When you're done writing to the Writer, call Bites to return an updated slice.

func (Put) PutBool

func (b Put) PutBool(bools ...bool) Put

Append a list of bools. The bools are set as bits consolidated into bytes, and are stored in the same order as they are given. This means that if there is a single true given, the byte appended will have the value 128.

func (Put) PutByte

func (b Put) PutByte(byt byte) Put

Append one byte.

func (Put) PutComplex128

func (b Put) PutComplex128(f complex128) Put

func (Put) PutComplex64

func (b Put) PutComplex64(f complex64) Put

func (Put) PutFloat32

func (b Put) PutFloat32(f float32) Put

func (Put) PutFloat64

func (b Put) PutFloat64(f float64) Put

func (Put) PutInt16

func (b Put) PutInt16(i int16) Put

func (Put) PutInt16LE

func (b Put) PutInt16LE(i int16) Put

func (Put) PutInt32

func (b Put) PutInt32(i int32) Put

func (Put) PutInt32LE

func (b Put) PutInt32LE(i int32) Put

func (Put) PutInt64

func (b Put) PutInt64(i int64) Put

func (Put) PutInt64LE

func (b Put) PutInt64LE(i int64) Put

func (Put) PutInt8

func (b Put) PutInt8(i int8) Put

func (Put) PutRune

func (b Put) PutRune(r rune, s *int) Put

Append the given rune as UTF8. If the rune is not valid, it panics with an error of type ErrorInvalidRune.

func (Put) PutSlice

func (b Put) PutSlice(slice []byte) Put

Append the given slice.

func (Put) PutString

func (b Put) PutString(str string) Put

Append the given string.

func (Put) PutUint16

func (b Put) PutUint16(i uint16) Put

func (Put) PutUint16LE

func (b Put) PutUint16LE(i uint16) Put

func (Put) PutUint32

func (b Put) PutUint32(i uint32) Put

func (Put) PutUint32LE

func (b Put) PutUint32LE(i uint32) Put

func (Put) PutUint64

func (b Put) PutUint64(i uint64) Put

func (Put) PutUint64LE

func (b Put) PutUint64LE(i uint64) Put

func (Put) PutUint8

func (b Put) PutUint8(i uint8) Put

func (Put) PutVar

func (b Put) PutVar(ii int) Put

func (Put) PutVarInt

func (b Put) PutVarInt(i int64, size *int) Put

func (Put) PutVarUint

func (b Put) PutVarUint(i uint64, size *int) Put

func (Put) Reuse

func (b Put) Reuse() Put

Set length to 0.

func (Put) Set

func (b Put) Set(s byte) Put

Set all bytes to s.

func (Put) Snip

func (b Put) Snip(s int) Put

Return a slice with the last s bytes snipped off.

func (Put) String

func (b Put) String() string

Return the slice as a string (allocates).

func (Put) Zero

func (b Put) Zero() Put

Set all bytes to 0. This is much faster than Set(0).

type Reader

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

A (Byte)Reader that reads everything contained in the given byte slice.

func (*Reader) Read

func (r *Reader) Read(buf []byte) (int, error)

Read from the slice. When there is nothing left to read, this returns 0, io.EOF.

func (*Reader) ReadByte

func (r *Reader) ReadByte() (byte, error)

Read a single byte. This is required to implement io.ByteReader. When everything has been read, this returns 0, io.EOF.

func (*Reader) Total

func (r *Reader) Total() int

Returns the total number of bytes read so far.

type Writer

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

A (Byte)Writer that appends everything written to the given byte slice.

Example
package main

import (
	"fmt"

	"github.com/PieterD/bites"
)

func main() {
	b := bites.New().PutString("Hello")
	w := b.NewWriter()
	fmt.Fprintf(w, ", world!")
	fmt.Println(w.Bites().String())
}
Output:

Hello, world!

func (*Writer) Bites

func (w *Writer) Bites() Put

Return the updated (possibly reallocated) byte slice, containing everything written to the Writer.

func (*Writer) Total

func (w *Writer) Total() int

Return the total amount of bytes written so far.

func (*Writer) Write

func (w *Writer) Write(buf []byte) (int, error)

This never returns an error.

func (*Writer) WriteByte

func (w *Writer) WriteByte(c byte) error

Write a single byte. This turns Writer into a ByteWriter.

Jump to

Keyboard shortcuts

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