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