-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.go
More file actions
52 lines (43 loc) · 1.02 KB
/
Copy pathbuffer.go
File metadata and controls
52 lines (43 loc) · 1.02 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
package xmlify
import (
"io"
)
// Buffer represents itemBuffer
type Buffer struct {
buffer []byte
dataLength int
offset int
}
// NewBuffer creates a itemBuffer instance with given initial size
func NewBuffer(size int) *Buffer {
return &Buffer{
buffer: make([]byte, size),
}
}
// writeString add string to the itemBuffer
func (b *Buffer) writeString(value string) {
if len(value)+b.dataLength > len(b.buffer) {
b.buffer = append(b.buffer[:b.dataLength], []byte(value)...)
b.dataLength = len(b.buffer)
return
}
b.dataLength += copy(b.buffer[b.dataLength:], value)
}
// len returns actual itemBuffer dataLength
func (b *Buffer) len() int {
return b.dataLength
}
// reset sets actual itemBuffer dataLength and offset to 0
func (b *Buffer) reset() {
b.dataLength = 0
b.offset = 0
}
// Read reads current item itemBuffer to dest
func (b *Buffer) Read(dest []byte) (int, error) {
n := copy(dest, b.buffer[b.offset:b.dataLength])
b.offset += n
if b.offset == b.dataLength {
return n, io.EOF
}
return n, nil
}