From 100e97d77299d3c7f1fb8f062a9e2872aca6c75a Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Sun, 18 Jul 2021 15:55:37 +0300 Subject: [PATCH] util: move ArrayReverse into package of its own Leave just uint160 and uint256 types in util. --- pkg/encoding/bigint/bigint.go | 4 ++-- pkg/encoding/bigint/bigint_test.go | 4 ++-- pkg/util/array.go | 10 ---------- pkg/util/slice/array.go | 14 ++++++++++++++ pkg/util/{ => slice}/array_test.go | 6 +++--- pkg/util/uint160.go | 3 ++- pkg/util/uint256.go | 5 +++-- pkg/vm/cli/cli.go | 3 ++- 8 files changed, 28 insertions(+), 21 deletions(-) delete mode 100644 pkg/util/array.go create mode 100644 pkg/util/slice/array.go rename pkg/util/{ => slice}/array_test.go (88%) diff --git a/pkg/encoding/bigint/bigint.go b/pkg/encoding/bigint/bigint.go index aa189ca0a..b2ab067b0 100644 --- a/pkg/encoding/bigint/bigint.go +++ b/pkg/encoding/bigint/bigint.go @@ -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) } diff --git a/pkg/encoding/bigint/bigint_test.go b/pkg/encoding/bigint/bigint_test.go index 287582670..79c277546 100644 --- a/pkg/encoding/bigint/bigint_test.go +++ b/pkg/encoding/bigint/bigint_test.go @@ -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) } } diff --git a/pkg/util/array.go b/pkg/util/array.go deleted file mode 100644 index 4fd4589ed..000000000 --- a/pkg/util/array.go +++ /dev/null @@ -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 -} diff --git a/pkg/util/slice/array.go b/pkg/util/slice/array.go new file mode 100644 index 000000000..50e3f5d6c --- /dev/null +++ b/pkg/util/slice/array.go @@ -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 +} diff --git a/pkg/util/array_test.go b/pkg/util/slice/array_test.go similarity index 88% rename from pkg/util/array_test.go rename to pkg/util/slice/array_test.go index 4f44ac5df..7ba88dec9 100644 --- a/pkg/util/array_test.go +++ b/pkg/util/slice/array_test.go @@ -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 diff --git a/pkg/util/uint160.go b/pkg/util/uint160.go index 0eddf3fc2..991183e0f 100644 --- a/pkg/util/uint160.go +++ b/pkg/util/uint160.go @@ -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. diff --git a/pkg/util/uint256.go b/pkg/util/uint256.go index ff764d4da..f3560b126 100644 --- a/pkg/util/uint256.go +++ b/pkg/util/uint256.go @@ -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. diff --git a/pkg/vm/cli/cli.go b/pkg/vm/cli/cli.go index 48dcc5b8f..cdc903f76 100644 --- a/pkg/vm/cli/cli.go +++ b/pkg/vm/cli/cli.go @@ -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))