aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/sourcegraph/go-diff/diff/reader_util.go
diff options
context:
space:
mode:
authorTaras Madan <tarasmadan@google.com>2023-02-22 22:16:50 +0100
committerTaras Madan <tarasmadan@google.com>2023-02-24 12:47:23 +0100
commit4165372ec8fd142475a4e35fd0cf4f8042132208 (patch)
tree21cd62211b4dd80bee469054c5b65db77342333c /vendor/github.com/sourcegraph/go-diff/diff/reader_util.go
parent2b3ed821a493b8936c8bacfa6f8b4f1c90a00855 (diff)
dependencies: update
set go min requirements to 1.19 update dependencies update vendor
Diffstat (limited to 'vendor/github.com/sourcegraph/go-diff/diff/reader_util.go')
-rw-r--r--vendor/github.com/sourcegraph/go-diff/diff/reader_util.go83
1 files changed, 83 insertions, 0 deletions
diff --git a/vendor/github.com/sourcegraph/go-diff/diff/reader_util.go b/vendor/github.com/sourcegraph/go-diff/diff/reader_util.go
index 395fb7baf..45300252b 100644
--- a/vendor/github.com/sourcegraph/go-diff/diff/reader_util.go
+++ b/vendor/github.com/sourcegraph/go-diff/diff/reader_util.go
@@ -2,9 +2,92 @@ package diff
import (
"bufio"
+ "bytes"
+ "errors"
"io"
)
+var ErrLineReaderUninitialized = errors.New("line reader not initialized")
+
+func newLineReader(r io.Reader) *lineReader {
+ return &lineReader{reader: bufio.NewReader(r)}
+}
+
+// lineReader is a wrapper around a bufio.Reader that caches the next line to
+// provide lookahead functionality for the next two lines.
+type lineReader struct {
+ reader *bufio.Reader
+
+ cachedNextLine []byte
+ cachedNextLineErr error
+}
+
+// readLine returns the next unconsumed line and advances the internal cache of
+// the lineReader.
+func (l *lineReader) readLine() ([]byte, error) {
+ if l.cachedNextLine == nil && l.cachedNextLineErr == nil {
+ l.cachedNextLine, l.cachedNextLineErr = readLine(l.reader)
+ }
+
+ if l.cachedNextLineErr != nil {
+ return nil, l.cachedNextLineErr
+ }
+
+ next := l.cachedNextLine
+
+ l.cachedNextLine, l.cachedNextLineErr = readLine(l.reader)
+
+ return next, nil
+}
+
+// nextLineStartsWith looks at the line that would be returned by the next call
+// to readLine to check whether it has the given prefix.
+//
+// io.EOF and bufio.ErrBufferFull errors are ignored so that the function can
+// be used when at the end of the file.
+func (l *lineReader) nextLineStartsWith(prefix string) (bool, error) {
+ if l.cachedNextLine == nil && l.cachedNextLineErr == nil {
+ l.cachedNextLine, l.cachedNextLineErr = readLine(l.reader)
+ }
+
+ return l.lineHasPrefix(l.cachedNextLine, prefix, l.cachedNextLineErr)
+}
+
+// nextNextLineStartsWith checks the prefix of the line *after* the line that
+// would be returned by the next readLine.
+//
+// io.EOF and bufio.ErrBufferFull errors are ignored so that the function can
+// be used when at the end of the file.
+//
+// The lineReader MUST be initialized by calling readLine at least once before
+// calling nextLineStartsWith. Otherwise ErrLineReaderUninitialized will be
+// returned.
+func (l *lineReader) nextNextLineStartsWith(prefix string) (bool, error) {
+ if l.cachedNextLine == nil && l.cachedNextLineErr == nil {
+ l.cachedNextLine, l.cachedNextLineErr = readLine(l.reader)
+ }
+
+ next, err := l.reader.Peek(len(prefix))
+ return l.lineHasPrefix(next, prefix, err)
+}
+
+// lineHasPrefix checks whether the given line has the given prefix with
+// bytes.HasPrefix.
+//
+// The readErr should be the error that was returned when the line was read.
+// lineHasPrefix checks the error to adjust its return value to, e.g., return
+// false and ignore the error when readErr is io.EOF.
+func (l *lineReader) lineHasPrefix(line []byte, prefix string, readErr error) (bool, error) {
+ if readErr != nil {
+ if readErr == io.EOF || readErr == bufio.ErrBufferFull {
+ return false, nil
+ }
+ return false, readErr
+ }
+
+ return bytes.HasPrefix(line, []byte(prefix)), nil
+}
+
// readLine is a helper that mimics the functionality of calling bufio.Scanner.Scan() and
// bufio.Scanner.Bytes(), but without the token size limitation. It will read and return
// the next line in the Reader with the trailing newline stripped. It will return an