forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterImpl.java
More file actions
219 lines (203 loc) · 7.03 KB
/
IterImpl.java
File metadata and controls
219 lines (203 loc) · 7.03 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package com.jsoniter;
import com.jsoniter.any.Any;
import java.io.IOException;
class IterImpl {
public static final int readObjectFieldAsHash(JsonIterator iter) throws IOException {
if (nextToken(iter) != '"') {
throw iter.reportError("readObjectFieldAsHash", "expect \"");
}
long hash = 0x811c9dc5;
for (int i = iter.head; i < iter.tail; i++) {
byte c = iter.buf[i];
if (c == '"') {
iter.head = i + 1;
if (nextToken(iter) != ':') {
throw iter.reportError("readObjectFieldAsHash", "expect :");
}
return (int) hash;
}
hash ^= c;
hash *= 0x1000193;
}
throw iter.reportError("readObjectFieldAsHash", "unmatched quote");
}
public static final Slice readObjectFieldAsSlice(JsonIterator iter) throws IOException {
Slice field = readSlice(iter);
if (nextToken(iter) != ':') {
throw iter.reportError("readObjectFieldAsSlice", "expect : after object field");
}
return field;
}
final static void skipArray(JsonIterator iter) throws IOException {
int level = 1;
for (int i = iter.head; i < iter.tail; i++) {
switch (iter.buf[i]) {
case '"': // If inside string, skip it
iter.head = i + 1;
skipString(iter);
i = iter.head - 1; // it will be i++ soon
break;
case '[': // If open symbol, increase level
level++;
break;
case ']': // If close symbol, increase level
level--;
// If we have returned to the original level, we're done
if (level == 0) {
iter.head = i + 1;
return;
}
break;
}
}
throw iter.reportError("skipArray", "incomplete array");
}
final static void skipObject(JsonIterator iter) throws IOException {
int level = 1;
for (int i = iter.head; i < iter.tail; i++) {
switch (iter.buf[i]) {
case '"': // If inside string, skip it
iter.head = i + 1;
skipString(iter);
i = iter.head - 1; // it will be i++ soon
break;
case '{': // If open symbol, increase level
level++;
break;
case '}': // If close symbol, increase level
level--;
// If we have returned to the original level, we're done
if (level == 0) {
iter.head = i + 1;
return;
}
break;
}
}
throw iter.reportError("skipObject", "incomplete object");
}
final static void skipString(JsonIterator iter) throws IOException {
int end = IterImplSkip.findStringEnd(iter);
if (end == -1) {
throw iter.reportError("skipString", "incomplete string");
} else {
iter.head = end;
}
}
final static void skipUntilBreak(JsonIterator iter) throws IOException {
// true, false, null, number
for (int i = iter.head; i < iter.tail; i++) {
byte c = iter.buf[i];
if (IterImplSkip.breaks[c]) {
iter.head = i;
return;
}
}
iter.head = iter.tail;
}
final static boolean skipNumber(JsonIterator iter) throws IOException {
// true, false, null, number
boolean dotFound = false;
for (int i = iter.head; i < iter.tail; i++) {
byte c = iter.buf[i];
if (c == '.') {
dotFound = true;
continue;
}
if (IterImplSkip.breaks[c]) {
iter.head = i;
return dotFound;
}
}
iter.head = iter.tail;
return dotFound;
}
// read the bytes between " "
public final static Slice readSlice(JsonIterator iter) throws IOException {
if (IterImpl.nextToken(iter) != '"') {
throw iter.reportError("readSlice", "expect \" for string");
}
int end = IterImplString.findSliceEnd(iter);
if (end == -1) {
throw iter.reportError("readSlice", "incomplete string");
} else {
// reuse current buffer
iter.reusableSlice.reset(iter.buf, iter.head, end - 1);
iter.head = end;
return iter.reusableSlice;
}
}
final static byte nextToken(JsonIterator iter) throws IOException {
int i = iter.head;
try {
for (; ; ) {
byte c = iter.buf[i++];
switch (c) {
case ' ':
case '\n':
case '\r':
case '\t':
continue;
default:
if (i > iter.tail) {
iter.head = iter.tail;
return 0;
}
iter.head = i;
return c;
}
}
} catch (IndexOutOfBoundsException e) {
iter.head = iter.tail;
return 0;
}
}
final static byte readByte(JsonIterator iter) throws IOException {
if (iter.head == iter.tail) {
return 0;
}
return iter.buf[iter.head++];
}
public static Any readAny(JsonIterator iter) throws IOException {
int start = iter.head;
byte c = nextToken(iter);
switch (c) {
case '"':
skipString(iter);
return Any.lazyString(iter.buf, start, iter.head);
case '-':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (skipNumber(iter)) {
return Any.lazyDouble(iter.buf, start, iter.head);
} else {
return Any.lazyLong(iter.buf, start, iter.head);
}
case 't':
skipUntilBreak(iter);
return Any.wrap(true);
case 'f':
skipUntilBreak(iter);
return Any.wrap(false);
case 'n':
skipUntilBreak(iter);
return Any.wrap((Object) null);
case '[':
skipArray(iter);
return Any.lazyArray(iter.buf, start, iter.head);
case '{':
skipObject(iter);
return Any.lazyObject(iter.buf, start, iter.head);
default:
throw iter.reportError("IterImplSkip", "do not know how to skip: " + c);
}
}
}