-
Notifications
You must be signed in to change notification settings - Fork 518
/
IterImpl.java
491 lines (464 loc) · 19.5 KB
/
IterImpl.java
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package com.jsoniter;
import com.jsoniter.any.Any;
import com.jsoniter.spi.JsonException;
import com.jsoniter.spi.Slice;
import java.io.IOException;
import java.math.BigInteger;
class IterImpl {
private static BigInteger maxLong = BigInteger.valueOf(Long.MAX_VALUE);
private static BigInteger minLong = BigInteger.valueOf(Long.MIN_VALUE);
private static BigInteger maxInt = BigInteger.valueOf(Integer.MAX_VALUE);
private static BigInteger minInt = BigInteger.valueOf(Integer.MIN_VALUE);
public static final int readObjectFieldAsHash(JsonIterator iter) throws IOException {
if (readByte(iter) != '"') {
if (nextToken(iter) != '"') {
throw iter.reportError("readObjectFieldAsHash", "expect \"");
}
}
long hash = 0x811c9dc5;
int i = iter.head;
for (; i < iter.tail; i++) {
byte c = iter.buf[i];
if (c == '"') {
break;
}
hash ^= c;
hash *= 0x1000193;
}
iter.head = i + 1;
if (readByte(iter) != ':') {
if (nextToken(iter) != ':') {
throw iter.reportError("readObjectFieldAsHash", "expect :");
}
}
return (int) hash;
}
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, decrease 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, decrease 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 == '.' || c == 'e' || c == 'E') {
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(final JsonIterator iter) throws IOException {
int i = iter.head;
for (; ; ) {
byte c = iter.buf[i++];
switch (c) {
case ' ':
case '\n':
case '\r':
case '\t':
continue;
default:
iter.head = i;
return c;
}
}
}
final static byte readByte(JsonIterator iter) throws IOException {
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 't':
skipFixedBytes(iter, 3);
return Any.wrap(true);
case 'f':
skipFixedBytes(iter, 4);
return Any.wrap(false);
case 'n':
skipFixedBytes(iter, 3);
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:
if (skipNumber(iter)) {
return Any.lazyDouble(iter.buf, start, iter.head);
} else {
return Any.lazyLong(iter.buf, start, iter.head);
}
}
}
public static void skipFixedBytes(JsonIterator iter, int n) throws IOException {
iter.head += n;
}
public final static boolean loadMore(JsonIterator iter) throws IOException {
return false;
}
public final static int readStringSlowPath(JsonIterator iter, int j) throws IOException {
try {
boolean isExpectingLowSurrogate = false;
for (int i = iter.head; i < iter.tail; ) {
int bc = iter.buf[i++];
if (bc == '"') {
iter.head = i;
return j;
}
if (bc == '\\') {
bc = iter.buf[i++];
switch (bc) {
case 'b':
bc = '\b';
break;
case 't':
bc = '\t';
break;
case 'n':
bc = '\n';
break;
case 'f':
bc = '\f';
break;
case 'r':
bc = '\r';
break;
case '"':
case '/':
case '\\':
break;
case 'u':
bc = (IterImplString.translateHex(iter.buf[i++]) << 12) +
(IterImplString.translateHex(iter.buf[i++]) << 8) +
(IterImplString.translateHex(iter.buf[i++]) << 4) +
IterImplString.translateHex(iter.buf[i++]);
if (Character.isHighSurrogate((char) bc)) {
if (isExpectingLowSurrogate) {
throw new JsonException("invalid surrogate");
} else {
isExpectingLowSurrogate = true;
}
} else if (Character.isLowSurrogate((char) bc)) {
if (isExpectingLowSurrogate) {
isExpectingLowSurrogate = false;
} else {
throw new JsonException("invalid surrogate");
}
} else {
if (isExpectingLowSurrogate) {
throw new JsonException("invalid surrogate");
}
}
break;
default:
throw iter.reportError("readStringSlowPath", "invalid escape character: " + bc);
}
} else if ((bc & 0x80) != 0) {
final int u2 = iter.buf[i++];
if ((bc & 0xE0) == 0xC0) {
bc = ((bc & 0x1F) << 6) + (u2 & 0x3F);
} else {
final int u3 = iter.buf[i++];
if ((bc & 0xF0) == 0xE0) {
bc = ((bc & 0x0F) << 12) + ((u2 & 0x3F) << 6) + (u3 & 0x3F);
} else {
final int u4 = iter.buf[i++];
if ((bc & 0xF8) == 0xF0) {
bc = ((bc & 0x07) << 18) + ((u2 & 0x3F) << 12) + ((u3 & 0x3F) << 6) + (u4 & 0x3F);
} else {
throw iter.reportError("readStringSlowPath", "invalid unicode character");
}
if (bc >= 0x10000) {
// check if valid unicode
if (bc >= 0x110000)
throw iter.reportError("readStringSlowPath", "invalid unicode character");
// split surrogates
final int sup = bc - 0x10000;
if (iter.reusableChars.length == j) {
char[] newBuf = new char[iter.reusableChars.length * 2];
System.arraycopy(iter.reusableChars, 0, newBuf, 0, iter.reusableChars.length);
iter.reusableChars = newBuf;
}
iter.reusableChars[j++] = (char) ((sup >>> 10) + 0xd800);
if (iter.reusableChars.length == j) {
char[] newBuf = new char[iter.reusableChars.length * 2];
System.arraycopy(iter.reusableChars, 0, newBuf, 0, iter.reusableChars.length);
iter.reusableChars = newBuf;
}
iter.reusableChars[j++] = (char) ((sup & 0x3ff) + 0xdc00);
continue;
}
}
}
}
if (iter.reusableChars.length == j) {
char[] newBuf = new char[iter.reusableChars.length * 2];
System.arraycopy(iter.reusableChars, 0, newBuf, 0, iter.reusableChars.length);
iter.reusableChars = newBuf;
}
iter.reusableChars[j++] = (char) bc;
}
throw iter.reportError("readStringSlowPath", "incomplete string");
} catch (IndexOutOfBoundsException e) {
throw iter.reportError("readString", "incomplete string");
}
}
public static int updateStringCopyBound(final JsonIterator iter, final int bound) {
return bound;
}
static final int readInt(final JsonIterator iter, final byte c) throws IOException {
int ind = IterImplNumber.intDigits[c];
if (ind == 0) {
IterImplForStreaming.assertNotLeadingZero(iter);
return 0;
}
if (ind == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
throw iter.reportError("readInt", "expect 0~9");
}
if (iter.tail - iter.head > 9) {
int i = iter.head;
int ind2 = IterImplNumber.intDigits[iter.buf[i]];
if (ind2 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
iter.head = i;
return -ind;
}
int ind3 = IterImplNumber.intDigits[iter.buf[++i]];
if (ind3 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
iter.head = i;
ind = ind * 10 + ind2;
return -ind;
}
int ind4 = IterImplNumber.intDigits[iter.buf[++i]];
if (ind4 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
iter.head = i;
ind = ind * 100 + ind2 * 10 + ind3;
return -ind;
}
int ind5 = IterImplNumber.intDigits[iter.buf[++i]];
if (ind5 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
iter.head = i;
ind = ind * 1000 + ind2 * 100 + ind3 * 10 + ind4;
return -ind;
}
int ind6 = IterImplNumber.intDigits[iter.buf[++i]];
if (ind6 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
iter.head = i;
ind = ind * 10000 + ind2 * 1000 + ind3 * 100 + ind4 * 10 + ind5;
return -ind;
}
int ind7 = IterImplNumber.intDigits[iter.buf[++i]];
if (ind7 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
iter.head = i;
ind = ind * 100000 + ind2 * 10000 + ind3 * 1000 + ind4 * 100 + ind5 * 10 + ind6;
return -ind;
}
int ind8 = IterImplNumber.intDigits[iter.buf[++i]];
if (ind8 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
iter.head = i;
ind = ind * 1000000 + ind2 * 100000 + ind3 * 10000 + ind4 * 1000 + ind5 * 100 + ind6 * 10 + ind7;
return -ind;
}
int ind9 = IterImplNumber.intDigits[iter.buf[++i]];
ind = ind * 10000000 + ind2 * 1000000 + ind3 * 100000 + ind4 * 10000 + ind5 * 1000 + ind6 * 100 + ind7 * 10 + ind8;
iter.head = i;
if (ind9 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
return -ind;
}
}
return IterImplForStreaming.readIntSlowPath(iter, ind);
}
static final long readLong(final JsonIterator iter, final byte c) throws IOException {
long ind = IterImplNumber.intDigits[c];
if (ind == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
throw iter.reportError("readLong", "expect 0~9");
}
if (iter.tail - iter.head > 9) {
int i = iter.head;
int ind2 = IterImplNumber.intDigits[iter.buf[i]];
if (ind2 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
iter.head = i;
return -ind;
}
int ind3 = IterImplNumber.intDigits[iter.buf[++i]];
if (ind3 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
iter.head = i;
ind = ind * 10 + ind2;
return -ind;
}
int ind4 = IterImplNumber.intDigits[iter.buf[++i]];
if (ind4 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
iter.head = i;
ind = ind * 100 + ind2 * 10 + ind3;
return -ind;
}
int ind5 = IterImplNumber.intDigits[iter.buf[++i]];
if (ind5 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
iter.head = i;
ind = ind * 1000 + ind2 * 100 + ind3 * 10 + ind4;
return -ind;
}
int ind6 = IterImplNumber.intDigits[iter.buf[++i]];
if (ind6 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
iter.head = i;
ind = ind * 10000 + ind2 * 1000 + ind3 * 100 + ind4 * 10 + ind5;
return -ind;
}
int ind7 = IterImplNumber.intDigits[iter.buf[++i]];
if (ind7 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
iter.head = i;
ind = ind * 100000 + ind2 * 10000 + ind3 * 1000 + ind4 * 100 + ind5 * 10 + ind6;
return -ind;
}
int ind8 = IterImplNumber.intDigits[iter.buf[++i]];
if (ind8 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
iter.head = i;
ind = ind * 1000000 + ind2 * 100000 + ind3 * 10000 + ind4 * 1000 + ind5 * 100 + ind6 * 10 + ind7;
return -ind;
}
int ind9 = IterImplNumber.intDigits[iter.buf[++i]];
ind = ind * 10000000 + ind2 * 1000000 + ind3 * 100000 + ind4 * 10000 + ind5 * 1000 + ind6 * 100 + ind7 * 10 + ind8;
iter.head = i;
if (ind9 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
return -ind;
}
}
return IterImplForStreaming.readLongSlowPath(iter, ind);
}
static final double readDouble(final JsonIterator iter) throws IOException {
int oldHead = iter.head;
try {
try {
long value = IterImplNumber.readLong(iter); // without the dot & sign
if (iter.head == iter.tail) {
return value;
}
byte c = iter.buf[iter.head];
if (c == '.') {
iter.head++;
int start = iter.head;
c = iter.buf[iter.head++];
long decimalPart = readLong(iter, c);
if (decimalPart == Long.MIN_VALUE) {
return IterImplForStreaming.readDoubleSlowPath(iter);
}
decimalPart = -decimalPart;
int decimalPlaces = iter.head - start;
if (decimalPlaces > 0 && decimalPlaces < IterImplNumber.POW10.length && (iter.head - oldHead) < 10) {
return value + (decimalPart / (double) IterImplNumber.POW10[decimalPlaces]);
} else {
iter.head = oldHead;
return IterImplForStreaming.readDoubleSlowPath(iter);
}
} else {
return value;
}
} finally {
if (iter.head < iter.tail && (iter.buf[iter.head] == 'e' || iter.buf[iter.head] == 'E')) {
iter.head = oldHead;
return IterImplForStreaming.readDoubleSlowPath(iter);
}
}
} catch (JsonException e) {
iter.head = oldHead;
return IterImplForStreaming.readDoubleSlowPath(iter);
}
}
}