1// 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
5package bytes
6
7// Simple byte buffer for marshaling data.
8
9import (
10	"errors"
11	"io"
12	"unicode/utf8"
13)
14
15// A Buffer is a variable-sized buffer of bytes with Read and Write methods.
16// The zero value for Buffer is an empty buffer ready to use.
17type Buffer struct {
18	buf      []byte // contents are the bytes buf[off : len(buf)]
19	off      int    // read at &buf[off], write at &buf[len(buf)]
20	lastRead readOp // last read operation, so that Unread* can work correctly.
21	// FIXME: lastRead can fit in a single byte
22
23	// memory to hold first slice; helps small buffers avoid allocation.
24	// FIXME: it would be advisable to align Buffer to cachelines to avoid false
25	// sharing.
26	bootstrap [64]byte
27}
28
29// The readOp constants describe the last action performed on
30// the buffer, so that UnreadRune and UnreadByte can check for
31// invalid usage. opReadRuneX constants are chosen such that
32// converted to int they correspond to the rune size that was read.
33type readOp int
34
35const (
36	opRead      readOp = -1 // Any other read operation.
37	opInvalid          = 0  // Non-read operation.
38	opReadRune1        = 1  // Read rune of size 1.
39	opReadRune2        = 2  // Read rune of size 2.
40	opReadRune3        = 3  // Read rune of size 3.
41	opReadRune4        = 4  // Read rune of size 4.
42)
43
44// ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.
45var ErrTooLarge = errors.New("bytes.Buffer: too large")
46
47// Bytes returns a slice of length b.Len() holding the unread portion of the buffer.
48// The slice is valid for use only until the next buffer modification (that is,
49// only until the next call to a method like Read, Write, Reset, or Truncate).
50// The slice aliases the buffer content at least until the next buffer modification,
51// so immediate changes to the slice will affect the result of future reads.
52func (b *Buffer) Bytes() []byte { return b.buf[b.off:] }
53
54// String returns the contents of the unread portion of the buffer
55// as a string. If the Buffer is a nil pointer, it returns "<nil>".
56func (b *Buffer) String() string {
57	if b == nil {
58		// Special case, useful in debugging.
59		return "<nil>"
60	}
61	return string(b.buf[b.off:])
62}
63
64// Len returns the number of bytes of the unread portion of the buffer;
65// b.Len() == len(b.Bytes()).
66func (b *Buffer) Len() int { return len(b.buf) - b.off }
67
68// Cap returns the capacity of the buffer's underlying byte slice, that is, the
69// total space allocated for the buffer's data.
70func (b *Buffer) Cap() int { return cap(b.buf) }
71
72// Truncate discards all but the first n unread bytes from the buffer
73// but continues to use the same allocated storage.
74// It panics if n is negative or greater than the length of the buffer.
75func (b *Buffer) Truncate(n int) {
76	if n == 0 {
77		b.Reset()
78		return
79	}
80	b.lastRead = opInvalid
81	if n < 0 || n > b.Len() {
82		panic("bytes.Buffer: truncation out of range")
83	}
84	b.buf = b.buf[0 : b.off+n]
85}
86
87// Reset resets the buffer to be empty,
88// but it retains the underlying storage for use by future writes.
89// Reset is the same as Truncate(0).
90func (b *Buffer) Reset() {
91	b.buf = b.buf[:0]
92	b.off = 0
93	b.lastRead = opInvalid
94}
95
96// tryGrowByReslice is a inlineable version of grow for the fast-case where the
97// internal buffer only needs to be resliced.
98// It returns the index where bytes should be written and whether it succeeded.
99func (b *Buffer) tryGrowByReslice(n int) (int, bool) {
100	if l := len(b.buf); l+n <= cap(b.buf) {
101		b.buf = b.buf[:l+n]
102		return l, true
103	}
104	return 0, false
105}
106
107// grow grows the buffer to guarantee space for n more bytes.
108// It returns the index where bytes should be written.
109// If the buffer can't grow it will panic with ErrTooLarge.
110func (b *Buffer) grow(n int) int {
111	m := b.Len()
112	// If buffer is empty, reset to recover space.
113	if m == 0 && b.off != 0 {
114		b.Reset()
115	}
116	// Try to grow by means of a reslice.
117	if i, ok := b.tryGrowByReslice(n); ok {
118		return i
119	}
120	// Check if we can make use of bootstrap array.
121	if b.buf == nil && n <= len(b.bootstrap) {
122		b.buf = b.bootstrap[:n]
123		return 0
124	}
125	if m+n <= cap(b.buf)/2 {
126		// We can slide things down instead of allocating a new
127		// slice. We only need m+n <= cap(b.buf) to slide, but
128		// we instead let capacity get twice as large so we
129		// don't spend all our time copying.
130		copy(b.buf[:], b.buf[b.off:])
131	} else {
132		// Not enough space anywhere, we need to allocate.
133		buf := makeSlice(2*cap(b.buf) + n)
134		copy(buf, b.buf[b.off:])
135		b.buf = buf
136	}
137	// Restore b.off and len(b.buf).
138	b.off = 0
139	b.buf = b.buf[:m+n]
140	return m
141}
142
143// Grow grows the buffer's capacity, if necessary, to guarantee space for
144// another n bytes. After Grow(n), at least n bytes can be written to the
145// buffer without another allocation.
146// If n is negative, Grow will panic.
147// If the buffer can't grow it will panic with ErrTooLarge.
148func (b *Buffer) Grow(n int) {
149	if n < 0 {
150		panic("bytes.Buffer.Grow: negative count")
151	}
152	m := b.grow(n)
153	b.buf = b.buf[0:m]
154}
155
156// Write appends the contents of p to the buffer, growing the buffer as
157// needed. The return value n is the length of p; err is always nil. If the
158// buffer becomes too large, Write will panic with ErrTooLarge.
159func (b *Buffer) Write(p []byte) (n int, err error) {
160	b.lastRead = opInvalid
161	m, ok := b.tryGrowByReslice(len(p))
162	if !ok {
163		m = b.grow(len(p))
164	}
165	return copy(b.buf[m:], p), nil
166}
167
168// WriteString appends the contents of s to the buffer, growing the buffer as
169// needed. The return value n is the length of s; err is always nil. If the
170// buffer becomes too large, WriteString will panic with ErrTooLarge.
171func (b *Buffer) WriteString(s string) (n int, err error) {
172	b.lastRead = opInvalid
173	m, ok := b.tryGrowByReslice(len(s))
174	if !ok {
175		m = b.grow(len(s))
176	}
177	return copy(b.buf[m:], s), nil
178}
179
180// MinRead is the minimum slice size passed to a Read call by
181// Buffer.ReadFrom. As long as the Buffer has at least MinRead bytes beyond
182// what is required to hold the contents of r, ReadFrom will not grow the
183// underlying buffer.
184const MinRead = 512
185
186// ReadFrom reads data from r until EOF and appends it to the buffer, growing
187// the buffer as needed. The return value n is the number of bytes read. Any
188// error except io.EOF encountered during the read is also returned. If the
189// buffer becomes too large, ReadFrom will panic with ErrTooLarge.
190func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) {
191	b.lastRead = opInvalid
192	// If buffer is empty, reset to recover space.
193	if b.off >= len(b.buf) {
194		b.Reset()
195	}
196	for {
197		if free := cap(b.buf) - len(b.buf); free < MinRead {
198			// not enough space at end
199			newBuf := b.buf
200			if b.off+free < MinRead {
201				// not enough space using beginning of buffer;
202				// double buffer capacity
203				newBuf = makeSlice(2*cap(b.buf) + MinRead)
204			}
205			copy(newBuf, b.buf[b.off:])
206			b.buf = newBuf[:len(b.buf)-b.off]
207			b.off = 0
208		}
209		m, e := r.Read(b.buf[len(b.buf):cap(b.buf)])
210		b.buf = b.buf[0 : len(b.buf)+m]
211		n += int64(m)
212		if e == io.EOF {
213			break
214		}
215		if e != nil {
216			return n, e
217		}
218	}
219	return n, nil // err is EOF, so return nil explicitly
220}
221
222// makeSlice allocates a slice of size n. If the allocation fails, it panics
223// with ErrTooLarge.
224func makeSlice(n int) []byte {
225	// If the make fails, give a known error.
226	defer func() {
227		if recover() != nil {
228			panic(ErrTooLarge)
229		}
230	}()
231	return make([]byte, n)
232}
233
234// WriteTo writes data to w until the buffer is drained or an error occurs.
235// The return value n is the number of bytes written; it always fits into an
236// int, but it is int64 to match the io.WriterTo interface. Any error
237// encountered during the write is also returned.
238func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) {
239	b.lastRead = opInvalid
240	if b.off < len(b.buf) {
241		nBytes := b.Len()
242		m, e := w.Write(b.buf[b.off:])
243		if m > nBytes {
244			panic("bytes.Buffer.WriteTo: invalid Write count")
245		}
246		b.off += m
247		n = int64(m)
248		if e != nil {
249			return n, e
250		}
251		// all bytes should have been written, by definition of
252		// Write method in io.Writer
253		if m != nBytes {
254			return n, io.ErrShortWrite
255		}
256	}
257	// Buffer is now empty; reset.
258	b.Reset()
259	return
260}
261
262// WriteByte appends the byte c to the buffer, growing the buffer as needed.
263// The returned error is always nil, but is included to match bufio.Writer's
264// WriteByte. If the buffer becomes too large, WriteByte will panic with
265// ErrTooLarge.
266func (b *Buffer) WriteByte(c byte) error {
267	b.lastRead = opInvalid
268	m, ok := b.tryGrowByReslice(1)
269	if !ok {
270		m = b.grow(1)
271	}
272	b.buf[m] = c
273	return nil
274}
275
276// WriteRune appends the UTF-8 encoding of Unicode code point r to the
277// buffer, returning its length and an error, which is always nil but is
278// included to match bufio.Writer's WriteRune. The buffer is grown as needed;
279// if it becomes too large, WriteRune will panic with ErrTooLarge.
280func (b *Buffer) WriteRune(r rune) (n int, err error) {
281	if r < utf8.RuneSelf {
282		b.WriteByte(byte(r))
283		return 1, nil
284	}
285	b.lastRead = opInvalid
286	m, ok := b.tryGrowByReslice(utf8.UTFMax)
287	if !ok {
288		m = b.grow(utf8.UTFMax)
289	}
290	n = utf8.EncodeRune(b.buf[m:m+utf8.UTFMax], r)
291	b.buf = b.buf[:m+n]
292	return n, nil
293}
294
295// Read reads the next len(p) bytes from the buffer or until the buffer
296// is drained. The return value n is the number of bytes read. If the
297// buffer has no data to return, err is io.EOF (unless len(p) is zero);
298// otherwise it is nil.
299func (b *Buffer) Read(p []byte) (n int, err error) {
300	b.lastRead = opInvalid
301	if b.off >= len(b.buf) {
302		// Buffer is empty, reset to recover space.
303		b.Reset()
304		if len(p) == 0 {
305			return
306		}
307		return 0, io.EOF
308	}
309	n = copy(p, b.buf[b.off:])
310	b.off += n
311	if n > 0 {
312		b.lastRead = opRead
313	}
314	return
315}
316
317// Next returns a slice containing the next n bytes from the buffer,
318// advancing the buffer as if the bytes had been returned by Read.
319// If there are fewer than n bytes in the buffer, Next returns the entire buffer.
320// The slice is only valid until the next call to a read or write method.
321func (b *Buffer) Next(n int) []byte {
322	b.lastRead = opInvalid
323	m := b.Len()
324	if n > m {
325		n = m
326	}
327	data := b.buf[b.off : b.off+n]
328	b.off += n
329	if n > 0 {
330		b.lastRead = opRead
331	}
332	return data
333}
334
335// ReadByte reads and returns the next byte from the buffer.
336// If no byte is available, it returns error io.EOF.
337func (b *Buffer) ReadByte() (byte, error) {
338	b.lastRead = opInvalid
339	if b.off >= len(b.buf) {
340		// Buffer is empty, reset to recover space.
341		b.Reset()
342		return 0, io.EOF
343	}
344	c := b.buf[b.off]
345	b.off++
346	b.lastRead = opRead
347	return c, nil
348}
349
350// ReadRune reads and returns the next UTF-8-encoded
351// Unicode code point from the buffer.
352// If no bytes are available, the error returned is io.EOF.
353// If the bytes are an erroneous UTF-8 encoding, it
354// consumes one byte and returns U+FFFD, 1.
355func (b *Buffer) ReadRune() (r rune, size int, err error) {
356	b.lastRead = opInvalid
357	if b.off >= len(b.buf) {
358		// Buffer is empty, reset to recover space.
359		b.Reset()
360		return 0, 0, io.EOF
361	}
362	c := b.buf[b.off]
363	if c < utf8.RuneSelf {
364		b.off++
365		b.lastRead = opReadRune1
366		return rune(c), 1, nil
367	}
368	r, n := utf8.DecodeRune(b.buf[b.off:])
369	b.off += n
370	b.lastRead = readOp(n)
371	return r, n, nil
372}
373
374// UnreadRune unreads the last rune returned by ReadRune.
375// If the most recent read or write operation on the buffer was
376// not a successful ReadRune, UnreadRune returns an error.  (In this regard
377// it is stricter than UnreadByte, which will unread the last byte
378// from any read operation.)
379func (b *Buffer) UnreadRune() error {
380	if b.lastRead <= opInvalid {
381		return errors.New("bytes.Buffer: UnreadRune: previous operation was not a successful ReadRune")
382	}
383	if b.off >= int(b.lastRead) {
384		b.off -= int(b.lastRead)
385	}
386	b.lastRead = opInvalid
387	return nil
388}
389
390// UnreadByte unreads the last byte returned by the most recent successful
391// read operation that read at least one byte. If a write has happened since
392// the last read, if the last read returned an error, or if the read read zero
393// bytes, UnreadByte returns an error.
394func (b *Buffer) UnreadByte() error {
395	if b.lastRead == opInvalid {
396		return errors.New("bytes.Buffer: UnreadByte: previous operation was not a successful read")
397	}
398	b.lastRead = opInvalid
399	if b.off > 0 {
400		b.off--
401	}
402	return nil
403}
404
405// ReadBytes reads until the first occurrence of delim in the input,
406// returning a slice containing the data up to and including the delimiter.
407// If ReadBytes encounters an error before finding a delimiter,
408// it returns the data read before the error and the error itself (often io.EOF).
409// ReadBytes returns err != nil if and only if the returned data does not end in
410// delim.
411func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) {
412	slice, err := b.readSlice(delim)
413	// return a copy of slice. The buffer's backing array may
414	// be overwritten by later calls.
415	line = append(line, slice...)
416	return
417}
418
419// readSlice is like ReadBytes but returns a reference to internal buffer data.
420func (b *Buffer) readSlice(delim byte) (line []byte, err error) {
421	i := IndexByte(b.buf[b.off:], delim)
422	end := b.off + i + 1
423	if i < 0 {
424		end = len(b.buf)
425		err = io.EOF
426	}
427	line = b.buf[b.off:end]
428	b.off = end
429	b.lastRead = opRead
430	return line, err
431}
432
433// ReadString reads until the first occurrence of delim in the input,
434// returning a string containing the data up to and including the delimiter.
435// If ReadString encounters an error before finding a delimiter,
436// it returns the data read before the error and the error itself (often io.EOF).
437// ReadString returns err != nil if and only if the returned data does not end
438// in delim.
439func (b *Buffer) ReadString(delim byte) (line string, err error) {
440	slice, err := b.readSlice(delim)
441	return string(slice), err
442}
443
444// NewBuffer creates and initializes a new Buffer using buf as its
445// initial contents. The new Buffer takes ownership of buf, and the
446// caller should not use buf after this call. NewBuffer is intended to
447// prepare a Buffer to read existing data. It can also be used to size
448// the internal buffer for writing. To do that, buf should have the
449// desired capacity but a length of zero.
450//
451// In most cases, new(Buffer) (or just declaring a Buffer variable) is
452// sufficient to initialize a Buffer.
453func NewBuffer(buf []byte) *Buffer { return &Buffer{buf: buf} }
454
455// NewBufferString creates and initializes a new Buffer using string s as its
456// initial contents. It is intended to prepare a buffer to read an existing
457// string.
458//
459// In most cases, new(Buffer) (or just declaring a Buffer variable) is
460// sufficient to initialize a Buffer.
461func NewBufferString(s string) *Buffer {
462	return &Buffer{buf: []byte(s)}
463}
464