OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / syndtr / goleveldb / leveldb / comparer / bytes_comparer.go
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
7 package comparer
8
9 import "bytes"
10
11 type bytesComparer struct{}
12
13 func (bytesComparer) Compare(a, b []byte) int {
14         return bytes.Compare(a, b)
15 }
16
17 func (bytesComparer) Name() string {
18         return "leveldb.BytewiseComparator"
19 }
20
21 func (bytesComparer) Separator(dst, a, b []byte) []byte {
22         i, n := 0, len(a)
23         if n > len(b) {
24                 n = len(b)
25         }
26         for ; i < n && a[i] == b[i]; i++ {
27         }
28         if i >= n {
29                 // Do not shorten if one string is a prefix of the other
30         } else if c := a[i]; c < 0xff && c+1 < b[i] {
31                 dst = append(dst, a[:i+1]...)
32                 dst[i]++
33                 return dst
34         }
35         return nil
36 }
37
38 func (bytesComparer) Successor(dst, b []byte) []byte {
39         for i, c := range b {
40                 if c != 0xff {
41                         dst = append(dst, b[:i+1]...)
42                         dst[i]++
43                         return dst
44                 }
45         }
46         return nil
47 }
48
49 // DefaultComparer are default implementation of the Comparer interface.
50 // It uses the natural ordering, consistent with bytes.Compare.
51 var DefaultComparer = bytesComparer{}