func processSingleExactMatcher(file io.Reader, matcher streammatch.Matcher, needBuffer bool) ([]matchRecord, error) { var reader io.Reader if needBuffer { reader = bufio.NewReaderSize(file, defaultBufSize) } else { reader = file } linereader := streammatch.NewLineReader(reader) lineOffset := 0 line := 0 lastLineMatches := 0 matches := make([]matchRecord, 0, 2) matcher.Reset() for { pos, err := matcher.FindMatch(linereader) if err == nil { matches = append(matches, matchRecord{line: line, lineOffset: lineOffset, linepos: pos}) } else if err == streammatch.EOL { newLineOffset := linereader.BytesRead() lastLineSize := newLineOffset - lineOffset for i := lastLineMatches; i < len(matches); i++ { matches[i].lineSize = lastLineSize } lastLineMatches = len(matches) line++ lineOffset = newLineOffset matcher.Reset() } else if err == io.EOF { newLineOffset := linereader.BytesRead() lastLineSize := newLineOffset - lineOffset for i := lastLineMatches; i < len(matches); i++ { matches[i].lineSize = lastLineSize matches[i].isLastLine = true } return matches, nil } else { return matches, err } } }
func processMultiExactMatcher(file io.Reader, matcher streammatch.MultiMatcher) ([]matchRecord, error) { reader := bufio.NewReader(file) linereader := streammatch.NewLineReader(reader) lineOffset := 0 line := 0 lastLineMatches := 0 matches := make([]matchRecord, 0, 2) matcher.Reset() for { pos, ptrns, err := matcher.FindMultipleMatches(linereader) if err == nil { for _, ptrn := range ptrns { matches = append(matches, matchRecord{line: line, lineOffset: lineOffset, linepos: pos, patternindex: ptrn}) } } else if err == streammatch.EOL { newLineOffset := linereader.BytesRead() lastLineSize := newLineOffset - lineOffset for i := lastLineMatches; i < len(matches); i++ { matches[i].lineSize = lastLineSize } lastLineMatches = len(matches) line++ lineOffset = newLineOffset matcher.Reset() } else if err == io.EOF { newLineOffset := linereader.BytesRead() lastLineSize := newLineOffset - lineOffset for i := lastLineMatches; i < len(matches); i++ { matches[i].lineSize = lastLineSize matches[i].isLastLine = true } return matches, nil } else { return matches, err } } }