2014-06-04 01:39:50 -07:00
|
|
|
package binary
|
2014-05-19 20:46:41 -07:00
|
|
|
|
2014-06-03 17:03:29 -07:00
|
|
|
import "io"
|
2014-05-19 20:46:41 -07:00
|
|
|
|
|
|
|
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-05-19 20:46:41 -07:00
|
|
|
}
|
|
|
|
|
2014-06-04 01:39:50 -07:00
|
|
|
func (self String) Less(other Binary) bool {
|
2014-05-19 20:46:41 -07:00
|
|
|
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-19 20:46:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05-19 20:46:41 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|