diff options
Diffstat (limited to 'libgo/go/strings/reader.go')
-rw-r--r-- | libgo/go/strings/reader.go | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/libgo/go/strings/reader.go b/libgo/go/strings/reader.go index 8569805..98325ce 100644 --- a/libgo/go/strings/reader.go +++ b/libgo/go/strings/reader.go @@ -10,7 +10,7 @@ import ( "unicode/utf8" ) -// A Reader implements the io.Reader, io.ReaderAt, io.Seeker, +// A Reader implements the io.Reader, io.ReaderAt, io.Seeker, io.WriterTo, // io.ByteScanner, and io.RuneScanner interfaces by reading // from a string. type Reader struct { @@ -120,6 +120,25 @@ func (r *Reader) Seek(offset int64, whence int) (int64, error) { return abs, nil } +// WriteTo implements the io.WriterTo interface. +func (r *Reader) WriteTo(w io.Writer) (n int64, err error) { + r.prevRune = -1 + if r.i >= len(r.s) { + return 0, io.EOF + } + s := r.s[r.i:] + m, err := io.WriteString(w, s) + if m > len(s) { + panic("strings.Reader.WriteTo: invalid WriteString count") + } + r.i += m + n = int64(m) + if m != len(s) && err == nil { + err = io.ErrShortWrite + } + return +} + // NewReader returns a new Reader reading from s. // It is similar to bytes.NewBufferString but more efficient and read-only. func NewReader(s string) *Reader { return &Reader{s, 0, -1} } |