diff options
author | René 'Necoro' Neumann <necoro@necoro.eu> | 2021-02-16 00:30:13 +0100 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.eu> | 2021-02-16 00:30:13 +0100 |
commit | 3a854c3bc47e75491b836c7fc12b617da5d68288 (patch) | |
tree | aaecfbf79eea65efaa0e770e806d13d996990c39 /pkg/rfc822/writer.go | |
parent | c2b6e7ff346e3373a4e33c946594bb6f08393ad3 (diff) | |
download | feed2imap-go-3a854c3bc47e75491b836c7fc12b617da5d68288.tar.gz feed2imap-go-3a854c3bc47e75491b836c7fc12b617da5d68288.tar.bz2 feed2imap-go-3a854c3bc47e75491b836c7fc12b617da5d68288.zip |
Issue #46: Fix semantics of `n` result
Per contract, the returned number of bytes written should be the number of bytes _from the input_.
Therefore, the added bytes (`\r` or `\n`) shall not count into that number.
Diffstat (limited to '')
-rw-r--r-- | pkg/rfc822/writer.go | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/pkg/rfc822/writer.go b/pkg/rfc822/writer.go index 07751ea..dd96fbb 100644 --- a/pkg/rfc822/writer.go +++ b/pkg/rfc822/writer.go @@ -23,29 +23,31 @@ func (f rfc822Writer) Write(p []byte) (n int, err error) { crFound := false start := 0 - write := func(str []byte) { + write := func(str []byte, count bool) { var j int j, err = f.w.Write(str) - n = n + j + if count { + n += j + } } for idx, b := range p { if crFound && b != '\n' { // insert '\n' - if write(p[start:idx]); err != nil { + if write(p[start:idx], true); err != nil { return } - if write(lf); err != nil { + if write(lf, false); err != nil { return } start = idx } else if !crFound && b == '\n' { // insert '\r' - if write(p[start:idx]); err != nil { + if write(p[start:idx], true); err != nil { return } - if write(cr); err != nil { + if write(cr, false); err != nil { return } @@ -55,12 +57,12 @@ func (f rfc822Writer) Write(p []byte) (n int, err error) { } // write the remainder - if write(p[start:]); err != nil { + if write(p[start:], true); err != nil { return } if crFound { // dangling \r - write(lf) + write(lf, false) } return |