this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Avoid buffering when reader supports ReadString

+16 -5
+14 -2
gitdiff/parser.go
··· 13 13 // the first file is returned as the second value. If an error occurs while 14 14 // parsing, it returns all files parsed before the error. 15 15 func Parse(r io.Reader) ([]*File, string, error) { 16 - p := &parser{r: bufio.NewReader(r)} 16 + p := newParser(r) 17 + 17 18 if err := p.Next(); err != nil { 18 19 if err == io.EOF { 19 20 return nil, "", nil ··· 66 67 // - if returning an object, advance to the first line after the object 67 68 // - any exported parsing methods must initialize the parser by calling Next() 68 69 70 + type stringReader interface { 71 + ReadString(delim byte) (string, error) 72 + } 73 + 69 74 type parser struct { 70 - r *bufio.Reader 75 + r stringReader 71 76 72 77 eof bool 73 78 lineno int64 74 79 lines [3]string 80 + } 81 + 82 + func newParser(r io.Reader) *parser { 83 + if r, ok := r.(stringReader); ok { 84 + return &parser{r: r} 85 + } 86 + return &parser{r: bufio.NewReader(r)} 75 87 } 76 88 77 89 // Next advances the parser by one line. It returns any error encountered while
+2 -3
gitdiff/parser_test.go
··· 1 1 package gitdiff 2 2 3 3 import ( 4 - "bufio" 4 + "bytes" 5 5 "encoding/binary" 6 6 "encoding/json" 7 7 "io" 8 8 "os" 9 9 "reflect" 10 - "strings" 11 10 "testing" 12 11 ) 13 12 ··· 490 489 } 491 490 492 491 func newTestParser(input string, init bool) *parser { 493 - p := &parser{r: bufio.NewReader(strings.NewReader(input))} 492 + p := newParser(bytes.NewBufferString(input)) 494 493 if init { 495 494 _ = p.Next() 496 495 }