-
Notifications
You must be signed in to change notification settings - Fork 1
/
key.go
51 lines (41 loc) · 913 Bytes
/
key.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package trie
import "bytes"
type Key []byte
func (k Key) CommonBytesLen(b []byte) (int, bool) {
i, short := k.commonBytesLen(b, 0)
return i, !short
}
func (k Key) CommonStringLen(s string) (int, bool) {
i, short := k.commonStringLen(s, 0)
return i, !short
}
func (k Key) EqualToBytes(b []byte) bool {
return bytes.Equal(k, b)
}
func (k Key) EqualToString(s string) bool {
return string(k) == s // cool, this doesn't copy!
}
func (k Key) commonBytesLen(b []byte, depth int) (n int, short bool) {
if len(b) < len(k) {
k, short = k[:len(b)], true
}
for i, c := range k[depth:] {
i += depth
if b[i] != c {
return i, true
}
}
return len(k), short
}
func (k Key) commonStringLen(s string, depth int) (n int, short bool) {
if len(s) < len(k) {
k, short = k[:len(s)], true
}
for i, c := range k[depth:] {
i += depth
if s[i] != c {
return i, true
}
}
return len(k), short
}