OSDN Git Service

Update to current version of Go library (revision 94d654be2064).
[pf3gnuchains/gcc-fork.git] / libgo / go / bytes / buffer.go
index 62cf828..1acd4e0 100644 (file)
@@ -154,17 +154,20 @@ func (b *Buffer) ReadFrom(r io.Reader) (n int64, err os.Error) {
 }
 
 // WriteTo writes data to w until the buffer is drained or an error
-// occurs. The return value n is the number of bytes written.
+// occurs. The return value n is the number of bytes written; it always
+// fits into an int, but it is int64 to match the io.WriterTo interface.
 // Any error encountered during the write is also returned.
 func (b *Buffer) WriteTo(w io.Writer) (n int64, err os.Error) {
        b.lastRead = opInvalid
-       for b.off < len(b.buf) {
+       if b.off < len(b.buf) {
                m, e := w.Write(b.buf[b.off:])
-               n += int64(m)
                b.off += m
+               n = int64(m)
                if e != nil {
                        return n, e
                }
+               // otherwise all bytes were written, by definition of
+               // Write method in io.Writer
        }
        // Buffer is now empty; reset.
        b.Truncate(0)
@@ -301,6 +304,36 @@ func (b *Buffer) UnreadByte() os.Error {
        return nil
 }
 
+// ReadBytes reads until the first occurrence of delim in the input,
+// returning a slice containing the data up to and including the delimiter.
+// If ReadBytes encounters an error before finding a delimiter,
+// it returns the data read before the error and the error itself (often os.EOF).
+// ReadBytes returns err != nil if and only if the returned data does not end in
+// delim.
+func (b *Buffer) ReadBytes(delim byte) (line []byte, err os.Error) {
+       i := IndexByte(b.buf[b.off:], delim)
+       size := i + 1
+       if i < 0 {
+               size = len(b.buf) - b.off
+               err = os.EOF
+       }
+       line = make([]byte, size)
+       copy(line, b.buf[b.off:])
+       b.off += size
+       return
+}
+
+// ReadString reads until the first occurrence of delim in the input,
+// returning a string containing the data up to and including the delimiter.
+// If ReadString encounters an error before finding a delimiter,
+// it returns the data read before the error and the error itself (often os.EOF).
+// ReadString returns err != nil if and only if the returned data does not end
+// in delim.
+func (b *Buffer) ReadString(delim byte) (line string, err os.Error) {
+       bytes, err := b.ReadBytes(delim)
+       return string(bytes), err
+}
+
 // NewBuffer creates and initializes a new Buffer using buf as its initial
 // contents.  It is intended to prepare a Buffer to read existing data.  It
 // can also be used to size the internal buffer for writing.  To do that,