mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-25 23:42:23 +00:00
slice: add Reverse function, deduplicate code a bit
This commit is contained in:
parent
100e97d772
commit
a54e3516d1
4 changed files with 21 additions and 16 deletions
|
@ -16,6 +16,7 @@ import (
|
|||
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
||||
"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/slice"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||
)
|
||||
|
||||
|
@ -232,7 +233,7 @@ func (s *Std) itoa(_ *interop.Context, args []stackitem.Item) stackitem.Item {
|
|||
break
|
||||
}
|
||||
bs := bigint.ToBytes(num)
|
||||
reverse(bs)
|
||||
slice.Reverse(bs)
|
||||
str = hex.EncodeToString(bs)
|
||||
if pad := bs[0] & 0xF8; pad == 0 || pad == 0xF8 {
|
||||
str = str[1:]
|
||||
|
@ -280,7 +281,7 @@ func (s *Std) atoi(_ *interop.Context, args []stackitem.Item) stackitem.Item {
|
|||
if changed && bs[0]&0x8 != 0 {
|
||||
bs[0] |= 0xF0
|
||||
}
|
||||
reverse(bs)
|
||||
slice.Reverse(bs)
|
||||
bi = bigint.FromBytes(bs)
|
||||
default:
|
||||
panic(ErrInvalidBase)
|
||||
|
@ -289,13 +290,6 @@ func (s *Std) atoi(_ *interop.Context, args []stackitem.Item) stackitem.Item {
|
|||
return stackitem.NewBigInteger(bi)
|
||||
}
|
||||
|
||||
func reverse(b []byte) {
|
||||
l := len(b)
|
||||
for i := 0; i < l/2; i++ {
|
||||
b[i], b[l-i-1] = b[l-i-1], b[i]
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Std) base64Encode(_ *interop.Context, args []stackitem.Item) stackitem.Item {
|
||||
src := s.toLimitedBytes(args[0])
|
||||
result := base64.StdEncoding.EncodeToString(src)
|
||||
|
|
|
@ -7,8 +7,17 @@ package slice
|
|||
// 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]
|
||||
}
|
||||
reverse(dest, b)
|
||||
return dest
|
||||
}
|
||||
|
||||
// Reverse does in-place reversing of byte slice.
|
||||
func Reverse(b []byte) {
|
||||
reverse(b, b)
|
||||
}
|
||||
|
||||
func reverse(dst []byte, src []byte) {
|
||||
for i, j := 0, len(src)-1; i <= j; i, j = i+1, j-1 {
|
||||
dst[i], dst[j] = src[j], src[i]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,5 +41,8 @@ func TestCopyReverse(t *testing.T) {
|
|||
have[i] = ^have[i]
|
||||
}
|
||||
require.Equal(t, tc.arr, arg)
|
||||
|
||||
Reverse(arg)
|
||||
require.Equal(t, tc.rev, arg)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ import (
|
|||
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
|
||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
||||
"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/opcode"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||
)
|
||||
|
@ -1167,10 +1168,8 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
|
|||
a[i], a[j] = a[j], a[i]
|
||||
}
|
||||
case *stackitem.Buffer:
|
||||
slice := t.Value().([]byte)
|
||||
for i, j := 0, t.Len()-1; i < j; i, j = i+1, j-1 {
|
||||
slice[i], slice[j] = slice[j], slice[i]
|
||||
}
|
||||
b := t.Value().([]byte)
|
||||
slice.Reverse(b)
|
||||
default:
|
||||
panic(fmt.Sprintf("invalid item type %s", t))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue