-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.go
66 lines (56 loc) · 1.69 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
package eventstream
import (
"bufio"
"bytes"
"io"
)
// Scanner reads event stream tokens from an underlying io.Reader. Each token
// represents a line of input.
type Scanner struct {
lines *bufio.Scanner
started bool
}
// NewScanner returns a new event stream token scanner that reads from r.
func NewScanner(r io.Reader) *Scanner {
return &Scanner{lines: bufio.NewScanner(r)}
}
// Buffer has the same behavior as Scanner.Buffer in the standard library's
// bufio package.
func (s *Scanner) Buffer(buf []byte, max int) {
s.lines.Buffer(buf, max)
}
// Scan advances the scanner to the next token, which will then be available
// through the Token method. It returns false when the scan stops, either by
// reaching the end of the stream or an error.
func (s *Scanner) Scan() bool {
if !s.lines.Scan() {
return false
}
return true
}
// Err returns the first non-EOF error that was encountered by the Scanner.
func (s *Scanner) Err() error {
return s.lines.Err()
}
// Token returns the most recent token generated by a call to Scan.
//
// A token is only valid until the next call to Scan, after which its
// underlying data may be overwritten.
func (s *Scanner) Token() Token {
// Collect the current line from bufio.Scanner
line := s.lines.Bytes()
// Skip the opening byte order mark if it's present
if !s.started {
s.started = true
if len(line) >= 3 && line[0] == 0xEF && line[1] == 0xBB && line[2] == 0xBF {
line = line[3:]
}
}
// Empty lines signal the end of a section
if len(line) == 0 {
return Token{line: line, colon: -1}
}
// Non-empty lines are interpreted according to the presence of a colon
colon := bytes.IndexRune(line, ':')
return Token{line: line, colon: colon}
}