-
Notifications
You must be signed in to change notification settings - Fork 4
/
dec_read.go
166 lines (148 loc) · 2.83 KB
/
dec_read.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package jx
import (
"io"
"math/bits"
)
// Next gets Type of relatively next json element
func (d *Decoder) Next() Type {
v, err := d.next()
if err == nil {
d.unread()
}
return types[v]
}
var spaceSet = [256]byte{
' ': 1, '\n': 1, '\t': 1, '\r': 1,
}
func (d *Decoder) consume(c byte) (err error) {
for {
buf := d.buf[d.head:d.tail]
for i, got := range buf {
switch spaceSet[got] {
default:
if c != got {
return badToken(got, d.offset()+i)
}
d.head += i + 1
return nil
case 1:
continue
}
}
if err = d.read(); err != nil {
if err == io.EOF {
return io.ErrUnexpectedEOF
}
return err
}
}
}
// more is next but io.EOF is unexpected.
func (d *Decoder) more() (byte, error) {
c, err := d.next()
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return c, err
}
// next reads next non-whitespace token or error.
func (d *Decoder) next() (byte, error) {
for {
buf := d.buf[d.head:d.tail]
for i, c := range buf {
switch spaceSet[c] {
default:
d.head += i + 1
return c, nil
case 1:
continue
}
}
if err := d.read(); err != nil {
return 0, err
}
}
}
// peek returns next byte without advancing.
func (d *Decoder) peek() (byte, error) {
if d.head == d.tail {
if err := d.read(); err != nil {
return 0, err
}
}
c := d.buf[d.head]
return c, nil
}
func (d *Decoder) byte() (byte, error) {
if d.head == d.tail {
err := d.read()
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
if err != nil {
return 0, err
}
}
c := d.buf[d.head]
d.head++
return c, nil
}
func (d *Decoder) read() error {
if d.reader == nil {
d.head = d.tail
return io.EOF
}
n, err := d.reader.Read(d.buf)
switch err {
case nil:
case io.EOF:
if n > 0 {
break
}
fallthrough
default:
return err
}
d.streamOffset += d.tail
d.head = 0
d.tail = n
return nil
}
func (d *Decoder) readAtLeast(n int) error {
if d.reader == nil {
d.head = d.tail
return io.ErrUnexpectedEOF
}
if need := n - len(d.buf); need > 0 {
d.buf = append(d.buf, make([]byte, need)...)
}
n, err := io.ReadAtLeast(d.reader, d.buf, n)
if err != nil {
if err == io.EOF && n == 0 {
return io.ErrUnexpectedEOF
}
return err
}
d.streamOffset += d.tail
d.head = 0
d.tail = n
return nil
}
func (d *Decoder) unread() { d.head-- }
func (d *Decoder) readExact4(b *[4]byte) error {
if buf := d.buf[d.head:d.tail]; len(buf) >= len(b) {
d.head += copy(b[:], buf[:4])
return nil
}
n := copy(b[:], d.buf[d.head:d.tail])
if err := d.readAtLeast(len(b) - n); err != nil {
return err
}
d.head += copy(b[n:], d.buf[d.head:d.tail])
return nil
}
func findInvalidToken4(buf [4]byte, mask uint32, offset int) error {
c := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24
idx := bits.TrailingZeros32(c^mask) / 8
return badToken(buf[idx], offset+idx)
}