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

@ -27,6 +27,7 @@ import (
"github.com/mjl-/mox/mlog"
"github.com/mjl-/mox/smtp"
"slices"
)
// Pedantic enables stricter parsing.
@ -848,10 +849,8 @@ func (b *bufAt) maxLineLength() int {
// ensure makes sure b.nbuf is up to maxLineLength, unless eof is encountered.
func (b *bufAt) ensure() error {
for _, c := range b.buf[:b.nbuf] {
if c == '\n' {
return nil
}
if slices.Contains(b.buf[:b.nbuf], '\n') {
return nil
}
if b.scratch == nil {
b.scratch = make([]byte, b.maxLineLength())
@ -1014,10 +1013,7 @@ func (b *boundReader) Read(buf []byte) (count int, rerr error) {
for {
// Read data from earlier line.
if b.nbuf > 0 {
n := b.nbuf
if n > len(buf) {
n = len(buf)
}
n := min(b.nbuf, len(buf))
copy(buf, b.buf[:n])
copy(b.buf, b.buf[n:])
buf = buf[n:]
@ -1046,10 +1042,7 @@ func (b *boundReader) Read(buf []byte) (count int, rerr error) {
return count, err
}
if len(b.crlf) > 0 {
n := len(b.crlf)
if n > len(buf) {
n = len(buf)
}
n := min(len(b.crlf), len(buf))
copy(buf, b.crlf[:n])
count += n
buf = buf[n:]

View File

@ -56,10 +56,7 @@ func (w *Writer) Write(buf []byte) (int, error) {
// Update w.tail after having written. Regardless of error, writers can't expect
// subsequent writes to work again properly anyway.
defer func() {
n := len(buf)
if n > 3 {
n = 3
}
n := min(len(buf), 3)
copy(w.tail[:], w.tail[n:])
copy(w.tail[3-n:], buf[len(buf)-n:])
}()