Implement fragment header parsing

Use a regexp for simplicity, unlike the direct parsing in Git.
This commit is contained in:
Billy Keyes
2019-03-13 23:04:01 -07:00
parent f41707bf2a
commit 4ddf1d962d
3 changed files with 126 additions and 6 deletions
+39 -1
View File
@@ -4,6 +4,8 @@ import (
"bufio"
"fmt"
"io"
"regexp"
"strconv"
"strings"
)
@@ -48,6 +50,11 @@ const (
newFilePrefix = "+++ "
)
var (
// TODO(bkeyes): are the boundary conditions necessary?
fragmentHeaderRegexp = regexp.MustCompile(`^@@ -(\d+),(\d+) \+(\d+)(?:,(\d+))? @@.*\n`)
)
// ParseNextFileHeader finds and parses the next file header in the stream. It
// returns nil if no headers are found before the end of the stream.
func (p *parser) ParseNextFileHeader() (file *File, err error) {
@@ -128,7 +135,38 @@ func (p *parser) ParseTraditionalFileHeader(f *File, oldFile, newFile string) er
}
func (p *parser) ParseFragmentHeader(f *Fragment, header string) error {
panic("unimplemented")
match := fragmentHeaderRegexp.FindStringSubmatch(header)
if len(match) < 5 {
return p.Errorf("invalid fragment header")
}
parseInt := func(s string, v *int64) (err error) {
if *v, err = strconv.ParseInt(s, 10, 64); err != nil {
nerr := err.(*strconv.NumError)
return p.Errorf("invalid fragment header value: %s: %v", s, nerr.Err)
}
return
}
if err := parseInt(match[1], &f.OldPosition); err != nil {
return err
}
if err := parseInt(match[2], &f.OldLines); err != nil {
return err
}
if err := parseInt(match[3], &f.NewPosition); err != nil {
return err
}
f.NewLines = 1
if match[4] != "" {
if err := parseInt(match[4], &f.NewLines); err != nil {
return err
}
}
return nil
}
// Line reads and returns the next line. The first call to Line after a call to