range.go 766 B

1234567891011121314151617181920212223242526272829303132
  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. package util
  7. // Range is a key range.
  8. type Range struct {
  9. // Start of the key range, include in the range.
  10. Start []byte
  11. // Limit of the key range, not include in the range.
  12. Limit []byte
  13. }
  14. // BytesPrefix returns key range that satisfy the given prefix.
  15. // This only applicable for the standard 'bytes comparer'.
  16. func BytesPrefix(prefix []byte) *Range {
  17. var limit []byte
  18. for i := len(prefix) - 1; i >= 0; i-- {
  19. c := prefix[i]
  20. if c < 0xff {
  21. limit = make([]byte, i+1)
  22. copy(limit, prefix)
  23. limit[i] = c + 1
  24. break
  25. }
  26. }
  27. return &Range{prefix, limit}
  28. }