-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackward.go
145 lines (129 loc) · 3.06 KB
/
backward.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
package linescanner
import (
"bytes"
"io"
)
type backward struct {
reader io.ReaderAt
maxChunkSize int
chunk []byte
maxBufferSize int
buffer []byte
readerPos int
readerLineEndPos int
err error
}
func NewBackward(reader io.ReaderAt, position int) *backward {
return NewBackwardWithSize(reader, position, defaultMaxChunkSize, defaultMaxBufferSize)
}
func NewBackwardWithSize(reader io.ReaderAt, position, maxChunkSize int, maxBufferSize int) *backward {
if reader == nil {
panic(ErrNilReader)
}
if maxChunkSize <= 0 {
panic(ErrInvalidMaxChunkSize)
}
if maxBufferSize <= 0 {
panic(ErrInvalidMaxBufferSize)
}
if maxChunkSize > maxBufferSize {
panic(ErrGreaterBufferSize)
}
return &backward{
reader: reader,
maxChunkSize: maxChunkSize,
maxBufferSize: maxBufferSize,
readerPos: position,
readerLineEndPos: position,
}
}
func (b *backward) endOfFile() bool {
return b.readerPos <= 0
}
func (b *backward) endOfScan() bool {
return b.endOfFile() && b.readerLineEndPos <= 0
}
func (b *backward) allocateChunk() error {
chunkSize := minInt(b.readerPos, b.maxChunkSize)
if b.chunk == nil {
b.chunk = make([]byte, chunkSize)
}
b.chunk = b.chunk[:chunkSize]
n, err := b.reader.ReadAt(b.chunk, int64(b.readerPos-chunkSize))
if err != nil {
if err == io.EOF {
return ErrInvalidPosition
}
return err
}
if n != chunkSize {
return ErrReadFailure
}
b.readerPos -= chunkSize
return nil
}
func (b *backward) allocateBuffer() error {
chunkSize := len(b.chunk)
bufferSize := len(b.buffer)
if chunkSize+bufferSize > b.maxBufferSize {
return ErrBufferOverflow
}
if chunkSize+bufferSize > cap(b.buffer) {
expandedBuffer := make([]byte, 0, chunkSize+bufferSize)
expandedBuffer = append(expandedBuffer, b.chunk...)
expandedBuffer = append(expandedBuffer, b.buffer...)
b.buffer = expandedBuffer
} else {
prevBufferSize := bufferSize
b.buffer = b.buffer[:chunkSize+prevBufferSize]
copy(b.buffer[chunkSize:], b.buffer[:prevBufferSize])
copy(b.buffer, b.chunk)
}
return nil
}
func (b *backward) removeLineFromBuffer(lineFeedStartPos int) string {
lineWithCR := b.buffer[lineFeedStartPos+1:]
line := removeCarriageReturn(lineWithCR)
b.buffer = b.buffer[:maxInt(lineFeedStartPos, 0)]
b.readerLineEndPos -= len(lineWithCR)
if lineFeedStartPos >= 0 {
b.readerLineEndPos--
}
return line
}
func (b *backward) read() error {
if err := b.allocateChunk(); err != nil {
return err
}
if err := b.allocateBuffer(); err != nil {
return err
}
return nil
}
func (b *backward) Line() (string, error) {
if b.err != nil {
return "", b.err
}
if b.endOfScan() {
return "", io.EOF
}
for {
lineFeedStartPos := bytes.LastIndexByte(b.buffer, '\n')
if lineFeedStartPos >= 0 {
return b.removeLineFromBuffer(lineFeedStartPos), nil
} else {
if b.endOfFile() {
return b.removeLineFromBuffer(-1), io.EOF
}
if b.err = b.read(); b.err != nil {
return "", b.err
}
}
}
}
func (b *backward) Position() int {
if b.readerLineEndPos <= 0 {
return endPosition
}
return b.readerLineEndPos
}