blob: 7592e8c63f668a5e1a9b8f1d4751784aecbfa265 [file] [log] [blame]
Scott Baker2d897982019-09-24 11:50:08 -07001// Copyright (c) 2016 Uber Technologies, Inc.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21// Package buffer provides a thin wrapper around a byte slice. Unlike the
22// standard library's bytes.Buffer, it supports a portion of the strconv
23// package's zero-allocation formatters.
24package buffer // import "go.uber.org/zap/buffer"
25
26import "strconv"
27
28const _size = 1024 // by default, create 1 KiB buffers
29
30// Buffer is a thin wrapper around a byte slice. It's intended to be pooled, so
31// the only way to construct one is via a Pool.
32type Buffer struct {
33 bs []byte
34 pool Pool
35}
36
37// AppendByte writes a single byte to the Buffer.
38func (b *Buffer) AppendByte(v byte) {
39 b.bs = append(b.bs, v)
40}
41
42// AppendString writes a string to the Buffer.
43func (b *Buffer) AppendString(s string) {
44 b.bs = append(b.bs, s...)
45}
46
47// AppendInt appends an integer to the underlying buffer (assuming base 10).
48func (b *Buffer) AppendInt(i int64) {
49 b.bs = strconv.AppendInt(b.bs, i, 10)
50}
51
52// AppendUint appends an unsigned integer to the underlying buffer (assuming
53// base 10).
54func (b *Buffer) AppendUint(i uint64) {
55 b.bs = strconv.AppendUint(b.bs, i, 10)
56}
57
58// AppendBool appends a bool to the underlying buffer.
59func (b *Buffer) AppendBool(v bool) {
60 b.bs = strconv.AppendBool(b.bs, v)
61}
62
63// AppendFloat appends a float to the underlying buffer. It doesn't quote NaN
64// or +/- Inf.
65func (b *Buffer) AppendFloat(f float64, bitSize int) {
66 b.bs = strconv.AppendFloat(b.bs, f, 'f', -1, bitSize)
67}
68
69// Len returns the length of the underlying byte slice.
70func (b *Buffer) Len() int {
71 return len(b.bs)
72}
73
74// Cap returns the capacity of the underlying byte slice.
75func (b *Buffer) Cap() int {
76 return cap(b.bs)
77}
78
79// Bytes returns a mutable reference to the underlying byte slice.
80func (b *Buffer) Bytes() []byte {
81 return b.bs
82}
83
84// String returns a string copy of the underlying byte slice.
85func (b *Buffer) String() string {
86 return string(b.bs)
87}
88
89// Reset resets the underlying byte slice. Subsequent writes re-use the slice's
90// backing array.
91func (b *Buffer) Reset() {
92 b.bs = b.bs[:0]
93}
94
95// Write implements io.Writer.
96func (b *Buffer) Write(bs []byte) (int, error) {
97 b.bs = append(b.bs, bs...)
98 return len(bs), nil
99}
100
101// TrimNewline trims any final "\n" byte from the end of the buffer.
102func (b *Buffer) TrimNewline() {
103 if i := len(b.bs) - 1; i >= 0 {
104 if b.bs[i] == '\n' {
105 b.bs = b.bs[:i]
106 }
107 }
108}
109
110// Free returns the Buffer to its Pool.
111//
112// Callers must not retain references to the Buffer after calling Free.
113func (b *Buffer) Free() {
114 b.pool.put(b)
115}