Skip to content

Commit 950c965

Browse files
authored
Add doc comments for buffer type and methods (#342)
1 parent 9540ce9 commit 950c965

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

internal/buffer/buffer.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@ import (
55
"io"
66
)
77

8-
// buffer is similar to bytes.Buffer but specialized for this package
8+
// Buffer is similar to bytes.Buffer but specialized for this module.
9+
// The zero-value is an empty buffer ready for use.
910
type Buffer struct {
1011
b []byte
1112
i int
1213
}
1314

15+
// New creates a new Buffer with b as its initial contents.
16+
// Use this to start reading from b.
1417
func New(b []byte) *Buffer {
1518
return &Buffer{b: b}
1619
}
1720

21+
// Next returns a slice containing the next n bytes from the buffer and advances the buffer.
22+
// If there are fewer than n bytes in the buffer, Next returns the remaining contents, false.
23+
// The slice is only valid until the next call to a read or write method.
1824
func (b *Buffer) Next(n int64) ([]byte, bool) {
1925
if b.readCheck(n) {
2026
buf := b.b[b.i:len(b.b)]
@@ -27,28 +33,34 @@ func (b *Buffer) Next(n int64) ([]byte, bool) {
2733
return buf, true
2834
}
2935

36+
// Skip advances the buffer by n bytes.
3037
func (b *Buffer) Skip(n int) {
3138
b.i += n
3239
}
3340

41+
// Reset resets the buffer to be empty but retains
42+
// the underlying storage for use by future writes.
3443
func (b *Buffer) Reset() {
3544
b.b = b.b[:0]
3645
b.i = 0
3746
}
3847

39-
// reclaim shifts used buffer space to the beginning of the
40-
// underlying slice.
48+
// Reclaim moves the unread portion of the buffer to the
49+
// beginning of the underlying slice and resets the index.
4150
func (b *Buffer) Reclaim() {
4251
l := b.Len()
4352
copy(b.b[:l], b.b[b.i:])
4453
b.b = b.b[:l]
4554
b.i = 0
4655
}
4756

57+
// returns true if n is larger than the unread portion of the buffer
4858
func (b *Buffer) readCheck(n int64) bool {
4959
return int64(b.i)+n > int64(len(b.b))
5060
}
5161

62+
// ReadByte reads one byte from the buffer and advances the buffer.
63+
// If there are insufficient bytes, an error is returned.
5264
func (b *Buffer) ReadByte() (byte, error) {
5365
if b.readCheck(1) {
5466
return 0, io.EOF
@@ -59,6 +71,8 @@ func (b *Buffer) ReadByte() (byte, error) {
5971
return byte_, nil
6072
}
6173

74+
// PeekByte returns the next byte in the buffer without advancing the buffer.
75+
// If there are insufficient bytes, an error is returned.
6276
func (b *Buffer) PeekByte() (byte, error) {
6377
if b.readCheck(1) {
6478
return 0, io.EOF
@@ -67,6 +81,9 @@ func (b *Buffer) PeekByte() (byte, error) {
6781
return b.b[b.i], nil
6882
}
6983

84+
// ReadUint16 reads two bytes from the buffer and decodes them
85+
// as big-endian into a uint16. Advances the buffer by two.
86+
// If there are insufficient bytes, an error is returned.
7087
func (b *Buffer) ReadUint16() (uint16, error) {
7188
if b.readCheck(2) {
7289
return 0, io.EOF
@@ -77,6 +94,9 @@ func (b *Buffer) ReadUint16() (uint16, error) {
7794
return n, nil
7895
}
7996

97+
// ReadUint32 reads four bytes from the buffer and decodes them
98+
// as big-endian into a uint32. Advances the buffer by four.
99+
// If there are insufficient bytes, an error is returned.
80100
func (b *Buffer) ReadUint32() (uint32, error) {
81101
if b.readCheck(4) {
82102
return 0, io.EOF
@@ -87,6 +107,9 @@ func (b *Buffer) ReadUint32() (uint32, error) {
87107
return n, nil
88108
}
89109

110+
// ReadUint64 reads eight bytes from the buffer and decodes them
111+
// as big-endian into a uint64. Advances the buffer by eight.
112+
// If there are insufficient bytes, an error is returned.
90113
func (b *Buffer) ReadUint64() (uint64, error) {
91114
if b.readCheck(8) {
92115
return 0, io.EOF
@@ -97,6 +120,8 @@ func (b *Buffer) ReadUint64() (uint64, error) {
97120
return n, nil
98121
}
99122

123+
// ReadFromOnce reads from r to populate the buffer.
124+
// Reads up to cap - len of the underlying slice.
100125
func (b *Buffer) ReadFromOnce(r io.Reader) error {
101126
const minRead = 512
102127

@@ -116,44 +141,54 @@ func (b *Buffer) ReadFromOnce(r io.Reader) error {
116141
return err
117142
}
118143

144+
// Append appends p to the existing buffer.
119145
func (b *Buffer) Append(p []byte) {
120146
b.b = append(b.b, p...)
121147
}
122148

149+
// AppendByte appends bb to the existing buffer.
123150
func (b *Buffer) AppendByte(bb byte) {
124151
b.b = append(b.b, bb)
125152
}
126153

154+
// AppendString appends s to the existing buffer.
127155
func (b *Buffer) AppendString(s string) {
128156
b.b = append(b.b, s...)
129157
}
130158

159+
// Len returns the number of bytes of the unread portion of the buffer.
131160
func (b *Buffer) Len() int {
132161
return len(b.b) - b.i
133162
}
134163

164+
// Size returns the number of bytes that have been read from this buffer.
165+
// This implies a minimum size of the underlying buffer.
135166
func (b *Buffer) Size() int {
136167
return b.i
137168
}
138169

170+
// Bytes returns a slice containing the unread portion of the buffer.
139171
func (b *Buffer) Bytes() []byte {
140172
return b.b[b.i:]
141173
}
142174

175+
// Detach returns the underlying byte slice, disassociating it from the buffer.
143176
func (b *Buffer) Detach() []byte {
144177
temp := b.b
145178
b.b = nil
146179
b.i = 0
147180
return temp
148181
}
149182

183+
// AppendUint16 appends n as two bytes in big-endian encoding.
150184
func (b *Buffer) AppendUint16(n uint16) {
151185
b.b = append(b.b,
152186
byte(n>>8),
153187
byte(n),
154188
)
155189
}
156190

191+
// AppendUint32 appends n as four bytes in big-endian encoding.
157192
func (b *Buffer) AppendUint32(n uint32) {
158193
b.b = append(b.b,
159194
byte(n>>24),
@@ -163,6 +198,7 @@ func (b *Buffer) AppendUint32(n uint32) {
163198
)
164199
}
165200

201+
// AppendUint64 appends n as eight bytes in big-endian encoding.
166202
func (b *Buffer) AppendUint64(n uint64) {
167203
b.b = append(b.b,
168204
byte(n>>56),

0 commit comments

Comments
 (0)