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

@ -10,6 +10,7 @@ import (
"github.com/mjl-/mox/imapclient"
"github.com/mjl-/mox/mox-"
"github.com/mjl-/mox/store"
"slices"
)
func TestCondstore(t *testing.T) {
@ -577,7 +578,7 @@ func testQresync(t *testing.T, tc *testconn, clientModseq int64) {
}
makeUntagged := func(l ...imapclient.Untagged) []imapclient.Untagged {
return append(append([]imapclient.Untagged{}, baseUntagged...), l...)
return slices.Concat(baseUntagged, l)
}
// uidvalidity 1, highest known modseq 1, sends full current state.

View File

@ -20,6 +20,7 @@ import (
"github.com/mjl-/mox/mox-"
"github.com/mjl-/mox/moxio"
"github.com/mjl-/mox/store"
"slices"
)
// functions to handle fetch attribute requests are defined on fetchCmd.
@ -203,9 +204,7 @@ func (c *conn) cmdxFetch(isUID bool, tag, cmdstr string, p *parser) {
}
// First sort the uids we already found, for fast lookup.
sort.Slice(vanishedUIDs, func(i, j int) bool {
return vanishedUIDs[i] < vanishedUIDs[j]
})
slices.Sort(vanishedUIDs)
// We'll be gathering any more vanished uids in more.
more := map[store.UID]struct{}{}
@ -239,9 +238,7 @@ func (c *conn) cmdxFetch(isUID bool, tag, cmdstr string, p *parser) {
if len(vanishedUIDs) > 0 {
// Mention all vanished UIDs in compact numset form.
// ../rfc/7162:1985
sort.Slice(vanishedUIDs, func(i, j int) bool {
return vanishedUIDs[i] < vanishedUIDs[j]
})
slices.Sort(vanishedUIDs)
// No hard limit on response sizes, but clients are recommended to not send more
// than 8k. We send a more conservative max 4k.
for _, s := range compactUIDSet(vanishedUIDs).Strings(4*1024 - 32) {
@ -734,10 +731,7 @@ func (cmd *fetchCmd) xbody(a fetchAtt) (string, token) {
var offset int64
count := m.Size
if a.partial != nil {
offset = int64(a.partial.offset)
if offset > m.Size {
offset = m.Size
}
offset = min(int64(a.partial.offset), m.Size)
count = int64(a.partial.count)
if offset+count > m.Size {
count = m.Size - offset

View File

@ -15,10 +15,7 @@ type prefixConn struct {
func (c *prefixConn) Read(buf []byte) (int, error) {
if len(c.prefix) > 0 {
n := len(buf)
if n > len(c.prefix) {
n = len(c.prefix)
}
n := min(len(buf), len(c.prefix))
copy(buf[:n], c.prefix[:n])
c.prefix = c.prefix[n:]
if len(c.prefix) == 0 {

View File

@ -11,6 +11,7 @@ import (
"github.com/mjl-/mox/message"
"github.com/mjl-/mox/store"
"slices"
)
// Search returns messages matching criteria specified in parameters.
@ -454,12 +455,7 @@ func (s *search) match0(sk searchKey) bool {
case "$mdnsent":
return s.m.MDNSent
default:
for _, k := range s.m.Keywords {
if k == kw {
return true
}
}
return false
return slices.Contains(s.m.Keywords, kw)
}
case "SEEN":
return s.m.Seen
@ -483,12 +479,7 @@ func (s *search) match0(sk searchKey) bool {
case "$mdnsent":
return !s.m.MDNSent
default:
for _, k := range s.m.Keywords {
if k == kw {
return false
}
}
return true
return !slices.Contains(s.m.Keywords, kw)
}
case "UNSEEN":
return !s.m.Seen

View File

@ -66,7 +66,7 @@ func TestSearch(t *testing.T) {
// Add 5 and delete first 4 messages. So UIDs start at 5.
received := time.Date(2020, time.January, 1, 10, 0, 0, 0, time.UTC)
saveDate := time.Now()
for i := 0; i < 5; i++ {
for range 5 {
tc.client.Append("inbox", makeAppendTime(exampleMsg, received))
}
tc.client.StoreFlagsSet("1:4", true, `\Deleted`)
@ -394,7 +394,7 @@ func TestSearch(t *testing.T) {
// More than 1mb total for literals.
_, err = fmt.Fprintf(tc.client, "x0 uid search")
tcheck(t, err, "write start of uit search")
for i := 0; i < 10; i++ {
for range 10 {
writeTextLit(100*1024, true)
}
writeTextLit(1, false)
@ -402,7 +402,7 @@ func TestSearch(t *testing.T) {
// More than 1000 literals.
_, err = fmt.Fprintf(tc.client, "x0 uid search")
tcheck(t, err, "write start of uit search")
for i := 0; i < 1000; i++ {
for range 1000 {
writeTextLit(1, true)
}
writeTextLit(1, false)

View File

@ -2760,9 +2760,7 @@ func (c *conn) cmdSelectExamine(isselect bool, tag, cmd string, p *parser) {
// Now that we have all vanished UIDs, send them over compactly.
if len(vanishedUIDs) > 0 {
l := maps.Keys(vanishedUIDs)
sort.Slice(l, func(i, j int) bool {
return l[i] < l[j]
})
slices.Sort(l)
// ../rfc/7162:1985
for _, s := range compactUIDSet(l).Strings(4*1024 - 32) {
c.bwritelinef("* VANISHED (EARLIER) %s", s)
@ -3913,9 +3911,7 @@ func (c *conn) gatherCopyMoveUIDs(isUID bool, nums numSet) ([]store.UID, []any)
// response and interpret it differently than we intended.
// ../rfc/9051:5072
uids := c.xnumSetUIDs(isUID, nums)
sort.Slice(uids, func(i, j int) bool {
return uids[i] < uids[j]
})
slices.Sort(uids)
uidargs := make([]any, len(uids))
for i, uid := range uids {
uidargs[i] = uid
@ -4200,7 +4196,7 @@ func (c *conn) cmdxMove(isUID bool, tag, cmd string, p *parser) {
c.bwritelinef("* OK [COPYUID %d %s %s] moved", mbDst.UIDValidity, compactUIDSet(uids).String(), newUIDs.String())
qresync := c.enabled[capQresync]
var vanishedUIDs numSet
for i := 0; i < len(uids); i++ {
for i := range uids {
seq := c.xsequence(uids[i])
c.sequenceRemove(seq, uids[i])
if qresync {
@ -4452,7 +4448,7 @@ func (c *conn) cmdxStore(isUID bool, tag, cmd string, p *parser) {
origFlags := m.Flags
m.Flags = m.Flags.Set(mask, flags)
oldKeywords := append([]string{}, m.Keywords...)
oldKeywords := slices.Clone(m.Keywords)
if minus {
m.Keywords, _ = store.RemoveKeywords(m.Keywords, keywords)
} else if plus {
@ -4463,7 +4459,7 @@ func (c *conn) cmdxStore(isUID bool, tag, cmd string, p *parser) {
keywordsChanged := func() bool {
sort.Strings(oldKeywords)
n := append([]string{}, m.Keywords...)
n := slices.Clone(m.Keywords)
sort.Strings(n)
return !slices.Equal(oldKeywords, n)
}
@ -4581,9 +4577,7 @@ func (c *conn) cmdxStore(isUID bool, tag, cmd string, p *parser) {
}
}
sort.Slice(mnums, func(i, j int) bool {
return mnums[i] < mnums[j]
})
slices.Sort(mnums)
set := compactUIDSet(mnums)
// ../rfc/7162:2506
c.writeresultf("%s OK [MODIFIED %s] conditional store did not modify all", tag, set.String())

View File

@ -25,6 +25,7 @@ import (
"github.com/mjl-/mox/mox-"
"github.com/mjl-/mox/moxvar"
"github.com/mjl-/mox/store"
"slices"
)
var ctxbg = context.Background()
@ -210,7 +211,7 @@ func (tc *testconn) xuntagged(exps ...imapclient.Untagged) {
func (tc *testconn) xuntaggedOpt(all bool, exps ...imapclient.Untagged) {
tc.t.Helper()
last := append([]imapclient.Untagged{}, tc.lastUntagged...)
last := slices.Clone(tc.lastUntagged)
var mismatch any
next:
for ei, exp := range exps {
@ -572,13 +573,13 @@ func TestState(t *testing.T) {
defer tc.close()
// Not authenticated, lots of commands not allowed.
for _, cmd := range append(append([]string{}, authenticatedOrSelected...), selected...) {
for _, cmd := range slices.Concat(authenticatedOrSelected, selected) {
tc.transactf("no", "%s", cmd)
}
// Some commands not allowed when authenticated.
tc.transactf("ok", `login mjl@mox.example "%s"`, password0)
for _, cmd := range append(append([]string{}, notAuthenticated...), selected...) {
for _, cmd := range slices.Concat(notAuthenticated, selected) {
tc.transactf("no", "%s", cmd)
}