Repository navigation
Expand file tree
/
Copy pathpeek_buffer.go
More file actions
119 lines (93 loc) · 2.57 KB
/
Copy pathpeek_buffer.go
File metadata and controls
119 lines (93 loc) · 2.57 KB
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
package gbuf
import (
"fmt"
"io"
)
// A PeekBuffer is a variable-sized buffer of T items with Read and Write methods.
// The zero value for PeekBuffer is an empty buffer ready to use.
//
// It is an extension of the Buffer type, that exposes Peek, PeekFrom and PeekRange methods.
type PeekBuffer[T any] struct {
*Buffer[T]
}
// Peek is just like Read, however it does not advance the buffer's offset after the items are read
func (b *PeekBuffer[T]) Peek(p []T) (n int, err error) {
if b.empty() {
// Buffer is empty, reset to recover space.
b.Reset()
if len(p) == 0 {
return 0, nil
}
return 0, io.EOF
}
n = copy(p, b.buf[b.off:])
return n, nil
}
// PeekFrom is just like Peek, but it reads from the buffer starting at offset `idx`
func (b *PeekBuffer[T]) PeekFrom(idx int, p []T) (n int, err error) {
if idx < 0 || idx >= len(b.buf) {
return 0, ErrPeekBufferIndexOutOfBounds
}
if b.empty() {
// Buffer is empty, reset to recover space.
b.Reset()
if len(p) == 0 {
return 0, nil
}
return 0, io.EOF
}
n = copy(p, b.buf[idx:])
return n, nil
}
// PeekRange is just like Peek, but it reads from the buffer starting at offset `from` until offset `to`
func (b *PeekBuffer[T]) PeekRange(from, to int, p []T) (n int, err error) {
var (
invert bool
ln = len(p)
)
if from == to {
return 0, nil
}
if from < 0 || from >= len(b.buf) {
return 0, fmt.Errorf("%w: from value: %d", ErrPeekBufferIndexOutOfBounds, from)
}
if to < 0 || to >= len(b.buf) {
return 0, fmt.Errorf("%w: to value: %d", ErrPeekBufferIndexOutOfBounds, to)
}
if from > to {
invert = true
to, from = from, to
}
if b.empty() {
// Buffer is empty, reset to recover space.
b.Reset()
if ln == 0 {
return 0, nil
}
return 0, io.EOF
}
n = copy(p, b.buf[to:from])
if invert {
for i, j := 0, ln-1; i < ln/2; i, j = i+1, j-1 {
p[i], p[j] = p[j], p[i]
}
}
return n, nil
}
// NewPeekBuffer creates and initializes a new Buffer using buf as its
// initial contents, as a PeekBuffer. The new Buffer takes ownership of buf,
// and the caller should not use buf after this call. NewPeekBuffer is intended to
// prepare a Buffer to read existing data. It can also be used to set
// the initial size of the internal buffer for writing. To do that,
// buf should have the desired capacity but a length of zero.
func NewPeekBuffer[T any](buf []T) *PeekBuffer[T] {
return &PeekBuffer[T]{
Buffer: NewBuffer(buf),
}
}
// AsPeekBuffer converts a Buffer[T] into a PeekBuffer[T]
func AsPeekBuffer[T any](buf *Buffer[T]) *PeekBuffer[T] {
return &PeekBuffer[T]{
Buffer: buf,
}
}