aboutsummaryrefslogtreecommitdiff
path: root/pkg/util
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2021-02-16 00:16:35 +0100
committerRené 'Necoro' Neumann <necoro@necoro.eu>2021-02-16 00:16:35 +0100
commitc2b6e7ff346e3373a4e33c946594bb6f08393ad3 (patch)
tree584016f7eda2672f8c25645d05cc3be9d9e9b999 /pkg/util
parent9d927b399e409990003b25efa8ca7395e9cad021 (diff)
downloadfeed2imap-go-c2b6e7ff346e3373a4e33c946594bb6f08393ad3.tar.gz
feed2imap-go-c2b6e7ff346e3373a4e33c946594bb6f08393ad3.tar.bz2
feed2imap-go-c2b6e7ff346e3373a4e33c946594bb6f08393ad3.zip
Issue #46: Move and rename writer; add comments
Diffstat (limited to 'pkg/util')
-rw-r--r--pkg/util/fixWriter.go63
-rw-r--r--pkg/util/fixWriter_test.go47
2 files changed, 0 insertions, 110 deletions
diff --git a/pkg/util/fixWriter.go b/pkg/util/fixWriter.go
deleted file mode 100644
index d78b618..0000000
--- a/pkg/util/fixWriter.go
+++ /dev/null
@@ -1,63 +0,0 @@
-package util
-
-import "io"
-
-type fixWriter struct {
- w io.Writer
-}
-
-var lf = []byte{'\n'}
-var cr = []byte{'\r'}
-
-func (f fixWriter) Write(p []byte) (n int, err error) {
- crFound := false
- start := 0
-
- write := func(str []byte) {
- var j int
- j, err = f.w.Write(str)
- n = n + j
- }
-
- for idx, b := range p {
- if crFound && b != '\n' {
- // insert '\n'
- if write(p[start:idx]); err != nil {
- return
- }
- if write(lf); err != nil {
- return
- }
-
- start = idx
- } else if !crFound && b == '\n' {
- // insert '\r'
- if write(p[start:idx]); err != nil {
- return
- }
- if write(cr); err != nil {
- return
- }
-
- start = idx
- }
- crFound = b == '\r'
- }
-
- // write the remainder
- if write(p[start:]); err != nil {
- return
- }
-
- if crFound { // dangling \r
- write(lf)
- }
-
- return
-}
-
-// Cyrus IMAP really cares about single \r and \n.
-// Implement this fixer to change them into \r\n.
-func FixWriter(w io.Writer) io.Writer {
- return fixWriter{w}
-}
diff --git a/pkg/util/fixWriter_test.go b/pkg/util/fixWriter_test.go
deleted file mode 100644
index 91961be..0000000
--- a/pkg/util/fixWriter_test.go
+++ /dev/null
@@ -1,47 +0,0 @@
-package util
-
-import (
- "bytes"
- "io"
- "testing"
-)
-
-func TestFixWriter_Write(t *testing.T) {
- tests := []struct {
- before string
- after string
- }{
- {"", ""},
- {"foo", "foo"},
- {"foo\r", "foo\r\n"},
- {"foo\n", "foo\r\n"},
- {"foo\r\n", "foo\r\n"},
- {"\r", "\r\n"},
- {"\n", "\r\n"},
- {"\r\n", "\r\n"},
- {"foo\rbar", "foo\r\nbar"},
- {"foo\nbar", "foo\r\nbar"},
- {"foo\r\nbar", "foo\r\nbar"},
- {"\r\r", "\r\n\r\n"},
- {"\n\n", "\r\n\r\n"},
- {"\r\r\n", "\r\n\r\n"},
- {"\n\r", "\r\n\r\n"},
- {"\rbar", "\r\nbar"},
- {"\nbar", "\r\nbar"},
- {"\r\nbar", "\r\nbar"},
- }
- for _, tt := range tests {
- t.Run(tt.before, func(t *testing.T) {
- b := bytes.Buffer{}
- w := FixWriter(&b)
- if _, err := io.WriteString(w, tt.before); err != nil {
- t.Errorf("Error: %v", err)
- return
- }
- res := b.String()
- if tt.after != res {
- t.Errorf("Expected: %q, got: %q", tt.after, res)
- }
- })
- }
-}