mirror of
https://github.com/tendermint/tendermint.git
synced 2026-05-30 10:56:20 +00:00
converting Binary struct model to native w/ global methods model
This commit is contained in:
@@ -2,67 +2,19 @@ package binary
|
||||
|
||||
import "io"
|
||||
|
||||
type String string
|
||||
|
||||
// String
|
||||
|
||||
func (self String) Equals(other interface{}) bool {
|
||||
return self == other
|
||||
func WriteString(w io.Writer, s string, n *int64, err *error) {
|
||||
WriteUInt32(w, uint32(len(s)), n, err)
|
||||
WriteTo(w, []byte(s), n, err)
|
||||
}
|
||||
|
||||
func (self String) Less(other interface{}) bool {
|
||||
if o, ok := other.(String); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
func ReadString(r io.Reader, n *int64, err *error) string {
|
||||
length := ReadUInt32(r, n, err)
|
||||
if *err != nil {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func (self String) ByteSize() int {
|
||||
return len(self) + 4
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func ReadStringSafe(r io.Reader) (str String, n int64, err error) {
|
||||
length, n_, err := ReadUInt32Safe(r)
|
||||
n += n_
|
||||
if err != nil {
|
||||
return "", n, err
|
||||
}
|
||||
bytes := make([]byte, int(length))
|
||||
n__, err := io.ReadFull(r, bytes)
|
||||
n += int64(n__)
|
||||
if err != nil {
|
||||
return "", n, err
|
||||
}
|
||||
return String(bytes), n, nil
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
func Readstring(r io.Reader) (str string) {
|
||||
return string(ReadString(r))
|
||||
buf := make([]byte, int(length))
|
||||
ReadFull(r, buf, n, err)
|
||||
return string(buf)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user