1
0
mirror of https://github.com/golang/go synced 2024-11-22 02:04:40 -07:00

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
This commit is contained in:
Russ Cox 2010-06-02 16:17:18 -07:00
parent 4fc97c4703
commit 041d978d84
2 changed files with 17 additions and 15 deletions

View File

@ -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 {

View File

@ -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)
}
}