本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.1KB

  1. // Copyright (c) 2012, 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 comparer
  7. import "bytes"
  8. type bytesComparer struct{}
  9. func (bytesComparer) Compare(a, b []byte) int {
  10. return bytes.Compare(a, b)
  11. }
  12. func (bytesComparer) Name() string {
  13. return "leveldb.BytewiseComparator"
  14. }
  15. func (bytesComparer) Separator(dst, a, b []byte) []byte {
  16. i, n := 0, len(a)
  17. if n > len(b) {
  18. n = len(b)
  19. }
  20. for ; i < n && a[i] == b[i]; i++ {
  21. }
  22. if i >= n {
  23. // Do not shorten if one string is a prefix of the other
  24. } else if c := a[i]; c < 0xff && c+1 < b[i] {
  25. dst = append(dst, a[:i+1]...)
  26. dst[len(dst)-1]++
  27. return dst
  28. }
  29. return nil
  30. }
  31. func (bytesComparer) Successor(dst, b []byte) []byte {
  32. for i, c := range b {
  33. if c != 0xff {
  34. dst = append(dst, b[:i+1]...)
  35. dst[len(dst)-1]++
  36. return dst
  37. }
  38. }
  39. return nil
  40. }
  41. // DefaultComparer are default implementation of the Comparer interface.
  42. // It uses the natural ordering, consistent with bytes.Compare.
  43. var DefaultComparer = bytesComparer{}
上海开阖软件有限公司 沪ICP备12045867号-1