forked from skull-squadron/bloomfilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextmarshaler.go
49 lines (42 loc) · 999 Bytes
/
textmarshaler.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
// Package bloomfilter is face-meltingly fast, thread-safe,
// marshalable, unionable, probability- and
// optimal-size-calculating Bloom filter in go
//
// https://github.com/steakknife/bloomfilter
//
// Copyright © 2014, 2015, 2018 Barry Allard
//
// MIT license
//
package bloomfilter
import "fmt"
// MarshalText conforms to encoding.TextMarshaler
func (f *Filter) MarshalText() (text []byte, err error) {
f.lock.RLock()
defer f.lock.RUnlock()
s := fmt.Sprintln("k")
s += fmt.Sprintln(f.K())
s += fmt.Sprintln("n")
s += fmt.Sprintln(f.n)
s += fmt.Sprintln("m")
s += fmt.Sprintln(f.m)
s += fmt.Sprintln("keys")
for key := range f.keys {
s += fmt.Sprintf(keyFormat, key) + nl()
}
s += fmt.Sprintln("bits")
for w := range f.bits {
s += fmt.Sprintf(bitsFormat, w) + nl()
}
_, hash, err := f.marshal()
if err != nil {
return nil, err
}
s += fmt.Sprintln("sha384")
for b := range hash {
s += fmt.Sprintf("%02x", b)
}
s += nl()
text = []byte(s)
return text, nil
}