forked from TrueCloudLab/neoneo-go
io: remove ReadLE/BE and WriteLE/BE
It was replaced with faster specialized versions earlier.
This commit is contained in:
parent
07b88796fb
commit
8a2e1c3d0a
3 changed files with 14 additions and 87 deletions
|
@ -38,15 +38,6 @@ func NewBinReaderFromBuf(b []byte) *BinReader {
|
||||||
return NewBinReaderFromIO(r)
|
return NewBinReaderFromIO(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadLE reads from the underlying io.Reader
|
|
||||||
// into the interface v in little-endian format.
|
|
||||||
func (r *BinReader) ReadLE(v interface{}) {
|
|
||||||
if r.Err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
r.Err = binary.Read(r.r, binary.LittleEndian, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadU64LE reads a little-endian encoded uint64 value from the underlying
|
// ReadU64LE reads a little-endian encoded uint64 value from the underlying
|
||||||
// io.Reader. On read failures it returns zero.
|
// io.Reader. On read failures it returns zero.
|
||||||
func (r *BinReader) ReadU64LE() uint64 {
|
func (r *BinReader) ReadU64LE() uint64 {
|
||||||
|
@ -153,15 +144,6 @@ func (r *BinReader) ReadArray(t interface{}, maxSize ...int) {
|
||||||
value.Elem().Set(arr)
|
value.Elem().Set(arr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadBE reads from the underlying io.Reader
|
|
||||||
// into the interface v in big-endian format.
|
|
||||||
func (r *BinReader) ReadBE(v interface{}) {
|
|
||||||
if r.Err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
r.Err = binary.Read(r.r, binary.BigEndian, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadVarUint reads a variable-length-encoded integer from the
|
// ReadVarUint reads a variable-length-encoded integer from the
|
||||||
// underlying reader.
|
// underlying reader.
|
||||||
func (r *BinReader) ReadVarUint() uint64 {
|
func (r *BinReader) ReadVarUint() uint64 {
|
||||||
|
|
|
@ -27,22 +27,6 @@ func NewBinWriterFromIO(iow io.Writer) *BinWriter {
|
||||||
return &BinWriter{w: iow, u64: u64, u32: u32, u16: u16, u8: u8}
|
return &BinWriter{w: iow, u64: u64, u32: u32, u16: u16, u8: u8}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteLE writes into the underlying io.Writer from an object v in little-endian format.
|
|
||||||
func (w *BinWriter) WriteLE(v interface{}) {
|
|
||||||
if w.Err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Err = binary.Write(w.w, binary.LittleEndian, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteBE writes into the underlying io.Writer from an object v in big-endian format.
|
|
||||||
func (w *BinWriter) WriteBE(v interface{}) {
|
|
||||||
if w.Err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Err = binary.Write(w.w, binary.BigEndian, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteU64LE writes an uint64 value into the underlying io.Writer in
|
// WriteU64LE writes an uint64 value into the underlying io.Writer in
|
||||||
// little-endian format.
|
// little-endian format.
|
||||||
func (w *BinWriter) WriteU64LE(u64 uint64) {
|
func (w *BinWriter) WriteU64LE(u64 uint64) {
|
||||||
|
|
|
@ -19,40 +19,6 @@ func (w *badRW) Read(p []byte) (int, error) {
|
||||||
return w.Write(p)
|
return w.Write(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWriteLE(t *testing.T) {
|
|
||||||
var (
|
|
||||||
val uint32 = 0xdeadbeef
|
|
||||||
readval uint32
|
|
||||||
bin = []byte{0xef, 0xbe, 0xad, 0xde}
|
|
||||||
)
|
|
||||||
bw := NewBufBinWriter()
|
|
||||||
bw.WriteLE(val)
|
|
||||||
assert.Nil(t, bw.Err)
|
|
||||||
wrotebin := bw.Bytes()
|
|
||||||
assert.Equal(t, wrotebin, bin)
|
|
||||||
br := NewBinReaderFromBuf(bin)
|
|
||||||
br.ReadLE(&readval)
|
|
||||||
assert.Nil(t, br.Err)
|
|
||||||
assert.Equal(t, val, readval)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWriteBE(t *testing.T) {
|
|
||||||
var (
|
|
||||||
val uint32 = 0xdeadbeef
|
|
||||||
readval uint32
|
|
||||||
bin = []byte{0xde, 0xad, 0xbe, 0xef}
|
|
||||||
)
|
|
||||||
bw := NewBufBinWriter()
|
|
||||||
bw.WriteBE(val)
|
|
||||||
assert.Nil(t, bw.Err)
|
|
||||||
wrotebin := bw.Bytes()
|
|
||||||
assert.Equal(t, wrotebin, bin)
|
|
||||||
br := NewBinReaderFromBuf(bin)
|
|
||||||
br.ReadBE(&readval)
|
|
||||||
assert.Nil(t, br.Err)
|
|
||||||
assert.Equal(t, val, readval)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWriteU64LE(t *testing.T) {
|
func TestWriteU64LE(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
val uint64 = 0xbadc0de15a11dead
|
val uint64 = 0xbadc0de15a11dead
|
||||||
|
@ -173,18 +139,18 @@ func TestReadLEErrors(t *testing.T) {
|
||||||
func TestBufBinWriter_Len(t *testing.T) {
|
func TestBufBinWriter_Len(t *testing.T) {
|
||||||
val := []byte{0xde}
|
val := []byte{0xde}
|
||||||
bw := NewBufBinWriter()
|
bw := NewBufBinWriter()
|
||||||
bw.WriteLE(val)
|
bw.WriteBytes(val)
|
||||||
require.Equal(t, 1, bw.Len())
|
require.Equal(t, 1, bw.Len())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWriterErrHandling(t *testing.T) {
|
func TestWriterErrHandling(t *testing.T) {
|
||||||
var badio = &badRW{}
|
var badio = &badRW{}
|
||||||
bw := NewBinWriterFromIO(badio)
|
bw := NewBinWriterFromIO(badio)
|
||||||
bw.WriteLE(uint32(0))
|
bw.WriteU32LE(uint32(0))
|
||||||
assert.NotNil(t, bw.Err)
|
assert.NotNil(t, bw.Err)
|
||||||
// these should work (without panic), preserving the Err
|
// these should work (without panic), preserving the Err
|
||||||
bw.WriteLE(uint32(0))
|
bw.WriteU32LE(uint32(0))
|
||||||
bw.WriteBE(uint32(0))
|
bw.WriteU16BE(uint16(0))
|
||||||
bw.WriteVarUint(0)
|
bw.WriteVarUint(0)
|
||||||
bw.WriteVarBytes([]byte{0x55, 0xaa})
|
bw.WriteVarBytes([]byte{0x55, 0xaa})
|
||||||
bw.WriteString("neo")
|
bw.WriteString("neo")
|
||||||
|
@ -193,19 +159,14 @@ func TestWriterErrHandling(t *testing.T) {
|
||||||
|
|
||||||
func TestReaderErrHandling(t *testing.T) {
|
func TestReaderErrHandling(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
i uint32 = 0xdeadbeef
|
badio = &badRW{}
|
||||||
iorig = i
|
|
||||||
badio = &badRW{}
|
|
||||||
)
|
)
|
||||||
br := NewBinReaderFromIO(badio)
|
br := NewBinReaderFromIO(badio)
|
||||||
br.ReadLE(&i)
|
br.ReadU32LE()
|
||||||
assert.NotNil(t, br.Err)
|
assert.NotNil(t, br.Err)
|
||||||
// i shouldn't change
|
|
||||||
assert.Equal(t, i, iorig)
|
|
||||||
// these should work (without panic), preserving the Err
|
// these should work (without panic), preserving the Err
|
||||||
br.ReadLE(&i)
|
br.ReadU32LE()
|
||||||
br.ReadBE(&i)
|
br.ReadU16BE()
|
||||||
assert.Equal(t, i, iorig)
|
|
||||||
val := br.ReadVarUint()
|
val := br.ReadVarUint()
|
||||||
assert.Equal(t, val, uint64(0))
|
assert.Equal(t, val, uint64(0))
|
||||||
b := br.ReadVarBytes()
|
b := br.ReadVarBytes()
|
||||||
|
@ -217,7 +178,7 @@ func TestReaderErrHandling(t *testing.T) {
|
||||||
|
|
||||||
func TestBufBinWriterErr(t *testing.T) {
|
func TestBufBinWriterErr(t *testing.T) {
|
||||||
bw := NewBufBinWriter()
|
bw := NewBufBinWriter()
|
||||||
bw.WriteLE(uint32(0))
|
bw.WriteU32LE(uint32(0))
|
||||||
assert.Nil(t, bw.Err)
|
assert.Nil(t, bw.Err)
|
||||||
// inject error
|
// inject error
|
||||||
bw.Err = errors.New("oopsie")
|
bw.Err = errors.New("oopsie")
|
||||||
|
@ -229,7 +190,7 @@ func TestBufBinWriterErr(t *testing.T) {
|
||||||
func TestBufBinWriterReset(t *testing.T) {
|
func TestBufBinWriterReset(t *testing.T) {
|
||||||
bw := NewBufBinWriter()
|
bw := NewBufBinWriter()
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 3; i++ {
|
||||||
bw.WriteLE(uint32(i))
|
bw.WriteU32LE(uint32(i))
|
||||||
assert.Nil(t, bw.Err)
|
assert.Nil(t, bw.Err)
|
||||||
_ = bw.Bytes()
|
_ = bw.Bytes()
|
||||||
assert.NotNil(t, bw.Err)
|
assert.NotNil(t, bw.Err)
|
||||||
|
@ -338,24 +299,24 @@ type testSerializable uint16
|
||||||
|
|
||||||
// EncodeBinary implements io.Serializable interface.
|
// EncodeBinary implements io.Serializable interface.
|
||||||
func (t testSerializable) EncodeBinary(w *BinWriter) {
|
func (t testSerializable) EncodeBinary(w *BinWriter) {
|
||||||
w.WriteLE(t)
|
w.WriteU16LE(uint16(t))
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecodeBinary implements io.Serializable interface.
|
// DecodeBinary implements io.Serializable interface.
|
||||||
func (t *testSerializable) DecodeBinary(r *BinReader) {
|
func (t *testSerializable) DecodeBinary(r *BinReader) {
|
||||||
r.ReadLE(t)
|
*t = testSerializable(r.ReadU16LE())
|
||||||
}
|
}
|
||||||
|
|
||||||
type testPtrSerializable uint16
|
type testPtrSerializable uint16
|
||||||
|
|
||||||
// EncodeBinary implements io.Serializable interface.
|
// EncodeBinary implements io.Serializable interface.
|
||||||
func (t *testPtrSerializable) EncodeBinary(w *BinWriter) {
|
func (t *testPtrSerializable) EncodeBinary(w *BinWriter) {
|
||||||
w.WriteLE(t)
|
w.WriteU16LE(uint16(*t))
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecodeBinary implements io.Serializable interface.
|
// DecodeBinary implements io.Serializable interface.
|
||||||
func (t *testPtrSerializable) DecodeBinary(r *BinReader) {
|
func (t *testPtrSerializable) DecodeBinary(r *BinReader) {
|
||||||
r.ReadLE(t)
|
*t = testPtrSerializable(r.ReadU16LE())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBinWriter_WriteArray(t *testing.T) {
|
func TestBinWriter_WriteArray(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue