-
Notifications
You must be signed in to change notification settings - Fork 25
/
scanner.go
82 lines (75 loc) · 1.75 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
80
81
82
package gitgo
import (
"bytes"
"fmt"
"io"
)
// scanner functions similarly to bufio.Scanner,
// except that it never reads more input than necessary,
// which allow predictable consumption (and reuse) of readers
type scanner struct {
r io.Reader
data []byte
err error
}
func (s *scanner) scan() bool {
if s.err != nil {
return false
}
s.data = s.read()
return s.err == nil
}
func (s *scanner) Err() error {
if s.err == io.EOF {
return nil
}
return s.err
}
func (s *scanner) read() []byte {
if s.err != nil {
return nil
}
result := make([]byte, 1)
n, err := s.r.Read(result)
if err != nil {
s.err = err
return nil
}
if n == 0 {
s.err = fmt.Errorf("read zero bytes")
}
return result
}
// ScanNullLines is like bufio.ScanLines, except it uses the null character as the delimiter
// instead of a newline
func ScanNullLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexByte(data, '\x00'); i >= 0 {
// We have a full null-terminated line.
return i + 1, data[0:i], nil
}
// If we're at EOF, we have a final, non-terminated "line". Return it.
if atEOF {
return len(data), data, nil
}
// Request more data.
return 0, nil, nil
}
// ScanLinesNoTrim is exactly like bufio.ScanLines, except it does not trim the newline
func ScanLinesNoTrim(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexByte(data, '\n'); i >= 0 {
// We have a full newline-terminated line.
return i + 1, data[0 : i+1], nil
}
// If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF {
return len(data), data, nil
}
// Request more data.
return 0, nil, nil
}