mirror of
https://github.com/tendermint/tendermint.git
synced 2026-07-20 06:52:30 +00:00
go fmt
This commit is contained in:
+7
-5
@@ -3,12 +3,14 @@ package binary
|
||||
import "io"
|
||||
|
||||
type Binary interface {
|
||||
WriteTo(w io.Writer) (int64, error)
|
||||
WriteTo(w io.Writer) (int64, error)
|
||||
}
|
||||
|
||||
func WriteOnto(b Binary, w io.Writer, n int64, err error) (int64, error) {
|
||||
if err != nil { return n, err }
|
||||
var n_ int64
|
||||
n_, err = b.WriteTo(w)
|
||||
return n+n_, err
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
var n_ int64
|
||||
n_, err = b.WriteTo(w)
|
||||
return n + n_, err
|
||||
}
|
||||
|
||||
+33
-25
@@ -6,44 +6,52 @@ import "bytes"
|
||||
type ByteSlice []byte
|
||||
|
||||
func (self ByteSlice) Equals(other Binary) bool {
|
||||
if o, ok := other.(ByteSlice); ok {
|
||||
return bytes.Equal(self, o)
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
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")
|
||||
}
|
||||
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
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
func ReadByteSliceSafe(r io.Reader) (ByteSlice, error) {
|
||||
length, err := ReadUInt32Safe(r)
|
||||
if err != nil { return nil, err }
|
||||
bytes := make([]byte, int(length))
|
||||
_, err = io.ReadFull(r, bytes)
|
||||
if err != nil { return nil, err }
|
||||
return bytes, nil
|
||||
length, err := ReadUInt32Safe(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bytes := make([]byte, int(length))
|
||||
_, err = io.ReadFull(r, bytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bytes, nil
|
||||
}
|
||||
|
||||
func ReadByteSlice(r io.Reader) ByteSlice {
|
||||
bytes, err := ReadByteSliceSafe(r)
|
||||
if r != nil { panic(err) }
|
||||
return bytes
|
||||
bytes, err := ReadByteSliceSafe(r)
|
||||
if r != nil {
|
||||
panic(err)
|
||||
}
|
||||
return bytes
|
||||
}
|
||||
|
||||
+79
-49
@@ -1,70 +1,100 @@
|
||||
package binary
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io"
|
||||
)
|
||||
|
||||
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_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)
|
||||
TYPE_STRING = Byte(0x10)
|
||||
TYPE_BYTESLICE = Byte(0x11)
|
||||
|
||||
TYPE_TIME = Byte(0x20)
|
||||
TYPE_TIME = Byte(0x20)
|
||||
)
|
||||
|
||||
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")
|
||||
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
|
||||
case String:
|
||||
return TYPE_STRING
|
||||
case ByteSlice:
|
||||
return TYPE_BYTESLICE
|
||||
|
||||
case Time: return TYPE_TIME
|
||||
case Time:
|
||||
return TYPE_TIME
|
||||
|
||||
default: panic("Unsupported type")
|
||||
}
|
||||
default:
|
||||
panic("Unsupported type")
|
||||
}
|
||||
}
|
||||
|
||||
func ReadBinary(r io.Reader) Binary {
|
||||
type_ := ReadByte(r)
|
||||
switch type_ {
|
||||
case TYPE_NIL: return nil
|
||||
case TYPE_BYTE: return ReadByte(r)
|
||||
case TYPE_INT8: return ReadInt8(r)
|
||||
case TYPE_UINT8: return ReadUInt8(r)
|
||||
case TYPE_INT16: return ReadInt16(r)
|
||||
case TYPE_UINT16: return ReadUInt16(r)
|
||||
case TYPE_INT32: return ReadInt32(r)
|
||||
case TYPE_UINT32: return ReadUInt32(r)
|
||||
case TYPE_INT64: return ReadInt64(r)
|
||||
case TYPE_UINT64: return ReadUInt64(r)
|
||||
type_ := ReadByte(r)
|
||||
switch type_ {
|
||||
case TYPE_NIL:
|
||||
return nil
|
||||
case TYPE_BYTE:
|
||||
return ReadByte(r)
|
||||
case TYPE_INT8:
|
||||
return ReadInt8(r)
|
||||
case TYPE_UINT8:
|
||||
return ReadUInt8(r)
|
||||
case TYPE_INT16:
|
||||
return ReadInt16(r)
|
||||
case TYPE_UINT16:
|
||||
return ReadUInt16(r)
|
||||
case TYPE_INT32:
|
||||
return ReadInt32(r)
|
||||
case TYPE_UINT32:
|
||||
return ReadUInt32(r)
|
||||
case TYPE_INT64:
|
||||
return ReadInt64(r)
|
||||
case TYPE_UINT64:
|
||||
return ReadUInt64(r)
|
||||
|
||||
case TYPE_STRING: return ReadString(r)
|
||||
case TYPE_BYTESLICE:return ReadByteSlice(r)
|
||||
case TYPE_STRING:
|
||||
return ReadString(r)
|
||||
case TYPE_BYTESLICE:
|
||||
return ReadByteSlice(r)
|
||||
|
||||
case TYPE_TIME: return ReadTime(r)
|
||||
case TYPE_TIME:
|
||||
return ReadTime(r)
|
||||
|
||||
default: panic("Unsupported type")
|
||||
}
|
||||
default:
|
||||
panic("Unsupported type")
|
||||
}
|
||||
}
|
||||
|
||||
+221
-192
@@ -1,8 +1,8 @@
|
||||
package binary
|
||||
|
||||
import (
|
||||
"io"
|
||||
"encoding/binary"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
)
|
||||
|
||||
type Byte byte
|
||||
@@ -17,397 +17,426 @@ type UInt64 uint64
|
||||
type Int int
|
||||
type UInt uint
|
||||
|
||||
|
||||
// Byte
|
||||
|
||||
func (self Byte) Equals(other Binary) bool {
|
||||
return self == other
|
||||
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")
|
||||
}
|
||||
if o, ok := other.(Byte); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self Byte) ByteSize() int {
|
||||
return 1
|
||||
return 1
|
||||
}
|
||||
|
||||
func (self Byte) WriteTo(w io.Writer) (int64, error) {
|
||||
n, err := w.Write([]byte{byte(self)})
|
||||
return int64(n), err
|
||||
n, err := w.Write([]byte{byte(self)})
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
func ReadByteSafe(r io.Reader) (Byte, error) {
|
||||
buf := [1]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil { return 0, err }
|
||||
return Byte(buf[0]), nil
|
||||
buf := [1]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return Byte(buf[0]), nil
|
||||
}
|
||||
|
||||
func ReadByte(r io.Reader) (Byte) {
|
||||
b, err := ReadByteSafe(r)
|
||||
if err != nil { panic(err) }
|
||||
return b
|
||||
func ReadByte(r io.Reader) Byte {
|
||||
b, err := ReadByteSafe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
// Int8
|
||||
|
||||
func (self Int8) Equals(other Binary) bool {
|
||||
return self == other
|
||||
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")
|
||||
}
|
||||
if o, ok := other.(Int8); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self Int8) ByteSize() int {
|
||||
return 1
|
||||
return 1
|
||||
}
|
||||
|
||||
func (self Int8) WriteTo(w io.Writer) (int64, error) {
|
||||
n, err := w.Write([]byte{byte(self)})
|
||||
return int64(n), err
|
||||
n, err := w.Write([]byte{byte(self)})
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
func ReadInt8Safe(r io.Reader) (Int8, error) {
|
||||
buf := [1]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil { return Int8(0), err }
|
||||
return Int8(buf[0]), nil
|
||||
buf := [1]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return Int8(0), err
|
||||
}
|
||||
return Int8(buf[0]), nil
|
||||
}
|
||||
|
||||
func ReadInt8(r io.Reader) (Int8) {
|
||||
b, err := ReadInt8Safe(r)
|
||||
if err != nil { panic(err) }
|
||||
return b
|
||||
func ReadInt8(r io.Reader) Int8 {
|
||||
b, err := ReadInt8Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
// UInt8
|
||||
|
||||
func (self UInt8) Equals(other Binary) bool {
|
||||
return self == other
|
||||
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")
|
||||
}
|
||||
if o, ok := other.(UInt8); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self UInt8) ByteSize() int {
|
||||
return 1
|
||||
return 1
|
||||
}
|
||||
|
||||
func (self UInt8) WriteTo(w io.Writer) (int64, error) {
|
||||
n, err := w.Write([]byte{byte(self)})
|
||||
return int64(n), err
|
||||
n, err := w.Write([]byte{byte(self)})
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
func ReadUInt8Safe(r io.Reader) (UInt8, error) {
|
||||
buf := [1]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil { return UInt8(0), err }
|
||||
return UInt8(buf[0]), nil
|
||||
buf := [1]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return UInt8(0), err
|
||||
}
|
||||
return UInt8(buf[0]), nil
|
||||
}
|
||||
|
||||
func ReadUInt8(r io.Reader) (UInt8) {
|
||||
b, err := ReadUInt8Safe(r)
|
||||
if err != nil { panic(err) }
|
||||
return b
|
||||
func ReadUInt8(r io.Reader) UInt8 {
|
||||
b, err := ReadUInt8Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
// Int16
|
||||
|
||||
func (self Int16) Equals(other Binary) bool {
|
||||
return self == other
|
||||
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")
|
||||
}
|
||||
if o, ok := other.(Int16); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self Int16) ByteSize() int {
|
||||
return 2
|
||||
return 2
|
||||
}
|
||||
|
||||
func (self Int16) WriteTo(w io.Writer) (int64, error) {
|
||||
err := binary.Write(w, binary.LittleEndian, int16(self))
|
||||
return 2, err
|
||||
err := binary.Write(w, binary.LittleEndian, int16(self))
|
||||
return 2, err
|
||||
}
|
||||
|
||||
func ReadInt16Safe(r io.Reader) (Int16, error) {
|
||||
buf := [2]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil { return Int16(0), err }
|
||||
return Int16(binary.LittleEndian.Uint16(buf[:])), nil
|
||||
buf := [2]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return Int16(0), err
|
||||
}
|
||||
return Int16(binary.LittleEndian.Uint16(buf[:])), nil
|
||||
}
|
||||
|
||||
func ReadInt16(r io.Reader) (Int16) {
|
||||
b, err := ReadInt16Safe(r)
|
||||
if err != nil { panic(err) }
|
||||
return b
|
||||
func ReadInt16(r io.Reader) Int16 {
|
||||
b, err := ReadInt16Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
// UInt16
|
||||
|
||||
func (self UInt16) Equals(other Binary) bool {
|
||||
return self == other
|
||||
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")
|
||||
}
|
||||
if o, ok := other.(UInt16); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self UInt16) ByteSize() int {
|
||||
return 2
|
||||
return 2
|
||||
}
|
||||
|
||||
func (self UInt16) WriteTo(w io.Writer) (int64, error) {
|
||||
err := binary.Write(w, binary.LittleEndian, uint16(self))
|
||||
return 2, err
|
||||
err := binary.Write(w, binary.LittleEndian, uint16(self))
|
||||
return 2, err
|
||||
}
|
||||
|
||||
func ReadUInt16Safe(r io.Reader) (UInt16, error) {
|
||||
buf := [2]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil { return UInt16(0), err }
|
||||
return UInt16(binary.LittleEndian.Uint16(buf[:])), nil
|
||||
buf := [2]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return UInt16(0), err
|
||||
}
|
||||
return UInt16(binary.LittleEndian.Uint16(buf[:])), nil
|
||||
}
|
||||
|
||||
func ReadUInt16(r io.Reader) (UInt16) {
|
||||
b, err := ReadUInt16Safe(r)
|
||||
if err != nil { panic(err) }
|
||||
return b
|
||||
func ReadUInt16(r io.Reader) UInt16 {
|
||||
b, err := ReadUInt16Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
// Int32
|
||||
|
||||
func (self Int32) Equals(other Binary) bool {
|
||||
return self == other
|
||||
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")
|
||||
}
|
||||
if o, ok := other.(Int32); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self Int32) ByteSize() int {
|
||||
return 4
|
||||
return 4
|
||||
}
|
||||
|
||||
func (self Int32) WriteTo(w io.Writer) (int64, error) {
|
||||
err := binary.Write(w, binary.LittleEndian, int32(self))
|
||||
return 4, err
|
||||
err := binary.Write(w, binary.LittleEndian, int32(self))
|
||||
return 4, err
|
||||
}
|
||||
|
||||
func ReadInt32Safe(r io.Reader) (Int32, error) {
|
||||
buf := [4]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil { return Int32(0), err }
|
||||
return Int32(binary.LittleEndian.Uint32(buf[:])), nil
|
||||
buf := [4]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return Int32(0), err
|
||||
}
|
||||
return Int32(binary.LittleEndian.Uint32(buf[:])), nil
|
||||
}
|
||||
|
||||
func ReadInt32(r io.Reader) (Int32) {
|
||||
b, err := ReadInt32Safe(r)
|
||||
if err != nil { panic(err) }
|
||||
return b
|
||||
func ReadInt32(r io.Reader) Int32 {
|
||||
b, err := ReadInt32Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
// UInt32
|
||||
|
||||
func (self UInt32) Equals(other Binary) bool {
|
||||
return self == other
|
||||
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")
|
||||
}
|
||||
if o, ok := other.(UInt32); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self UInt32) ByteSize() int {
|
||||
return 4
|
||||
return 4
|
||||
}
|
||||
|
||||
func (self UInt32) WriteTo(w io.Writer) (int64, error) {
|
||||
err := binary.Write(w, binary.LittleEndian, uint32(self))
|
||||
return 4, err
|
||||
err := binary.Write(w, binary.LittleEndian, uint32(self))
|
||||
return 4, err
|
||||
}
|
||||
|
||||
func ReadUInt32Safe(r io.Reader) (UInt32, error) {
|
||||
buf := [4]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil { return UInt32(0), err }
|
||||
return UInt32(binary.LittleEndian.Uint32(buf[:])), nil
|
||||
buf := [4]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return UInt32(0), err
|
||||
}
|
||||
return UInt32(binary.LittleEndian.Uint32(buf[:])), nil
|
||||
}
|
||||
|
||||
func ReadUInt32(r io.Reader) (UInt32) {
|
||||
b, err := ReadUInt32Safe(r)
|
||||
if err != nil { panic(err) }
|
||||
return b
|
||||
func ReadUInt32(r io.Reader) UInt32 {
|
||||
b, err := ReadUInt32Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
// Int64
|
||||
|
||||
func (self Int64) Equals(other Binary) bool {
|
||||
return self == other
|
||||
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")
|
||||
}
|
||||
if o, ok := other.(Int64); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self Int64) ByteSize() int {
|
||||
return 8
|
||||
return 8
|
||||
}
|
||||
|
||||
func (self Int64) WriteTo(w io.Writer) (int64, error) {
|
||||
err := binary.Write(w, binary.LittleEndian, int64(self))
|
||||
return 8, err
|
||||
err := binary.Write(w, binary.LittleEndian, int64(self))
|
||||
return 8, err
|
||||
}
|
||||
|
||||
func ReadInt64Safe(r io.Reader) (Int64, error) {
|
||||
buf := [8]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil { return Int64(0), err }
|
||||
return Int64(binary.LittleEndian.Uint64(buf[:])), nil
|
||||
buf := [8]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return Int64(0), err
|
||||
}
|
||||
return Int64(binary.LittleEndian.Uint64(buf[:])), nil
|
||||
}
|
||||
|
||||
func ReadInt64(r io.Reader) (Int64) {
|
||||
b, err := ReadInt64Safe(r)
|
||||
if err != nil { panic(err) }
|
||||
return b
|
||||
func ReadInt64(r io.Reader) Int64 {
|
||||
b, err := ReadInt64Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
// UInt64
|
||||
|
||||
func (self UInt64) Equals(other Binary) bool {
|
||||
return self == other
|
||||
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")
|
||||
}
|
||||
if o, ok := other.(UInt64); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self UInt64) ByteSize() int {
|
||||
return 8
|
||||
return 8
|
||||
}
|
||||
|
||||
func (self UInt64) WriteTo(w io.Writer) (int64, error) {
|
||||
err := binary.Write(w, binary.LittleEndian, uint64(self))
|
||||
return 8, err
|
||||
err := binary.Write(w, binary.LittleEndian, uint64(self))
|
||||
return 8, err
|
||||
}
|
||||
|
||||
func ReadUInt64Safe(r io.Reader) (UInt64, error) {
|
||||
buf := [8]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil { return UInt64(0), err }
|
||||
return UInt64(binary.LittleEndian.Uint64(buf[:])), nil
|
||||
buf := [8]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return UInt64(0), err
|
||||
}
|
||||
return UInt64(binary.LittleEndian.Uint64(buf[:])), nil
|
||||
}
|
||||
|
||||
func ReadUInt64(r io.Reader) (UInt64) {
|
||||
b, err := ReadUInt64Safe(r)
|
||||
if err != nil { panic(err) }
|
||||
return b
|
||||
func ReadUInt64(r io.Reader) UInt64 {
|
||||
b, err := ReadUInt64Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
// Int
|
||||
|
||||
func (self Int) Equals(other Binary) bool {
|
||||
return self == other
|
||||
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")
|
||||
}
|
||||
if o, ok := other.(Int); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self Int) ByteSize() int {
|
||||
return 8
|
||||
return 8
|
||||
}
|
||||
|
||||
func (self Int) WriteTo(w io.Writer) (int64, error) {
|
||||
err := binary.Write(w, binary.LittleEndian, int64(self))
|
||||
return 8, err
|
||||
err := binary.Write(w, binary.LittleEndian, int64(self))
|
||||
return 8, err
|
||||
}
|
||||
|
||||
func ReadInt(r io.Reader) Int {
|
||||
buf := [8]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil { panic(err) }
|
||||
return Int(binary.LittleEndian.Uint64(buf[:]))
|
||||
buf := [8]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return Int(binary.LittleEndian.Uint64(buf[:]))
|
||||
}
|
||||
|
||||
|
||||
// UInt
|
||||
|
||||
func (self UInt) Equals(other Binary) bool {
|
||||
return self == other
|
||||
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")
|
||||
}
|
||||
if o, ok := other.(UInt); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self UInt) ByteSize() int {
|
||||
return 8
|
||||
return 8
|
||||
}
|
||||
|
||||
func (self UInt) WriteTo(w io.Writer) (int64, error) {
|
||||
err := binary.Write(w, binary.LittleEndian, uint64(self))
|
||||
return 8, err
|
||||
err := binary.Write(w, binary.LittleEndian, uint64(self))
|
||||
return 8, err
|
||||
}
|
||||
|
||||
func ReadUInt(r io.Reader) UInt {
|
||||
buf := [8]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil { panic(err) }
|
||||
return UInt(binary.LittleEndian.Uint64(buf[:]))
|
||||
buf := [8]byte{0}
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return UInt(binary.LittleEndian.Uint64(buf[:]))
|
||||
}
|
||||
|
||||
+29
-21
@@ -7,40 +7,48 @@ type String string
|
||||
// String
|
||||
|
||||
func (self String) Equals(other Binary) bool {
|
||||
return self == other
|
||||
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")
|
||||
}
|
||||
if o, ok := other.(String); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self String) ByteSize() int {
|
||||
return len(self)+4
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
func ReadStringSafe(r io.Reader) (String, error) {
|
||||
length, err := ReadUInt32Safe(r)
|
||||
if err != nil { return "", err }
|
||||
bytes := make([]byte, int(length))
|
||||
_, err = io.ReadFull(r, bytes)
|
||||
if err != nil { return "", err }
|
||||
return String(bytes), nil
|
||||
length, err := ReadUInt32Safe(r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
bytes := make([]byte, int(length))
|
||||
_, err = io.ReadFull(r, bytes)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return String(bytes), nil
|
||||
}
|
||||
|
||||
func ReadString(r io.Reader) String {
|
||||
str, err := ReadStringSafe(r)
|
||||
if r != nil { panic(err) }
|
||||
return str
|
||||
str, err := ReadStringSafe(r)
|
||||
if r != nil {
|
||||
panic(err)
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
+16
-16
@@ -1,38 +1,38 @@
|
||||
package binary
|
||||
|
||||
import (
|
||||
"io"
|
||||
"time"
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Time struct {
|
||||
time.Time
|
||||
time.Time
|
||||
}
|
||||
|
||||
func (self Time) Equals(other Binary) bool {
|
||||
if o, ok := other.(Time); ok {
|
||||
return self.Equal(o.Time)
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
if o, ok := other.(Time); ok {
|
||||
return self.Equal(o.Time)
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (self Time) Less(other Binary) bool {
|
||||
if o, ok := other.(Time); ok {
|
||||
return self.Before(o.Time)
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
if o, ok := other.(Time); ok {
|
||||
return self.Before(o.Time)
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self Time) ByteSize() int {
|
||||
return 8
|
||||
return 8
|
||||
}
|
||||
|
||||
func (self Time) WriteTo(w io.Writer) (int64, error) {
|
||||
return Int64(self.Unix()).WriteTo(w)
|
||||
return Int64(self.Unix()).WriteTo(w)
|
||||
}
|
||||
|
||||
func ReadTime(r io.Reader) Time {
|
||||
return Time{time.Unix(int64(ReadInt64(r)), 0)}
|
||||
return Time{time.Unix(int64(ReadInt64(r)), 0)}
|
||||
}
|
||||
|
||||
+17
-15
@@ -1,33 +1,35 @@
|
||||
package binary
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"bytes"
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
)
|
||||
|
||||
func BinaryBytes(b Binary) ByteSlice {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
b.WriteTo(buf)
|
||||
return ByteSlice(buf.Bytes())
|
||||
buf := bytes.NewBuffer(nil)
|
||||
b.WriteTo(buf)
|
||||
return ByteSlice(buf.Bytes())
|
||||
}
|
||||
|
||||
// NOTE: does not care about the type, only the binary representation.
|
||||
func BinaryEqual(a, b Binary) bool {
|
||||
aBytes := BinaryBytes(a)
|
||||
bBytes := BinaryBytes(b)
|
||||
return bytes.Equal(aBytes, bBytes)
|
||||
aBytes := BinaryBytes(a)
|
||||
bBytes := BinaryBytes(b)
|
||||
return bytes.Equal(aBytes, bBytes)
|
||||
}
|
||||
|
||||
// NOTE: does not care about the type, only the binary representation.
|
||||
func BinaryCompare(a, b Binary) int {
|
||||
aBytes := BinaryBytes(a)
|
||||
bBytes := BinaryBytes(b)
|
||||
return bytes.Compare(aBytes, bBytes)
|
||||
aBytes := BinaryBytes(a)
|
||||
bBytes := BinaryBytes(b)
|
||||
return bytes.Compare(aBytes, bBytes)
|
||||
}
|
||||
|
||||
func BinaryHash(b Binary) ByteSlice {
|
||||
hasher := sha256.New()
|
||||
_, err := b.WriteTo(hasher)
|
||||
if err != nil { panic(err) }
|
||||
return ByteSlice(hasher.Sum(nil))
|
||||
hasher := sha256.New()
|
||||
_, err := b.WriteTo(hasher)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return ByteSlice(hasher.Sum(nil))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user