-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpositioned_scanner.go
56 lines (44 loc) · 1.19 KB
/
positioned_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
package ldsview
import (
"bufio"
"io"
)
type PositionedScanner struct {
pos int64
scanner *bufio.Scanner
}
func NewPositionedScanner(inputStream io.Reader) *PositionedScanner {
positionedScanner := PositionedScanner{
pos: 0,
scanner: bufio.NewScanner(inputStream),
}
lineBuf := make([]byte, LDAPMaxLineSize)
positionedScanner.Buffer(lineBuf, LDAPMaxLineSize)
scanLines := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
advance, token, err = bufio.ScanLines(data, atEOF)
positionedScanner.pos += int64(advance)
return
}
positionedScanner.scanner.Split(scanLines)
return &positionedScanner
}
func (ps PositionedScanner) Fork(input io.ReadSeeker) (*PositionedScanner, error) {
if _, err := input.Seek(ps.Position(), 0); err != nil {
return nil, err
}
newScanner := NewPositionedScanner(input)
newScanner.pos = ps.Position()
return newScanner, nil
}
func (ps PositionedScanner) Buffer(buffer []byte, max int) {
ps.scanner.Buffer(buffer, max)
}
func (ps *PositionedScanner) Scan() bool {
return ps.scanner.Scan()
}
func (ps PositionedScanner) Position() int64 {
return ps.pos
}
func (ps PositionedScanner) Text() string {
return ps.scanner.Text()
}