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