tendermint/merkle/string.go
2014-05-29 22:02:36 -07:00

74 lines
1.7 KiB
Go

package merkle
import "bytes"
type String string
type ByteSlice []byte
// String
func (self String) Equals(other Binary) bool {
return self == other
}
func (self String) Less(other Key) 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(buf []byte) int {
if len(buf) < self.ByteSize() { panic("buf too small") }
UInt32(len(self)).WriteTo(buf)
copy(buf[4:], []byte(self))
return len(self)+4
}
// 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 Key) 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(buf []byte) int {
if len(buf) < self.ByteSize() { panic("buf too small") }
UInt32(len(self)).WriteTo(buf)
copy(buf[4:], self)
return len(self)+4
}
// 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
}