1 // Copyright (c) 2014, Suryandaru Triandana <syndtr@gmail.com> 2 // All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style license that can be 5 // found in the LICENSE file. 6 7 package util 8 9 // Range is a key range. 10 type Range struct { 11 // Start of the key range, include in the range. 12 Start []byte 13 14 // Limit of the key range, not include in the range. 15 Limit []byte 16 } 17 18 // BytesPrefix returns key range that satisfy the given prefix. 19 // This only applicable for the standard 'bytes comparer'. 20 func BytesPrefix(prefix []byte) *Range { 21 var limit []byte 22 for i := len(prefix) - 1; i >= 0; i-- { 23 c := prefix[i] 24 if c < 0xff { 25 limit = make([]byte, i+1) 26 copy(limit, prefix) 27 limit[i] = c + 1 28 break 29 } 30 } 31 return &Range{prefix, limit} 32 } 33