-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.go
More file actions
304 lines (242 loc) · 6.98 KB
/
reader.go
File metadata and controls
304 lines (242 loc) · 6.98 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package searchreader
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"strings"
"unicode"
)
// Match is search result, see SearcherReader.search().
type Match struct {
Index uint // Index of search []*bytes.Reader given in New (see: Option)
StartPosition uint // StartPosition of first matched byte (relative position of Read())
Length uint // Length in bytes
}
type findThis struct {
r Stream // Reader containing what bytes we are searching for
firstRune rune // Quick lookup for first matching rune
size uint64 // Size in bytes
caseSensitive bool // is search case-sensitive?
}
type SearcherReader struct {
src *bufio.Reader // Source io.Reader which is used for search source
searchers map[uint]findThis // What we are searching for
requiredBytes uint64 // Minimum buffer size required
buffer []byte // Internal source buffer for the SearcherReader.searchers
}
func New(source io.Reader, opts ...Option) (sr *SearcherReader) {
sr = &SearcherReader{
requiredBytes: 0,
src: bufio.NewReader(source),
searchers: make(map[uint]findThis),
}
if opts != nil {
for sIdx, opt := range opts {
if opt == nil {
// should this error out?
continue
}
// Default
def := &findThis{
r: nil,
firstRune: 0,
size: 0,
caseSensitive: false,
}
// Apply option
opt(def)
if def.r == nil {
panic(fmt.Errorf(`nil searcher #%d`, sIdx))
}
def.size = uint64(def.r.Size())
if def.size > sr.requiredBytes {
// Update minimum required bytes required for searching the source SearcherReader.src reader
sr.requiredBytes = def.size
}
// Get first byte as a possible search match hint so that lookup loops are a bit faster
firstByte, _, err := def.r.ReadRune()
if err != nil {
panic(err)
}
def.firstRune = firstByte
// Rewind to start
_, err = def.r.Seek(0, io.SeekStart)
if err != nil {
panic(err)
}
sr.searchers[uint(sIdx)] = *def
}
}
return sr
}
// readActual reads from the source io.Reader to a internal buffer
func (sr *SearcherReader) readActual() (err error) {
if uint64(len(sr.buffer)) > sr.requiredBytes*2 {
// We have enough bytes in internal buffer for search, so we don't read more
return nil
}
// We need double buffer space because the match could begin at the last Read() block byte
// For example Read(buf) buffer length 5:
// find = `xyz`
// Internal buffer = [`a`, `b`, `c`, `d`, `x`, `y`, `z`, `0`, `1`, `2`]
// User gets [`a`, `b`, `c`, `d`, `x`] with Read() with result beginning at last index and
// internal buffer in the reader is left with [`y`, `z`, `0`, `1`, `2`].
// User's next Read() gets the rest of the match
buffer := make([]byte, sr.requiredBytes*2)
readBytes, err := sr.src.Read(buffer)
if err != nil {
if errors.Is(err, io.EOF) && len(sr.buffer) > 0 {
// We have data still in the sr.buffer
err = nil
}
}
// Add read data from source stream to internal buffer
if readBytes > 0 {
sr.buffer = append(sr.buffer, buffer[0:readBytes]...)
}
return err
}
// search searches each sr.searchers bytes from internal buffer
func (sr *SearcherReader) search() (matches []Match) {
if sr.searchers == nil {
// No searchers, skip
return
}
if len(sr.searchers) == 0 {
// No searchers, skip
return
}
// key = searcher Index and value(s) are potential match start position(s)
potentialMatches := make(map[uint][]uint64)
// Change raw bytes internal buffer to string so that case-sensitive search can be made
sourceBuffer := strings.NewReader(string(sr.buffer))
for searcherIndex, searcher := range sr.searchers {
// Rewind to start
_, _ = sourceBuffer.Seek(0, io.SeekStart)
for {
offset, err := sourceBuffer.Seek(0, io.SeekCurrent)
if err != nil {
panic(err)
}
if uint64(offset) > sr.requiredBytes {
// Do not search beyond sr.requiredBytes limit
break
}
srcRune, srcSize, err := sourceBuffer.ReadRune()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
panic(err)
}
if srcSize == 0 {
panic(`size 0??`)
}
if unicode.IsLetter(srcRune) && !searcher.caseSensitive {
// searcher has all letters in lower case if it's case-insensitive.
// See: Option.WithCaseInsensitive
srcRune = unicode.ToLower(srcRune)
}
if srcRune == searcher.firstRune {
// Add potential start for match start
potentialMatches[searcherIndex] = append(potentialMatches[searcherIndex], uint64(offset))
}
}
}
if potentialMatches == nil {
// No matches
return
}
if len(potentialMatches) == 0 {
// No matches
return
}
// Search the potential matches
for searcherIndex, startingPositions := range potentialMatches {
for _, startingPosition := range startingPositions {
// Rewind source buffer to starting position of potential match
_, err := sourceBuffer.Seek(int64(startingPosition), io.SeekStart)
if err != nil {
panic(err)
}
searcher, ok := sr.searchers[searcherIndex]
if !ok {
panic(`invalid searcher index`)
}
foundSize := uint64(0)
// Rewind searcher to start
_, err = searcher.r.Seek(0, io.SeekStart)
if err != nil {
panic(err)
}
// Search rune-by-rune
for {
srcRune, srcSize, err := sourceBuffer.ReadRune()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
panic(err)
}
if srcSize == 0 {
panic(`size 0??`)
}
if unicode.IsLetter(srcRune) && !searcher.caseSensitive {
// searcher has all letters in lower case if it's case-insensitive.
// See: Option.WithCaseInsensitive
srcRune = unicode.ToLower(srcRune)
}
findRune, chsize, err := searcher.r.ReadRune()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
panic(err)
}
if chsize == 0 {
panic(`size 0??`)
}
if srcSize == chsize && findRune == srcRune {
foundSize++
} else {
// No match
foundSize = 0
break
}
}
if foundSize == searcher.size {
// match found
matches = append(matches, Match{
Index: searcherIndex,
StartPosition: uint(startingPosition),
Length: uint(searcher.size),
})
}
}
}
return matches
}
// Read reads internal buffer and returns bytes from the internal buffer's start and possible matches.
// End user has to keep track of matching Match data lengths search window.
func (sr *SearcherReader) Read(b []byte) (readBytes int, matches []Match, err error) {
if uint64(len(b)) > sr.requiredBytes {
// Update sr.requiredBytes if it's larger than search string(s)
sr.requiredBytes = uint64(len(b))
}
// Read to internal buffer
err = sr.readActual()
if err != nil {
return 0, matches, err
}
if len(sr.searchers) > 0 {
// We have searchers, do search
matches = sr.search()
}
tmp := bytes.NewReader(sr.buffer)
readBytes, err = tmp.Read(b)
// remove already read data from buffer
sr.buffer = sr.buffer[readBytes:]
return readBytes, matches, err
}