Refactor variable and function names in msgWriter

The commit includes changes such as renaming the variables `wbuf` to `buffer`, `cl` to `charLength` and functions `f` to `writeFunc` in msgWriter. This refactoring makes the code easier to read and understand. All the changes are aimed at enhancing code clarity without altering underlying logic or functionality.
This commit is contained in:
Winni Neessen 2024-02-24 17:38:42 +01:00
parent 263f6bb3de
commit c126670f70
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D

View file

@ -284,65 +284,66 @@ func (mw *msgWriter) writeString(s string) {
// writeHeader writes a header into the msgWriter's io.Writer // writeHeader writes a header into the msgWriter's io.Writer
func (mw *msgWriter) writeHeader(key Header, values ...string) { func (mw *msgWriter) writeHeader(key Header, values ...string) {
wbuf := bytes.Buffer{} buffer := strings.Builder{}
cl := MaxHeaderLength - 2 charLength := MaxHeaderLength - 2
wbuf.WriteString(string(key)) buffer.WriteString(string(key))
cl -= len(key) charLength -= len(key)
if len(values) == 0 { if len(values) == 0 {
wbuf.WriteString(":\r\n") buffer.WriteString(":\r\n")
return return
} }
wbuf.WriteString(": ") buffer.WriteString(": ")
cl -= 2 charLength -= 2
fs := strings.Join(values, ", ") fullValueStr := strings.Join(values, ", ")
sfs := strings.Split(fs, " ") words := strings.Split(fullValueStr, " ")
for i, v := range sfs { for i, val := range words {
if cl-len(v) <= 1 { if charLength-len(val) <= 1 {
wbuf.WriteString(fmt.Sprintf("%s ", SingleNewLine)) buffer.WriteString(fmt.Sprintf("%s ", SingleNewLine))
cl = MaxHeaderLength - 3 charLength = MaxHeaderLength - 3
} }
wbuf.WriteString(v) buffer.WriteString(val)
if i < len(sfs)-1 { if i < len(words)-1 {
wbuf.WriteString(" ") buffer.WriteString(" ")
cl -= 1 charLength -= 1
} }
cl -= len(v) charLength -= len(val)
} }
bufs := wbuf.String() bufferString := buffer.String()
bufs = strings.ReplaceAll(bufs, fmt.Sprintf(" %s", SingleNewLine), SingleNewLine) bufferString = strings.ReplaceAll(bufferString, fmt.Sprintf(" %s", SingleNewLine),
mw.writeString(bufs) SingleNewLine)
mw.writeString(bufferString)
mw.writeString("\r\n") mw.writeString("\r\n")
} }
// writeBody writes an io.Reader into an io.Writer using provided Encoding // writeBody writes an io.Reader into an io.Writer using provided Encoding
func (mw *msgWriter) writeBody(f func(io.Writer) (int64, error), e Encoding) { func (mw *msgWriter) writeBody(writeFunc func(io.Writer) (int64, error), encoding Encoding) {
var w io.Writer var writer io.Writer
var ew io.WriteCloser var encodedWriter io.WriteCloser
var n int64 var n int64
var err error var err error
if mw.depth == 0 { if mw.depth == 0 {
w = mw.writer writer = mw.writer
} }
if mw.depth > 0 { if mw.depth > 0 {
w = mw.partWriter writer = mw.partWriter
} }
wbuf := bytes.Buffer{} writeBuffer := bytes.Buffer{}
lb := Base64LineBreaker{} lineBreaker := Base64LineBreaker{}
lb.out = &wbuf lineBreaker.out = &writeBuffer
switch e { switch encoding {
case EncodingQP: case EncodingQP:
ew = quotedprintable.NewWriter(&wbuf) encodedWriter = quotedprintable.NewWriter(&writeBuffer)
case EncodingB64: case EncodingB64:
ew = base64.NewEncoder(base64.StdEncoding, &lb) encodedWriter = base64.NewEncoder(base64.StdEncoding, &lineBreaker)
case NoEncoding: case NoEncoding:
_, err = f(&wbuf) _, err = writeFunc(&writeBuffer)
if err != nil { if err != nil {
mw.err = fmt.Errorf("bodyWriter function: %w", err) mw.err = fmt.Errorf("bodyWriter function: %w", err)
} }
n, err = io.Copy(w, &wbuf) n, err = io.Copy(writer, &writeBuffer)
if err != nil && mw.err == nil { if err != nil && mw.err == nil {
mw.err = fmt.Errorf("bodyWriter io.Copy: %w", err) mw.err = fmt.Errorf("bodyWriter io.Copy: %w", err)
} }
@ -351,22 +352,22 @@ func (mw *msgWriter) writeBody(f func(io.Writer) (int64, error), e Encoding) {
} }
return return
default: default:
ew = quotedprintable.NewWriter(w) encodedWriter = quotedprintable.NewWriter(writer)
} }
_, err = f(ew) _, err = writeFunc(encodedWriter)
if err != nil { if err != nil {
mw.err = fmt.Errorf("bodyWriter function: %w", err) mw.err = fmt.Errorf("bodyWriter function: %w", err)
} }
err = ew.Close() err = encodedWriter.Close()
if err != nil && mw.err == nil { if err != nil && mw.err == nil {
mw.err = fmt.Errorf("bodyWriter close encoded writer: %w", err) mw.err = fmt.Errorf("bodyWriter close encoded writer: %w", err)
} }
err = lb.Close() err = lineBreaker.Close()
if err != nil && mw.err == nil { if err != nil && mw.err == nil {
mw.err = fmt.Errorf("bodyWriter close linebreaker: %w", err) mw.err = fmt.Errorf("bodyWriter close linebreaker: %w", err)
} }
n, err = io.Copy(w, &wbuf) n, err = io.Copy(writer, &writeBuffer)
if err != nil && mw.err == nil { if err != nil && mw.err == nil {
mw.err = fmt.Errorf("bodyWriter io.Copy: %w", err) mw.err = fmt.Errorf("bodyWriter io.Copy: %w", err)
} }