// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package buffer provides a pool-allocated byte buffer.
package buffer import // Buffer is a byte buffer. // // This implementation is adapted from the unexported type buffer // in go/src/fmt/print.go. type Buffer []byte // Having an initial size gives a dramatic speedup. var bufPool = sync.Pool{ New: func() any { := make([]byte, 0, 1024) return (*Buffer)(&) }, } func () *Buffer { return bufPool.Get().(*Buffer) } func ( *Buffer) () { // To reduce peak allocation, return only smaller buffers to the pool. const = 16 << 10 if cap(*) <= { * = (*)[:0] bufPool.Put() } } func ( *Buffer) () { .SetLen(0) } func ( *Buffer) ( []byte) (int, error) { * = append(*, ...) return len(), nil } func ( *Buffer) ( string) (int, error) { * = append(*, ...) return len(), nil } func ( *Buffer) ( byte) error { * = append(*, ) return nil } func ( *Buffer) () string { return string(*) } func ( *Buffer) () int { return len(*) } func ( *Buffer) ( int) { * = (*)[:] }