From: Evan Shaw Date: Tue, 28 Jun 2011 06:10:39 +0000 (+1000) Subject: io.WriteString: if the object has a WriteString method, use it X-Git-Tag: weekly.2011-07-07~98 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=cf3eeb29841d671c63d4d308bf8f5a99906d33d8;p=gostls13.git io.WriteString: if the object has a WriteString method, use it This avoids allocation when writing to bytes.Buffers and bufio.Writers, for example. R=golang-dev, rsc, r, consalus, r CC=golang-dev https://golang.org/cl/4625068 --- diff --git a/src/pkg/io/io.go b/src/pkg/io/io.go index 790cf94e7c..b879fe5b72 100644 --- a/src/pkg/io/io.go +++ b/src/pkg/io/io.go @@ -209,8 +209,16 @@ type RuneScanner interface { UnreadRune() os.Error } +// stringWriter is the interface that wraps the WriteString method. +type stringWriter interface { + WriteString(s string) (n int, err os.Error) +} + // WriteString writes the contents of the string s to w, which accepts an array of bytes. func WriteString(w Writer, s string) (n int, err os.Error) { + if sw, ok := w.(stringWriter); ok { + return sw.WriteString(s) + } return w.Write([]byte(s)) }