-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
queue.go
56 lines (49 loc) · 1.44 KB
/
queue.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
package main
import (
"bufio"
"bytes"
"io"
)
// This is our concurrency-safe line queue, meant to wrap some io.Reader.
type queue struct {
ch <-chan string
}
func newQueue(reader io.Reader, splitChar byte, queueBuffer int) queue {
ch := make(chan string, queueBuffer*2) // Buffer the channel to a reasonable value
// Build the scanner and start scanning lines into the job queue in the
// background while we return our new queue.
go func() {
scanner := newScanner(reader, splitChar)
for scanner.Scan() {
ch <- scanner.Text()
}
close(ch)
}()
return queue{ch: ch}
}
func newScanner(reader io.Reader, splitChar byte) *bufio.Scanner {
scanner := bufio.NewScanner(reader)
scanner.Split(newSplitFunc(splitChar))
return scanner
}
// This function is used to return a new `bufio.SplitFunc` splitting on
// whichever character the user specifies. The code for this is mostly just
// lifted out of `bufio.ScanLines`, replacing the newline character with a
// paramter.
func newSplitFunc(char byte) bufio.SplitFunc {
return func(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexByte(data, char); 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
}
}