-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathtac_scanner.go
131 lines (121 loc) · 2.41 KB
/
tac_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package tac
import (
"bufio"
"errors"
"io"
)
const maxBufSize = 8 * 1024
type tacScanner struct {
r io.ReadSeeker
splitFunc bufio.SplitFunc
buf []byte
offset int64
atEOF bool
tokens [][]byte
partialToken int
lastErr error
}
func newTacScanner(r io.ReadSeeker) *tacScanner {
b := &tacScanner{
r: r,
buf: make([]byte, 4096),
atEOF: true,
splitFunc: bufio.ScanLines,
}
b.offset, b.lastErr = r.Seek(0, 2)
return b
}
func (b *tacScanner) fillbuf() error {
b.tokens = b.tokens[:0]
if b.offset == 0 {
return io.EOF
}
space := len(b.buf) - b.partialToken
if space == 0 {
if len(b.buf) >= maxBufSize {
return errors.New("token too long")
}
n := len(b.buf) * 2
if n > maxBufSize {
n = maxBufSize
}
newBuf := make([]byte, n)
copy(newBuf, b.buf[0:b.partialToken])
b.buf = newBuf
space = len(b.buf) - b.partialToken
}
if int64(space) > b.offset {
b.buf = b.buf[0 : b.partialToken+int(b.offset)]
space = len(b.buf) - b.partialToken
}
newOffset := b.offset - int64(space)
copy(b.buf[space:], b.buf[0:b.partialToken])
_, err := b.r.Seek(newOffset, 0)
if err != nil {
return err
}
b.offset = newOffset
if _, err := io.ReadFull(b.r, b.buf[0:space]); err != nil {
return err
}
if b.offset > 0 {
advance, _, err := b.splitFunc(b.buf, b.atEOF)
if err != nil {
return err
}
b.partialToken = advance
if advance == 0 || advance == len(b.buf) {
return b.fillbuf()
}
} else {
b.partialToken = 0
}
for i := b.partialToken; i < len(b.buf); {
advance, token, err := b.splitFunc(b.buf[i:], b.atEOF)
if err != nil {
b.tokens = b.tokens[:0]
return err
}
if advance == 0 {
break
}
b.tokens = append(b.tokens, token)
i += advance
}
b.atEOF = false
if len(b.tokens) == 0 {
return b.fillbuf()
}
return nil
}
func (b *tacScanner) scan() bool {
if len(b.tokens) > 0 {
b.tokens = b.tokens[0 : len(b.tokens)-1]
}
if len(b.tokens) > 0 {
return true
}
if b.lastErr != nil {
return false
}
b.lastErr = b.fillbuf()
return len(b.tokens) > 0
}
func (b *tacScanner) split(split bufio.SplitFunc) {
b.splitFunc = split
}
func (b *tacScanner) bytes() []byte {
return b.tokens[len(b.tokens)-1]
}
func (b *tacScanner) text() string {
return string(b.bytes())
}
func (b *tacScanner) err() error {
if len(b.tokens) > 0 {
return nil
}
if b.lastErr == io.EOF {
return nil
}
return b.lastErr
}