util: move ArrayReverse into package of its own
Leave just uint160 and uint256 types in util.
This commit is contained in:
parent
588f3fbbd3
commit
100e97d772
8 changed files with 28 additions and 21 deletions
|
@ -5,7 +5,7 @@ import (
|
|||
"math/big"
|
||||
"math/bits"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -17,7 +17,7 @@ const (
|
|||
|
||||
// FromBytesUnsigned converts data in little-endian format to an unsigned integer.
|
||||
func FromBytesUnsigned(data []byte) *big.Int {
|
||||
bs := util.ArrayReverse(data)
|
||||
bs := slice.CopyReverse(data)
|
||||
return new(big.Int).SetBytes(bs)
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
"math/big"
|
||||
"testing"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
@ -204,7 +204,7 @@ func TestVeryBigInts(t *testing.T) {
|
|||
num, ok := new(big.Int).SetString(tc.numStr, 10)
|
||||
assert.True(t, ok)
|
||||
|
||||
result := FromBytes(util.ArrayReverse(tc.buf))
|
||||
result := FromBytes(slice.CopyReverse(tc.buf))
|
||||
assert.Equal(t, num, result, "error while converting %s from bytes", tc.numStr)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
package util
|
||||
|
||||
// ArrayReverse returns a reversed version of the given byte slice.
|
||||
func ArrayReverse(b []byte) []byte {
|
||||
dest := make([]byte, len(b))
|
||||
for i, j := 0, len(b)-1; i <= j; i, j = i+1, j-1 {
|
||||
dest[i], dest[j] = b[j], b[i]
|
||||
}
|
||||
return dest
|
||||
}
|
14
pkg/util/slice/array.go
Normal file
14
pkg/util/slice/array.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
/*
|
||||
Package slice contains byte slice helpers.
|
||||
*/
|
||||
package slice
|
||||
|
||||
// CopyReverse returns a new byte slice containing reversed version of the
|
||||
// original.
|
||||
func CopyReverse(b []byte) []byte {
|
||||
dest := make([]byte, len(b))
|
||||
for i, j := 0, len(b)-1; i <= j; i, j = i+1, j-1 {
|
||||
dest[i], dest[j] = b[j], b[i]
|
||||
}
|
||||
return dest
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package util
|
||||
package slice
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
@ -28,12 +28,12 @@ var testCases = []struct {
|
|||
},
|
||||
}
|
||||
|
||||
func TestArrayReverse(t *testing.T) {
|
||||
func TestCopyReverse(t *testing.T) {
|
||||
for _, tc := range testCases {
|
||||
arg := make([]byte, len(tc.arr))
|
||||
copy(arg, tc.arr)
|
||||
|
||||
have := ArrayReverse(arg)
|
||||
have := CopyReverse(arg)
|
||||
require.Equal(t, tc.rev, have)
|
||||
|
||||
// test that argument was copied
|
|
@ -7,6 +7,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
||||
)
|
||||
|
||||
// Uint160Size is the size of Uint160 in bytes.
|
||||
|
@ -74,7 +75,7 @@ func (u Uint160) BytesBE() []byte {
|
|||
|
||||
// BytesLE returns a little-endian byte representation of u.
|
||||
func (u Uint160) BytesLE() []byte {
|
||||
return ArrayReverse(u.BytesBE())
|
||||
return slice.CopyReverse(u.BytesBE())
|
||||
}
|
||||
|
||||
// String implements the stringer interface.
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
||||
)
|
||||
|
||||
// Uint256Size is the size of Uint256 in bytes.
|
||||
|
@ -54,7 +55,7 @@ func Uint256DecodeBytesBE(b []byte) (u Uint256, err error) {
|
|||
|
||||
// Uint256DecodeBytesLE attempts to decode the given string (in LE representation) into an Uint256.
|
||||
func Uint256DecodeBytesLE(b []byte) (u Uint256, err error) {
|
||||
b = ArrayReverse(b)
|
||||
b = slice.CopyReverse(b)
|
||||
return Uint256DecodeBytesBE(b)
|
||||
}
|
||||
|
||||
|
@ -71,7 +72,7 @@ func (u Uint256) Reverse() Uint256 {
|
|||
|
||||
// BytesLE return a little-endian byte representation of u.
|
||||
func (u Uint256) BytesLE() []byte {
|
||||
return ArrayReverse(u.BytesBE())
|
||||
return slice.CopyReverse(u.BytesBE())
|
||||
}
|
||||
|
||||
// Equals returns true if both Uint256 values are the same.
|
||||
|
|
|
@ -21,6 +21,7 @@ import (
|
|||
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||
"gopkg.in/abiosoft/ishell.v2"
|
||||
|
@ -565,7 +566,7 @@ func Parse(args []string) (string, error) {
|
|||
}
|
||||
buf.WriteString(fmt.Sprintf("Hex to String\t%s\n", fmt.Sprintf("%q", string(rawStr))))
|
||||
buf.WriteString(fmt.Sprintf("Hex to Integer\t%s\n", bigint.FromBytes(rawStr)))
|
||||
buf.WriteString(fmt.Sprintf("Swap Endianness\t%s\n", hex.EncodeToString(util.ArrayReverse(rawStr))))
|
||||
buf.WriteString(fmt.Sprintf("Swap Endianness\t%s\n", hex.EncodeToString(slice.CopyReverse(rawStr))))
|
||||
}
|
||||
if addr, err := address.StringToUint160(arg); err == nil {
|
||||
buf.WriteString(fmt.Sprintf("Address to BE ScriptHash\t%s\n", addr))
|
||||
|
|
Loading…
Reference in a new issue