-
Notifications
You must be signed in to change notification settings - Fork 4
/
reader.go
35 lines (30 loc) · 862 Bytes
/
reader.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
/*
Package macreader changes Classic Mac (CR) line endings to Linux (LF) line
endings in files. This is useful when handling CSV files generated by the Mac
version of Microsoft Excel as documented in this issue:
https://github.com/golang/go/issues/7802.
*/
package macreader
import "io"
var (
rByte byte = 13 // the byte that corresponds to the '\r' rune.
nByte byte = 10 // the byte that corresponds to the '\n' rune.
)
type reader struct {
r io.Reader
}
// New creates a new io.Reader that wraps r to convert Classic Mac (CR)
// line endings to Linux (LF) line endings.
func New(r io.Reader) io.Reader {
return &reader{r: r}
}
// Read replaces CR line endings in the source reader with LF line endings.
func (r reader) Read(p []byte) (n int, err error) {
n, err = r.r.Read(p)
for i, b := range p {
if b == rByte {
p[i] = nByte
}
}
return
}