-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuffer.go
130 lines (105 loc) · 3.15 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package logf
import (
"strconv"
)
// PageSize is the recommended buffer size.
const (
PageSize = 4 * 1024
)
// NewBuffer creates the new instance of Buffer with default capacity.
func NewBuffer() *Buffer {
return NewBufferWithCapacity(PageSize)
}
// NewBufferWithCapacity creates the new instance of Buffer with the given
// capacity.
func NewBufferWithCapacity(capacity int) *Buffer {
return &Buffer{make([]byte, 0, capacity)}
}
// Buffer is a helping wrapper for byte slice.
type Buffer struct {
Data []byte
}
// Write implements io.Writer.
func (b *Buffer) Write(p []byte) (n int, err error) {
b.Data = append(b.Data, p...)
return len(p), nil
}
// String implements fmt.Stringer.
func (b *Buffer) String() string {
return string(b.Bytes())
}
// EnsureSize ensures that the Buffer is able to append 's' bytes without
// a further realloc.
func (b *Buffer) EnsureSize(s int) []byte {
if cap(b.Data)-len(b.Data) < s {
tmpLen := len(b.Data)
tmp := make([]byte, tmpLen, tmpLen+s+(tmpLen>>1))
copy(tmp, b.Data)
b.Data = tmp
}
return b.Data
}
// ExtendBytes extends the Buffer with the given size and returns a slice
// tp extended part of the Buffer.
func (b *Buffer) ExtendBytes(s int) []byte {
b.Data = append(b.Data, make([]byte, s)...)
return b.Data[len(b.Data)-s:]
}
// AppendString appends a string to the Buffer.
func (b *Buffer) AppendString(data string) {
b.Data = append(b.Data, data...)
}
// AppendBytes appends a byte slice to the Buffer.
func (b *Buffer) AppendBytes(data []byte) {
b.Data = append(b.Data, data...)
}
// AppendByte appends a single byte to the Buffer.
func (b *Buffer) AppendByte(data byte) {
b.Data = append(b.Data, data)
}
// Reset resets the underlying byte slice.
func (b *Buffer) Reset() {
b.Data = b.Data[:0]
}
// Back returns the last byte of the underlying byte slice. A caller is in
// charge of checking that the Buffer is not empty.
func (b *Buffer) Back() byte {
return b.Data[len(b.Data)-1]
}
// Bytes returns the underlying byte slice as is.
func (b *Buffer) Bytes() []byte {
return b.Data
}
// Len returns the length of the underlying byte slice.
func (b *Buffer) Len() int {
return len(b.Data)
}
// Cap returns the capacity of the underlying byte slice.
func (b *Buffer) Cap() int {
return cap(b.Data)
}
// AppendUint appends the string form in the base 10 of the given unsigned
// integer to the given Buffer.
func AppendUint(b *Buffer, n uint64) {
b.Data = strconv.AppendUint(b.Data, n, 10)
}
// AppendInt appends the string form in the base 10 of the given integer
// to the given Buffer.
func AppendInt(b *Buffer, n int64) {
b.Data = strconv.AppendInt(b.Data, n, 10)
}
// AppendFloat32 appends the string form of the given float32 to the given
// Buffer.
func AppendFloat32(b *Buffer, n float32) {
b.Data = strconv.AppendFloat(b.Data, float64(n), 'g', -1, 32)
}
// AppendFloat64 appends the string form of the given float32 to the given
// Buffer.
func AppendFloat64(b *Buffer, n float64) {
b.Data = strconv.AppendFloat(b.Data, n, 'g', -1, 64)
}
// AppendBool appends "true" or "false", according to the given bool to the
// given Buffer.
func AppendBool(b *Buffer, n bool) {
b.Data = strconv.AppendBool(b.Data, n)
}