refactor out binary

This commit is contained in:
Jae Kwon
2014-06-04 01:39:50 -07:00
parent 15f8441068
commit 1c4f5e2506
8 changed files with 68 additions and 38 deletions
+9
View File
@@ -0,0 +1,9 @@
package binary
import "io"
type Binary interface {
ByteSize() int
WriteTo(io.Writer) (int64, error)
Equals(Binary) bool
}
+60
View File
@@ -0,0 +1,60 @@
package binary
const (
TYPE_NIL = byte(0x00)
TYPE_BYTE = byte(0x01)
TYPE_INT8 = byte(0x02)
TYPE_UINT8 = byte(0x03)
TYPE_INT16 = byte(0x04)
TYPE_UINT16 = byte(0x05)
TYPE_INT32 = byte(0x06)
TYPE_UINT32 = byte(0x07)
TYPE_INT64 = byte(0x08)
TYPE_UINT64 = byte(0x09)
TYPE_STRING = byte(0x10)
TYPE_BYTESLICE = byte(0x11)
)
func GetBinaryType(o Binary) byte {
switch o.(type) {
case nil: return TYPE_NIL
case Byte: return TYPE_BYTE
case Int8: return TYPE_INT8
case UInt8: return TYPE_UINT8
case Int16: return TYPE_INT16
case UInt16: return TYPE_UINT16
case Int32: return TYPE_INT32
case UInt32: return TYPE_UINT32
case Int64: return TYPE_INT64
case UInt64: return TYPE_UINT64
case Int: panic("Int not supported")
case UInt: panic("UInt not supported")
case String: return TYPE_STRING
case ByteSlice: return TYPE_BYTESLICE
default: panic("Unsupported type")
}
}
func ReadBinary(buf []byte, start int) (Binary, int) {
typeByte := buf[start]
switch typeByte {
case TYPE_NIL: return nil, start+1
case TYPE_BYTE: return ReadByte(buf[start+1:]), start+2
case TYPE_INT8: return ReadInt8(buf[start+1:]), start+2
case TYPE_UINT8: return ReadUInt8(buf[start+1:]), start+2
case TYPE_INT16: return ReadInt16(buf[start+1:]), start+3
case TYPE_UINT16: return ReadUInt16(buf[start+1:]), start+3
case TYPE_INT32: return ReadInt32(buf[start+1:]), start+5
case TYPE_UINT32: return ReadUInt32(buf[start+1:]), start+5
case TYPE_INT64: return ReadInt64(buf[start+1:]), start+9
case TYPE_UINT64: return ReadUInt64(buf[start+1:]), start+9
case TYPE_STRING: return ReadString(buf, start+1)
case TYPE_BYTESLICE:return ReadByteSlice(buf, start+1)
default: panic("Unsupported type")
}
}
+325
View File
@@ -0,0 +1,325 @@
package binary
import (
"io"
"encoding/binary"
)
type Byte byte
type Int8 int8
type UInt8 uint8
type Int16 int16
type UInt16 uint16
type Int32 int32
type UInt32 uint32
type Int64 int64
type UInt64 uint64
type Int int
type UInt uint
// Byte
func (self Byte) Equals(other Binary) bool {
return self == other
}
func (self Byte) Less(other Binary) bool {
if o, ok := other.(Byte); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
func (self Byte) ByteSize() int {
return 1
}
func (self Byte) WriteTo(w io.Writer) (int64, error) {
n, err := w.Write([]byte{byte(self)})
return int64(n), err
}
func ReadByte(bytes []byte) Byte {
return Byte(bytes[0])
}
// Int8
func (self Int8) Equals(other Binary) bool {
return self == other
}
func (self Int8) Less(other Binary) bool {
if o, ok := other.(Int8); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
func (self Int8) ByteSize() int {
return 1
}
func (self Int8) WriteTo(w io.Writer) (int64, error) {
n, err := w.Write([]byte{byte(self)})
return int64(n), err
}
func ReadInt8(bytes []byte) Int8 {
return Int8(bytes[0])
}
// UInt8
func (self UInt8) Equals(other Binary) bool {
return self == other
}
func (self UInt8) Less(other Binary) bool {
if o, ok := other.(UInt8); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
func (self UInt8) ByteSize() int {
return 1
}
func (self UInt8) WriteTo(w io.Writer) (int64, error) {
n, err := w.Write([]byte{byte(self)})
return int64(n), err
}
func ReadUInt8(bytes []byte) UInt8 {
return UInt8(bytes[0])
}
// Int16
func (self Int16) Equals(other Binary) bool {
return self == other
}
func (self Int16) Less(other Binary) bool {
if o, ok := other.(Int16); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
func (self Int16) ByteSize() int {
return 2
}
func (self Int16) WriteTo(w io.Writer) (int64, error) {
err := binary.Write(w, binary.LittleEndian, int16(self))
return 2, err
}
func ReadInt16(bytes []byte) Int16 {
return Int16(binary.LittleEndian.Uint16(bytes))
}
// UInt16
func (self UInt16) Equals(other Binary) bool {
return self == other
}
func (self UInt16) Less(other Binary) bool {
if o, ok := other.(UInt16); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
func (self UInt16) ByteSize() int {
return 2
}
func (self UInt16) WriteTo(w io.Writer) (int64, error) {
err := binary.Write(w, binary.LittleEndian, uint16(self))
return 2, err
}
func ReadUInt16(bytes []byte) UInt16 {
return UInt16(binary.LittleEndian.Uint16(bytes))
}
// Int32
func (self Int32) Equals(other Binary) bool {
return self == other
}
func (self Int32) Less(other Binary) bool {
if o, ok := other.(Int32); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
func (self Int32) ByteSize() int {
return 4
}
func (self Int32) WriteTo(w io.Writer) (int64, error) {
err := binary.Write(w, binary.LittleEndian, int32(self))
return 4, err
}
func ReadInt32(bytes []byte) Int32 {
return Int32(binary.LittleEndian.Uint32(bytes))
}
// UInt32
func (self UInt32) Equals(other Binary) bool {
return self == other
}
func (self UInt32) Less(other Binary) bool {
if o, ok := other.(UInt32); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
func (self UInt32) ByteSize() int {
return 4
}
func (self UInt32) WriteTo(w io.Writer) (int64, error) {
err := binary.Write(w, binary.LittleEndian, uint32(self))
return 4, err
}
func ReadUInt32(bytes []byte) UInt32 {
return UInt32(binary.LittleEndian.Uint32(bytes))
}
// Int64
func (self Int64) Equals(other Binary) bool {
return self == other
}
func (self Int64) Less(other Binary) bool {
if o, ok := other.(Int64); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
func (self Int64) ByteSize() int {
return 8
}
func (self Int64) WriteTo(w io.Writer) (int64, error) {
err := binary.Write(w, binary.LittleEndian, int64(self))
return 8, err
}
func ReadInt64(bytes []byte) Int64 {
return Int64(binary.LittleEndian.Uint64(bytes))
}
// UInt64
func (self UInt64) Equals(other Binary) bool {
return self == other
}
func (self UInt64) Less(other Binary) bool {
if o, ok := other.(UInt64); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
func (self UInt64) ByteSize() int {
return 8
}
func (self UInt64) WriteTo(w io.Writer) (int64, error) {
err := binary.Write(w, binary.LittleEndian, uint64(self))
return 8, err
}
func ReadUInt64(bytes []byte) UInt64 {
return UInt64(binary.LittleEndian.Uint64(bytes))
}
// Int
func (self Int) Equals(other Binary) bool {
return self == other
}
func (self Int) Less(other Binary) bool {
if o, ok := other.(Int); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
func (self Int) ByteSize() int {
return 8
}
func (self Int) WriteTo(w io.Writer) (int64, error) {
err := binary.Write(w, binary.LittleEndian, int64(self))
return 8, err
}
func ReadInt(bytes []byte) Int {
return Int(binary.LittleEndian.Uint64(bytes))
}
// UInt
func (self UInt) Equals(other Binary) bool {
return self == other
}
func (self UInt) Less(other Binary) bool {
if o, ok := other.(UInt); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
func (self UInt) ByteSize() int {
return 8
}
func (self UInt) WriteTo(w io.Writer) (int64, error) {
err := binary.Write(w, binary.LittleEndian, uint64(self))
return 8, err
}
func ReadUInt(bytes []byte) UInt {
return UInt(binary.LittleEndian.Uint64(bytes))
}
+76
View File
@@ -0,0 +1,76 @@
package binary
import "io"
import "bytes"
type String string
type ByteSlice []byte
// String
func (self String) Equals(other Binary) bool {
return self == other
}
func (self String) Less(other Binary) bool {
if o, ok := other.(String); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
func (self String) ByteSize() int {
return len(self)+4
}
func (self String) WriteTo(w io.Writer) (n int64, err error) {
var n_ int
_, err = UInt32(len(self)).WriteTo(w)
if err != nil { return n, err }
n_, err = w.Write([]byte(self))
return int64(n_+4), err
}
// NOTE: keeps a reference to the original byte slice
func ReadString(bytes []byte, start int) (String, int) {
length := int(ReadUInt32(bytes[start:]))
return String(bytes[start+4:start+4+length]), start+4+length
}
// ByteSlice
func (self ByteSlice) Equals(other Binary) bool {
if o, ok := other.(ByteSlice); ok {
return bytes.Equal(self, o)
} else {
return false
}
}
func (self ByteSlice) Less(other Binary) bool {
if o, ok := other.(ByteSlice); ok {
return bytes.Compare(self, o) < 0 // -1 if a < b
} else {
panic("Cannot compare unequal types")
}
}
func (self ByteSlice) ByteSize() int {
return len(self)+4
}
func (self ByteSlice) WriteTo(w io.Writer) (n int64, err error) {
var n_ int
_, err = UInt32(len(self)).WriteTo(w)
if err != nil { return n, err }
n_, err = w.Write([]byte(self))
return int64(n_+4), err
}
// NOTE: keeps a reference to the original byte slice
func ReadByteSlice(bytes []byte, start int) (ByteSlice, int) {
length := int(ReadUInt32(bytes[start:]))
return ByteSlice(bytes[start+4:start+4+length]), start+4+length
}