Skip to content

Commit 3d1274d

Browse files
committed
Remove unused LineReader interface
This is no longer used for text application, so revert the parser back to using StringReader.
1 parent 0b7af3f commit 3d1274d

File tree

3 files changed

+10
-139
lines changed

3 files changed

+10
-139
lines changed

gitdiff/io.go

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,10 @@
11
package gitdiff
22

33
import (
4-
"bufio"
54
"errors"
6-
"fmt"
75
"io"
86
)
97

10-
// StringReader is the interface that wraps the ReadString method.
11-
//
12-
// ReadString reads until the first occurrence of delim in the input, returning
13-
// a string containing the data up to and including the delimiter. If
14-
// ReadString encounters an error before finding a delimiter, it returns the
15-
// data read before the error and the error itself (often io.EOF). ReadString
16-
// returns err != nil if and only if the returned data does not end in delim.
17-
type StringReader interface {
18-
ReadString(delim byte) (string, error)
19-
}
20-
21-
type readStringReader interface {
22-
io.Reader
23-
StringReader
24-
}
25-
26-
// LineReader is the interface that wraps the ReadLine method.
27-
//
28-
// ReadLine reads the next full line in the input, returing the the data
29-
// including the line ending character(s) and the zero-indexed line number. If
30-
// ReadLine encounters an error before reaching the end of the line, it returns
31-
// the data read before the error, the number of the line, and the error itself
32-
// (often io.EOF). ReadLine returns err != nil if and only if the returned data
33-
// is not a complete line.
34-
//
35-
// If an implementation defines other methods for reading the same input, line
36-
// numbers may be incorrect if calls to ReadLine are mixed with calls to other
37-
// read methods.
38-
type LineReader interface {
39-
ReadLine() (string, int64, error)
40-
}
41-
42-
// NewLineReader returns a LineReader starting at a specific line and using the
43-
// newline character, \n, as a line separator. If r is a StringReader, it is
44-
// used directly. Otherwise, it is wrapped in a way that may read extra data
45-
// from the underlying input.
46-
func NewLineReader(r io.Reader, lineno int64) LineReader {
47-
sr, ok := r.(readStringReader)
48-
if !ok {
49-
sr = bufio.NewReader(r)
50-
}
51-
return &lineReader{r: sr, n: lineno}
52-
}
53-
54-
type lineReader struct {
55-
r readStringReader
56-
n int64
57-
}
58-
59-
func (lr *lineReader) ReadLine() (line string, lineno int64, err error) {
60-
lineno = lr.n
61-
line, err = lr.r.ReadString('\n')
62-
if err == nil {
63-
lr.n++
64-
}
65-
return
66-
}
67-
68-
// unwrapLineReader returns a plain io.Reader that was converted to a
69-
// LineReader by wrapping or casting. It should only be called from functions
70-
// that accept an io.Reader as an argument and then convert it.
71-
func unwrapLineReader(lr LineReader) io.Reader {
72-
switch r := lr.(type) {
73-
case io.Reader:
74-
return r
75-
case *lineReader:
76-
return r.r
77-
default:
78-
panic(fmt.Sprintf("%T does not implement io.Reader and is not a gitdiff wrapper", lr))
79-
}
80-
}
81-
828
// LineReaderAt is the interface that wraps the ReadLinesAt method.
839
//
8410
// ReadLinesAt reads len(lines) into lines starting at line offset in the

gitdiff/io_test.go

Lines changed: 0 additions & 57 deletions
This file was deleted.

gitdiff/parser.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@
44
package gitdiff
55

66
import (
7+
"bufio"
78
"fmt"
89
"io"
910
)
1011

1112
// Parse parses a patch with changes to one or more files. Any content before
1213
// the first file is returned as the second value. If an error occurs while
1314
// parsing, it returns all files parsed before the error.
14-
//
15-
// If r is a LineReader or StringReader, it is used directly. Otherwise, it is
16-
// wrapped in a way that may read extra data from the underlying input.
1715
func Parse(r io.Reader) ([]*File, string, error) {
1816
p := newParser(r)
1917

@@ -69,19 +67,23 @@ func Parse(r io.Reader) ([]*File, string, error) {
6967
// - if returning an object, advance to the first line after the object
7068
// - any exported parsing methods must initialize the parser by calling Next()
7169

70+
type stringReader interface {
71+
ReadString(delim byte) (string, error)
72+
}
73+
7274
type parser struct {
73-
r LineReader
75+
r stringReader
7476

7577
eof bool
7678
lineno int64
7779
lines [3]string
7880
}
7981

8082
func newParser(r io.Reader) *parser {
81-
if lr, ok := r.(LineReader); ok {
82-
return &parser{r: lr}
83+
if r, ok := r.(stringReader); ok {
84+
return &parser{r: r}
8385
}
84-
return &parser{r: NewLineReader(r, 0)}
86+
return &parser{r: bufio.NewReader(r)}
8587
}
8688

8789
// Next advances the parser by one line. It returns any error encountered while
@@ -117,7 +119,7 @@ func (p *parser) shiftLines() (err error) {
117119
for i := 0; i < len(p.lines)-1; i++ {
118120
p.lines[i] = p.lines[i+1]
119121
}
120-
p.lines[len(p.lines)-1], _, err = p.r.ReadLine()
122+
p.lines[len(p.lines)-1], err = p.r.ReadString('\n')
121123
return
122124
}
123125

0 commit comments

Comments
 (0)