From 472e191a23996db0195317f65ba067872d3cdcb6 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 14 May 2009 17:03:47 -0700 Subject: [PATCH] ByteBuffer.WriteByte R=r DELTA=17 (10 added, 0 deleted, 7 changed) OCL=28860 CL=28862 --- src/lib/io/bytebuffer.go | 7 +++++++ src/lib/io/bytebuffer_test.go | 21 ++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/lib/io/bytebuffer.go b/src/lib/io/bytebuffer.go index bc6a73741b7..5d4cd8add39 100644 --- a/src/lib/io/bytebuffer.go +++ b/src/lib/io/bytebuffer.go @@ -74,6 +74,13 @@ func (b *ByteBuffer) Write(p []byte) (n int, err os.Error) { return n, nil } +// WriteByte appends the byte c to the buffer. +// Because Write never fails and WriteByte is not part of the +// io.Writer interface, it does not need to return a value. +func (b *ByteBuffer) WriteByte(c byte) { + b.Write([]byte{c}); +} + // Read reads the next len(p) bytes from the buffer or until the buffer // is drained. The return value n is the number of bytes read; err is always nil. func (b *ByteBuffer) Read(p []byte) (n int, err os.Error) { diff --git a/src/lib/io/bytebuffer_test.go b/src/lib/io/bytebuffer_test.go index e91a6415ee1..5a54322237f 100644 --- a/src/lib/io/bytebuffer_test.go +++ b/src/lib/io/bytebuffer_test.go @@ -101,20 +101,23 @@ func TestBasicOperations(t *testing.T) { } check(t, "TestBasicOperations (4)", &buf, "a"); - n, err = buf.Write(data[1 : 26]); - if n != 25 { + buf.WriteByte(data[1]); + check(t, "TestBasicOperations (5)", &buf, "ab"); + + n, err = buf.Write(data[2 : 26]); + if n != 24 { t.Errorf("wrote 25 bytes, but n == %d\n", n); } - check(t, "TestBasicOperations (5)", &buf, string(data[0 : 26])); - - buf.Truncate(26); check(t, "TestBasicOperations (6)", &buf, string(data[0 : 26])); - buf.Truncate(20); - check(t, "TestBasicOperations (7)", &buf, string(data[0 : 20])); + buf.Truncate(26); + check(t, "TestBasicOperations (7)", &buf, string(data[0 : 26])); - empty(t, "TestBasicOperations (8)", &buf, string(data[0 : 20]), make([]byte, 5)); - empty(t, "TestBasicOperations (9)", &buf, "", make([]byte, 100)); + buf.Truncate(20); + check(t, "TestBasicOperations (8)", &buf, string(data[0 : 20])); + + empty(t, "TestBasicOperations (9)", &buf, string(data[0 : 20]), make([]byte, 5)); + empty(t, "TestBasicOperations (10)", &buf, "", make([]byte, 100)); } }