mirror of
https://github.com/mjl-/mox.git
synced 2025-07-12 17:44:35 +03:00
Run modernize to rewrite some older go constructs to newer ones
Mostly using slice.Sort, using min/max, slices.Concat, range of int and fmt.Appendf for byte slices instead of strings.
This commit is contained in:
@ -70,14 +70,14 @@ func NewBloom(data []byte, k int) (*Bloom, error) {
|
||||
|
||||
func (b *Bloom) Add(s string) {
|
||||
h := hash([]byte(s), b.w)
|
||||
for i := 0; i < b.k; i++ {
|
||||
for range b.k {
|
||||
b.set(h.nextPos())
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bloom) Has(s string) bool {
|
||||
h := hash([]byte(s), b.w)
|
||||
for i := 0; i < b.k; i++ {
|
||||
for range b.k {
|
||||
if !b.has(h.nextPos()) {
|
||||
return false
|
||||
}
|
||||
@ -96,7 +96,7 @@ func (b *Bloom) Modified() bool {
|
||||
// Ones returns the number of ones.
|
||||
func (b *Bloom) Ones() (n int) {
|
||||
for _, d := range b.data {
|
||||
for i := 0; i < 8; i++ {
|
||||
for range 8 {
|
||||
if d&1 != 0 {
|
||||
n++
|
||||
}
|
||||
|
@ -62,26 +62,26 @@ func TestBloom(t *testing.T) {
|
||||
|
||||
func TestBits(t *testing.T) {
|
||||
b := &bits{width: 1, buf: []byte{0xff, 0xff}}
|
||||
for i := 0; i < 16; i++ {
|
||||
for range 16 {
|
||||
if b.nextPos() != 1 {
|
||||
t.Fatalf("pos not 1")
|
||||
}
|
||||
}
|
||||
b = &bits{width: 2, buf: []byte{0xff, 0xff}}
|
||||
for i := 0; i < 8; i++ {
|
||||
for range 8 {
|
||||
if b.nextPos() != 0b11 {
|
||||
t.Fatalf("pos not 0b11")
|
||||
}
|
||||
}
|
||||
|
||||
b = &bits{width: 1, buf: []byte{0b10101010, 0b10101010}}
|
||||
for i := 0; i < 16; i++ {
|
||||
for i := range 16 {
|
||||
if b.nextPos() != ((i + 1) % 2) {
|
||||
t.Fatalf("bad pos")
|
||||
}
|
||||
}
|
||||
b = &bits{width: 2, buf: []byte{0b10101010, 0b10101010}}
|
||||
for i := 0; i < 8; i++ {
|
||||
for range 8 {
|
||||
if b.nextPos() != 0b10 {
|
||||
t.Fatalf("pos not 0b10")
|
||||
}
|
||||
@ -97,7 +97,7 @@ func TestSet(t *testing.T) {
|
||||
0b01010101,
|
||||
},
|
||||
}
|
||||
for i := 0; i < 8; i++ {
|
||||
for i := range 8 {
|
||||
v := b.has(i)
|
||||
if v != (i%2 == 0) {
|
||||
t.Fatalf("bad has")
|
||||
|
@ -27,6 +27,7 @@ import (
|
||||
"github.com/mjl-/mox/message"
|
||||
"github.com/mjl-/mox/mlog"
|
||||
"github.com/mjl-/mox/moxvar"
|
||||
"slices"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -270,9 +271,7 @@ func (f *Filter) Save() error {
|
||||
words[i] = w
|
||||
i++
|
||||
}
|
||||
sort.Slice(words, func(i, j int) bool {
|
||||
return words[i] < words[j]
|
||||
})
|
||||
slices.Sort(words)
|
||||
|
||||
f.log.Debug("inserting words in junkfilter db", slog.Any("words", len(f.changed)))
|
||||
// start := time.Now()
|
||||
@ -336,9 +335,7 @@ func (f *Filter) Save() error {
|
||||
}
|
||||
|
||||
func loadWords(ctx context.Context, db *bstore.DB, l []string, dst map[string]word) error {
|
||||
sort.Slice(l, func(i, j int) bool {
|
||||
return l[i] < l[j]
|
||||
})
|
||||
slices.Sort(l)
|
||||
|
||||
err := db.Read(ctx, func(tx *bstore.Tx) error {
|
||||
for _, w := range l {
|
||||
@ -478,14 +475,8 @@ func (f *Filter) ClassifyWords(ctx context.Context, words map[string]struct{}) (
|
||||
return a.Score > b.Score
|
||||
})
|
||||
|
||||
nham := f.TopWords
|
||||
if nham > len(topHam) {
|
||||
nham = len(topHam)
|
||||
}
|
||||
nspam := f.TopWords
|
||||
if nspam > len(topSpam) {
|
||||
nspam = len(topSpam)
|
||||
}
|
||||
nham := min(f.TopWords, len(topHam))
|
||||
nspam := min(f.TopWords, len(topSpam))
|
||||
topHam = topHam[:nham]
|
||||
topSpam = topSpam[:nspam]
|
||||
|
||||
|
@ -253,10 +253,7 @@ func (r *htmlTextReader) Read(buf []byte) (n int, err error) {
|
||||
// todo: deal with inline elements? they shouldn't cause a word break.
|
||||
|
||||
give := func(nbuf []byte) (int, error) {
|
||||
n := len(buf)
|
||||
if n > len(nbuf) {
|
||||
n = len(nbuf)
|
||||
}
|
||||
n := min(len(buf), len(nbuf))
|
||||
copy(buf, nbuf[:n])
|
||||
nbuf = nbuf[n:]
|
||||
if len(nbuf) < cap(r.buf) {
|
||||
|
Reference in New Issue
Block a user