From 11b459cba8723309c30d39b877b90d78feb8a482 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Sat, 16 Jan 2010 13:06:50 +1100 Subject: [PATCH] give bufio.Writer.WriteString the same signature as bytes.Buffer.WriteString Fixes #535. R=rsc CC=golang-dev https://golang.org/cl/189096 --- src/pkg/bufio/bufio.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/pkg/bufio/bufio.go b/src/pkg/bufio/bufio.go index 00bc53cd73..7b7f25831e 100644 --- a/src/pkg/bufio/bufio.go +++ b/src/pkg/bufio/bufio.go @@ -387,7 +387,7 @@ func (b *Writer) Buffered() int { return b.n } // Write writes the contents of p into the buffer. // It returns the number of bytes written. -// If nn < len(p), also returns an error explaining +// If nn < len(p), it also returns an error explaining // why the write is short. func (b *Writer) Write(p []byte) (nn int, err os.Error) { if b.err != nil { @@ -438,9 +438,12 @@ func (b *Writer) WriteByte(c byte) os.Error { } // WriteString writes a string. -func (b *Writer) WriteString(s string) os.Error { +// It returns the number of bytes written. +// If the count is less than len(s), it also returns an error explaining +// why the write is short. +func (b *Writer) WriteString(s string) (int, os.Error) { if b.err != nil { - return b.err + return 0, b.err } // Common case, worth making fast. if b.Available() >= len(s) || len(b.buf) >= len(s) && b.Flush() == nil { @@ -448,12 +451,15 @@ func (b *Writer) WriteString(s string) os.Error { b.buf[b.n] = s[i] b.n++ } - return nil + return len(s), nil } for i := 0; i < len(s); i++ { // loop over bytes, not runes. b.WriteByte(s[i]) + if b.err != nil { + return i, b.err + } } - return b.err + return len(s), nil } // buffered input and output