From 3a61c7072642334ab9aa738230063811cba8db25 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Fri, 20 Jan 2023 10:54:14 +0300 Subject: [PATCH] Simplify :-finding --- r.go | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/r.go b/r.go index 7a6d284..68a93ca 100644 --- a/r.go +++ b/r.go @@ -19,7 +19,6 @@ package netstring import ( "bufio" - "bytes" "errors" "io" "strconv" @@ -44,19 +43,12 @@ func (r *Reader) Next() (uint64, error) { if !r.eof { return 0, errors.New("current chunk is unread") } - p, _ := r.r.Peek(21) - if len(p) == 0 { - return 0, io.EOF - } - idx := bytes.IndexByte(p, ':') - if idx == -1 { - return 0, errors.New("no length separator found") - } - size, err := strconv.ParseUint(string(p[:idx]), 10, 64) + lenRaw, err := r.r.ReadSlice(':') if err != nil { return 0, err } - if _, err = r.r.Discard(idx + 1); err != nil { + size, err := strconv.ParseUint(string(lenRaw[:len(lenRaw)-1]), 10, 64) + if err != nil { return 0, err } r.left = size -- 2.44.0