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 7package comparer 8 9import "bytes" 10 11type bytesComparer struct{} 12 13func (bytesComparer) Compare(a, b []byte) int { 14 return bytes.Compare(a, b) 15} 16 17func (bytesComparer) Name() string { 18 return "leveldb.BytewiseComparator" 19} 20 21func (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[len(dst)-1]++ 33 return dst 34 } 35 return nil 36} 37 38func (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[len(dst)-1]++ 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. 51var DefaultComparer = bytesComparer{} 52