-
Notifications
You must be signed in to change notification settings - Fork 0
/
fold.go
96 lines (82 loc) · 1.67 KB
/
fold.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package fold
import (
"bytes"
"io"
"strings"
)
type foldReader struct {
r io.Reader
width int
lineLen int
//hamper stores remaining bytes from last read which had been delayed du to
//newline insertion.
hamper []byte
}
// NewReader returns an io.Reader which wraps r.
// It streams the content of given r with maximum
// line size of width.
//
// width must be non-zero otherwise it panics.
func NewReader(r io.Reader, width int) io.Reader {
if width == 0 {
panic("width must be non-zero")
}
return &foldReader{
r: r,
width: width,
}
}
// NewReaderBytes returns an folded io.Reader from a
// slice of bytes.
func NewReaderBytes(b []byte, width int) io.Reader {
return NewReader(bytes.NewReader(b), width)
}
// NewReaderString returns an folded io.Reader from a
// sring.
func NewReaderString(s string, width int) io.Reader {
return NewReader(strings.NewReader(s), width)
}
// Read implements io.Reader for foldReader
func (f *foldReader) Read(p []byte) (n int, err error) {
hn := copy(p, f.hamper)
f.hamper = f.hamper[hn:]
n, err = f.r.Read(p[hn:])
n += hn
switch err {
case nil:
case io.EOF:
if n == 0 {
return
}
default:
return
}
var s int
for bi := 0; bi < n; {
bi = bytes.IndexAny(p[s:n], "\r\n")
e := bi
if e == -1 {
e = n - s
}
i := f.width - f.lineLen
for i < e {
trim := make([]byte, len(p[s+i:n]))
copy(trim, p[s+i:n])
p[s+i] = '\n'
nn := copy(p[s+i+1:], trim)
n += 1 - len(trim[nn:])
f.hamper = append(trim[nn:], f.hamper...)
f.lineLen = 0
s += i + 1
e = e - i - len(trim[nn:])
i = f.width
}
s += e + 1
f.lineLen = e
if bi == -1 {
break
}
f.lineLen = 0
}
return n, nil
}