-
Notifications
You must be signed in to change notification settings - Fork 2
/
scanner.go
75 lines (60 loc) · 1.34 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
// Copyright 2018 The margo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package daq
import (
"bufio"
"bytes"
"encoding/binary"
"io"
)
var (
evtBeg = make([]byte, 4)
evtEnd = make([]byte, 4)
detEnd = make([]byte, 4)
)
func init() {
binary.LittleEndian.PutUint32(evtBeg, EvtBeg)
binary.LittleEndian.PutUint32(evtEnd, EvtEnd)
binary.LittleEndian.PutUint32(detEnd, DetEnd)
}
func NewEvtScanner(r io.Reader) *bufio.Scanner {
s := bufio.NewScanner(r)
s.Split(splitEvts)
return s
}
func NewDetScanner(r io.Reader) *bufio.Scanner {
s := bufio.NewScanner(r)
s.Split(splitDets)
return s
}
func splitEvts(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.Index(data, evtEnd); i >= 0 {
return i + 4, data[0:i], nil
}
if atEOF {
return len(data), data, nil
}
return 0, nil, nil
}
func splitDets(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.Index(data, detEnd); i >= 0 {
return i + 4, data[0:i], nil
}
if atEOF {
return len(data), data, nil
}
return 0, nil, nil
}
func dropMagic(data []byte) []byte {
if len(data) > 4 {
return data[0 : len(data)-4]
}
return data
}