-
Notifications
You must be signed in to change notification settings - Fork 108
/
matching.go
638 lines (599 loc) · 19.3 KB
/
matching.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// sift
// Copyright (C) 2014-2016 Sven Taute
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"bufio"
"bytes"
"io"
"os"
"regexp"
"sort"
)
// processReader is the main routine working on an io.Reader
func processReader(reader io.Reader, matchRegexes []*regexp.Regexp, data []byte, testBuffer []byte, target string) error {
var (
bufferOffset int
err error
isEOF bool
lastInputBlockSize int
lastMatch *Match
lastRoundMultilineWindow bool
lastSeekAmount int
lastValidMatchRange int
linecount int64 = 1
matchChan chan Matches
matchCount int64
offset int64
resultIsBinary bool
resultStreaming bool
validMatchRange int
)
matches := make([]Match, 0, 16)
conditionMatches := make([]Match, 0, 16)
for {
if isEOF {
break
}
var length int
lastConditionMatch := len(conditionMatches) - 1
if options.Multiline {
if lastRoundMultilineWindow {
// if the last input block was greater than the sliding window size, that last part has to be processed again
copy(data[bufferOffset:bufferOffset+InputMultilineWindow], data[lastInputBlockSize-InputMultilineWindow:lastInputBlockSize])
length, err = reader.Read(data[bufferOffset+InputMultilineWindow:])
length += bufferOffset
length += InputMultilineWindow
} else {
length, err = reader.Read(data[bufferOffset:])
length += bufferOffset
}
if err != nil {
if err == io.EOF {
isEOF = true
} else {
return err
}
}
lastInputBlockSize = length
// if the input block was greater than the sliding window size, only process a part of it
if !isEOF && length > InputMultilineWindow {
validMatchRange = length - InputMultilineWindow
lastRoundMultilineWindow = true
} else {
validMatchRange = length
lastRoundMultilineWindow = false
}
} else {
// single line mode
length, err = reader.Read(data[bufferOffset:])
if err != nil {
if err == io.EOF {
isEOF = true
} else {
return err
}
}
length += bufferOffset
validMatchRange = length
lastInputBlockSize = length
}
lastValidMatchRange = validMatchRange
// check if file is binary (0x00 found in first 256 bytes)
if offset == 0 {
s := 256
if length < s {
s = length
}
for i := 0; i < s; i++ {
if data[i] == 0 {
resultIsBinary = true
if options.BinarySkip {
return nil
}
break
}
}
}
// adjust validMatchRange to end on a newline
lastSeekAmount = 0
if !isEOF {
newLineFound := false
var pos int
for pos = validMatchRange - 1; pos >= 0; pos-- {
if data[pos] == '\n' {
newLineFound = true
break
}
}
if newLineFound {
lastSeekAmount = validMatchRange - 1 - pos
validMatchRange = validMatchRange - lastSeekAmount
bufferOffset = 0
} else {
if lastInputBlockSize == InputBlockSize {
return errLineTooLong
}
bufferOffset = validMatchRange
continue
}
}
var testDataPtr []byte
if options.IgnoreCase {
bytesToLower(data, testBuffer, length)
testDataPtr = testBuffer[0:length]
} else {
testDataPtr = data[0:length]
}
var newMatches Matches
for _, re := range matchRegexes {
tmpMatches := getMatches(re, data, testDataPtr, offset, length, validMatchRange, 0, target)
if len(tmpMatches) > 0 {
newMatches = append(newMatches, tmpMatches...)
}
}
// sort matches and filter duplicates
if len(newMatches) > 0 {
sort.Sort(Matches(newMatches))
var validMatch bool
prevMatch := lastMatch
for i := 0; i < len(newMatches); {
validMatch = false
if prevMatch == nil {
validMatch = true
} else {
m := newMatches[i]
if (!options.Multiline && m.lineEnd > prevMatch.lineEnd) ||
(options.Multiline && m.start >= prevMatch.end) {
validMatch = true
}
}
if validMatch {
prevMatch = &newMatches[i]
i++
} else {
copy(newMatches[i:], newMatches[i+1:])
newMatches = newMatches[0 : len(newMatches)-1]
}
}
}
for conditionID, condition := range global.conditions {
tmpMatches := getMatches(condition.regex, data, testDataPtr, offset, length, validMatchRange, conditionID, target)
if len(tmpMatches) > 0 {
conditionMatches = append(conditionMatches, tmpMatches...)
}
}
if len(conditionMatches) > 0 {
sort.Sort(Matches(conditionMatches))
}
if options.ShowLineNumbers || options.ContextBefore > 0 || options.ContextAfter > 0 || len(global.conditions) > 0 {
linecount = countLines(data, lastConditionMatch, newMatches, conditionMatches, offset, validMatchRange, linecount)
}
if len(newMatches) > 0 {
// if a list option is used exit here if possible
if (options.FilesWithMatches || options.FilesWithoutMatch) && !options.Count && len(global.conditions) == 0 {
global.resultsChan <- &Result{target: target, matches: []Match{Match{}}}
return nil
}
lastMatch = &newMatches[len(newMatches)-1]
if resultStreaming {
matchChan <- newMatches
} else {
matches = append(matches, newMatches...)
if len(matches) > global.streamingThreshold && global.streamingAllowed {
resultStreaming = true
matchChan = make(chan Matches, 16)
global.resultsChan <- &Result{target: target, matches: matches, streaming: true, matchChan: matchChan, isBinary: resultIsBinary}
defer func() {
close(matchChan)
}()
}
}
matchCount += int64(len(newMatches))
if options.Limit != 0 && matchCount >= options.Limit {
break
}
}
// copy the bytes not processed after the last newline to the beginning of the buffer
if lastSeekAmount > 0 {
copy(data[bufferOffset:bufferOffset+lastSeekAmount], data[lastValidMatchRange-lastSeekAmount:lastValidMatchRange])
bufferOffset += lastSeekAmount
}
offset += int64(validMatchRange)
}
if !resultStreaming {
global.resultsChan <- &Result{target: target, matches: matches, conditionMatches: conditionMatches, streaming: false, isBinary: resultIsBinary}
}
return nil
}
// getMatches gets all matches in the provided data, it is used for normal and condition matches.
//
// data contains the original data.
// testBuffer contains the data to test the regex against (potentially modified, e.g. to support the ignore case option).
// length contains the length of the provided data.
// matches are only valid if they start within the validMatchRange.
func getMatches(regex *regexp.Regexp, data []byte, testBuffer []byte, offset int64, length int, validMatchRange int, conditionID int, target string) Matches {
var matches Matches
if allIndex := regex.FindAllIndex(testBuffer, -1); allIndex != nil {
// for _, index := range allindex {
for mi := 0; mi < len(allIndex); mi++ {
index := allIndex[mi]
start := index[0]
end := index[1]
// \s always matches newline, leading to incorrect matches in non-multiline mode
// analyze match and reject false matches
if !options.Multiline {
// remove newlines at the beginning of the match
for ; start < length && end > start && data[start] == 0x0a; start++ {
}
// remove newlines at the end of the match
for ; end > 0 && end > start && data[end-1] == 0x0a; end-- {
}
// check if the corrected match is still valid
if !regex.Match(testBuffer[start:end]) {
continue
}
// check if the match contains newlines
if bytes.Contains(data[start:end], []byte{0x0a}) {
// Rebuild the complete lines to check whether these contain valid matches.
// In very rare cases, multiple lines may contain a valid match. As multiple
// matches cannot be processed correctly here, requeue them to be processed again.
lineStart := start
lineEnd := end
for lineStart > 0 && data[lineStart-1] != 0x0a {
lineStart--
}
for lineEnd < length && data[lineEnd] != 0x0a {
lineEnd++
}
lastStart := lineStart
for pos := lastStart + 1; pos < lineEnd; pos++ {
if data[pos] == 0x0a || pos == lineEnd-1 {
if pos == lineEnd-1 && data[pos] != 0x0a {
pos++
}
if idx := regex.FindIndex(testBuffer[lastStart:pos]); idx != nil {
start = lastStart
end = pos
start = lastStart + idx[0]
end = lastStart + idx[1]
allIndex = append(allIndex, []int{start, end})
}
lastStart = pos + 1
}
}
continue
}
}
lineStart := start
lineEnd := end
if options.Multiline && start >= validMatchRange {
continue
}
for lineStart > 0 && data[lineStart-1] != 0x0a {
lineStart--
}
for lineEnd < length && data[lineEnd] != 0x0a {
lineEnd++
}
var contextBefore *string
var contextAfter *string
if options.ContextBefore > 0 {
var contextBeforeStart int
if lineStart > 0 {
contextBeforeStart = lineStart - 1
precedingLinesFound := 0
for contextBeforeStart > 0 {
if data[contextBeforeStart-1] == 0x0a {
precedingLinesFound++
if precedingLinesFound == options.ContextBefore {
break
}
}
contextBeforeStart--
}
if precedingLinesFound < options.ContextBefore && contextBeforeStart == 0 && offset > 0 {
contextBefore = getBeforeContextFromFile(target, offset, start)
} else {
tmp := string(data[contextBeforeStart : lineStart-1])
contextBefore = &tmp
}
} else {
if offset > 0 {
contextBefore = getBeforeContextFromFile(target, offset, start)
} else {
contextBefore = nil
}
}
}
if options.ContextAfter > 0 {
var contextAfterEnd int
if lineEnd < length-1 {
contextAfterEnd = lineEnd
followingLinesFound := 0
for contextAfterEnd < length-1 {
if data[contextAfterEnd+1] == 0x0a {
followingLinesFound++
if followingLinesFound == options.ContextAfter {
contextAfterEnd++
break
}
}
contextAfterEnd++
}
if followingLinesFound < options.ContextAfter && contextAfterEnd == length-1 {
contextAfter = getAfterContextFromFile(target, offset, end)
} else {
tmp := string(data[lineEnd+1 : contextAfterEnd])
contextAfter = &tmp
}
} else {
contextAfter = getAfterContextFromFile(target, offset, end)
}
}
m := Match{
conditionID: conditionID,
start: offset + int64(start),
end: offset + int64(end),
lineStart: offset + int64(lineStart),
lineEnd: offset + int64(lineEnd),
match: string(data[start:end]),
line: string(data[lineStart:lineEnd]),
contextBefore: contextBefore,
contextAfter: contextAfter,
}
// handle special case where '^' matches after the last newline
if int(lineStart) != validMatchRange {
matches = append(matches, m)
}
}
}
return matches
}
// countLines counts the linebreaks within the given buffer and calculates the correct line numbers for new matches
func countLines(data []byte, lastConditionMatch int, matches Matches, conditionMatches Matches, offset int64, validMatchRange int, lineCount int64) int64 {
currentMatch := 0
currentConditionMatch := lastConditionMatch + 1
if currentMatch < len(matches) || currentConditionMatch < len(conditionMatches) {
for i := 0; i < validMatchRange; i++ {
if data[i] == 0xa {
for currentMatch < len(matches) && offset+int64(i) >= matches[currentMatch].lineStart {
matches[currentMatch].lineno = lineCount
currentMatch++
}
for currentConditionMatch < len(conditionMatches) && offset+int64(i) >= conditionMatches[currentConditionMatch].lineStart {
conditionMatches[currentConditionMatch].lineno = lineCount
currentConditionMatch++
}
lineCount++
}
}
// check for matches on last line without newline
for currentMatch < len(matches) && offset+int64(validMatchRange) >= matches[currentMatch].lineStart {
matches[currentMatch].lineno = lineCount
currentMatch++
}
for currentConditionMatch < len(conditionMatches) && offset+int64(validMatchRange) >= conditionMatches[currentConditionMatch].lineStart {
conditionMatches[currentConditionMatch].lineno = lineCount
currentConditionMatch++
}
} else {
lineCount += int64(countNewlines(data, validMatchRange))
}
return lineCount
}
// applyConditions removes matches from a result that do not fulfill all conditions
func (result *Result) applyConditions() {
if len(result.matches) == 0 || len(global.conditions) == 0 {
return
}
// check conditions that are independent of found matches
conditionStatus := make([]bool, len(global.conditions))
var conditionFulfilled bool
for _, conditionMatch := range result.conditionMatches {
conditionFulfilled = false
switch global.conditions[conditionMatch.conditionID].conditionType {
case ConditionFileMatches:
conditionFulfilled = true
case ConditionLineMatches:
if conditionMatch.lineno == global.conditions[conditionMatch.conditionID].lineRangeStart {
conditionFulfilled = true
}
case ConditionRangeMatches:
if conditionMatch.lineno >= global.conditions[conditionMatch.conditionID].lineRangeStart &&
conditionMatch.lineno <= global.conditions[conditionMatch.conditionID].lineRangeEnd {
conditionFulfilled = true
}
default:
// ingore other condition types
conditionFulfilled = !global.conditions[conditionMatch.conditionID].negated
}
if conditionFulfilled {
if global.conditions[conditionMatch.conditionID].negated {
result.matches = Matches{}
return
}
conditionStatus[conditionMatch.conditionID] = true
}
}
for i := range conditionStatus {
if conditionStatus[i] != true && !global.conditions[i].negated {
result.matches = Matches{}
return
}
}
MatchLoop:
// check for each match whether preceded/followed/surrounded conditions are fulfilled
for matchIndex := 0; matchIndex < len(result.matches); {
match := result.matches[matchIndex]
lineno := match.lineno
conditionStatus := make([]bool, len(global.conditions))
for _, conditionMatch := range result.conditionMatches {
conditionFulfilled := false
maxAllowedDistance := global.conditions[conditionMatch.conditionID].within
var actualDistance int64 = -1
switch global.conditions[conditionMatch.conditionID].conditionType {
case ConditionPreceded:
actualDistance = lineno - conditionMatch.lineno
if actualDistance == 0 {
conditionFulfilled = conditionMatch.start < match.start
} else {
conditionFulfilled = (actualDistance >= 0) && (maxAllowedDistance == -1 || actualDistance <= maxAllowedDistance)
}
case ConditionFollowed:
actualDistance = conditionMatch.lineno - lineno
if actualDistance == 0 {
conditionFulfilled = conditionMatch.start > match.start
} else {
conditionFulfilled = (actualDistance >= 0) && (maxAllowedDistance == -1 || actualDistance <= maxAllowedDistance)
}
case ConditionSurrounded:
if lineno > conditionMatch.lineno {
actualDistance = lineno - conditionMatch.lineno
} else {
actualDistance = conditionMatch.lineno - lineno
}
if actualDistance == 0 {
conditionFulfilled = true
} else {
conditionFulfilled = (actualDistance >= 0) && (maxAllowedDistance == -1 || actualDistance <= maxAllowedDistance)
}
default:
// ingore other condition types
conditionFulfilled = !global.conditions[conditionMatch.conditionID].negated
}
if conditionFulfilled {
if global.conditions[conditionMatch.conditionID].negated {
goto ConditionFailed
} else {
conditionStatus[conditionMatch.conditionID] = true
}
}
}
for i := range conditionStatus {
if conditionStatus[i] != true && !global.conditions[i].negated {
goto ConditionFailed
}
}
matchIndex++
continue MatchLoop
ConditionFailed:
copy(result.matches[matchIndex:], result.matches[matchIndex+1:])
result.matches = result.matches[0 : len(result.matches)-1]
}
}
// getBeforeContextFromFile gets the context lines directly from the file.
// It is used when the context lines exceed the currently buffered data from the file.
func getBeforeContextFromFile(target string, offset int64, start int) *string {
var contextBeforeStart int
infile, _ := os.Open(target)
seekPosition := offset + int64(start) - int64(InputBlockSize)
if seekPosition < 0 {
seekPosition = 0
}
count := InputBlockSize
if offset == 0 && start < InputBlockSize {
count = start
}
infile.Seek(seekPosition, 0)
reader := bufio.NewReader(infile)
buffer := make([]byte, count)
reader.Read(buffer)
lineStart := len(buffer)
for lineStart > 0 && buffer[lineStart-1] != 0x0a {
lineStart--
}
if lineStart > 0 {
contextBeforeStart = lineStart - 1
precedingLinesFound := 0
for contextBeforeStart > 0 {
if buffer[contextBeforeStart-1] == 0x0a {
precedingLinesFound++
if precedingLinesFound == options.ContextBefore {
break
}
}
contextBeforeStart--
}
tmp := string(buffer[contextBeforeStart : lineStart-1])
return &tmp
}
return nil
}
// getAfterContextFromFile gets the context lines directly from the file.
// It is used when the context lines exceed the currently buffered data from the file.
func getAfterContextFromFile(target string, offset int64, end int) *string {
var contextAfterEnd int
infile, _ := os.Open(target)
seekPosition := offset + int64(end)
infile.Seek(seekPosition, 0)
reader := bufio.NewReader(infile)
buffer := make([]byte, InputBlockSize)
length, _ := reader.Read(buffer)
lineEnd := 0
for lineEnd < length && buffer[lineEnd] != 0x0a {
lineEnd++
}
if lineEnd < length-1 {
contextAfterEnd = lineEnd
followingLinesFound := 0
for contextAfterEnd < length-1 {
if buffer[contextAfterEnd+1] == 0x0a {
followingLinesFound++
if followingLinesFound == options.ContextAfter {
contextAfterEnd++
break
}
}
contextAfterEnd++
}
if followingLinesFound < options.ContextAfter && contextAfterEnd == length-1 && buffer[length-1] != 0x0a {
contextAfterEnd++
}
tmp := string(buffer[lineEnd+1 : contextAfterEnd])
return &tmp
}
return nil
}
// processInvertMatchesReader is used to handle the '--invert' option.
// This function works line based and provides very limited support for options.
func processReaderInvertMatch(reader io.Reader, matchRegexes []*regexp.Regexp, target string) error {
matches := make([]Match, 0, 16)
var linecount int64
var matchFound bool
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
linecount++
matchFound = false
for _, re := range global.matchRegexes {
if re.MatchString(line) {
matchFound = true
}
}
if !matchFound {
if options.FilesWithMatches || options.FilesWithoutMatch {
global.resultsChan <- &Result{matches: []Match{Match{}}, target: target}
return nil
}
m := Match{
lineno: linecount,
line: line}
matches = append(matches, m)
}
}
result := &Result{matches: matches, target: target}
global.resultsChan <- result
return nil
}