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"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
"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/smartcontract/manifest"
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
"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
|
break
|
||||||
}
|
}
|
||||||
bs := bigint.ToBytes(num)
|
bs := bigint.ToBytes(num)
|
||||||
reverse(bs)
|
slice.Reverse(bs)
|
||||||
str = hex.EncodeToString(bs)
|
str = hex.EncodeToString(bs)
|
||||||
if pad := bs[0] & 0xF8; pad == 0 || pad == 0xF8 {
|
if pad := bs[0] & 0xF8; pad == 0 || pad == 0xF8 {
|
||||||
str = str[1:]
|
str = str[1:]
|
||||||
|
@ -280,7 +281,7 @@ func (s *Std) atoi(_ *interop.Context, args []stackitem.Item) stackitem.Item {
|
||||||
if changed && bs[0]&0x8 != 0 {
|
if changed && bs[0]&0x8 != 0 {
|
||||||
bs[0] |= 0xF0
|
bs[0] |= 0xF0
|
||||||
}
|
}
|
||||||
reverse(bs)
|
slice.Reverse(bs)
|
||||||
bi = bigint.FromBytes(bs)
|
bi = bigint.FromBytes(bs)
|
||||||
default:
|
default:
|
||||||
panic(ErrInvalidBase)
|
panic(ErrInvalidBase)
|
||||||
|
@ -289,13 +290,6 @@ func (s *Std) atoi(_ *interop.Context, args []stackitem.Item) stackitem.Item {
|
||||||
return stackitem.NewBigInteger(bi)
|
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 {
|
func (s *Std) base64Encode(_ *interop.Context, args []stackitem.Item) stackitem.Item {
|
||||||
src := s.toLimitedBytes(args[0])
|
src := s.toLimitedBytes(args[0])
|
||||||
result := base64.StdEncoding.EncodeToString(src)
|
result := base64.StdEncoding.EncodeToString(src)
|
||||||
|
|
|
@ -7,8 +7,17 @@ package slice
|
||||||
// original.
|
// original.
|
||||||
func CopyReverse(b []byte) []byte {
|
func CopyReverse(b []byte) []byte {
|
||||||
dest := make([]byte, len(b))
|
dest := make([]byte, len(b))
|
||||||
for i, j := 0, len(b)-1; i <= j; i, j = i+1, j-1 {
|
reverse(dest, b)
|
||||||
dest[i], dest[j] = b[j], b[i]
|
|
||||||
}
|
|
||||||
return dest
|
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]
|
have[i] = ^have[i]
|
||||||
}
|
}
|
||||||
require.Equal(t, tc.arr, arg)
|
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/nef"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
"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"
|
||||||
|
"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/opcode"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
"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]
|
a[i], a[j] = a[j], a[i]
|
||||||
}
|
}
|
||||||
case *stackitem.Buffer:
|
case *stackitem.Buffer:
|
||||||
slice := t.Value().([]byte)
|
b := t.Value().([]byte)
|
||||||
for i, j := 0, t.Len()-1; i < j; i, j = i+1, j-1 {
|
slice.Reverse(b)
|
||||||
slice[i], slice[j] = slice[j], slice[i]
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("invalid item type %s", t))
|
panic(fmt.Sprintf("invalid item type %s", t))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue