|
1 | 1 | package gitdiff |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bufio" |
5 | 4 | "errors" |
6 | | - "fmt" |
7 | 5 | "io" |
8 | 6 | ) |
9 | 7 |
|
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 | | - |
82 | 8 | // LineReaderAt is the interface that wraps the ReadLinesAt method. |
83 | 9 | // |
84 | 10 | // ReadLinesAt reads len(lines) into lines starting at line offset in the |
|
0 commit comments