*: use slices.Concat where appropriate

Signed-off-by: Roman Khimov <roman@nspcc.ru>
This commit is contained in:
Roman Khimov 2024-08-24 13:07:53 +03:00
parent d5b7fc54e7
commit 357bc76882
6 changed files with 16 additions and 12 deletions

View file

@ -4,6 +4,7 @@ import (
"io" "io"
"math/big" "math/big"
"path/filepath" "path/filepath"
"slices"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
@ -133,8 +134,7 @@ func TestNEP17Transfer(t *testing.T) {
} }
t.Run("missing receiver", func(t *testing.T) { t.Run("missing receiver", func(t *testing.T) {
as := append([]string{}, args[:8]...) as := slices.Concat(args[:8], args[10:])
as = append(as, args[10:]...)
e.In.WriteString("one\r") e.In.WriteString("one\r")
e.RunWithErrorCheck(t, `Required flag "to" not set`, as...) e.RunWithErrorCheck(t, `Required flag "to" not set`, as...)
e.In.Reset() e.In.Reset()

View file

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"slices"
"github.com/nspcc-dev/neo-go/pkg/core/interop" "github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/core/storage"
@ -61,7 +62,7 @@ func (s *Iterator) Value() stackitem.Item {
} }
key := s.curr.Key key := s.curr.Key
if s.opts&FindRemovePrefix == 0 { if s.opts&FindRemovePrefix == 0 {
key = append(bytes.Clone(s.prefix), key...) key = slices.Concat(s.prefix, key)
} }
if s.opts&FindKeysOnly != 0 { if s.opts&FindKeysOnly != 0 {
return stackitem.NewByteArray(key) return stackitem.NewByteArray(key)

View file

@ -1,6 +1,10 @@
package mpt package mpt
import "github.com/nspcc-dev/neo-go/pkg/util" import (
"slices"
"github.com/nspcc-dev/neo-go/pkg/util"
)
// lcp returns the longest common prefix of a and b. // lcp returns the longest common prefix of a and b.
// Note: it does no allocations. // Note: it does no allocations.
@ -85,9 +89,7 @@ func GetChildrenPaths(path []byte, node Node) map[util.Uint256][][]byte {
} }
case *ExtensionNode: case *ExtensionNode:
if n.next.Type() == HashT { if n.next.Type() == HashT {
cPath := make([]byte, len(path)+len(n.key)) cPath := slices.Concat(path, n.key)
copy(cPath, path)
copy(cPath[len(path):], n.key)
res[n.next.Hash()] = [][]byte{cPath} res[n.next.Hash()] = [][]byte{cPath}
} }
default: default:

View file

@ -5,6 +5,7 @@ import (
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt" "fmt"
"slices"
"github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/core/storage"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
@ -616,7 +617,7 @@ func (t *Trie) Find(prefix, from []byte, maxNum int) ([]storage.KeyValue, error)
if leaf, ok := node.(*LeafNode); ok { if leaf, ok := node.(*LeafNode); ok {
if from == nil || !bytes.Equal(pathToNode, from) { // (*Billet).traverse includes `from` path into result if so. Need to filter out manually. if from == nil || !bytes.Equal(pathToNode, from) { // (*Billet).traverse includes `from` path into result if so. Need to filter out manually.
res = append(res, storage.KeyValue{ res = append(res, storage.KeyValue{
Key: append(bytes.Clone(prefix), pathToNode...), Key: slices.Concat(prefix, pathToNode),
Value: bytes.Clone(leaf.value), Value: bytes.Clone(leaf.value),
}) })
count++ count++

View file

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"slices"
"github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/core/storage"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
@ -98,7 +99,7 @@ func (m *TrieStore) Seek(rng storage.SeekRange, f func(k, v []byte) bool) {
if leaf, ok := node.(*LeafNode); ok { if leaf, ok := node.(*LeafNode); ok {
// (*Billet).traverse includes `from` path into the result if so. It's OK for Seek, so shouldn't be filtered out. // (*Billet).traverse includes `from` path into the result if so. It's OK for Seek, so shouldn't be filtered out.
kv := storage.KeyValue{ kv := storage.KeyValue{
Key: append(bytes.Clone(rng.Prefix), pathToNode...), // Do not cut prefix. Key: slices.Concat(rng.Prefix, pathToNode), // Do not cut prefix.
Value: bytes.Clone(leaf.value), Value: bytes.Clone(leaf.value),
} }
return !f(kv.Key, kv.Value) // Should return whether to stop. return !f(kv.Key, kv.Value) // Should return whether to stop.

View file

@ -3,6 +3,7 @@ package storage
import ( import (
"errors" "errors"
"fmt" "fmt"
"slices"
"github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig" "github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig"
"github.com/nspcc-dev/neo-go/pkg/core/storage/dboper" "github.com/nspcc-dev/neo-go/pkg/core/storage/dboper"
@ -105,10 +106,8 @@ type (
func seekRangeToPrefixes(sr SeekRange) *util.Range { func seekRangeToPrefixes(sr SeekRange) *util.Range {
var ( var (
rang *util.Range rang *util.Range
start = make([]byte, len(sr.Prefix)+len(sr.Start)) start = slices.Concat(sr.Prefix, sr.Start)
) )
copy(start, sr.Prefix)
copy(start[len(sr.Prefix):], sr.Start)
if !sr.Backwards { if !sr.Backwards {
rang = util.BytesPrefix(sr.Prefix) rang = util.BytesPrefix(sr.Prefix)