From 8071395b87f7611b06b6121ff4d8ae1db5aef3cb Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 22 Nov 2017 22:40:53 -0500 Subject: favor SplitN over repeated IndexByte --- cmd_commit.go | 16 ++++++++++------ parse_catblob.go | 21 +++++++++++++-------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/cmd_commit.go b/cmd_commit.go index b108158..1e2672d 100644 --- a/cmd_commit.go +++ b/cmd_commit.go @@ -28,17 +28,19 @@ func (FileModify) fiCmdRead(fir fiReader) (cmd Cmd, err error) { return nil, err } str := trimLinePrefix(line, "M ") - sp1 := strings.IndexByte(str, ' ') - sp2 := strings.IndexByte(str[sp1+1:], ' ') - if sp1 < 0 || sp2 < 0 { + fields := strings.SplitN(str, " ", 3) + if len(fields) != 3 { return nil, fmt.Errorf("commit: malformed modify command: %v", line) } - nMode, err := strconv.ParseUint(str[:sp1], 8, 18) + + nMode, err := strconv.ParseUint(fields[0], 8, 18) if err != nil { return nil, err } - ref := str[sp1+1 : sp2] - path := textproto.PathUnescape(str[sp2+1:]) + + ref := fields[1] + path := textproto.PathUnescape(fields[2]) + if ref == "inline" { line, err = fir.ReadLine() if err != nil { @@ -170,8 +172,10 @@ func (NoteModify) fiCmdRead(fir fiReader) (cmd Cmd, err error) { if sp < 0 { return nil, fmt.Errorf("commit: malformed notemodify command: %v", line) } + ref := str[:sp] commitish := str[sp+1:] + if ref == "inline" { line, err = fir.ReadLine() if err != nil { diff --git a/parse_catblob.go b/parse_catblob.go index 5daaa77..7a018cf 100644 --- a/parse_catblob.go +++ b/parse_catblob.go @@ -74,19 +74,24 @@ func cbpLs(line string) (mode textproto.Mode, dataref string, path textproto.Pat if line[len(line)-1] != '\n' { return 0, "", "", fmt.Errorf("ls: missing trailing newline") } + line = line[:len(line)-1] + if strings.HasPrefix(line, "missing ") { - strPath := line[8 : len(line)-1] + strPath := line[8:] return 0, "", textproto.PathUnescape(strPath), nil } else { - sp1 := strings.IndexByte(line, ' ') - sp2 := strings.IndexByte(line[sp1+1:], ' ') - ht := strings.IndexByte(line[sp2+1:], '\t') - if sp1 < 0 || sp2 < 0 || ht < 0 { + fields := strings.SplitN(line, " ", 3) + if len(fields) < 3 { + return 0, "", "", fmt.Errorf("ls: malformed line: %q", line) + } + ht := strings.IndexByte(fields[2], '\t') + if ht < 0 { return 0, "", "", fmt.Errorf("ls: malformed line: %q", line) } - strMode := line[:sp1] - strRef := line[sp2+1 : ht] - strPath := line[ht+1 : len(line)-1] + strMode := fields[0] + //strType := fields[1] + strRef := fields[2][:ht] + strPath := fields[2][ht+1:] nMode, err := strconv.ParseUint(strMode, 8, 18) if err != nil { -- cgit v1.2.3