1 |
747 |
jeremybenn |
// 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 |
|
|
|
5 |
|
|
package bytes
|
6 |
|
|
|
7 |
|
|
// Simple byte buffer for marshaling data.
|
8 |
|
|
|
9 |
|
|
import (
|
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.
|
17 |
|
|
type 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 |
|
|
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.
|
22 |
|
|
lastRead readOp // last read operation, so that Unread* can work correctly.
|
23 |
|
|
}
|
24 |
|
|
|
25 |
|
|
// The readOp constants describe the last action performed on
|
26 |
|
|
// the buffer, so that UnreadRune and UnreadByte can
|
27 |
|
|
// check for invalid usage.
|
28 |
|
|
type readOp int
|
29 |
|
|
|
30 |
|
|
const (
|
31 |
|
|
opInvalid readOp = iota // Non-read operation.
|
32 |
|
|
opReadRune // Read rune.
|
33 |
|
|
opRead // Any other read operation.
|
34 |
|
|
)
|
35 |
|
|
|
36 |
|
|
// ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.
|
37 |
|
|
var ErrTooLarge = errors.New("bytes.Buffer: too large")
|
38 |
|
|
|
39 |
|
|
// Bytes returns a slice of the contents of the unread portion of the buffer;
|
40 |
|
|
// len(b.Bytes()) == b.Len(). If the caller changes the contents of the
|
41 |
|
|
// returned slice, the contents of the buffer will change provided there
|
42 |
|
|
// are no intervening method calls on the Buffer.
|
43 |
|
|
func (b *Buffer) Bytes() []byte { return b.buf[b.off:] }
|
44 |
|
|
|
45 |
|
|
// String returns the contents of the unread portion of the buffer
|
46 |
|
|
// as a string. If the Buffer is a nil pointer, it returns "".
|
47 |
|
|
func (b *Buffer) String() string {
|
48 |
|
|
if b == nil {
|
49 |
|
|
// Special case, useful in debugging.
|
50 |
|
|
return ""
|
51 |
|
|
}
|
52 |
|
|
return string(b.buf[b.off:])
|
53 |
|
|
}
|
54 |
|
|
|
55 |
|
|
// Len returns the number of bytes of the unread portion of the buffer;
|
56 |
|
|
// b.Len() == len(b.Bytes()).
|
57 |
|
|
func (b *Buffer) Len() int { return len(b.buf) - b.off }
|
58 |
|
|
|
59 |
|
|
// Truncate discards all but the first n unread bytes from the buffer.
|
60 |
|
|
// It panics if n is negative or greater than the length of the buffer.
|
61 |
|
|
func (b *Buffer) Truncate(n int) {
|
62 |
|
|
b.lastRead = opInvalid
|
63 |
|
|
switch {
|
64 |
|
|
case n < 0 || n > b.Len():
|
65 |
|
|
panic("bytes.Buffer: truncation out of range")
|
66 |
|
|
case n == 0:
|
67 |
|
|
// Reuse buffer space.
|
68 |
|
|
b.off = 0
|
69 |
|
|
}
|
70 |
|
|
b.buf = b.buf[0 : b.off+n]
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
// Reset resets the buffer so it has no content.
|
74 |
|
|
// b.Reset() is the same as b.Truncate(0).
|
75 |
|
|
func (b *Buffer) Reset() { b.Truncate(0) }
|
76 |
|
|
|
77 |
|
|
// grow grows the buffer to guarantee space for n more bytes.
|
78 |
|
|
// It returns the index where bytes should be written.
|
79 |
|
|
// If the buffer can't grow it will panic with ErrTooLarge.
|
80 |
|
|
func (b *Buffer) grow(n int) int {
|
81 |
|
|
m := b.Len()
|
82 |
|
|
// If buffer is empty, reset to recover space.
|
83 |
|
|
if m == 0 && b.off != 0 {
|
84 |
|
|
b.Truncate(0)
|
85 |
|
|
}
|
86 |
|
|
if len(b.buf)+n > cap(b.buf) {
|
87 |
|
|
var buf []byte
|
88 |
|
|
if b.buf == nil && n <= len(b.bootstrap) {
|
89 |
|
|
buf = b.bootstrap[0:]
|
90 |
|
|
} else {
|
91 |
|
|
// not enough space anywhere
|
92 |
|
|
buf = makeSlice(2*cap(b.buf) + n)
|
93 |
|
|
copy(buf, b.buf[b.off:])
|
94 |
|
|
}
|
95 |
|
|
b.buf = buf
|
96 |
|
|
b.off = 0
|
97 |
|
|
}
|
98 |
|
|
b.buf = b.buf[0 : b.off+m+n]
|
99 |
|
|
return b.off + m
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
// Write appends the contents of p to the buffer. The return
|
103 |
|
|
// value n is the length of p; err is always nil.
|
104 |
|
|
// If the buffer becomes too large, Write will panic with
|
105 |
|
|
// ErrTooLarge.
|
106 |
|
|
func (b *Buffer) Write(p []byte) (n int, err error) {
|
107 |
|
|
b.lastRead = opInvalid
|
108 |
|
|
m := b.grow(len(p))
|
109 |
|
|
return copy(b.buf[m:], p), nil
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
// WriteString appends the contents of s to the buffer. The return
|
113 |
|
|
// value n is the length of s; err is always nil.
|
114 |
|
|
// If the buffer becomes too large, WriteString will panic with
|
115 |
|
|
// ErrTooLarge.
|
116 |
|
|
func (b *Buffer) WriteString(s string) (n int, err error) {
|
117 |
|
|
b.lastRead = opInvalid
|
118 |
|
|
m := b.grow(len(s))
|
119 |
|
|
return copy(b.buf[m:], s), nil
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
// MinRead is the minimum slice size passed to a Read call by
|
123 |
|
|
// Buffer.ReadFrom. As long as the Buffer has at least MinRead bytes beyond
|
124 |
|
|
// what is required to hold the contents of r, ReadFrom will not grow the
|
125 |
|
|
// underlying buffer.
|
126 |
|
|
const MinRead = 512
|
127 |
|
|
|
128 |
|
|
// ReadFrom reads data from r until EOF and appends it to the buffer.
|
129 |
|
|
// The return value n is the number of bytes read.
|
130 |
|
|
// Any error except io.EOF encountered during the read
|
131 |
|
|
// is also returned.
|
132 |
|
|
// If the buffer becomes too large, ReadFrom will panic with
|
133 |
|
|
// ErrTooLarge.
|
134 |
|
|
func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) {
|
135 |
|
|
b.lastRead = opInvalid
|
136 |
|
|
// If buffer is empty, reset to recover space.
|
137 |
|
|
if b.off >= len(b.buf) {
|
138 |
|
|
b.Truncate(0)
|
139 |
|
|
}
|
140 |
|
|
for {
|
141 |
|
|
if free := cap(b.buf) - len(b.buf); free < MinRead {
|
142 |
|
|
// not enough space at end
|
143 |
|
|
newBuf := b.buf
|
144 |
|
|
if b.off+free < MinRead {
|
145 |
|
|
// not enough space using beginning of buffer;
|
146 |
|
|
// double buffer capacity
|
147 |
|
|
newBuf = makeSlice(2*cap(b.buf) + MinRead)
|
148 |
|
|
}
|
149 |
|
|
copy(newBuf, b.buf[b.off:])
|
150 |
|
|
b.buf = newBuf[:len(b.buf)-b.off]
|
151 |
|
|
b.off = 0
|
152 |
|
|
}
|
153 |
|
|
m, e := r.Read(b.buf[len(b.buf):cap(b.buf)])
|
154 |
|
|
b.buf = b.buf[0 : len(b.buf)+m]
|
155 |
|
|
n += int64(m)
|
156 |
|
|
if e == io.EOF {
|
157 |
|
|
break
|
158 |
|
|
}
|
159 |
|
|
if e != nil {
|
160 |
|
|
return n, e
|
161 |
|
|
}
|
162 |
|
|
}
|
163 |
|
|
return n, nil // err is EOF, so return nil explicitly
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
// makeSlice allocates a slice of size n. If the allocation fails, it panics
|
167 |
|
|
// with ErrTooLarge.
|
168 |
|
|
func makeSlice(n int) []byte {
|
169 |
|
|
// If the make fails, give a known error.
|
170 |
|
|
defer func() {
|
171 |
|
|
if recover() != nil {
|
172 |
|
|
panic(ErrTooLarge)
|
173 |
|
|
}
|
174 |
|
|
}()
|
175 |
|
|
return make([]byte, n)
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
// WriteTo writes data to w until the buffer is drained or an error
|
179 |
|
|
// occurs. The return value n is the number of bytes written; it always
|
180 |
|
|
// fits into an int, but it is int64 to match the io.WriterTo interface.
|
181 |
|
|
// Any error encountered during the write is also returned.
|
182 |
|
|
func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) {
|
183 |
|
|
b.lastRead = opInvalid
|
184 |
|
|
if b.off < len(b.buf) {
|
185 |
|
|
m, e := w.Write(b.buf[b.off:])
|
186 |
|
|
b.off += m
|
187 |
|
|
n = int64(m)
|
188 |
|
|
if e != nil {
|
189 |
|
|
return n, e
|
190 |
|
|
}
|
191 |
|
|
// otherwise all bytes were written, by definition of
|
192 |
|
|
// Write method in io.Writer
|
193 |
|
|
}
|
194 |
|
|
// Buffer is now empty; reset.
|
195 |
|
|
b.Truncate(0)
|
196 |
|
|
return
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
// WriteByte appends the byte c to the buffer.
|
200 |
|
|
// The returned error is always nil, but is included
|
201 |
|
|
// to match bufio.Writer's WriteByte.
|
202 |
|
|
// If the buffer becomes too large, WriteByte will panic with
|
203 |
|
|
// ErrTooLarge.
|
204 |
|
|
func (b *Buffer) WriteByte(c byte) error {
|
205 |
|
|
b.lastRead = opInvalid
|
206 |
|
|
m := b.grow(1)
|
207 |
|
|
b.buf[m] = c
|
208 |
|
|
return nil
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
// WriteRune appends the UTF-8 encoding of Unicode
|
212 |
|
|
// code point r to the buffer, returning its length and
|
213 |
|
|
// an error, which is always nil but is included
|
214 |
|
|
// to match bufio.Writer's WriteRune.
|
215 |
|
|
// If the buffer becomes too large, WriteRune will panic with
|
216 |
|
|
// ErrTooLarge.
|
217 |
|
|
func (b *Buffer) WriteRune(r rune) (n int, err error) {
|
218 |
|
|
if r < utf8.RuneSelf {
|
219 |
|
|
b.WriteByte(byte(r))
|
220 |
|
|
return 1, nil
|
221 |
|
|
}
|
222 |
|
|
n = utf8.EncodeRune(b.runeBytes[0:], r)
|
223 |
|
|
b.Write(b.runeBytes[0:n])
|
224 |
|
|
return n, nil
|
225 |
|
|
}
|
226 |
|
|
|
227 |
|
|
// Read reads the next len(p) bytes from the buffer or until the buffer
|
228 |
|
|
// is drained. The return value n is the number of bytes read. If the
|
229 |
|
|
// buffer has no data to return, err is io.EOF (unless len(p) is zero);
|
230 |
|
|
// otherwise it is nil.
|
231 |
|
|
func (b *Buffer) Read(p []byte) (n int, err error) {
|
232 |
|
|
b.lastRead = opInvalid
|
233 |
|
|
if b.off >= len(b.buf) {
|
234 |
|
|
// Buffer is empty, reset to recover space.
|
235 |
|
|
b.Truncate(0)
|
236 |
|
|
if len(p) == 0 {
|
237 |
|
|
return
|
238 |
|
|
}
|
239 |
|
|
return 0, io.EOF
|
240 |
|
|
}
|
241 |
|
|
n = copy(p, b.buf[b.off:])
|
242 |
|
|
b.off += n
|
243 |
|
|
if n > 0 {
|
244 |
|
|
b.lastRead = opRead
|
245 |
|
|
}
|
246 |
|
|
return
|
247 |
|
|
}
|
248 |
|
|
|
249 |
|
|
// Next returns a slice containing the next n bytes from the buffer,
|
250 |
|
|
// advancing the buffer as if the bytes had been returned by Read.
|
251 |
|
|
// If there are fewer than n bytes in the buffer, Next returns the entire buffer.
|
252 |
|
|
// The slice is only valid until the next call to a read or write method.
|
253 |
|
|
func (b *Buffer) Next(n int) []byte {
|
254 |
|
|
b.lastRead = opInvalid
|
255 |
|
|
m := b.Len()
|
256 |
|
|
if n > m {
|
257 |
|
|
n = m
|
258 |
|
|
}
|
259 |
|
|
data := b.buf[b.off : b.off+n]
|
260 |
|
|
b.off += n
|
261 |
|
|
if n > 0 {
|
262 |
|
|
b.lastRead = opRead
|
263 |
|
|
}
|
264 |
|
|
return data
|
265 |
|
|
}
|
266 |
|
|
|
267 |
|
|
// ReadByte reads and returns the next byte from the buffer.
|
268 |
|
|
// If no byte is available, it returns error io.EOF.
|
269 |
|
|
func (b *Buffer) ReadByte() (c byte, err error) {
|
270 |
|
|
b.lastRead = opInvalid
|
271 |
|
|
if b.off >= len(b.buf) {
|
272 |
|
|
// Buffer is empty, reset to recover space.
|
273 |
|
|
b.Truncate(0)
|
274 |
|
|
return 0, io.EOF
|
275 |
|
|
}
|
276 |
|
|
c = b.buf[b.off]
|
277 |
|
|
b.off++
|
278 |
|
|
b.lastRead = opRead
|
279 |
|
|
return c, nil
|
280 |
|
|
}
|
281 |
|
|
|
282 |
|
|
// ReadRune reads and returns the next UTF-8-encoded
|
283 |
|
|
// Unicode code point from the buffer.
|
284 |
|
|
// If no bytes are available, the error returned is io.EOF.
|
285 |
|
|
// If the bytes are an erroneous UTF-8 encoding, it
|
286 |
|
|
// consumes one byte and returns U+FFFD, 1.
|
287 |
|
|
func (b *Buffer) ReadRune() (r rune, size int, err error) {
|
288 |
|
|
b.lastRead = opInvalid
|
289 |
|
|
if b.off >= len(b.buf) {
|
290 |
|
|
// Buffer is empty, reset to recover space.
|
291 |
|
|
b.Truncate(0)
|
292 |
|
|
return 0, 0, io.EOF
|
293 |
|
|
}
|
294 |
|
|
b.lastRead = opReadRune
|
295 |
|
|
c := b.buf[b.off]
|
296 |
|
|
if c < utf8.RuneSelf {
|
297 |
|
|
b.off++
|
298 |
|
|
return rune(c), 1, nil
|
299 |
|
|
}
|
300 |
|
|
r, n := utf8.DecodeRune(b.buf[b.off:])
|
301 |
|
|
b.off += n
|
302 |
|
|
return r, n, nil
|
303 |
|
|
}
|
304 |
|
|
|
305 |
|
|
// UnreadRune unreads the last rune returned by ReadRune.
|
306 |
|
|
// If the most recent read or write operation on the buffer was
|
307 |
|
|
// not a ReadRune, UnreadRune returns an error. (In this regard
|
308 |
|
|
// it is stricter than UnreadByte, which will unread the last byte
|
309 |
|
|
// from any read operation.)
|
310 |
|
|
func (b *Buffer) UnreadRune() error {
|
311 |
|
|
if b.lastRead != opReadRune {
|
312 |
|
|
return errors.New("bytes.Buffer: UnreadRune: previous operation was not ReadRune")
|
313 |
|
|
}
|
314 |
|
|
b.lastRead = opInvalid
|
315 |
|
|
if b.off > 0 {
|
316 |
|
|
_, n := utf8.DecodeLastRune(b.buf[0:b.off])
|
317 |
|
|
b.off -= n
|
318 |
|
|
}
|
319 |
|
|
return nil
|
320 |
|
|
}
|
321 |
|
|
|
322 |
|
|
// UnreadByte unreads the last byte returned by the most recent
|
323 |
|
|
// read operation. If write has happened since the last read, UnreadByte
|
324 |
|
|
// returns an error.
|
325 |
|
|
func (b *Buffer) UnreadByte() error {
|
326 |
|
|
if b.lastRead != opReadRune && b.lastRead != opRead {
|
327 |
|
|
return errors.New("bytes.Buffer: UnreadByte: previous operation was not a read")
|
328 |
|
|
}
|
329 |
|
|
b.lastRead = opInvalid
|
330 |
|
|
if b.off > 0 {
|
331 |
|
|
b.off--
|
332 |
|
|
}
|
333 |
|
|
return nil
|
334 |
|
|
}
|
335 |
|
|
|
336 |
|
|
// ReadBytes reads until the first occurrence of delim in the input,
|
337 |
|
|
// returning a slice containing the data up to and including the delimiter.
|
338 |
|
|
// If ReadBytes encounters an error before finding a delimiter,
|
339 |
|
|
// it returns the data read before the error and the error itself (often io.EOF).
|
340 |
|
|
// ReadBytes returns err != nil if and only if the returned data does not end in
|
341 |
|
|
// delim.
|
342 |
|
|
func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) {
|
343 |
|
|
i := IndexByte(b.buf[b.off:], delim)
|
344 |
|
|
size := i + 1
|
345 |
|
|
if i < 0 {
|
346 |
|
|
size = len(b.buf) - b.off
|
347 |
|
|
err = io.EOF
|
348 |
|
|
}
|
349 |
|
|
line = make([]byte, size)
|
350 |
|
|
copy(line, b.buf[b.off:])
|
351 |
|
|
b.off += size
|
352 |
|
|
return
|
353 |
|
|
}
|
354 |
|
|
|
355 |
|
|
// ReadString reads until the first occurrence of delim in the input,
|
356 |
|
|
// returning a string containing the data up to and including the delimiter.
|
357 |
|
|
// If ReadString encounters an error before finding a delimiter,
|
358 |
|
|
// it returns the data read before the error and the error itself (often io.EOF).
|
359 |
|
|
// ReadString returns err != nil if and only if the returned data does not end
|
360 |
|
|
// in delim.
|
361 |
|
|
func (b *Buffer) ReadString(delim byte) (line string, err error) {
|
362 |
|
|
bytes, err := b.ReadBytes(delim)
|
363 |
|
|
return string(bytes), err
|
364 |
|
|
}
|
365 |
|
|
|
366 |
|
|
// NewBuffer creates and initializes a new Buffer using buf as its initial
|
367 |
|
|
// contents. It is intended to prepare a Buffer to read existing data. It
|
368 |
|
|
// can also be used to size the internal buffer for writing. To do that,
|
369 |
|
|
// buf should have the desired capacity but a length of zero.
|
370 |
|
|
//
|
371 |
|
|
// In most cases, new(Buffer) (or just declaring a Buffer variable) is
|
372 |
|
|
// sufficient to initialize a Buffer.
|
373 |
|
|
func NewBuffer(buf []byte) *Buffer { return &Buffer{buf: buf} }
|
374 |
|
|
|
375 |
|
|
// NewBufferString creates and initializes a new Buffer using string s as its
|
376 |
|
|
// initial contents. It is intended to prepare a buffer to read an existing
|
377 |
|
|
// string.
|
378 |
|
|
//
|
379 |
|
|
// In most cases, new(Buffer) (or just declaring a Buffer variable) is
|
380 |
|
|
// sufficient to initialize a Buffer.
|
381 |
|
|
func NewBufferString(s string) *Buffer {
|
382 |
|
|
return &Buffer{buf: []byte(s)}
|
383 |
|
|
}
|