1package jsoniter
2
3import "fmt"
4
5// ReadNil reads a json object as nil and
6// returns whether it's a nil or not
7func (iter *Iterator) ReadNil() (ret bool) {
8	c := iter.nextToken()
9	if c == 'n' {
10		iter.skipThreeBytes('u', 'l', 'l') // null
11		return true
12	}
13	iter.unreadByte()
14	return false
15}
16
17// ReadBool reads a json object as BoolValue
18func (iter *Iterator) ReadBool() (ret bool) {
19	c := iter.nextToken()
20	if c == 't' {
21		iter.skipThreeBytes('r', 'u', 'e')
22		return true
23	}
24	if c == 'f' {
25		iter.skipFourBytes('a', 'l', 's', 'e')
26		return false
27	}
28	iter.ReportError("ReadBool", "expect t or f")
29	return
30}
31
32// SkipAndReturnBytes skip next JSON element, and return its content as []byte.
33// The []byte can be kept, it is a copy of data.
34func (iter *Iterator) SkipAndReturnBytes() []byte {
35	iter.startCapture(iter.head)
36	iter.Skip()
37	return iter.stopCapture()
38}
39
40type captureBuffer struct {
41	startedAt int
42	captured  []byte
43}
44
45func (iter *Iterator) startCapture(captureStartedAt int) {
46	if iter.captured != nil {
47		panic("already in capture mode")
48	}
49	iter.captureStartedAt = captureStartedAt
50	iter.captured = make([]byte, 0, 32)
51}
52
53func (iter *Iterator) stopCapture() []byte {
54	if iter.captured == nil {
55		panic("not in capture mode")
56	}
57	captured := iter.captured
58	remaining := iter.buf[iter.captureStartedAt:iter.head]
59	iter.captureStartedAt = -1
60	iter.captured = nil
61	if len(captured) == 0 {
62		return remaining
63	}
64	captured = append(captured, remaining...)
65	return captured
66}
67
68// Skip skips a json object and positions to relatively the next json object
69func (iter *Iterator) Skip() {
70	c := iter.nextToken()
71	switch c {
72	case '"':
73		iter.skipString()
74	case 'n':
75		iter.skipThreeBytes('u', 'l', 'l') // null
76	case 't':
77		iter.skipThreeBytes('r', 'u', 'e') // true
78	case 'f':
79		iter.skipFourBytes('a', 'l', 's', 'e') // false
80	case '0':
81		iter.unreadByte()
82		iter.ReadFloat32()
83	case '-', '1', '2', '3', '4', '5', '6', '7', '8', '9':
84		iter.skipNumber()
85	case '[':
86		iter.skipArray()
87	case '{':
88		iter.skipObject()
89	default:
90		iter.ReportError("Skip", fmt.Sprintf("do not know how to skip: %v", c))
91		return
92	}
93}
94
95func (iter *Iterator) skipFourBytes(b1, b2, b3, b4 byte) {
96	if iter.readByte() != b1 {
97		iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
98		return
99	}
100	if iter.readByte() != b2 {
101		iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
102		return
103	}
104	if iter.readByte() != b3 {
105		iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
106		return
107	}
108	if iter.readByte() != b4 {
109		iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
110		return
111	}
112}
113
114func (iter *Iterator) skipThreeBytes(b1, b2, b3 byte) {
115	if iter.readByte() != b1 {
116		iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
117		return
118	}
119	if iter.readByte() != b2 {
120		iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
121		return
122	}
123	if iter.readByte() != b3 {
124		iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
125		return
126	}
127}
128