-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforward.go
143 lines (127 loc) · 3.14 KB
/
forward.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
package linescanner
import (
"bytes"
"io"
)
type forward struct {
reader io.ReaderAt
maxChunkSize int
chunk []byte
maxBufferSize int
buffer []byte
readerPos int
readerLineStartPos int
bufferLineStartPos int
err error
}
func NewForward(reader io.ReaderAt, position int) *forward {
return NewForwardWithSize(reader, position, defaultMaxChunkSize, defaultMaxBufferSize)
}
func NewForwardWithSize(reader io.ReaderAt, position int, maxChunkSize int, maxBufferSize int) *forward {
if reader == nil {
panic(ErrNilReader)
}
if maxChunkSize <= 0 {
panic(ErrInvalidMaxChunkSize)
}
if maxBufferSize <= 0 {
panic(ErrInvalidMaxBufferSize)
}
if maxChunkSize > maxBufferSize {
panic(ErrGreaterBufferSize)
}
return &forward{
reader: reader,
maxChunkSize: maxChunkSize,
maxBufferSize: maxBufferSize,
readerPos: position,
readerLineStartPos: position,
}
}
func (f *forward) endOfFile() bool {
return f.readerPos < 0
}
func (f *forward) endOfScan() bool {
return f.endOfFile() && f.readerLineStartPos < 0
}
func (f *forward) allocateChunk() error {
if f.chunk == nil {
f.chunk = make([]byte, f.maxChunkSize)
} else {
f.chunk = f.chunk[:f.maxChunkSize]
}
n, err := f.reader.ReadAt(f.chunk, int64(f.readerPos))
if err == nil {
f.readerPos += len(f.chunk)
} else {
if err != io.EOF {
return err
}
f.readerPos = endPosition
}
f.chunk = f.chunk[:n]
return nil
}
func (f *forward) allocateBuffer() error {
chunkSize := len(f.chunk)
if chunkSize == 0 {
return nil
}
bufferLineSize := len(f.buffer[f.bufferLineStartPos:])
if bufferLineSize+chunkSize > f.maxBufferSize {
return ErrBufferOverflow
}
if bufferLineSize+chunkSize > cap(f.buffer) {
expandedBuffer := make([]byte, 0, bufferLineSize+chunkSize)
expandedBuffer = append(expandedBuffer, f.buffer[f.bufferLineStartPos:]...)
f.buffer = expandedBuffer
f.bufferLineStartPos = 0
} else if f.bufferLineStartPos+bufferLineSize+chunkSize > cap(f.buffer) {
copy(f.buffer, f.buffer[f.bufferLineStartPos:])
f.buffer = f.buffer[:bufferLineSize]
f.bufferLineStartPos = 0
}
f.buffer = append(f.buffer, f.chunk...)
return nil
}
func (f *forward) removeLineFromBuffer(lineSize int) string {
line := removeCarriageReturn(f.buffer[f.bufferLineStartPos : f.bufferLineStartPos+lineSize])
f.readerLineStartPos += lineSize + 1
f.bufferLineStartPos += lineSize + 1
return line
}
func (f *forward) read() (err error) {
if err = f.allocateChunk(); err != nil {
return err
}
if err := f.allocateBuffer(); err != nil {
return err
}
return nil
}
func (f *forward) Line() (string, error) {
if f.err != nil {
return "", f.err
}
if f.endOfScan() {
return "", io.EOF
}
for {
lineSize := bytes.IndexByte(f.buffer[f.bufferLineStartPos:], '\n')
if lineSize >= 0 {
return f.removeLineFromBuffer(lineSize), nil
} else {
if f.endOfFile() {
line := f.removeLineFromBuffer(len(f.buffer[f.bufferLineStartPos:]))
f.readerLineStartPos = endPosition
return line, io.EOF
}
if f.err = f.read(); f.err != nil {
return "", f.err
}
}
}
}
func (f *forward) Position() int {
return f.readerLineStartPos
}