]> Cypherpunks repositories - gostls13.git/commitdiff
return "<nil>" when calling String() on a nil bytes.Buffer.
authorRob Pike <r@golang.org>
Sat, 31 Oct 2009 20:28:22 +0000 (13:28 -0700)
committerRob Pike <r@golang.org>
Sat, 31 Oct 2009 20:28:22 +0000 (13:28 -0700)
R=rsc
CC=go-dev
http://go/go-review/1016005

src/pkg/bytes/buffer.go
src/pkg/bytes/buffer_test.go

index 7acddc4bcdaaa12dd0b62bdfc7b2e93cc914b8d6..6e5887cb0b590376b81dcbd12bf030d5f47aac52 100644 (file)
@@ -42,8 +42,12 @@ func (b *Buffer) Bytes() []byte {
 }
 
 // String returns the contents of the unread portion of the buffer
-// as a string.
+// as a string.  If the Buffer is a nil pointer, it returns "<nil>".
 func (b *Buffer) String() string {
+       if b == nil {
+               // Special case, useful in debugging.
+               return "<nil>"
+       }
        return string(b.buf[b.off : len(b.buf)]);
 }
 
index f82c984685f0034be30564e34fe7324c84b65b6b..c364eece4f992125cc9c2e72ab26523c0d98347c 100644 (file)
@@ -232,3 +232,11 @@ func TestMixedReadsAndWrites(t *testing.T) {
        }
        empty(t, "TestMixedReadsAndWrites (2)", &buf, s, make([]byte, buf.Len()));
 }
+
+
+func TestNil(t *testing.T) {
+       var b *Buffer;
+       if b.String() != "<nil>" {
+               t.Error("expcted <nil>; got %q", b.String());
+       }
+}