Skip to content

Commit

Permalink
Replace CRLF with LF in Writer
Browse files Browse the repository at this point in the history
This is what the mbox format uses, despite the fact that the Internet Message
Format uses CRLF.

Closes: #20
  • Loading branch information
emersion committed Sep 5, 2019
1 parent 6e2e15e commit 8b8f2e6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
13 changes: 10 additions & 3 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ func (mw *messageWriter) Write(p []byte) (int, error) {
var l []byte
l, b = b[:i+1], b[i+1:]

n, err := mw.writeLine(l)
n := len(l)
// Replace CRLF with LF
if len(l) > 1 && l[len(l)-2] == '\r' {
l = l[:len(l)-2]
l = append(l, '\n')
}

_, err := mw.writeLine(l)
N += n
if err != nil {
return N, err
Expand All @@ -54,7 +61,7 @@ func (mw *messageWriter) Close() error {
return err
}

_, err := mw.w.Write([]byte("\r\n\r\n"))
_, err := mw.w.Write([]byte("\n\n"))
return err
}

Expand Down Expand Up @@ -94,7 +101,7 @@ func (w *Writer) CreateMessage(from string, t time.Time) (io.Writer, error) {
}
date := t.Format(time.ANSIC)

line := "From " + from + " " + date + "\r\n"
line := "From " + from + " " + date + "\n"
if _, err := io.WriteString(w.w, line); err != nil {
return nil, err
}
Expand Down
24 changes: 12 additions & 12 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ Bye.`,
},
{
"Thu, 02 Jan 2015 00:00:01 +0100",
`Date: Thu, 02 Jan 2015 00:00:01 +0100
This is another simple test.
Another line.
`Date: Thu, 02 Jan 2015 00:00:01 +0100` + "\r" + `
` + "\r" + `
This is another simple test.` + "\r" + `
` + "\r" + `
Another line.` + "\r" + `
` + "\r" + `
Bye.`,
},
}

expected := `From ???@??? Thu Jan 1 00:00:01 2015` + "\r" + `
expected := `From ???@??? Thu Jan 1 00:00:01 2015
Date: Thu, 01 Jan 2015 00:00:01 +0100
This is a simple test.
Expand All @@ -74,17 +74,17 @@ And, by the way, this is how a "From" line is escaped in mboxo format:
>From Herp Derp with love.
Bye.` + "\r" + `
` + "\r" + `
From ???@??? Fri Jan 2 00:00:01 2015` + "\r" + `
Bye.
From ???@??? Fri Jan 2 00:00:01 2015
Date: Thu, 02 Jan 2015 00:00:01 +0100
This is another simple test.
Another line.
Bye.` + "\r" + `
` + "\r" + `
Bye.
`

s := testWriter(t, messages)
Expand Down

0 comments on commit 8b8f2e6

Please sign in to comment.