chore: (x/authz) add helpers AppendBytes, ParseByteSlice (#11713)

This commit is contained in:
atheeshp
2022-04-21 14:52:31 -04:00
committed by GitHub
parent 44c9180485
commit d4dd44469f
3 changed files with 66 additions and 31 deletions
+28
View File
@@ -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
}