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:
Mechiel Lukkien
2025-03-06 17:33:06 +01:00
parent f6132bdbc0
commit 64f2f788b1
61 changed files with 146 additions and 232 deletions

View File

@ -826,10 +826,7 @@ func (w Webmail) MessageSubmit(ctx context.Context, m SubmitMessage) {
for len(base64Data) > 0 {
line := base64Data
n := len(line)
if n > 78 {
n = 78
}
n := min(len(line), 78)
line, base64Data = base64Data[:n], base64Data[n:]
_, err := ap.Write(line)
xcheckf(ctx, err, "writing attachment")

View File

@ -143,7 +143,7 @@ func TestAPI(t *testing.T) {
testLogin("bad@bad.example", pw0, "user:loginFailed")
}
// Ensure rate limiter is triggered, also for slow tests.
for i := 0; i < 10; i++ {
for range 10 {
testLogin("bad@bad.example", pw0, "user:loginFailed", "user:error")
}
testLogin("bad@bad.example", pw0, "user:error")

View File

@ -20,6 +20,7 @@ import (
"github.com/mjl-/mox/moxio"
"github.com/mjl-/mox/smtp"
"github.com/mjl-/mox/store"
"slices"
)
// todo: we should have all needed information for messageItem in store.Message (perhaps some data in message.Part) for fast access, not having to parse the on-disk message file.
@ -139,12 +140,7 @@ func formatFirstLine(r io.Reader) (string, error) {
if len(l) >= 5 {
return false
}
for _, line := range l {
if line == "" {
return false
}
}
return true
return !slices.Contains(l, "")
}
result := ""
@ -287,7 +283,7 @@ func parsedMessage(log mlog.Log, m store.Message, state *msgState, full, msgitem
if mt == "MULTIPART/SIGNED" && i >= 1 {
continue
}
usePart(sp, i, &p, append(append([]int{}, path...), i), newParentMixed)
usePart(sp, i, &p, append(slices.Clone(path), i), newParentMixed)
}
switch mt {
case "TEXT/PLAIN", "/":
@ -313,7 +309,7 @@ func parsedMessage(log mlog.Log, m store.Message, state *msgState, full, msgitem
return
}
pm.Texts = append(pm.Texts, string(buf))
pm.TextPaths = append(pm.TextPaths, append([]int{}, path...))
pm.TextPaths = append(pm.TextPaths, slices.Clone(path))
}
if msgitem && pm.firstLine == "" {
pm.firstLine, rerr = formatFirstLine(p.ReaderUTF8OrBinary())
@ -326,7 +322,7 @@ func parsedMessage(log mlog.Log, m store.Message, state *msgState, full, msgitem
case "TEXT/HTML":
pm.HasHTML = true
if full && pm.HTMLPath == nil {
pm.HTMLPath = append([]int{}, path...)
pm.HTMLPath = slices.Clone(path)
}
default:
@ -353,7 +349,7 @@ func parsedMessage(log mlog.Log, m store.Message, state *msgState, full, msgitem
return
}
pm.Texts = append(pm.Texts, string(buf))
pm.TextPaths = append(pm.TextPaths, append([]int{}, path...))
pm.TextPaths = append(pm.TextPaths, slices.Clone(path))
}
return
}
@ -365,7 +361,7 @@ func parsedMessage(log mlog.Log, m store.Message, state *msgState, full, msgitem
return
}
pm.Texts = append(pm.Texts, string(buf))
pm.TextPaths = append(pm.TextPaths, append([]int{}, path...))
pm.TextPaths = append(pm.TextPaths, slices.Clone(path))
}
return
}

View File

@ -1971,7 +1971,7 @@ func (q Query) envFilterFn(log mlog.Log, state *msgState) func(m store.Message)
return false
}
if len(filterTo) > 0 || len(notFilterTo) > 0 {
to := append(append(append([]message.Address{}, env.To...), env.CC...), env.BCC...)
to := slices.Concat(env.To, env.CC, env.BCC)
if len(filterTo) > 0 && !contains(filterTo, to, true) {
return false
}

View File

@ -99,7 +99,7 @@ func TestView(t *testing.T) {
// Token
tokens := []string{}
for i := 0; i < 20; i++ {
for range 20 {
tokens = append(tokens, api.Token(ctx))
}
// Only last 10 tokens are still valid and around, checked below.