chore: (x/authz) add helpers AppendBytes, ParseByteSlice (#11713)
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/types/kv"
|
||||
dbm "github.com/tendermint/tm-db"
|
||||
)
|
||||
|
||||
@@ -106,3 +107,30 @@ func CopyBytes(bz []byte) (ret []byte) {
|
||||
copy(ret, bz)
|
||||
return ret
|
||||
}
|
||||
|
||||
// AppendLengthPrefixedBytes combines the slices of bytes to one slice of bytes.
|
||||
func AppendLengthPrefixedBytes(args ...[]byte) []byte {
|
||||
length := 0
|
||||
for _, v := range args {
|
||||
length += len(v)
|
||||
}
|
||||
res := make([]byte, length)
|
||||
|
||||
length = 0
|
||||
for _, v := range args {
|
||||
copy(res[length:length+len(v)], v)
|
||||
length += len(v)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
// ParseLengthPrefixedBytes panics when store key length is not equal to the given length.
|
||||
func ParseLengthPrefixedBytes(key []byte, startIndex int, sliceLength int) ([]byte, int) {
|
||||
neededLength := startIndex + sliceLength
|
||||
endIndex := neededLength - 1
|
||||
kv.AssertKeyAtLeastLength(key, neededLength)
|
||||
byteSlice := key[startIndex:neededLength]
|
||||
|
||||
return byteSlice, endIndex
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user