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 {
|
2014-07-01 14:50:24 -07:00
|
|
|
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-07-01 14:50:24 -07:00
|
|
|
if o, ok := other.(String); ok {
|
|
|
|
return self < o
|
|
|
|
} else {
|
|
|
|
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 {
|
2014-07-01 14:50:24 -07:00
|
|
|
return len(self) + 4
|
2014-05-22 18:08:49 -07:00
|
|
|
}
|
|
|
|
|
2014-06-03 17:03:29 -07:00
|
|
|
func (self String) WriteTo(w io.Writer) (n int64, err error) {
|
2014-07-01 14:50:24 -07:00
|
|
|
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-30 16:53:04 -07:00
|
|
|
func ReadStringSafe(r io.Reader) (String, error) {
|
2014-07-01 14:50:24 -07:00
|
|
|
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
|
2014-06-30 16:53:04 -07:00
|
|
|
}
|
|
|
|
|
2014-06-05 02:33:50 -07:00
|
|
|
func ReadString(r io.Reader) String {
|
2014-07-01 14:50:24 -07:00
|
|
|
str, err := ReadStringSafe(r)
|
2014-07-08 15:33:26 -07:00
|
|
|
if err != nil {
|
2014-07-01 14:50:24 -07:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return str
|
2014-05-22 18:08:49 -07:00
|
|
|
}
|