aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/sourcegraph/go-diff/diff/reader_util.go
diff options
context:
space:
mode:
authorTaras Madan <tarasmadan@google.com>2025-01-22 16:07:17 +0100
committerTaras Madan <tarasmadan@google.com>2025-01-23 10:42:36 +0000
commit7b4377ad9d8a7205416df8d6217ef2b010f89481 (patch)
treee6fec4fd12ff807a16d847923f501075bf71d16c /vendor/github.com/sourcegraph/go-diff/diff/reader_util.go
parent475a4c203afb8b7d3af51c4fd32bb170ff32a45e (diff)
vendor: delete
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.go120
1 files changed, 0 insertions, 120 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
deleted file mode 100644
index 45300252b..000000000
--- a/vendor/github.com/sourcegraph/go-diff/diff/reader_util.go
+++ /dev/null
@@ -1,120 +0,0 @@
-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
-// io.EOF error when there is nothing left to read (at the start of the function call). It
-// will return any other errors it receives from the underlying call to ReadBytes.
-func readLine(r *bufio.Reader) ([]byte, error) {
- line_, err := r.ReadBytes('\n')
- if err == io.EOF {
- if len(line_) == 0 {
- return nil, io.EOF
- }
-
- // ReadBytes returned io.EOF, because it didn't find another newline, but there is
- // still the remainder of the file to return as a line.
- line := line_
- return line, nil
- } else if err != nil {
- return nil, err
- }
- line := line_[0 : len(line_)-1]
- return dropCR(line), nil
-}
-
-// dropCR drops a terminal \r from the data.
-func dropCR(data []byte) []byte {
- if len(data) > 0 && data[len(data)-1] == '\r' {
- return data[0 : len(data)-1]
- }
- return data
-}