From f9091033da16659b8aa23890b2294c42550e76b2 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 16 Nov 2017 16:29:19 -0500 Subject: simplify --- frontend.go | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/frontend.go b/frontend.go index 256ba52..3319eee 100644 --- a/frontend.go +++ b/frontend.go @@ -16,6 +16,16 @@ func (e UnsupportedCommand) Error() string { return "Unsupported command: " + string(e) } +func trimLinePrefix(line string, prefix string) string { + if !strings.HasPrefix(line, prefix) { + panic("line didn't have prefix") + } + if !strings.HasSuffix(line, "\n") { + panic("line didn't have prefix") + } + return strings.TrimSuffix(strings.TrimPrefix(line, prefix), "\n") +} + // A Frontend is something that produces a fast-import stream; the // Frontend object provides methods for reading from it. type Frontend struct { @@ -53,19 +63,17 @@ func (f *Frontend) nextLine() (line string, err error) { f.cmd <- CmdComment{Comment: line[1:]} case strings.HasPrefix(line, "cat-blob "): // 'cat-blob' SP LF - dataref := strings.TrimSuffix(strings.TrimPrefix(line, "cat-blob "), "\n") - f.cmd <- CmdCatBlob{DataRef: dataref} + f.cmd <- CmdCatBlob{DataRef: trimLinePrefix(line, "cat-blob ")} case strings.HasPrefix(line, "get-mark :"): // 'get-mark' SP ':' LF - strIdnum := strings.TrimSuffix(strings.TrimPrefix(line, "get-mark :"), "\n") - var nIdnum int - nIdnum, err = strconv.Atoi(strIdnum) + c := CmdGetMark{} + c.Mark, err = strconv.Atoi(trimLinePrefix(line, "get-mark :")) if err != nil { line = "" err = fmt.Errorf("get-mark: %v", err) return } - f.cmd <- CmdGetMark{Mark: nIdnum} + f.cmd <- c default: return } @@ -77,14 +85,14 @@ func parse_data(line string) (data string, err error) { if nl < 0 { return "", fmt.Errorf("data: expected newline: %v", data) } - head := line[:nl] + head := line[:nl+1] rest := line[nl+1:] if !strings.HasPrefix(head, "data ") { return "", fmt.Errorf("data: could not parse: %v", data) } if strings.HasPrefix(head, "data <<") { // Delimited format - delim := strings.TrimPrefix(head, "data <<") + delim := trimLinePrefix(head, "data <<") suffix := "\n" + delim + "\n" if !strings.HasSuffix(rest, suffix) { return "", fmt.Errorf("data: did not find suffix: %v", suffix) @@ -92,12 +100,11 @@ func parse_data(line string) (data string, err error) { data = strings.TrimSuffix(rest, suffix) } else { // Exact byte count format - strN := strings.TrimSuffix(head, "data ") - intN, err := strconv.Atoi(strN) + size, err := strconv.Atoi(trimLinePrefix(head, "data ")) if err != nil { return "", err } - if intN != len(rest) { + if size != len(rest) { panic("FIReader should not have let this happen") } data = rest @@ -122,8 +129,7 @@ func (f *Frontend) parse() error { return err } if strings.HasPrefix(line, "mark :") { - str := strings.TrimSuffix(strings.TrimPrefix(line, "mark :"), "\n") - c.Mark, err = strconv.Atoi(str) + c.Mark, err = strconv.Atoi(trimLinePrefix(line, "mark :")) if err != nil { return err } @@ -157,7 +163,7 @@ func (f *Frontend) parse() error { continue case strings.HasPrefix(line, "feature "): // 'feature' SP ('=' )? LF - str := strings.TrimSuffix(strings.TrimPrefix(line, "feature "), "\n") + str := trimLinePrefix(line, "feature ") eq := strings.IndexByte(str, '=') if eq < 0 { f.cmd <- CmdFeature{ @@ -178,28 +184,25 @@ func (f *Frontend) parse() error { return fmt.Errorf("ls: outside of a commit both and are required: %v", line) } f.cmd <- CmdLs{ - DataRef: line[sp1+1:sp2], - Path: textproto.PathUnescape(line[sp2+1:lf]), + DataRef: line[sp1+1 : sp2], + Path: textproto.PathUnescape(line[sp2+1 : lf]), } case strings.HasPrefix(line, "option "): // 'option' SP