Path: blob/main/vendor/go.uber.org/zap/buffer/buffer.go
2875 views
// Copyright (c) 2016 Uber Technologies, Inc.1//2// Permission is hereby granted, free of charge, to any person obtaining a copy3// of this software and associated documentation files (the "Software"), to deal4// in the Software without restriction, including without limitation the rights5// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell6// copies of the Software, and to permit persons to whom the Software is7// furnished to do so, subject to the following conditions:8//9// The above copyright notice and this permission notice shall be included in10// all copies or substantial portions of the Software.11//12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR13// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,14// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE15// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER16// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,17// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN18// THE SOFTWARE.1920// Package buffer provides a thin wrapper around a byte slice. Unlike the21// standard library's bytes.Buffer, it supports a portion of the strconv22// package's zero-allocation formatters.23package buffer // import "go.uber.org/zap/buffer"2425import (26"strconv"27"time"28)2930const _size = 1024 // by default, create 1 KiB buffers3132// Buffer is a thin wrapper around a byte slice. It's intended to be pooled, so33// the only way to construct one is via a Pool.34type Buffer struct {35bs []byte36pool Pool37}3839// AppendByte writes a single byte to the Buffer.40func (b *Buffer) AppendByte(v byte) {41b.bs = append(b.bs, v)42}4344// AppendBytes writes the given slice of bytes to the Buffer.45func (b *Buffer) AppendBytes(v []byte) {46b.bs = append(b.bs, v...)47}4849// AppendString writes a string to the Buffer.50func (b *Buffer) AppendString(s string) {51b.bs = append(b.bs, s...)52}5354// AppendInt appends an integer to the underlying buffer (assuming base 10).55func (b *Buffer) AppendInt(i int64) {56b.bs = strconv.AppendInt(b.bs, i, 10)57}5859// AppendTime appends the time formatted using the specified layout.60func (b *Buffer) AppendTime(t time.Time, layout string) {61b.bs = t.AppendFormat(b.bs, layout)62}6364// AppendUint appends an unsigned integer to the underlying buffer (assuming65// base 10).66func (b *Buffer) AppendUint(i uint64) {67b.bs = strconv.AppendUint(b.bs, i, 10)68}6970// AppendBool appends a bool to the underlying buffer.71func (b *Buffer) AppendBool(v bool) {72b.bs = strconv.AppendBool(b.bs, v)73}7475// AppendFloat appends a float to the underlying buffer. It doesn't quote NaN76// or +/- Inf.77func (b *Buffer) AppendFloat(f float64, bitSize int) {78b.bs = strconv.AppendFloat(b.bs, f, 'f', -1, bitSize)79}8081// Len returns the length of the underlying byte slice.82func (b *Buffer) Len() int {83return len(b.bs)84}8586// Cap returns the capacity of the underlying byte slice.87func (b *Buffer) Cap() int {88return cap(b.bs)89}9091// Bytes returns a mutable reference to the underlying byte slice.92func (b *Buffer) Bytes() []byte {93return b.bs94}9596// String returns a string copy of the underlying byte slice.97func (b *Buffer) String() string {98return string(b.bs)99}100101// Reset resets the underlying byte slice. Subsequent writes re-use the slice's102// backing array.103func (b *Buffer) Reset() {104b.bs = b.bs[:0]105}106107// Write implements io.Writer.108func (b *Buffer) Write(bs []byte) (int, error) {109b.bs = append(b.bs, bs...)110return len(bs), nil111}112113// WriteByte writes a single byte to the Buffer.114//115// Error returned is always nil, function signature is compatible116// with bytes.Buffer and bufio.Writer117func (b *Buffer) WriteByte(v byte) error {118b.AppendByte(v)119return nil120}121122// WriteString writes a string to the Buffer.123//124// Error returned is always nil, function signature is compatible125// with bytes.Buffer and bufio.Writer126func (b *Buffer) WriteString(s string) (int, error) {127b.AppendString(s)128return len(s), nil129}130131// TrimNewline trims any final "\n" byte from the end of the buffer.132func (b *Buffer) TrimNewline() {133if i := len(b.bs) - 1; i >= 0 {134if b.bs[i] == '\n' {135b.bs = b.bs[:i]136}137}138}139140// Free returns the Buffer to its Pool.141//142// Callers must not retain references to the Buffer after calling Free.143func (b *Buffer) Free() {144b.pool.put(b)145}146147148