forked from Fantom-foundation/go-opera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.go
73 lines (59 loc) · 1.14 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 fast
type Reader struct {
buf []byte
offset int
}
type Writer struct {
buf []byte
}
// NewReader wraps bytes with reading buffer.
func NewReader(bb []byte) *Reader {
return &Reader{
buf: bb,
offset: 0,
}
}
// NewWriter wraps bytes with writing buffer.
func NewWriter(bb []byte) *Writer {
return &Writer{
buf: bb,
}
}
// WriteByte to the buffer.
func (b *Writer) WriteByte(v byte) {
b.buf = append(b.buf, v)
}
// Write the byte to the buffer.
func (b *Writer) Write(v []byte) {
b.buf = append(b.buf, v...)
}
// Read n bytes.
func (b *Reader) Read(n int) []byte {
var res []byte
res = b.buf[b.offset : b.offset+n]
b.offset += n
return res
}
// ReadByte reads 1 byte.
func (b *Reader) ReadByte() byte {
var res byte
res = b.buf[b.offset]
b.offset++
return res
}
// Position of internal cursor.
func (b *Reader) Position() int {
return b.offset
}
// Bytes of internal buffer
func (b *Reader) Bytes() []byte {
return b.buf
}
// Bytes of internal buffer
func (b *Writer) Bytes() []byte {
return b.buf
}
// Empty returns true if the whole buffer is consumed
func (b *Reader) Empty() bool {
return len(b.buf) == b.offset
}