blob: c2a8c9fe59a1dac78881ffb646bd512ec834fac7 [file] [log] [blame]
Rob Pike5f9254c2008-11-25 09:41:58 -08001// Copyright 2009 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Russ Coxd3a412a2009-06-29 15:24:23 -07005package bytes
Rob Pike5f9254c2008-11-25 09:41:58 -08006
Rob Pike9e7f3a42009-09-22 14:53:48 -07007// Simple byte buffer for marshaling data.
Rob Pike5f9254c2008-11-25 09:41:58 -08008
Rob Pike9e7f3a42009-09-22 14:53:48 -07009import (
Robert Griesemer5a1d3322009-12-15 15:33:31 -080010 "io"
11 "os"
Rob Pike0ac5ef72010-03-05 11:34:53 -080012 "utf8"
Rob Pike9e7f3a42009-09-22 14:53:48 -070013)
Rob Pike5f9254c2008-11-25 09:41:58 -080014
Rob Pike1f0e6a42009-12-19 08:42:31 +110015// A Buffer is a variable-sized buffer of bytes with Read and Write methods.
Rob Pike9e7f3a42009-09-22 14:53:48 -070016// The zero value for Buffer is an empty buffer ready to use.
17type Buffer struct {
Rob Pike0ac5ef72010-03-05 11:34:53 -080018 buf []byte // contents are the bytes buf[off : len(buf)]
19 off int // read at &buf[off], write at &buf[len(buf)]
20 runeBytes [utf8.UTFMax]byte // avoid allocation of slice on each WriteByte or Rune
21 bootstrap [64]byte // memory to hold first slice; helps small buffers (Printf) avoid allocation.
Roger Peppe415545e2010-12-06 14:10:10 -050022 lastRead readOp // last read operation, so that Unread* can work correctly.
Rob Pike9e7f3a42009-09-22 14:53:48 -070023}
24
Roger Peppe415545e2010-12-06 14:10:10 -050025// The readOp constants describe the last action performed on
26// the buffer, so that UnreadRune and UnreadByte can
27// check for invalid usage.
28type readOp int
29
30const (
31 opInvalid readOp = iota // Non-read operation.
32 opReadRune // Read rune.
33 opRead // Any other read operation.
34)
35
Rob Pike1f0e6a42009-12-19 08:42:31 +110036// Bytes returns a slice of the contents of the unread portion of the buffer;
37// len(b.Bytes()) == b.Len(). If the caller changes the contents of the
38// returned slice, the contents of the buffer will change provided there
39// are no intervening method calls on the Buffer.
Robert Griesemer5a1d3322009-12-15 15:33:31 -080040func (b *Buffer) Bytes() []byte { return b.buf[b.off:] }
Robert Griesemereea33fc2009-05-05 12:00:52 -070041
Rob Piked5be41f2009-09-16 15:15:00 -070042// String returns the contents of the unread portion of the buffer
Rob Pike63e668d2009-10-31 13:28:22 -070043// as a string. If the Buffer is a nil pointer, it returns "<nil>".
Rob Piked5be41f2009-09-16 15:15:00 -070044func (b *Buffer) String() string {
Rob Pike63e668d2009-10-31 13:28:22 -070045 if b == nil {
46 // Special case, useful in debugging.
Robert Griesemer40621d52009-11-09 12:07:39 -080047 return "<nil>"
Rob Pike63e668d2009-10-31 13:28:22 -070048 }
Robert Griesemer5a1d3322009-12-15 15:33:31 -080049 return string(b.buf[b.off:])
Rob Piked5be41f2009-09-16 15:15:00 -070050}
51
Rob Pike9e7f3a42009-09-22 14:53:48 -070052// Len returns the number of bytes of the unread portion of the buffer;
53// b.Len() == len(b.Bytes()).
Robert Griesemer5a1d3322009-12-15 15:33:31 -080054func (b *Buffer) Len() int { return len(b.buf) - b.off }
Rob Pike5f9254c2008-11-25 09:41:58 -080055
Russ Coxea79b822009-05-14 13:39:17 -070056// Truncate discards all but the first n unread bytes from the buffer.
Rob Pike9e7f3a42009-09-22 14:53:48 -070057// It is an error to call b.Truncate(n) with n > b.Len().
Russ Coxd3a412a2009-06-29 15:24:23 -070058func (b *Buffer) Truncate(n int) {
Roger Peppe415545e2010-12-06 14:10:10 -050059 b.lastRead = opInvalid
Rob Pike9e7f3a42009-09-22 14:53:48 -070060 if n == 0 {
61 // Reuse buffer space.
Robert Griesemer40621d52009-11-09 12:07:39 -080062 b.off = 0
Russ Coxbe869ba2009-05-18 13:31:56 -070063 }
Robert Griesemer5a1d3322009-12-15 15:33:31 -080064 b.buf = b.buf[0 : b.off+n]
Robert Griesemer28db3e82009-05-14 10:14:29 -070065}
66
Rob Pike7bb335c2009-03-06 03:43:44 -080067// Reset resets the buffer so it has no content.
Robert Griesemer28db3e82009-05-14 10:14:29 -070068// b.Reset() is the same as b.Truncate(0).
Robert Griesemer5a1d3322009-12-15 15:33:31 -080069func (b *Buffer) Reset() { b.Truncate(0) }
Rob Pike5f9254c2008-11-25 09:41:58 -080070
Russ Cox1baf35e2010-04-14 01:52:56 -070071// Grow buffer to guarantee space for n more bytes.
72// Return index where bytes should be written.
73func (b *Buffer) grow(n int) int {
74 m := b.Len()
75 // If buffer is empty, reset to recover space.
76 if m == 0 && b.off != 0 {
77 b.Truncate(0)
Rob Pike4c0e51c2009-12-06 12:03:52 -080078 }
Russ Cox1baf35e2010-04-14 01:52:56 -070079 if len(b.buf)+n > cap(b.buf) {
80 var buf []byte
81 if b.buf == nil && n <= len(b.bootstrap) {
Russ Coxbb84f4b2010-05-27 14:51:47 -070082 buf = b.bootstrap[0:]
Russ Cox1baf35e2010-04-14 01:52:56 -070083 } else {
84 // not enough space anywhere
85 buf = make([]byte, 2*cap(b.buf)+n)
86 copy(buf, b.buf[b.off:])
87 }
88 b.buf = buf
89 b.off = 0
90 }
91 b.buf = b.buf[0 : b.off+m+n]
92 return b.off + m
Rob Pike4c0e51c2009-12-06 12:03:52 -080093}
94
Rob Pike7bb335c2009-03-06 03:43:44 -080095// Write appends the contents of p to the buffer. The return
Robert Griesemereea33fc2009-05-05 12:00:52 -070096// value n is the length of p; err is always nil.
Russ Coxd3a412a2009-06-29 15:24:23 -070097func (b *Buffer) Write(p []byte) (n int, err os.Error) {
Roger Peppe415545e2010-12-06 14:10:10 -050098 b.lastRead = opInvalid
Russ Cox1baf35e2010-04-14 01:52:56 -070099 m := b.grow(len(p))
100 copy(b.buf[m:], p)
101 return len(p), nil
Rob Pike9e7f3a42009-09-22 14:53:48 -0700102}
103
Rob Pike4c0e51c2009-12-06 12:03:52 -0800104// WriteString appends the contents of s to the buffer. The return
105// value n is the length of s; err is always nil.
106func (b *Buffer) WriteString(s string) (n int, err os.Error) {
Roger Peppe415545e2010-12-06 14:10:10 -0500107 b.lastRead = opInvalid
Russ Cox1baf35e2010-04-14 01:52:56 -0700108 m := b.grow(len(s))
Russ Coxd86ab012010-10-26 21:52:54 -0700109 return copy(b.buf[m:], s), nil
Rob Pike4c0e51c2009-12-06 12:03:52 -0800110}
111
Rob Pikebc3e3472009-12-03 12:56:16 -0800112// MinRead is the minimum slice size passed to a Read call by
113// Buffer.ReadFrom. As long as the Buffer has at least MinRead bytes beyond
114// what is required to hold the contents of r, ReadFrom will not grow the
115// underlying buffer.
116const MinRead = 512
117
118// ReadFrom reads data from r until EOF and appends it to the buffer.
119// The return value n is the number of bytes read.
120// Any error except os.EOF encountered during the read
121// is also returned.
122func (b *Buffer) ReadFrom(r io.Reader) (n int64, err os.Error) {
Roger Peppe415545e2010-12-06 14:10:10 -0500123 b.lastRead = opInvalid
Rob Piked14c8132009-12-14 13:13:01 +1100124 // If buffer is empty, reset to recover space.
125 if b.off >= len(b.buf) {
126 b.Truncate(0)
127 }
Rob Pikebc3e3472009-12-03 12:56:16 -0800128 for {
129 if cap(b.buf)-len(b.buf) < MinRead {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800130 var newBuf []byte
Rob Pikebc3e3472009-12-03 12:56:16 -0800131 // can we get space without allocation?
132 if b.off+cap(b.buf)-len(b.buf) >= MinRead {
133 // reuse beginning of buffer
134 newBuf = b.buf[0 : len(b.buf)-b.off]
135 } else {
136 // not enough space at end; put space on end
137 newBuf = make([]byte, len(b.buf)-b.off, 2*(cap(b.buf)-b.off)+MinRead)
138 }
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800139 copy(newBuf, b.buf[b.off:])
140 b.buf = newBuf
141 b.off = 0
Rob Pikebc3e3472009-12-03 12:56:16 -0800142 }
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800143 m, e := r.Read(b.buf[len(b.buf):cap(b.buf)])
Rob Pike9baa7a52010-06-15 17:40:47 -0700144 b.buf = b.buf[0 : len(b.buf)+m]
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800145 n += int64(m)
Rob Pikebc3e3472009-12-03 12:56:16 -0800146 if e == os.EOF {
147 break
148 }
149 if e != nil {
150 return n, e
151 }
152 }
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800153 return n, nil // err is EOF, so return nil explicitly
Rob Pikebc3e3472009-12-03 12:56:16 -0800154}
155
156// WriteTo writes data to w until the buffer is drained or an error
Robert Griesemer4438f502011-02-09 15:09:08 -0800157// occurs. The return value n is the number of bytes written; it always
158// fits into an int, but it is int64 to match the io.WriterTo interface.
Rob Pikebc3e3472009-12-03 12:56:16 -0800159// Any error encountered during the write is also returned.
160func (b *Buffer) WriteTo(w io.Writer) (n int64, err os.Error) {
Roger Peppe415545e2010-12-06 14:10:10 -0500161 b.lastRead = opInvalid
Robert Griesemer4438f502011-02-09 15:09:08 -0800162 if b.off < len(b.buf) {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800163 m, e := w.Write(b.buf[b.off:])
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800164 b.off += m
Robert Griesemer4438f502011-02-09 15:09:08 -0800165 n = int64(m)
Rob Pikebc3e3472009-12-03 12:56:16 -0800166 if e != nil {
167 return n, e
168 }
Robert Griesemer4438f502011-02-09 15:09:08 -0800169 // otherwise all bytes were written, by definition of
170 // Write method in io.Writer
Rob Pikebc3e3472009-12-03 12:56:16 -0800171 }
Rob Piked14c8132009-12-14 13:13:01 +1100172 // Buffer is now empty; reset.
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800173 b.Truncate(0)
174 return
Rob Pikebc3e3472009-12-03 12:56:16 -0800175}
176
Robert Griesemer472e1912009-05-14 17:03:47 -0700177// WriteByte appends the byte c to the buffer.
Russ Cox55b70d6c2009-05-15 10:46:14 -0700178// The returned error is always nil, but is included
179// to match bufio.Writer's WriteByte.
Russ Coxd3a412a2009-06-29 15:24:23 -0700180func (b *Buffer) WriteByte(c byte) os.Error {
Roger Peppe415545e2010-12-06 14:10:10 -0500181 b.lastRead = opInvalid
Russ Cox1baf35e2010-04-14 01:52:56 -0700182 m := b.grow(1)
183 b.buf[m] = c
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800184 return nil
Robert Griesemer472e1912009-05-14 17:03:47 -0700185}
186
Rob Pike0ac5ef72010-03-05 11:34:53 -0800187// WriteRune appends the UTF-8 encoding of Unicode
188// code point r to the buffer, returning its length and
189// an error, which is always nil but is included
190// to match bufio.Writer's WriteRune.
Russ Cox8f571812011-10-25 22:22:09 -0700191func (b *Buffer) WriteRune(r rune) (n int, err os.Error) {
Rob Pike0ac5ef72010-03-05 11:34:53 -0800192 if r < utf8.RuneSelf {
193 b.WriteByte(byte(r))
194 return 1, nil
195 }
Adam Langley3cb4bdb2010-11-30 16:59:43 -0500196 n = utf8.EncodeRune(b.runeBytes[0:], r)
Rob Pike0ac5ef72010-03-05 11:34:53 -0800197 b.Write(b.runeBytes[0:n])
198 return n, nil
199}
200
Rob Pike7bb335c2009-03-06 03:43:44 -0800201// Read reads the next len(p) bytes from the buffer or until the buffer
Rob Pike57665532009-06-23 15:20:40 -0700202// is drained. The return value n is the number of bytes read. If the
203// buffer has no data to return, err is os.EOF even if len(p) is zero;
204// otherwise it is nil.
Russ Coxd3a412a2009-06-29 15:24:23 -0700205func (b *Buffer) Read(p []byte) (n int, err os.Error) {
Roger Peppe415545e2010-12-06 14:10:10 -0500206 b.lastRead = opInvalid
Rob Pike9e7f3a42009-09-22 14:53:48 -0700207 if b.off >= len(b.buf) {
Rob Piked14c8132009-12-14 13:13:01 +1100208 // Buffer is empty, reset to recover space.
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800209 b.Truncate(0)
210 return 0, os.EOF
Rob Pike57665532009-06-23 15:20:40 -0700211 }
Russ Cox78551a92010-04-26 10:02:01 -0700212 n = copy(p, b.buf[b.off:])
213 b.off += n
Roger Peppe415545e2010-12-06 14:10:10 -0500214 if n > 0 {
215 b.lastRead = opRead
216 }
Russ Cox78551a92010-04-26 10:02:01 -0700217 return
218}
Rob Pike9e7f3a42009-09-22 14:53:48 -0700219
Russ Cox78551a92010-04-26 10:02:01 -0700220// Next returns a slice containing the next n bytes from the buffer,
221// advancing the buffer as if the bytes had been returned by Read.
222// If there are fewer than n bytes in the buffer, Next returns the entire buffer.
223// The slice is only valid until the next call to a read or write method.
224func (b *Buffer) Next(n int) []byte {
Roger Peppe415545e2010-12-06 14:10:10 -0500225 b.lastRead = opInvalid
Russ Cox78551a92010-04-26 10:02:01 -0700226 m := b.Len()
Rob Pike9e7f3a42009-09-22 14:53:48 -0700227 if n > m {
Robert Griesemer40621d52009-11-09 12:07:39 -0800228 n = m
Robert Griesemereea33fc2009-05-05 12:00:52 -0700229 }
Russ Cox78551a92010-04-26 10:02:01 -0700230 data := b.buf[b.off : b.off+n]
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800231 b.off += n
Roger Peppe415545e2010-12-06 14:10:10 -0500232 if n > 0 {
233 b.lastRead = opRead
234 }
Russ Cox78551a92010-04-26 10:02:01 -0700235 return data
Rob Pike5f9254c2008-11-25 09:41:58 -0800236}
237
Scott Schwartz08aab442009-06-19 16:29:30 -0700238// ReadByte reads and returns the next byte from the buffer.
Russ Cox64684cc2009-06-22 13:26:13 -0700239// If no byte is available, it returns error os.EOF.
Russ Coxd3a412a2009-06-29 15:24:23 -0700240func (b *Buffer) ReadByte() (c byte, err os.Error) {
Roger Peppe415545e2010-12-06 14:10:10 -0500241 b.lastRead = opInvalid
Rob Pike9e7f3a42009-09-22 14:53:48 -0700242 if b.off >= len(b.buf) {
Rob Piked14c8132009-12-14 13:13:01 +1100243 // Buffer is empty, reset to recover space.
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800244 b.Truncate(0)
245 return 0, os.EOF
Scott Schwartz08aab442009-06-19 16:29:30 -0700246 }
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800247 c = b.buf[b.off]
248 b.off++
Roger Peppe415545e2010-12-06 14:10:10 -0500249 b.lastRead = opRead
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800250 return c, nil
Scott Schwartz08aab442009-06-19 16:29:30 -0700251}
252
Rob Pike0ac5ef72010-03-05 11:34:53 -0800253// ReadRune reads and returns the next UTF-8-encoded
254// Unicode code point from the buffer.
255// If no bytes are available, the error returned is os.EOF.
256// If the bytes are an erroneous UTF-8 encoding, it
257// consumes one byte and returns U+FFFD, 1.
Russ Cox8f571812011-10-25 22:22:09 -0700258func (b *Buffer) ReadRune() (r rune, size int, err os.Error) {
Roger Peppe415545e2010-12-06 14:10:10 -0500259 b.lastRead = opInvalid
Rob Pike0ac5ef72010-03-05 11:34:53 -0800260 if b.off >= len(b.buf) {
261 // Buffer is empty, reset to recover space.
262 b.Truncate(0)
263 return 0, 0, os.EOF
264 }
Roger Peppe415545e2010-12-06 14:10:10 -0500265 b.lastRead = opReadRune
Rob Pike0ac5ef72010-03-05 11:34:53 -0800266 c := b.buf[b.off]
267 if c < utf8.RuneSelf {
268 b.off++
Russ Cox8f571812011-10-25 22:22:09 -0700269 return rune(c), 1, nil
Rob Pike0ac5ef72010-03-05 11:34:53 -0800270 }
271 r, n := utf8.DecodeRune(b.buf[b.off:])
272 b.off += n
273 return r, n, nil
274}
275
Roger Peppe415545e2010-12-06 14:10:10 -0500276// UnreadRune unreads the last rune returned by ReadRune.
277// If the most recent read or write operation on the buffer was
278// not a ReadRune, UnreadRune returns an error. (In this regard
279// it is stricter than UnreadByte, which will unread the last byte
280// from any read operation.)
281func (b *Buffer) UnreadRune() os.Error {
282 if b.lastRead != opReadRune {
Robert Griesemer712fb6d2011-06-22 10:52:47 -0700283 return os.NewError("bytes.Buffer: UnreadRune: previous operation was not ReadRune")
Roger Peppe415545e2010-12-06 14:10:10 -0500284 }
285 b.lastRead = opInvalid
286 if b.off > 0 {
287 _, n := utf8.DecodeLastRune(b.buf[0:b.off])
288 b.off -= n
289 }
290 return nil
291}
292
293// UnreadByte unreads the last byte returned by the most recent
294// read operation. If write has happened since the last read, UnreadByte
295// returns an error.
296func (b *Buffer) UnreadByte() os.Error {
Rob Pike353fd102011-01-07 14:41:33 -0800297 if b.lastRead != opReadRune && b.lastRead != opRead {
Robert Griesemer712fb6d2011-06-22 10:52:47 -0700298 return os.NewError("bytes.Buffer: UnreadByte: previous operation was not a read")
Roger Peppe415545e2010-12-06 14:10:10 -0500299 }
300 b.lastRead = opInvalid
301 if b.off > 0 {
302 b.off--
303 }
304 return nil
305}
306
Evan Shawc9bf30c2011-01-27 14:00:31 -0800307// ReadBytes reads until the first occurrence of delim in the input,
308// returning a slice containing the data up to and including the delimiter.
309// If ReadBytes encounters an error before finding a delimiter,
310// it returns the data read before the error and the error itself (often os.EOF).
311// ReadBytes returns err != nil if and only if the returned data does not end in
312// delim.
313func (b *Buffer) ReadBytes(delim byte) (line []byte, err os.Error) {
314 i := IndexByte(b.buf[b.off:], delim)
Evan Shawbbfad5f2011-02-11 12:39:18 -0500315 size := i + 1
Evan Shawc9bf30c2011-01-27 14:00:31 -0800316 if i < 0 {
317 size = len(b.buf) - b.off
318 err = os.EOF
319 }
320 line = make([]byte, size)
321 copy(line, b.buf[b.off:])
Evan Shawbbfad5f2011-02-11 12:39:18 -0500322 b.off += size
Evan Shawc9bf30c2011-01-27 14:00:31 -0800323 return
324}
325
326// ReadString reads until the first occurrence of delim in the input,
327// returning a string containing the data up to and including the delimiter.
328// If ReadString encounters an error before finding a delimiter,
329// it returns the data read before the error and the error itself (often os.EOF).
330// ReadString returns err != nil if and only if the returned data does not end
331// in delim.
332func (b *Buffer) ReadString(delim byte) (line string, err os.Error) {
333 bytes, err := b.ReadBytes(delim)
334 return string(bytes), err
335}
336
Rob Pike1f0e6a42009-12-19 08:42:31 +1100337// NewBuffer creates and initializes a new Buffer using buf as its initial
338// contents. It is intended to prepare a Buffer to read existing data. It
Rob Pikea45c6572011-08-26 15:09:23 +1000339// can also be used to size the internal buffer for writing. To do that,
Rob Pike1f0e6a42009-12-19 08:42:31 +1100340// buf should have the desired capacity but a length of zero.
Rob Pikea45c6572011-08-26 15:09:23 +1000341//
342// In most cases, new(Buffer) (or just declaring a Buffer variable) is
343// preferable to NewBuffer. In particular, passing a non-empty buf to
344// NewBuffer and then writing to the Buffer will overwrite buf, not append to
345// it.
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800346func NewBuffer(buf []byte) *Buffer { return &Buffer{buf: buf} }
Rob Pike9e7f3a42009-09-22 14:53:48 -0700347
Rob Pike1f0e6a42009-12-19 08:42:31 +1100348// NewBufferString creates and initializes a new Buffer using string s as its
349// initial contents. It is intended to prepare a buffer to read an existing
Rob Pikea45c6572011-08-26 15:09:23 +1000350// string. See the warnings about NewBuffer; similar issues apply here.
Rob Pike9e7f3a42009-09-22 14:53:48 -0700351func NewBufferString(s string) *Buffer {
Russ Coxd86ab012010-10-26 21:52:54 -0700352 return &Buffer{buf: []byte(s)}
Rob Pike5f9254c2008-11-25 09:41:58 -0800353}