-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlftocrlf.go
73 lines (61 loc) · 1.13 KB
/
lftocrlf.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
package sqlbless
import (
"io"
"golang.org/x/text/transform"
)
type lfToCrlf struct{}
func (t lfToCrlf) Reset() {}
func (f lfToCrlf) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
for _, c := range src {
if c == '\n' {
if len(dst) < 2 {
return nDst, nSrc, transform.ErrShortDst
}
dst[0] = '\r'
dst[1] = '\n'
dst = dst[2:]
nDst += 2
} else {
if len(dst) < 1 {
return nDst, nSrc, transform.ErrShortDst
}
dst[0] = c
dst = dst[1:]
nDst++
}
nSrc++
}
return nDst, nSrc, nil
}
type FilterSource interface {
Write([]byte) (int, error)
Name() string
Close() error
}
type Filter struct {
body FilterSource
filter io.WriteCloser
}
func (s *Filter) Write(b []byte) (int, error) {
if s.filter != nil {
return s.filter.Write(b)
} else {
return s.body.Write(b)
}
}
func (s *Filter) Close() error {
if s.filter != nil {
s.filter.Close()
}
return s.body.Close()
}
func (s *Filter) Name() string {
return s.body.Name()
}
func newFilter(fd FilterSource) *Filter {
filter := transform.NewWriter(fd, lfToCrlf{})
return &Filter{
filter: filter,
body: fd,
}
}