mirror of
https://github.com/golang/go
synced 2024-11-21 19:14:44 -07:00
bytes: fix bugs in buffer.ReadBytes
Fixes #1498. R=golang-dev, mattn, r, rsc CC=golang-dev https://golang.org/cl/4140041
This commit is contained in:
parent
4ee90b764e
commit
bbfad5f1cc
@ -312,13 +312,14 @@ func (b *Buffer) UnreadByte() os.Error {
|
|||||||
// delim.
|
// delim.
|
||||||
func (b *Buffer) ReadBytes(delim byte) (line []byte, err os.Error) {
|
func (b *Buffer) ReadBytes(delim byte) (line []byte, err os.Error) {
|
||||||
i := IndexByte(b.buf[b.off:], delim)
|
i := IndexByte(b.buf[b.off:], delim)
|
||||||
size := i + 1 - b.off
|
size := i + 1
|
||||||
if i < 0 {
|
if i < 0 {
|
||||||
size = len(b.buf) - b.off
|
size = len(b.buf) - b.off
|
||||||
err = os.EOF
|
err = os.EOF
|
||||||
}
|
}
|
||||||
line = make([]byte, size)
|
line = make([]byte, size)
|
||||||
copy(line, b.buf[b.off:])
|
copy(line, b.buf[b.off:])
|
||||||
|
b.off += size
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -350,25 +350,36 @@ func TestNext(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var readBytesTests = []struct {
|
var readBytesTests = []struct {
|
||||||
buffer []byte
|
buffer string
|
||||||
delim byte
|
delim byte
|
||||||
expected []byte
|
expected []string
|
||||||
err os.Error
|
err os.Error
|
||||||
}{
|
}{
|
||||||
{err: os.EOF},
|
{"", 0, []string{""}, os.EOF},
|
||||||
{[]byte{}, 0, []byte{}, os.EOF},
|
{"a\x00", 0, []string{"a\x00"}, nil},
|
||||||
{[]byte("a\x00"), 0, []byte("a\x00"), nil},
|
{"abbbaaaba", 'b', []string{"ab", "b", "b", "aaab"}, nil},
|
||||||
{[]byte("hello\x01world"), 1, []byte("hello\x01"), nil},
|
{"hello\x01world", 1, []string{"hello\x01"}, nil},
|
||||||
{[]byte("foo\nbar"), 0, []byte("foo\nbar"), os.EOF},
|
{"foo\nbar", 0, []string{"foo\nbar"}, os.EOF},
|
||||||
{[]byte("alpha beta gamma"), ' ', []byte("alpha "), nil},
|
{"alpha\nbeta\ngamma\n", '\n', []string{"alpha\n", "beta\n", "gamma\n"}, nil},
|
||||||
|
{"alpha\nbeta\ngamma", '\n', []string{"alpha\n", "beta\n", "gamma"}, os.EOF},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadBytes(t *testing.T) {
|
func TestReadBytes(t *testing.T) {
|
||||||
for _, test := range readBytesTests {
|
for _, test := range readBytesTests {
|
||||||
buf := NewBuffer(test.buffer)
|
buf := NewBufferString(test.buffer)
|
||||||
bytes, err := buf.ReadBytes(test.delim)
|
var err os.Error
|
||||||
if !Equal(bytes, test.expected) || err != test.err {
|
for _, expected := range test.expected {
|
||||||
t.Errorf("expected %q, %v got %q, %v", test.expected, test.err, bytes, err)
|
var bytes []byte
|
||||||
|
bytes, err = buf.ReadBytes(test.delim)
|
||||||
|
if string(bytes) != expected {
|
||||||
|
t.Errorf("expected %q, got %q", expected, bytes)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != test.err {
|
||||||
|
t.Errorf("expected error %v, got %v", test.err, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user