-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscanner.go
79 lines (61 loc) · 1.38 KB
/
scanner.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
package eclint
import (
"bufio"
"io"
)
// LineFunc is the callback for a line.
//
// It returns the line number starting from zero.
type LineFunc func(int, []byte, bool) error
// SplitLines works like bufio.ScanLines while keeping the line endings.
func SplitLines(data []byte, atEOF bool) (int, []byte, error) {
i := 0
for i < len(data) {
if data[i] == cr {
i++
if i < len(data) && !atEOF {
// Request more data
return 0, nil, nil
}
if i < len(data) && data[i] == lf {
i++
}
return i, data[0:i], nil
} else if data[i] == lf {
i++
return i, data[0:i], nil
}
i++
}
if !atEOF {
// Request more data
return 0, nil, nil
}
if atEOF && i != 0 {
return 0, data, bufio.ErrFinalToken
}
return 0, nil, io.EOF
}
// ReadLines consumes the reader and emit each line via the LineFunc
//
// Line numbering starts at 0. Scanner is pretty smart an will reuse
// its memory structure. This is something we explicitly avoid by copying
// the content to a new slice.
func ReadLines(r io.Reader, fileSize int64, fn LineFunc) []error {
errs := make([]error, 0)
sc := bufio.NewScanner(r)
sc.Split(SplitLines)
var read int64
i := 0
for sc.Scan() {
l := sc.Bytes()
line := make([]byte, len(l))
copy(line, l)
read += int64(len(line))
if err := fn(i, line, read == fileSize); err != nil {
errs = append(errs, err)
}
i++
}
return errs
}