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-08-10 16:35:08 -07:00
|
|
|
func (self String) Equals(other interface{}) bool {
|
2014-07-01 14:50:24 -07:00
|
|
|
return self == other
|
2014-05-19 20:46:41 -07:00
|
|
|
}
|
|
|
|
|
2014-08-10 16:35:08 -07:00
|
|
|
func (self String) Less(other interface{}) 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-07-28 01:41:25 -07:00
|
|
|
func ReadStringSafe(r io.Reader) (str String, n int64, err error) {
|
|
|
|
length, n_, err := ReadUInt32Safe(r)
|
|
|
|
n += n_
|
2014-07-01 14:50:24 -07:00
|
|
|
if err != nil {
|
2014-07-28 01:41:25 -07:00
|
|
|
return "", n, err
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
bytes := make([]byte, int(length))
|
2014-07-28 01:41:25 -07:00
|
|
|
n__, err := io.ReadFull(r, bytes)
|
|
|
|
n += int64(n__)
|
2014-07-01 14:50:24 -07:00
|
|
|
if err != nil {
|
2014-07-28 01:41:25 -07:00
|
|
|
return "", n, err
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
2014-07-28 01:41:25 -07:00
|
|
|
return String(bytes), n, nil
|
2014-06-30 16:53:04 -07:00
|
|
|
}
|
|
|
|
|
2014-07-28 01:41:25 -07:00
|
|
|
func ReadStringN(r io.Reader) (str String, n int64) {
|
|
|
|
str, n, err := ReadStringSafe(r)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return str, n
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadString(r io.Reader) (str String) {
|
|
|
|
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
|
|
|
}
|