buffer.go

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