Debian Codegraph @

buffer/buffer.go

raw symbols SCIP
1 // Copyright 2020 The gVisor Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Package buffer provides the implementation of a buffer view.
16 //
17 // A view is an flexible buffer, supporting the safecopy operations natively as
18 // well as the ability to grow via either prepend or append, as well as shrink.
19 package buffer
20
21 // buffer encapsulates a queueable byte buffer.
22 //
23 // +stateify savable
24 type buffer struct {
25 data []byte
26 read int
27 write int
28 bufferEntry
29 }
30
31 // init performs in-place initialization for zero value.
32 func (b *buffer) init(size int) {
33 b.data = make([]byte, size)
34 }
35
36 // initWithData initializes b with data, taking ownership.
37 func (b *buffer) initWithData(data []byte) {
38 b.data = data
39 b.read = 0
40 b.write = len(data)
41 }
42
43 // Reset resets read and write locations, effectively emptying the buffer.
44 func (b *buffer) Reset() {
45 b.read = 0
46 b.write = 0
47 }
48
49 // Remove removes r from the unread portion. It returns false if r does not
50 // fully reside in b.
51 func (b *buffer) Remove(r Range) bool {
52 sz := b.ReadSize()
53 switch {
54 case r.Len() != r.Intersect(Range{end: sz}).Len():
55 return false
56 case r.Len() == 0:
57 // Noop
58 case r.begin == 0:
59 b.read += r.end
60 case r.end == sz:
61 b.write -= r.Len()
62 default:
63 // Remove from the middle of b.data.
64 copy(b.data[b.read+r.begin:], b.data[b.read+r.end:b.write])
65 b.write -= r.Len()
66 }
67 return true
68 }
69
70 // Full indicates the buffer is full.
71 //
72 // This indicates there is no capacity left to write.
73 func (b *buffer) Full() bool {
74 return b.write == len(b.data)
75 }
76
77 // ReadSize returns the number of bytes available for reading.
78 func (b *buffer) ReadSize() int {
79 return b.write - b.read
80 }
81
82 // ReadMove advances the read index by the given amount.
83 func (b *buffer) ReadMove(n int) {
84 b.read += n
85 }
86
87 // ReadSlice returns the read slice for this buffer.
88 func (b *buffer) ReadSlice() []byte {
89 return b.data[b.read:b.write]
90 }
91
92 // WriteSize returns the number of bytes available for writing.
93 func (b *buffer) WriteSize() int {
94 return len(b.data) - b.write
95 }
96
97 // WriteMove advances the write index by the given amount.
98 func (b *buffer) WriteMove(n int) {
99 b.write += n
100 }
101
102 // WriteSlice returns the write slice for this buffer.
103 func (b *buffer) WriteSlice() []byte {
104 return b.data[b.write:]
105 }