]> Cypherpunks repositories - gostls13.git/commitdiff
io: export StringWriter
authorDaniel Martí <mvdan@mvdan.cc>
Wed, 3 Oct 2018 20:01:14 +0000 (21:01 +0100)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 3 Oct 2018 20:13:35 +0000 (20:13 +0000)
And start using it elsewhere in the standard library, removing the
copies in the process.

While at it, rewrite the io.WriteString godoc to be more clear, since it
can now make reference to the defined interface.

Fixes #27946.

Change-Id: Id5ba223c09c19e5fb49815bd3b1bd3254fc786f3
Reviewed-on: https://go-review.googlesource.com/c/139457
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/fmt/fmt_test.go
src/io/io.go
src/io/multi.go
src/net/http/header.go
src/net/http/serve_test.go
src/strings/replace.go

index 9581becd32c3b63727285bf1ee3d56c07b446573..d63271a805b86840116d9a7c88a8f2eef990be72 100644 (file)
@@ -131,15 +131,10 @@ func (byteFormatter) Format(f State, _ rune) {
 
 var byteFormatterSlice = []byteFormatter{'h', 'e', 'l', 'l', 'o'}
 
-// Copy of io.stringWriter interface used by writeStringFormatter for type assertion.
-type stringWriter interface {
-       WriteString(s string) (n int, err error)
-}
-
 type writeStringFormatter string
 
 func (sf writeStringFormatter) Format(f State, c rune) {
-       if sw, ok := f.(stringWriter); ok {
+       if sw, ok := f.(io.StringWriter); ok {
                sw.WriteString("***" + string(sf) + "***")
        }
 }
index 72b75813a5e348c542acf5c628ae438789ef8dfd..2010770e6a428ee5d0d669dc7dd8cdf5a1b0abf6 100644 (file)
@@ -278,16 +278,16 @@ type RuneScanner interface {
        UnreadRune() error
 }
 
-// stringWriter is the interface that wraps the WriteString method.
-type stringWriter interface {
+// StringWriter is the interface that wraps the WriteString method.
+type StringWriter interface {
        WriteString(s string) (n int, err error)
 }
 
 // WriteString writes the contents of the string s to w, which accepts a slice of bytes.
-// If w implements a WriteString method, it is invoked directly.
+// If w implements StringWriter, its WriteString method is invoked directly.
 // Otherwise, w.Write is called exactly once.
 func WriteString(w Writer, s string) (n int, err error) {
-       if sw, ok := w.(stringWriter); ok {
+       if sw, ok := w.(StringWriter); ok {
                return sw.WriteString(s)
        }
        return w.Write([]byte(s))
index 65f99099ca7ea727040f7e483daf18cac1a59a88..24ee71e4ca65b19c19b83c366e5fcdb48571ac4f 100644 (file)
@@ -69,12 +69,12 @@ func (t *multiWriter) Write(p []byte) (n int, err error) {
        return len(p), nil
 }
 
-var _ stringWriter = (*multiWriter)(nil)
+var _ StringWriter = (*multiWriter)(nil)
 
 func (t *multiWriter) WriteString(s string) (n int, err error) {
        var p []byte // lazily initialized if/when needed
        for _, w := range t.writers {
-               if sw, ok := w.(stringWriter); ok {
+               if sw, ok := w.(StringWriter); ok {
                        n, err = sw.WriteString(s)
                } else {
                        if p == nil {
index d932f0900abecf7bc09601990aac323839ff7e18..611ee047050c76062ff61769199c193c01893ccf 100644 (file)
@@ -99,10 +99,6 @@ func ParseTime(text string) (t time.Time, err error) {
 
 var headerNewlineToSpace = strings.NewReplacer("\n", " ", "\r", " ")
 
-type writeStringer interface {
-       WriteString(string) (int, error)
-}
-
 // stringWriter implements WriteString on a Writer.
 type stringWriter struct {
        w io.Writer
@@ -158,7 +154,7 @@ func (h Header) WriteSubset(w io.Writer, exclude map[string]bool) error {
 }
 
 func (h Header) writeSubset(w io.Writer, exclude map[string]bool, trace *httptrace.ClientTrace) error {
-       ws, ok := w.(writeStringer)
+       ws, ok := w.(io.StringWriter)
        if !ok {
                ws = stringWriter{w}
        }
index b12fcf4f9eb2ecd1540d9639662170bf19d023fe..a282c4bc1776d6ba09fab6b673af3cfbe225c2ef 100644 (file)
@@ -4028,21 +4028,18 @@ func TestRequestBodyCloseDoesntBlock(t *testing.T) {
        }
 }
 
-// test that ResponseWriter implements io.stringWriter.
+// test that ResponseWriter implements io.StringWriter.
 func TestResponseWriterWriteString(t *testing.T) {
        okc := make(chan bool, 1)
        ht := newHandlerTest(HandlerFunc(func(w ResponseWriter, r *Request) {
-               type stringWriter interface {
-                       WriteString(s string) (n int, err error)
-               }
-               _, ok := w.(stringWriter)
+               _, ok := w.(io.StringWriter)
                okc <- ok
        }))
        ht.rawResponse("GET / HTTP/1.0")
        select {
        case ok := <-okc:
                if !ok {
-                       t.Error("ResponseWriter did not implement io.stringWriter")
+                       t.Error("ResponseWriter did not implement io.StringWriter")
                }
        default:
                t.Error("handler was never called")
index dbda9501945b8df85eeed51c1939ef72b3da2b73..9ddf5e1e3ff9612820a8c2dd82b13f4adf5073c8 100644 (file)
@@ -308,10 +308,6 @@ func (w *appendSliceWriter) WriteString(s string) (int, error) {
        return len(s), nil
 }
 
-type stringWriterIface interface {
-       WriteString(string) (int, error)
-}
-
 type stringWriter struct {
        w io.Writer
 }
@@ -320,8 +316,8 @@ func (w stringWriter) WriteString(s string) (int, error) {
        return w.w.Write([]byte(s))
 }
 
-func getStringWriter(w io.Writer) stringWriterIface {
-       sw, ok := w.(stringWriterIface)
+func getStringWriter(w io.Writer) io.StringWriter {
+       sw, ok := w.(io.StringWriter)
        if !ok {
                sw = stringWriter{w}
        }