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 --- parse_catblob.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'parse_catblob.go') 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