tendermint/binary/string.go

40 lines
795 B
Go
Raw Normal View History

2014-06-04 01:39:50 -07:00
package binary
2014-06-03 17:03:29 -07:00
import "io"
type String string
2014-05-22 18:08:49 -07:00
// String
2014-05-23 17:49:28 -07:00
func (self String) Equals(other Binary) bool {
return self == other
}
2014-06-04 01:39:50 -07:00
func (self String) Less(other Binary) bool {
if o, ok := other.(String); ok {
return self < o
} else {
2014-05-23 17:49:28 -07:00
panic("Cannot compare unequal types")
}
}
2014-05-22 18:08:49 -07:00
func (self String) ByteSize() int {
return len(self)+4
}
2014-06-03 17:03:29 -07:00
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
}
2014-06-05 02:33:50 -07:00
func ReadString(r io.Reader) String {
length := int(ReadUInt32(r))
bytes := make([]byte, length)
_, err := io.ReadFull(r, bytes)
if err != nil { panic(err) }
return String(bytes)
2014-05-22 18:08:49 -07:00
}