-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinescanner.go
85 lines (75 loc) · 1.43 KB
/
linescanner.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
package gosudachisb
import (
"bufio"
"io"
)
type normalizer struct {
r io.Reader
lastChar byte
}
func newNormalizer(r io.Reader) *normalizer {
return &normalizer{r: r}
}
func (norm *normalizer) Read(p []byte) (n int, err error) {
n, err = norm.r.Read(p)
for i := 0; i < n; i++ {
switch {
case p[i] == '\n' && norm.lastChar == '\r':
copy(p[i:n], p[i+1:])
norm.lastChar = p[i]
n--
i--
case p[i] == '\r':
norm.lastChar = p[i]
p[i] = '\n'
default:
norm.lastChar = p[i]
}
}
return
}
type LineScanner struct {
r *bufio.Reader
line []byte
rawBuffer []byte
err error
}
func NewLineScanner(r io.Reader) *LineScanner {
return &LineScanner{r: bufio.NewReader(newNormalizer(r))}
}
func (s *LineScanner) Bytes() []byte {
return s.line
}
func (s *LineScanner) Err() error {
if s.err == io.EOF {
return nil
}
return s.err
}
func (s *LineScanner) Scan() bool {
s.line, s.err = s.r.ReadSlice('\n')
if s.err == bufio.ErrBufferFull {
s.rawBuffer = append(s.rawBuffer[:0], s.line...)
for s.err == bufio.ErrBufferFull {
s.line, s.err = s.r.ReadSlice('\n')
s.rawBuffer = append(s.rawBuffer, s.line...)
}
s.line = s.rawBuffer
}
if s.err == io.EOF {
s.err = nil
if len(s.line) > 0 {
return true
} else {
return false
}
}
if s.err != nil {
return false
}
s.line = s.line[:len(s.line)-1]
return true
}
func (s *LineScanner) Text() string {
return string(s.line)
}