tendermint/binary/int.go

463 lines
8.3 KiB
Go
Raw Normal View History

2014-06-04 01:39:50 -07:00
package binary
2014-05-21 16:24:50 -07:00
import (
2014-07-01 14:50:24 -07:00
"encoding/binary"
"io"
2014-05-21 16:24:50 -07:00
)
2014-05-23 17:49:28 -07:00
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
2014-05-23 17:49:28 -07:00
// Byte
2014-05-22 18:08:49 -07:00
2014-08-10 16:35:08 -07:00
func (self Byte) Equals(other interface{}) bool {
2014-07-01 14:50:24 -07:00
return self == other
2014-05-23 17:49:28 -07:00
}
2014-08-10 16:35:08 -07:00
func (self Byte) Less(other interface{}) bool {
2014-07-01 14:50:24 -07:00
if o, ok := other.(Byte); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
2014-05-23 17:49:28 -07:00
func (self Byte) ByteSize() int {
2014-07-01 14:50:24 -07:00
return 1
2014-05-23 17:49:28 -07:00
}
2014-06-03 17:03:29 -07:00
func (self Byte) WriteTo(w io.Writer) (int64, error) {
2014-07-01 14:50:24 -07:00
n, err := w.Write([]byte{byte(self)})
return int64(n), err
2014-05-23 17:49:28 -07:00
}
func ReadByteSafe(r io.Reader) (Byte, int64, error) {
2014-07-01 14:50:24 -07:00
buf := [1]byte{0}
n, err := io.ReadFull(r, buf[:])
2014-07-01 14:50:24 -07:00
if err != nil {
return 0, int64(n), err
2014-07-01 14:50:24 -07:00
}
return Byte(buf[0]), int64(n), nil
}
func ReadByteN(r io.Reader) (Byte, int64) {
b, n, err := ReadByteSafe(r)
if err != nil {
panic(err)
}
return b, n
2014-06-24 17:28:40 -07:00
}
2014-07-01 14:50:24 -07:00
func ReadByte(r io.Reader) Byte {
b, _, err := ReadByteSafe(r)
2014-07-01 14:50:24 -07:00
if err != nil {
panic(err)
}
return b
}
2014-05-23 17:49:28 -07:00
// Int8
2014-08-10 16:35:08 -07:00
func (self Int8) Equals(other interface{}) bool {
2014-07-01 14:50:24 -07:00
return self == other
2014-05-23 17:49:28 -07:00
}
2014-08-10 16:35:08 -07:00
func (self Int8) Less(other interface{}) bool {
2014-07-01 14:50:24 -07:00
if o, ok := other.(Int8); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
2014-05-22 18:08:49 -07:00
func (self Int8) ByteSize() int {
2014-07-01 14:50:24 -07:00
return 1
2014-05-22 18:08:49 -07:00
}
2014-06-03 17:03:29 -07:00
func (self Int8) WriteTo(w io.Writer) (int64, error) {
2014-07-01 14:50:24 -07:00
n, err := w.Write([]byte{byte(self)})
return int64(n), err
}
func ReadInt8Safe(r io.Reader) (Int8, int64, error) {
2014-07-01 14:50:24 -07:00
buf := [1]byte{0}
n, err := io.ReadFull(r, buf[:])
if err != nil {
return Int8(0), int64(n), err
}
return Int8(buf[0]), int64(n), nil
}
func ReadInt8N(r io.Reader) (Int8, int64) {
b, n, err := ReadInt8Safe(r)
2014-07-01 14:50:24 -07:00
if err != nil {
panic(err)
2014-07-01 14:50:24 -07:00
}
return b, n
}
2014-07-01 14:50:24 -07:00
func ReadInt8(r io.Reader) Int8 {
b, _, err := ReadInt8Safe(r)
2014-07-01 14:50:24 -07:00
if err != nil {
panic(err)
}
return b
2014-05-22 18:08:49 -07:00
}
// UInt8
2014-08-10 16:35:08 -07:00
func (self UInt8) Equals(other interface{}) bool {
2014-07-01 14:50:24 -07:00
return self == other
}
2014-08-10 16:35:08 -07:00
func (self UInt8) Less(other interface{}) bool {
2014-07-01 14:50:24 -07:00
if o, ok := other.(UInt8); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
2014-05-22 18:08:49 -07:00
func (self UInt8) ByteSize() int {
2014-07-01 14:50:24 -07:00
return 1
}
2014-06-03 17:03:29 -07:00
func (self UInt8) WriteTo(w io.Writer) (int64, error) {
2014-07-01 14:50:24 -07:00
n, err := w.Write([]byte{byte(self)})
return int64(n), err
2014-05-22 18:08:49 -07:00
}
func ReadUInt8Safe(r io.Reader) (UInt8, int64, error) {
2014-07-01 14:50:24 -07:00
buf := [1]byte{0}
n, err := io.ReadFull(r, buf[:])
2014-07-01 14:50:24 -07:00
if err != nil {
return UInt8(0), int64(n), err
2014-07-01 14:50:24 -07:00
}
return UInt8(buf[0]), int64(n), nil
}
func ReadUInt8N(r io.Reader) (UInt8, int64) {
b, n, err := ReadUInt8Safe(r)
if err != nil {
panic(err)
}
return b, n
2014-06-25 21:37:20 -07:00
}
2014-07-01 14:50:24 -07:00
func ReadUInt8(r io.Reader) UInt8 {
b, _, err := ReadUInt8Safe(r)
2014-07-01 14:50:24 -07:00
if err != nil {
panic(err)
}
return b
}
2014-05-22 18:08:49 -07:00
// Int16
2014-08-10 16:35:08 -07:00
func (self Int16) Equals(other interface{}) bool {
2014-07-01 14:50:24 -07:00
return self == other
}
2014-08-10 16:35:08 -07:00
func (self Int16) Less(other interface{}) bool {
2014-07-01 14:50:24 -07:00
if o, ok := other.(Int16); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
2014-05-22 18:08:49 -07:00
func (self Int16) ByteSize() int {
2014-07-01 14:50:24 -07:00
return 2
}
2014-06-03 17:03:29 -07:00
func (self Int16) WriteTo(w io.Writer) (int64, error) {
2014-07-03 13:44:19 -07:00
buf := []byte{0, 0}
binary.LittleEndian.PutUint16(buf, uint16(self))
n, err := w.Write(buf)
return int64(n), err
2014-05-22 18:08:49 -07:00
}
func ReadInt16Safe(r io.Reader) (Int16, int64, error) {
2014-07-01 14:50:24 -07:00
buf := [2]byte{0}
n, err := io.ReadFull(r, buf[:])
2014-07-01 14:50:24 -07:00
if err != nil {
return Int16(0), int64(n), err
2014-07-01 14:50:24 -07:00
}
return Int16(binary.LittleEndian.Uint16(buf[:])), int64(n), nil
}
func ReadInt16N(r io.Reader) (Int16, int64) {
b, n, err := ReadInt16Safe(r)
if err != nil {
panic(err)
}
return b, n
}
2014-07-01 14:50:24 -07:00
func ReadInt16(r io.Reader) Int16 {
b, _, err := ReadInt16Safe(r)
2014-07-01 14:50:24 -07:00
if err != nil {
panic(err)
}
return b
2014-05-22 18:08:49 -07:00
}
// UInt16
2014-08-10 16:35:08 -07:00
func (self UInt16) Equals(other interface{}) bool {
2014-07-01 14:50:24 -07:00
return self == other
}
2014-08-10 16:35:08 -07:00
func (self UInt16) Less(other interface{}) bool {
2014-07-01 14:50:24 -07:00
if o, ok := other.(UInt16); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
2014-05-22 18:08:49 -07:00
func (self UInt16) ByteSize() int {
2014-07-01 14:50:24 -07:00
return 2
2014-05-22 18:08:49 -07:00
}
2014-06-03 17:03:29 -07:00
func (self UInt16) WriteTo(w io.Writer) (int64, error) {
2014-07-03 13:44:19 -07:00
buf := []byte{0, 0}
binary.LittleEndian.PutUint16(buf, uint16(self))
n, err := w.Write(buf)
return int64(n), err
2014-05-22 18:08:49 -07:00
}
func ReadUInt16Safe(r io.Reader) (UInt16, int64, error) {
2014-07-01 14:50:24 -07:00
buf := [2]byte{0}
n, err := io.ReadFull(r, buf[:])
2014-07-01 14:50:24 -07:00
if err != nil {
return UInt16(0), int64(n), err
2014-07-01 14:50:24 -07:00
}
return UInt16(binary.LittleEndian.Uint16(buf[:])), int64(n), nil
}
func ReadUInt16N(r io.Reader) (UInt16, int64) {
b, n, err := ReadUInt16Safe(r)
if err != nil {
panic(err)
}
return b, n
}
2014-07-01 14:50:24 -07:00
func ReadUInt16(r io.Reader) UInt16 {
b, _, err := ReadUInt16Safe(r)
2014-07-01 14:50:24 -07:00
if err != nil {
panic(err)
}
return b
}
2014-05-22 18:08:49 -07:00
// Int32
2014-08-10 16:35:08 -07:00
func (self Int32) Equals(other interface{}) bool {
2014-07-01 14:50:24 -07:00
return self == other
}
2014-08-10 16:35:08 -07:00
func (self Int32) Less(other interface{}) bool {
2014-07-01 14:50:24 -07:00
if o, ok := other.(Int32); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
2014-05-22 18:08:49 -07:00
func (self Int32) ByteSize() int {
2014-07-01 14:50:24 -07:00
return 4
2014-05-22 18:08:49 -07:00
}
2014-06-03 17:03:29 -07:00
func (self Int32) WriteTo(w io.Writer) (int64, error) {
2014-07-03 13:44:19 -07:00
buf := []byte{0, 0, 0, 0}
binary.LittleEndian.PutUint32(buf, uint32(self))
n, err := w.Write(buf)
return int64(n), err
2014-05-22 18:08:49 -07:00
}
func ReadInt32Safe(r io.Reader) (Int32, int64, error) {
2014-07-01 14:50:24 -07:00
buf := [4]byte{0}
n, err := io.ReadFull(r, buf[:])
2014-07-01 14:50:24 -07:00
if err != nil {
return Int32(0), int64(n), err
2014-07-01 14:50:24 -07:00
}
return Int32(binary.LittleEndian.Uint32(buf[:])), int64(n), nil
}
func ReadInt32N(r io.Reader) (Int32, int64) {
b, n, err := ReadInt32Safe(r)
if err != nil {
panic(err)
}
return b, n
}
2014-07-01 14:50:24 -07:00
func ReadInt32(r io.Reader) Int32 {
b, _, err := ReadInt32Safe(r)
2014-07-01 14:50:24 -07:00
if err != nil {
panic(err)
}
return b
}
2014-05-22 18:08:49 -07:00
// UInt32
2014-08-10 16:35:08 -07:00
func (self UInt32) Equals(other interface{}) bool {
2014-07-01 14:50:24 -07:00
return self == other
}
2014-08-10 16:35:08 -07:00
func (self UInt32) Less(other interface{}) bool {
2014-07-01 14:50:24 -07:00
if o, ok := other.(UInt32); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
2014-05-22 18:08:49 -07:00
func (self UInt32) ByteSize() int {
2014-07-01 14:50:24 -07:00
return 4
2014-05-22 18:08:49 -07:00
}
2014-06-03 17:03:29 -07:00
func (self UInt32) WriteTo(w io.Writer) (int64, error) {
2014-07-03 13:44:19 -07:00
buf := []byte{0, 0, 0, 0}
binary.LittleEndian.PutUint32(buf, uint32(self))
n, err := w.Write(buf)
return int64(n), err
}
func ReadUInt32Safe(r io.Reader) (UInt32, int64, error) {
2014-07-01 14:50:24 -07:00
buf := [4]byte{0}
n, err := io.ReadFull(r, buf[:])
if err != nil {
return UInt32(0), int64(n), err
}
return UInt32(binary.LittleEndian.Uint32(buf[:])), int64(n), nil
}
func ReadUInt32N(r io.Reader) (UInt32, int64) {
b, n, err := ReadUInt32Safe(r)
2014-07-01 14:50:24 -07:00
if err != nil {
panic(err)
2014-07-01 14:50:24 -07:00
}
return b, n
}
2014-07-01 14:50:24 -07:00
func ReadUInt32(r io.Reader) UInt32 {
b, _, err := ReadUInt32Safe(r)
2014-07-01 14:50:24 -07:00
if err != nil {
panic(err)
}
return b
2014-05-22 18:08:49 -07:00
}
// Int64
2014-08-10 16:35:08 -07:00
func (self Int64) Equals(other interface{}) bool {
2014-07-01 14:50:24 -07:00
return self == other
}
2014-08-10 16:35:08 -07:00
func (self Int64) Less(other interface{}) bool {
2014-07-01 14:50:24 -07:00
if o, ok := other.(Int64); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
2014-05-22 18:08:49 -07:00
func (self Int64) ByteSize() int {
2014-07-01 14:50:24 -07:00
return 8
}
2014-06-03 17:03:29 -07:00
func (self Int64) WriteTo(w io.Writer) (int64, error) {
2014-07-03 13:44:19 -07:00
buf := []byte{0, 0, 0, 0, 0, 0, 0, 0}
binary.LittleEndian.PutUint64(buf, uint64(self))
n, err := w.Write(buf)
return int64(n), err
2014-05-22 18:08:49 -07:00
}
func ReadInt64Safe(r io.Reader) (Int64, int64, error) {
2014-07-01 14:50:24 -07:00
buf := [8]byte{0}
n, err := io.ReadFull(r, buf[:])
if err != nil {
return Int64(0), int64(n), err
}
return Int64(binary.LittleEndian.Uint64(buf[:])), int64(n), nil
}
func ReadInt64N(r io.Reader) (Int64, int64) {
b, n, err := ReadInt64Safe(r)
2014-07-01 14:50:24 -07:00
if err != nil {
panic(err)
2014-07-01 14:50:24 -07:00
}
return b, n
}
2014-07-01 14:50:24 -07:00
func ReadInt64(r io.Reader) Int64 {
b, _, err := ReadInt64Safe(r)
2014-07-01 14:50:24 -07:00
if err != nil {
panic(err)
}
return b
2014-05-22 18:08:49 -07:00
}
// UInt64
2014-08-10 16:35:08 -07:00
func (self UInt64) Equals(other interface{}) bool {
2014-07-01 14:50:24 -07:00
return self == other
}
2014-08-10 16:35:08 -07:00
func (self UInt64) Less(other interface{}) bool {
2014-07-01 14:50:24 -07:00
if o, ok := other.(UInt64); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
2014-05-22 18:08:49 -07:00
func (self UInt64) ByteSize() int {
2014-07-01 14:50:24 -07:00
return 8
2014-05-22 18:08:49 -07:00
}
2014-06-03 17:03:29 -07:00
func (self UInt64) WriteTo(w io.Writer) (int64, error) {
2014-07-03 13:44:19 -07:00
buf := []byte{0, 0, 0, 0, 0, 0, 0, 0}
binary.LittleEndian.PutUint64(buf, uint64(self))
n, err := w.Write(buf)
return int64(n), err
2014-05-22 18:08:49 -07:00
}
func ReadUInt64Safe(r io.Reader) (UInt64, int64, error) {
2014-07-01 14:50:24 -07:00
buf := [8]byte{0}
n, err := io.ReadFull(r, buf[:])
2014-07-01 14:50:24 -07:00
if err != nil {
return UInt64(0), int64(n), err
2014-07-01 14:50:24 -07:00
}
return UInt64(binary.LittleEndian.Uint64(buf[:])), int64(n), nil
}
func ReadUInt64N(r io.Reader) (UInt64, int64) {
b, n, err := ReadUInt64Safe(r)
2014-07-01 14:50:24 -07:00
if err != nil {
panic(err)
}
return b, n
}
func ReadUInt64(r io.Reader) UInt64 {
b, _, err := ReadUInt64Safe(r)
2014-07-01 14:50:24 -07:00
if err != nil {
panic(err)
}
return b
}