github.com/koko1123/flow-go-1@v0.29.6/utils/slices/slices.go (about) 1 package slices 2 3 import "sort" 4 5 // Concat concatenates multiple []byte into one []byte with efficient one-time allocation. 6 func Concat(slices [][]byte) []byte { 7 var totalLen int 8 for _, s := range slices { 9 totalLen += len(s) 10 } 11 tmp := make([]byte, totalLen) 12 var i int 13 for _, s := range slices { 14 i += copy(tmp[i:], s) 15 } 16 return tmp 17 } 18 19 // EnsureByteSliceSize returns a copy of input bytes with given length 20 // trimming or left-padding with zero bytes accordingly 21 func EnsureByteSliceSize(b []byte, length int) []byte { 22 if len(b) > length { 23 b = b[len(b)-length:] 24 } 25 var stateBytes = make([]byte, length) 26 copy(stateBytes[length-len(b):], b) 27 28 return stateBytes 29 } 30 31 // MakeRange returns a slice of int from [min, max] 32 func MakeRange(min, max int) []int { 33 a := make([]int, max-min+1) 34 for i := range a { 35 a[i] = min + i 36 } 37 return a 38 } 39 40 // AreStringSlicesEqual returns true if the two string slices are equal. 41 func AreStringSlicesEqual(a, b []string) bool { 42 if len(a) != len(b) { 43 return false 44 } 45 46 sort.Strings(a) 47 sort.Strings(b) 48 49 for i := 0; i < len(a); i++ { 50 if a[i] != b[i] { 51 return false 52 } 53 } 54 55 return true 56 } 57 58 // StringSliceContainsElement returns true if the string slice contains the element. 59 func StringSliceContainsElement(a []string, v string) bool { 60 for _, x := range a { 61 if x == v { 62 return true 63 } 64 } 65 66 return false 67 }