Documentation ¶
Overview ¶
Package binary approaches binary encoding with a different approach from encoding/binary. The main aim of this package is to be *fast*, and thus not to use reflection, and with more type-safety.
By its nature, this package is statically typed, and as such will not support arrays, structs and slices of compound/non-builtin types (or slices of slices). Apart from that, behaviour is pretty much like that of encoding/binary.
Index ¶
- Variables
- type ByteOrder
- type Reader
- func (r *Reader) Bool() bool
- func (r *Reader) Byte() byte
- func (r *Reader) Complex128() complex128
- func (r *Reader) Complex64() complex64
- func (r *Reader) End() (int, error)
- func (r *Reader) Float32() float32
- func (r *Reader) Float64() float64
- func (r *Reader) Int16() int16
- func (r *Reader) Int32() int32
- func (r *Reader) Int64() int64
- func (r *Reader) Int8() int8
- func (r *Reader) Read(p []byte) (int, error)
- func (r *Reader) Rune() rune
- func (r *Reader) Uint16() uint16
- func (r *Reader) Uint32() uint32
- func (r *Reader) Uint64() uint64
- func (r *Reader) Uint8() uint8
- type WriteChain
- func (c *WriteChain) Bool(b bool) *WriteChain
- func (c *WriteChain) Byte(b byte) *WriteChain
- func (c *WriteChain) ByteSlice(b []byte) *WriteChain
- func (c *WriteChain) Complex128(com complex128) *WriteChain
- func (c *WriteChain) Complex64(com complex64) *WriteChain
- func (c *WriteChain) End() (int, error)
- func (c *WriteChain) Float32(f float32) *WriteChain
- func (c *WriteChain) Float64(f float64) *WriteChain
- func (c *WriteChain) Int16(i int16) *WriteChain
- func (c *WriteChain) Int32(i int32) *WriteChain
- func (c *WriteChain) Int64(i int64) *WriteChain
- func (c *WriteChain) Int8(i int8) *WriteChain
- func (c *WriteChain) Rune(r rune) *WriteChain
- func (c *WriteChain) String(s string) *WriteChain
- func (c *WriteChain) Uint16(u uint16) *WriteChain
- func (c *WriteChain) Uint32(u uint32) *WriteChain
- func (c *WriteChain) Uint64(u uint64) *WriteChain
- func (c *WriteChain) Uint8(u uint8) *WriteChain
- func (c *WriteChain) Write(p []byte) (int, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var BigEndian bigEndian
BigEndian is the big-endian implementation of ByteOrder.
var LittleEndian littleEndian
LittleEndian is the little-endian implementation of ByteOrder.
Functions ¶
This section is empty.
Types ¶
type ByteOrder ¶
type ByteOrder interface { Uint16([]byte) uint16 Uint32([]byte) uint32 Uint64([]byte) uint64 PutUint16([]byte, uint16) PutUint32([]byte, uint32) PutUint64([]byte, uint64) String() string }
A ByteOrder specifies how to convert byte sequences into 16-, 32-, or 64-bit unsigned integers.
type Reader ¶
type Reader struct { Reader io.Reader ByteOrder ByteOrder // contains filtered or unexported fields }
Reader is the simplified version of ReadChain. Instead of having to pass pointers to decode bytes, using Reader you can read data directly and do, for instance, simple assignments.
Example ¶
package main import ( "bytes" "fmt" "howl.moe/binary" ) func main() { buf := bytes.NewBuffer([]byte("\x05\xff\x02\x00")) r := &binary.Reader{ Reader: buf, ByteOrder: binary.LittleEndian, } var ( b1 = r.Byte() b2 = r.Int8() i = r.Int16() ) _, err := r.End() if err != nil { panic(err) } fmt.Printf("%d - %d - %d\n", b1, b2, i) }
Output: 5 - -1 - 2
func (*Reader) Complex128 ¶
func (r *Reader) Complex128() complex128
Complex128 decodes a Complex128 from the Reader.
func (*Reader) End ¶
End returns the amount of read bytes and any eventual error, and sets the internal amount of written bytes to 0, and the error to nil.
type WriteChain ¶
type WriteChain struct { Writer io.Writer ByteOrder ByteOrder // contains filtered or unexported fields }
WriteChain wraps around an io.Writer and a encoding.ByteOrder, and writes data into the Writer using the given ByteOrder. WriteChain is not thread-safe.
Example ¶
package main import ( "bytes" "fmt" "howl.moe/binary" ) func main() { buf := &bytes.Buffer{} writer := &binary.WriteChain{ Writer: buf, ByteOrder: binary.LittleEndian, } _, err := writer. Uint16(266). Byte(1). Uint32(2). End() if err != nil { panic(err) } fmt.Printf("% x\n", buf.Bytes()) }
Output: 0a 01 01 02 00 00 00
func (*WriteChain) Bool ¶
func (c *WriteChain) Bool(b bool) *WriteChain
Bool encodes a boolean into the write, transforming true into uint8(1) and false into uint8(0).
func (*WriteChain) Byte ¶
func (c *WriteChain) Byte(b byte) *WriteChain
Byte encodes a byte into the writer.
func (*WriteChain) ByteSlice ¶
func (c *WriteChain) ByteSlice(b []byte) *WriteChain
ByteSlice writes a raw byte slice into the WriteChain
func (*WriteChain) Complex128 ¶
func (c *WriteChain) Complex128(com complex128) *WriteChain
Complex128 encodes a complex128 into the writer.
func (*WriteChain) Complex64 ¶
func (c *WriteChain) Complex64(com complex64) *WriteChain
Complex64 encodes a complex64 into the writer.
func (*WriteChain) End ¶
func (c *WriteChain) End() (int, error)
End finishes writing to the Writer, and returns the amount of written bytes and any eventual error occured during the WriteChain lifetime. It also clears out the written bytes and the error, making WriteChain usable as defined by the user initially again.
func (*WriteChain) Float32 ¶
func (c *WriteChain) Float32(f float32) *WriteChain
Float32 encodes a float32 into the writer.
func (*WriteChain) Float64 ¶
func (c *WriteChain) Float64(f float64) *WriteChain
Float64 encodes a float64 into the writer.
func (*WriteChain) Int16 ¶
func (c *WriteChain) Int16(i int16) *WriteChain
Int16 encodes an int16 into the writer.
func (*WriteChain) Int32 ¶
func (c *WriteChain) Int32(i int32) *WriteChain
Int32 encodes an int32 into the writer.
func (*WriteChain) Int64 ¶
func (c *WriteChain) Int64(i int64) *WriteChain
Int64 encodes an int64 into the writer.
func (*WriteChain) Int8 ¶
func (c *WriteChain) Int8(i int8) *WriteChain
Int8 encodes an int8 into the writer.
func (*WriteChain) Rune ¶
func (c *WriteChain) Rune(r rune) *WriteChain
Rune encodes a rune into the writer.
func (*WriteChain) String ¶
func (c *WriteChain) String(s string) *WriteChain
String writes a string to the write chain.
func (*WriteChain) Uint16 ¶
func (c *WriteChain) Uint16(u uint16) *WriteChain
Uint16 encodes an uint16 into the writer.
func (*WriteChain) Uint32 ¶
func (c *WriteChain) Uint32(u uint32) *WriteChain
Uint32 encodes an uint32 into the writer.
func (*WriteChain) Uint64 ¶
func (c *WriteChain) Uint64(u uint64) *WriteChain
Uint64 encodes an uint64 into the writer.
func (*WriteChain) Uint8 ¶
func (c *WriteChain) Uint8(u uint8) *WriteChain
Uint8 encodes an uint8 into the writer.