-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathiter_str_reader.go
45 lines (37 loc) · 1 KB
/
iter_str_reader.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
package jsoniter
import "io"
// Returns a reader that can be used to read the following string until EOF.
// Using the reader after EOF is undefined behavior, and so is using the reader
// when when the current token isn't a string.
func (iter *Iterator) ReadStringAsReader() (r io.Reader) {
c := iter.nextToken()
if c == '"' {
return iterStrReader{iter}
}
iter.ReportError("ReadStringAsReader", `expects " or n, but found `+string([]byte{c}))
return
}
var _ io.Reader = iterStrReader{}
type iterStrReader struct {
iter *Iterator
}
func (r iterStrReader) Read(dst []byte) (n int, err error) {
for i := r.iter.head; i < r.iter.tail; i++ {
// require ascii string and no escape
// for: field name, base64, number
if r.iter.buf[i] == '"' {
n = copy(dst, r.iter.buf[r.iter.head:i])
r.iter.head += n + 1
err = io.EOF
return
}
}
n = copy(dst, r.iter.buf[r.iter.head:])
r.iter.head += n
if r.iter.head == r.iter.tail {
if !r.iter.loadMore() {
err = io.ErrUnexpectedEOF
}
}
return
}