forked from domodwyer/mailyak
-
Notifications
You must be signed in to change notification settings - Fork 1
/
splitter.go
44 lines (33 loc) · 856 Bytes
/
splitter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package mailyak
import "io"
const maxLineLen = 60
// lineSplitter breaks the given input into lines of maxLineLen characters
// before writing a "\r\n" newline
type lineSplitter struct {
w io.Writer
maxLen int
}
type lineSplitterBuilder struct{}
func (b lineSplitterBuilder) new(w io.Writer) io.Writer {
return &lineSplitter{w: w, maxLen: maxLineLen}
}
func (w *lineSplitter) Write(p []byte) (int, error) {
offset := 0
breaks := (len(p) / w.maxLen)
for i := 0; i < breaks; i++ {
// Write line
if i, err := w.w.Write(p[offset : offset+w.maxLen]); err != nil {
return i, err
}
// Write line break
if i, err := w.w.Write([]byte("\r\n")); err != nil {
return i, err
}
offset += w.maxLen
}
// Write remaining
if i, err := w.w.Write(p[offset:]); err != nil {
return i, err
}
return (len(p) + (breaks * 2)), nil
}