]> Cypherpunks.ru repositories - netstring.git/commitdiff
Simplify :-finding
authorSergey Matveev <stargrave@stargrave.org>
Fri, 20 Jan 2023 07:54:14 +0000 (10:54 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Fri, 20 Jan 2023 07:54:14 +0000 (10:54 +0300)
r.go

diff --git a/r.go b/r.go
index 7a6d28452521f0f03eb5a09822ab1e8c8b69092a..68a93ca4985d19ad81966c4f9327f350d7c24452 100644 (file)
--- 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