Skip to content

Commit efbbe07

Browse files
committed
extract out skip
1 parent 4d335a4 commit efbbe07

File tree

4 files changed

+205
-197
lines changed

4 files changed

+205
-197
lines changed

src/main/java/com/jsoniter/Jsoniter.java

Lines changed: 8 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
public class Jsoniter implements Closeable {
1414

15-
private static final boolean[] breaks = new boolean[256];
1615
final static ValueType[] valueTypes = new ValueType[256];
1716
InputStream in;
1817
byte[] buf;
@@ -43,13 +42,6 @@ public class Jsoniter implements Closeable {
4342
valueTypes['n'] = ValueType.NULL;
4443
valueTypes['['] = ValueType.ARRAY;
4544
valueTypes['{'] = ValueType.OBJECT;
46-
breaks[' '] = true;
47-
breaks['\t'] = true;
48-
breaks['\n'] = true;
49-
breaks['\r'] = true;
50-
breaks[','] = true;
51-
breaks['}'] = true;
52-
breaks[']'] = true;
5345
}
5446

5547
public Jsoniter(InputStream in, byte[] buf) {
@@ -153,7 +145,7 @@ public final String currentBuffer() {
153145
public final boolean readNull() throws IOException {
154146
byte c = nextToken();
155147
if (c == 'n') {
156-
skipUntilBreak();
148+
Skip.skipUntilBreak(this);
157149
return true;
158150
}
159151
unreadByte();
@@ -164,10 +156,10 @@ public final boolean readBoolean() throws IOException {
164156
byte c = nextToken();
165157
switch (c) {
166158
case 't':
167-
skipUntilBreak();
159+
Skip.skipUntilBreak(this);
168160
return true;
169161
case 'f':
170-
skipUntilBreak();
162+
Skip.skipUntilBreak(this);
171163
return false;
172164
default:
173165
throw reportError("readBoolean", "expect t or f, found: " + c);
@@ -245,7 +237,7 @@ public final String readObject() throws IOException {
245237
byte c = nextToken();
246238
switch (c) {
247239
case 'n':
248-
skipUntilBreak();
240+
Skip.skipUntilBreak(this);
249241
return null;
250242
case '{':
251243
c = nextToken();
@@ -332,191 +324,16 @@ public final <T> T read(TypeLiteral<T> typeLiteral) throws IOException {
332324
return (T) Codegen.getDecoder(typeLiteral.cacheKey, type).decode(this);
333325
}
334326

335-
public final void skip() throws IOException {
336-
byte c = nextToken();
337-
switch (c) {
338-
case '"':
339-
skipString();
340-
return;
341-
case '-':
342-
case '0':
343-
case '1':
344-
case '2':
345-
case '3':
346-
case '4':
347-
case '5':
348-
case '6':
349-
case '7':
350-
case '8':
351-
case '9':
352-
case 't':
353-
case 'f':
354-
case 'n':
355-
skipUntilBreak();
356-
return;
357-
case '[':
358-
skipArray();
359-
return;
360-
case '{':
361-
skipObject();
362-
return;
363-
default:
364-
throw reportError("Skip", "do not know how to skip: " + c);
365-
}
366-
}
367-
368-
final void skipObject() throws IOException {
369-
int level = 1;
370-
for (; ; ) {
371-
for (int i = head; i < tail; i++) {
372-
switch (buf[i]) {
373-
case '"': // If inside string, skip it
374-
head = i + 1;
375-
skipString();
376-
i = head - 1; // it will be i++ soon
377-
break;
378-
case '{': // If open symbol, increase level
379-
level++;
380-
break;
381-
case '}': // If close symbol, increase level
382-
level--;
383-
384-
// If we have returned to the original level, we're done
385-
if (level == 0) {
386-
head = i + 1;
387-
return;
388-
}
389-
break;
390-
}
391-
}
392-
if (!loadMore()) {
393-
return;
394-
}
395-
}
396-
}
397-
398-
final void skipArray() throws IOException {
399-
int level = 1;
400-
for (; ; ) {
401-
for (int i = head; i < tail; i++) {
402-
switch (buf[i]) {
403-
case '"': // If inside string, skip it
404-
head = i + 1;
405-
skipString();
406-
i = head - 1; // it will be i++ soon
407-
break;
408-
case '[': // If open symbol, increase level
409-
level++;
410-
break;
411-
case ']': // If close symbol, increase level
412-
level--;
413-
414-
// If we have returned to the original level, we're done
415-
if (level == 0) {
416-
head = i + 1;
417-
return;
418-
}
419-
break;
420-
}
421-
}
422-
if (!loadMore()) {
423-
return;
424-
}
425-
}
426-
}
427-
428-
final void skipUntilBreak() throws IOException {
429-
// true, false, null, number
430-
for (; ; ) {
431-
for (int i = head; i < tail; i++) {
432-
byte c = buf[i];
433-
if (breaks[c]) {
434-
head = i;
435-
return;
436-
}
437-
}
438-
if (!loadMore()) {
439-
return;
440-
}
441-
}
442-
}
443-
444-
final void skipString() throws IOException {
445-
for (; ; ) {
446-
int end = findStringEnd();
447-
if (end == -1) {
448-
int j = tail - 1;
449-
boolean escaped = true;
450-
for (; ; ) {
451-
if (j < head || buf[j] != '\\') {
452-
// even number of backslashes
453-
// either end of buffer, or " found
454-
escaped = false;
455-
break;
456-
}
457-
j--;
458-
if (j < head || buf[j] != '\\') {
459-
// odd number of backslashes
460-
// it is \" or \\\"
461-
break;
462-
}
463-
j--;
464-
465-
}
466-
if (!loadMore()) {
467-
return;
468-
}
469-
if (escaped) {
470-
head = 1; // skip the first char as last char readAny is \
471-
}
472-
} else {
473-
head = end;
474-
return;
475-
}
476-
}
477-
}
478-
479-
// adapted from: https://github.com/buger/jsonparser/blob/master/parser.go
480-
// Tries to find the end of string
481-
// Support if string contains escaped quote symbols.
482-
final int findStringEnd() {
483-
boolean escaped = false;
484-
for (int i = head; i < tail; i++) {
485-
byte c = buf[i];
486-
if (c == '"') {
487-
if (!escaped) {
488-
return i + 1;
489-
} else {
490-
int j = i - 1;
491-
for (; ; ) {
492-
if (j < head || buf[j] != '\\') {
493-
// even number of backslashes
494-
// either end of buffer, or " found
495-
return i + 1;
496-
}
497-
j--;
498-
if (j < head || buf[j] != '\\') {
499-
// odd number of backslashes
500-
// it is \" or \\\"
501-
break;
502-
}
503-
j--;
504-
}
505-
}
506-
} else if (c == '\\') {
507-
escaped = true;
508-
}
509-
}
510-
return -1;
511-
}
512-
513-
514327
public ValueType whatIsNext() throws IOException {
515328
ValueType valueType = valueTypes[nextToken()];
516329
unreadByte();
517330
return valueType;
518331
}
519332

333+
public void skip() throws IOException {
334+
Skip.skip(this);
335+
}
336+
520337
public static void registerTypeDecoder(Class clazz, Decoder decoder) {
521338
Codegen.addNewDecoder(TypeLiteral.generateCacheKey(clazz), decoder);
522339
}

0 commit comments

Comments
 (0)