-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.go
66 lines (57 loc) · 1 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
package regexp2gen
import (
"bytes"
)
type Buffer struct {
*bytes.Buffer
buffers []*bytes.Buffer
marks map[int]*bytes.Buffer
}
func NewBuffer() *Buffer {
return &Buffer{
Buffer: &bytes.Buffer{},
buffers: []*bytes.Buffer{},
marks: make(map[int]*bytes.Buffer),
}
}
func (b *Buffer) WriteAll(p []byte) (n int, err error) {
cnt := 0
for {
i := cnt - 1
if i < 0 {
i = 0
}
n, err := b.Write(p[i:])
cnt += n
if err != nil {
return cnt, err
}
if cnt == len(p) {
return cnt, nil
}
}
}
// push
func (b *Buffer) Setmark() {
b.buffers = append(b.buffers, b.Buffer)
b.Buffer = &bytes.Buffer{}
}
// pop
func (b *Buffer) Backmark(capture bool, index int) error {
outer := b.Buffer
l := len(b.buffers)
b.Buffer = b.buffers[l-1]
b.buffers = b.buffers[:l-1]
_, err := b.WriteAll(outer.Bytes())
if err != nil {
return err
}
if capture {
b.marks[index] = outer
}
return nil
}
func (b *Buffer) Getmark(index int) (*bytes.Buffer, bool) {
d, ok := b.marks[index]
return d, ok
}