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