Cleanup readLine

This commit is contained in:
Vladimir Mihailenco 2020-10-10 12:26:21 +03:00
parent b66d05bd63
commit e1b0a0bd7e
1 changed files with 18 additions and 20 deletions

View File

@ -69,29 +69,27 @@ func (r *Reader) ReadLine() ([]byte, error) {
// - there is a pending read error; // - there is a pending read error;
// - or line does not end with \r\n. // - or line does not end with \r\n.
func (r *Reader) readLine() ([]byte, error) { func (r *Reader) readLine() ([]byte, error) {
var s []byte b, err := r.rd.ReadSlice('\n')
multi := false if err != nil {
for { if err != bufio.ErrBufferFull {
b, err := r.rd.ReadSlice('\n') return nil, err
}
full := make([]byte, len(b))
copy(full, b)
b, err = r.rd.ReadBytes('\n')
if err != nil { if err != nil {
// in case the end of the buffer is not reached return nil, err
if err == bufio.ErrBufferFull {
s = append(s, b...)
multi = true
continue
} else {
return nil, err
}
} }
if len(b) <= 2 || b[len(b)-1] != '\n' || b[len(b)-2] != '\r' {
return nil, fmt.Errorf("redis: invalid reply: %q", b) full = append(full, b...)
} b = full
if multi {
b = append(s, b...)
}
b = b[:len(b)-2]
return b, nil
} }
if len(b) <= 2 || b[len(b)-1] != '\n' || b[len(b)-2] != '\r' {
return nil, fmt.Errorf("redis: invalid reply: %q", b)
}
return b[:len(b)-2], nil
} }
func (r *Reader) ReadReply(m MultiBulkParse) (interface{}, error) { func (r *Reader) ReadReply(m MultiBulkParse) (interface{}, error) {