-
Notifications
You must be signed in to change notification settings - Fork 11
/
buffer.go
73 lines (60 loc) · 1.72 KB
/
buffer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package logf
import (
"strconv"
"sync"
"time"
)
// ref: https://github.com/VictoriaMetrics/VictoriaMetrics/blob/master/lib/bytesutil/bytebuffer.go
// byteBufferPool is a pool of byteBuffer
type byteBufferPool struct {
p sync.Pool
}
// Get returns a new instance of byteBuffer or gets from the object pool
func (bbp *byteBufferPool) Get() *byteBuffer {
bbv := bbp.p.Get()
if bbv == nil {
return &byteBuffer{}
}
return bbv.(*byteBuffer)
}
// Put puts back the ByteBuffer into the object pool
func (bbp *byteBufferPool) Put(bb *byteBuffer) {
bb.Reset()
bbp.p.Put(bb)
}
// byteBuffer is a wrapper around byte array
type byteBuffer struct {
B []byte
}
// AppendByte appends a single byte to the buffer.
func (bb *byteBuffer) AppendByte(b byte) {
bb.B = append(bb.B, b)
}
// AppendString appends a string to the buffer.
func (bb *byteBuffer) AppendString(s string) {
bb.B = append(bb.B, s...)
}
// AppendInt appends an integer to the underlying buffer (assuming base 10).
func (bb *byteBuffer) AppendInt(i int64) {
bb.B = strconv.AppendInt(bb.B, i, 10)
}
// AppendTime appends the time formatted using the specified layout.
func (bb *byteBuffer) AppendTime(t time.Time, layout string) {
bb.B = t.AppendFormat(bb.B, layout)
}
// AppendBool appends a bool to the underlying buffer.
func (bb *byteBuffer) AppendBool(v bool) {
bb.B = strconv.AppendBool(bb.B, v)
}
// AppendFloat appends a float to the underlying buffer.
func (bb *byteBuffer) AppendFloat(f float64, bitSize int) {
bb.B = strconv.AppendFloat(bb.B, f, 'f', -1, bitSize)
}
// Bytes returns a mutable reference to the underlying buffer.
func (bb *byteBuffer) Bytes() []byte {
return bb.B
}
// Reset resets the underlying buffer.
func (bb *byteBuffer) Reset() {
bb.B = bb.B[:0]
}