-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream.go
52 lines (47 loc) · 957 Bytes
/
stream.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
package forest
import (
"bytes"
"strings"
)
type streamInBetween struct {
startData bool
initiate strings.Builder
temp *bytes.Buffer
startPoint string
endPoint rune
}
func newStreamInBetween(startPoint string, endPoint rune) *streamInBetween {
return &streamInBetween{
temp: bytes.NewBuffer(nil),
startPoint: startPoint,
endPoint: endPoint,
}
}
func (e *streamInBetween) Read(p []byte) (n int, err error) {
return e.temp.Read(p)
}
func (e *streamInBetween) Write(p []byte) (n int, err error) {
for _, b := range p {
if e.startData {
// Stop early since we hit the closing quote
if b == byte(e.endPoint) {
return 0, nil
}
err = e.temp.WriteByte(b)
if err != nil {
return 0, err
}
} else {
err = e.initiate.WriteByte(b)
if err != nil {
return 0, err
}
dd := e.initiate.String()
if strings.Contains(dd, e.startPoint) {
e.startData = true
}
}
n++
}
return
}