Commit 041d978d authored by Russ Cox's avatar Russ Cox

bufio: change ReadSlice to match description

On error, ReadSlice is defined to return the available data
and advance past that data, but it was not behaving that
way for err == ErrBufferFull, making it harder for callers to
handle well.

R=r
CC=golang-dev
https://golang.org/cl/1480041
parent 4fc97c47
......@@ -229,7 +229,8 @@ func (b *Reader) ReadSlice(delim byte) (line []byte, err os.Error) {
// Buffer is full?
if b.Buffered() >= len(b.buf) {
return nil, ErrBufferFull
b.r = b.w
return b.buf, ErrBufferFull
}
}
panic("not reached")
......@@ -259,20 +260,9 @@ func (b *Reader) ReadBytes(delim byte) (line []byte, err os.Error) {
break
}
// Read bytes out of buffer.
buf := make([]byte, b.Buffered())
var n int
n, e = b.Read(buf)
if e != nil {
frag = buf[0:n]
err = e
break
}
if n != len(buf) {
frag = buf[0:n]
err = errInternal
break
}
// Make a copy of the buffer.
buf := make([]byte, len(frag))
copy(buf, frag)
// Grow list if needed.
if full == nil {
......
......@@ -407,3 +407,15 @@ func TestWriteString(t *testing.T) {
t.Errorf("WriteString wants %q gets %q", s, string(buf.Bytes()))
}
}
func TestBufferFull(t *testing.T) {
buf, _ := NewReaderSize(strings.NewReader("hello, world"), 5)
line, err := buf.ReadSlice(',')
if string(line) != "hello" || err != ErrBufferFull {
t.Errorf("first ReadSlice(,) = %q, %v", line, err)
}
line, err = buf.ReadSlice(',')
if string(line) != "," || err != nil {
t.Errorf("second ReadSlice(,) = %q, %v", line, err)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment