github.com/cs3org/reva/v2@v2.27.7/pkg/store/etcd/utils.go (about) 1 package etcd 2 3 import ( 4 "strings" 5 ) 6 7 // Returns true if the limit isn't 0 AND is greater or equal to the number 8 // of results. 9 // If the limit is 0 or the number of items is less than the number of items, 10 // it will return false 11 func shouldFinish(numberOfResults, limit int64) bool { 12 if limit == 0 || numberOfResults < limit { 13 return false 14 } 15 return true 16 } 17 18 // Return the first key out of the prefix represented by the parameter, 19 // as a byte sequence. Note that it applies to byte sequences and not 20 // rune sequences, so it might be ill-suited for multi-byte chars 21 func firstKeyOutOfPrefix(src []byte) []byte { 22 dst := make([]byte, len(src)) 23 copy(dst, src) 24 var i int 25 for i = len(dst) - 1; i >= 0; i-- { 26 if dst[i] < 255 { 27 dst[i]++ 28 break 29 } 30 } 31 return dst[:i+1] 32 } 33 34 // Return the first key out of the prefix represented by the parameter. 35 // This function relies on the firstKeyOutOfPrefix one, which uses a byte 36 // sequence, so it might be ill-suited if the string contains multi-byte chars. 37 func firstKeyOutOfPrefixString(src string) string { 38 srcBytes := []byte(src) 39 dstBytes := firstKeyOutOfPrefix(srcBytes) 40 return string(dstBytes) 41 } 42 43 // Reverse the string based on the containing runes 44 func reverseString(s string) string { 45 r := []rune(s) 46 for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 { 47 r[i], r[j] = r[j], r[i] 48 } 49 return string(r) 50 } 51 52 // Build a string based on the parts, to be used as a prefix. Empty string is 53 // expected if no part is passed as parameter. 54 // The string will contain all the parts separated by '/'. The last char will 55 // also be '/' 56 // 57 // For example `buildPrefix(P1, P2, P3)` will return "P1/P2/P3/" 58 func buildPrefix(parts ...string) string { 59 var b strings.Builder 60 for _, part := range parts { 61 b.WriteString(part) 62 b.WriteRune('/') 63 } 64 return b.String() 65 }