-
Notifications
You must be signed in to change notification settings - Fork 67
/
skim.go
222 lines (208 loc) · 5.49 KB
/
skim.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package skim
import (
"bytes"
"errors"
"io"
)
// ErrLineTooLong means there was a line encountered that exceeded max_line_size
// for the space.
var ErrLineTooLong = errors.New("line too long")
type Stats struct {
Bytes int `json:"bytes_read"`
Lines int `json:"lines_read"`
BlankLines int `json:"blank_lines"`
LineTooLong int `json:"line_too_long"`
}
// Scanner is like bufio.Scanner but it
// it understands how to skip over and report lines that are too long.
type Scanner struct {
Stats
reader io.Reader
buffer []byte
limit int
window []byte
}
// We handle lines terminated either by "\n" or by "\r\n".
const token = byte('\n')
func NewScanner(r io.Reader, buffer []byte, limit int) *Scanner {
return &Scanner{Stats{}, r, buffer, limit, nil}
}
// grow the buffer and copy the data from the old buffer to
// the new buffer. also, update the window. returns false if the buffer
// has already hit the max allowable size and doesn't do anything.
func (s *Scanner) grow() bool {
n := len(s.buffer)
if n >= s.limit {
return false
}
newsize := n * 2
if newsize > s.limit {
newsize = s.limit
}
s.buffer = make([]byte, newsize)
s.window = append(s.buffer[:0], s.window...)
return true
}
func (s *Scanner) more() error {
var cushion int
if s.window != nil {
cushion = copy(s.buffer, s.window)
}
cc, err := s.reader.Read(s.buffer[cushion:])
s.window = s.buffer[:cushion+cc]
if cc <= 0 {
return err
}
return nil
}
// Skip discards all input up to and including the next newline or
// end of file, and returns the number of bytes skipped. Returns an
// error if the underlying reader returns an error, except for EOF,
// which is ignored since the caller will detect EOF on the next
// call to Scan.
func (s *Scanner) Skip() (int, error) {
var nskip int
for {
if s.window == nil {
if err := s.more(); err != nil {
// Don't return EOF here...
// client might have more data to
// process and can rely upon calling
// the Scan returning nil, eof
if err == io.EOF {
err = nil
}
return nskip, err
}
}
off := bytes.IndexByte(s.window, token)
if off < 0 {
nskip += len(s.window)
s.window = nil
} else {
n := off + 1
if n < len(s.window) {
s.window = s.window[n:]
} else {
// the newline is precisely at the end
// of the current buffer so we can start
// fresh on the next call to Scan
s.window = nil
}
nskip += n
return nskip, nil
}
}
}
func (s *Scanner) check() error {
if len(s.window) == 0 {
return s.more()
}
return nil
}
func (s *Scanner) Peek() byte {
if err := s.check(); err != nil {
return 0
}
return s.window[0]
}
// normalizeLineEnding replaces a terminal "\r\n" sequence in buf with
// a newline.
func normalizeLineEnding(buf []byte) []byte {
if bytes.HasSuffix(buf, []byte("\r\n")) {
buf = buf[:len(buf)-1]
buf[len(buf)-1] = '\n'
}
return buf
}
// Scan returns the next line of input as a byte slice or nil and an
// error indicating the state of things. A terminal "\r\n" sequence
// is first replaced with a newline, and then the terminal newline
// is returned in the slice, except in the case of a final line
// without a newline. When a line is encountered that is larger than
// the max line size, then the partial line is returned along with
// ErrLineTooLong. In this case, Scan can be subsequently called for
// the rest of the line, possibly with another line too long error,
// and so on. Skip can also be called to easily skip over the rest of
// the line. At EOF, nil is returned. XXX If Scan is called directly
// instead of ScanLine, then Stats are not properly tracked. for the
// slice and io.EOF for the error.
func (s *Scanner) Scan() ([]byte, error) {
if err := s.check(); err != nil {
return nil, err
}
for {
if off := bytes.IndexByte(s.window, token); off >= 0 {
off++
result := s.window[:off]
// we found a line... advance the window
// if the newline lands exactly at the end of
// the buffer, just start over fresh for the
// next call
if off == len(s.window) {
s.window = nil
} else {
s.window = s.window[off:]
}
return normalizeLineEnding(result), nil
}
// we didn't find a line.
// if the buffer is full, it means it's too small to
// hold a whole line... grow it and try again
// if there is just a partial line at the end of the
// buffer, then read more input and try again
if len(s.window) == len(s.buffer) {
// if we hit the max line size and can't
// fit a line in the buffer, then we return
// the current, partial line with an error.
// and start over fresh
if !s.grow() {
result := s.window
s.window = nil
return result, ErrLineTooLong
}
// otherwise, we grew the buffer and fall
// through here to read more input
}
if err := s.more(); err != nil {
if err == io.EOF && len(s.window) > 0 {
result := s.window
s.window = nil
return normalizeLineEnding(result), nil
}
return nil, err
}
}
}
// Scan returns the next line skipping blank lines and too-long lines
// and accumulating statistics.
func (s *Scanner) ScanLine() ([]byte, error) {
for {
line, err := s.Scan()
s.Bytes += len(line)
s.Lines++
if err == nil {
if line == nil {
return nil, nil
}
if len(line) <= 1 {
// blank line, keep going
s.BlankLines++
continue
}
return line, nil
}
if err == io.EOF {
return nil, nil
}
if err == ErrLineTooLong {
s.LineTooLong++
n, err := s.Skip()
s.Bytes += n
if err == nil {
continue
}
}
return nil, err
}
}