streaming_parser.go
937 字节
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
package parser
import (
"fmt"
"github.com/go-pg/pg/v10/internal/pool"
)
type StreamingParser struct {
pool.Reader
}
func NewStreamingParser(rd pool.Reader) StreamingParser {
return StreamingParser{
Reader: rd,
}
}
func (p StreamingParser) SkipByte(skip byte) error {
c, err := p.ReadByte()
if err != nil {
return err
}
if c == skip {
return nil
}
_ = p.UnreadByte()
return fmt.Errorf("got %q, wanted %q", c, skip)
}
func (p StreamingParser) ReadSubstring(b []byte) ([]byte, error) {
c, err := p.ReadByte()
if err != nil {
return b, err
}
for {
if c == '"' {
return b, nil
}
next, err := p.ReadByte()
if err != nil {
return b, err
}
if c == '\\' {
switch next {
case '\\', '"':
b = append(b, next)
c, err = p.ReadByte()
if err != nil {
return nil, err
}
default:
b = append(b, '\\')
c = next
}
continue
}
b = append(b, c)
c = next
}
}