Add PrefixRangeEnd to util package
@@ -39,6 +39,28 @@ func HasPrefix(key, prefix []byte) bool {
return bytes.HasPrefix(key, p)
}
+// Returns the bytes that mark the end of the key range for the given prefix.
+func PrefixRangeEnd(prefix []byte) []byte {
+ if prefix == nil {
+ return nil
+ }
+
+ end := make([]byte, len(prefix))
+ copy(end, prefix)
+
+ for {
+ if end[len(end)-1] != byte(255) {
+ end[len(end)-1]++
+ break
+ } else if len(end) == 1 {
+ end = nil
+ break
+ }
+ end = end[:len(end)-1]
+ }
+ return end
+}
+
func FileExists(filePath string) bool {
_, err := os.Stat(filePath)
return !os.IsNotExist(err)
