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