aboutsummaryrefslogtreecommitdiff
path: root/pkg/rfc822
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2021-02-16 00:30:13 +0100
committerRené 'Necoro' Neumann <necoro@necoro.eu>2021-02-16 00:30:13 +0100
commit3a854c3bc47e75491b836c7fc12b617da5d68288 (patch)
treeaaecfbf79eea65efaa0e770e806d13d996990c39 /pkg/rfc822
parentc2b6e7ff346e3373a4e33c946594bb6f08393ad3 (diff)
downloadfeed2imap-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 'pkg/rfc822')
-rw-r--r--pkg/rfc822/writer.go18
-rw-r--r--pkg/rfc822/writer_test.go6
2 files changed, 15 insertions, 9 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
diff --git a/pkg/rfc822/writer_test.go b/pkg/rfc822/writer_test.go
index 7beae8d..34c9d4a 100644
--- a/pkg/rfc822/writer_test.go
+++ b/pkg/rfc822/writer_test.go
@@ -34,10 +34,14 @@ func TestRfc822Writer_Write(t *testing.T) {
t.Run(tt.before, func(t *testing.T) {
b := bytes.Buffer{}
w := Writer(&b)
- if _, err := io.WriteString(w, tt.before); err != nil {
+ n, err := io.WriteString(w, tt.before)
+ if err != nil {
t.Errorf("Error: %v", err)
return
}
+ if n != len(tt.before) {
+ t.Errorf("Unexpected number of bytes written: %d, expected: %d", n, len(tt.before))
+ }
res := b.String()
if tt.after != res {
t.Errorf("Expected: %q, got: %q", tt.after, res)