Permalink
Browse files

Replace switch with if/else when processing whitespace. If/else is fa…

…ster!
  • Loading branch information...
swankjesse committed Aug 27, 2012
1 parent b0a1729 commit bdf2cac6d4a5c18cf3462330eefd8e7c57990e81
@@ -1312,14 +1312,11 @@ private int nextNonWhitespace(boolean throwOnEof) throws IOException {
}
int c = buffer[p++];
switch (c) {
case '\t':
case ' ':
case '\n':
case '\r':
if (c == ' ' || c == '\n' || c == '\r' || c == '\t') {
continue;
}
case '/':
if (c == '/') {
pos = p;
if (p == l) {
pos--; // push back '/' so it's still in the buffer when this method returns
@@ -1354,8 +1351,7 @@ private int nextNonWhitespace(boolean throwOnEof) throws IOException {
default:
return c;
}
case '#':
} else if (c == '#') {
pos = p;
/*
* Skip a # hash end-of-line comment. The JSON RFC doesn't
@@ -1366,9 +1362,7 @@ private int nextNonWhitespace(boolean throwOnEof) throws IOException {
skipToEndOfLine();
p = pos;
l = limit;
continue;
default:
} else {
pos = p;
return c;
}
@@ -146,45 +146,41 @@ public void parse(char[] data, Document document) throws Exception {
jsonReader.close();
}
public void readObject(com.google.gson.stream.JsonReader reader) throws IOException {
reader.beginObject();
while (reader.hasNext()) {
reader.nextName();
readToken(reader);
}
reader.endObject();
}
public void readArray(com.google.gson.stream.JsonReader reader) throws IOException {
reader.beginArray();
while (reader.hasNext()) {
readToken(reader);
}
reader.endArray();
}
private void readToken(com.google.gson.stream.JsonReader reader) throws IOException {
switch (reader.peek()) {
case BEGIN_ARRAY:
readArray(reader);
break;
case BEGIN_OBJECT:
readObject(reader);
break;
case BOOLEAN:
reader.nextBoolean();
break;
case NULL:
reader.nextNull();
break;
case NUMBER:
reader.nextLong();
break;
case STRING:
reader.nextString();
break;
default:
throw new IllegalArgumentException("Unexpected token" + reader.peek());
while (true) {
switch (reader.peek()) {
case BEGIN_ARRAY:
reader.beginArray();
break;
case END_ARRAY:
reader.endArray();
break;
case BEGIN_OBJECT:
reader.beginObject();
break;
case END_OBJECT:
reader.endObject();
break;
case NAME:
reader.nextName();
break;
case BOOLEAN:
reader.nextBoolean();
break;
case NULL:
reader.nextNull();
break;
case NUMBER:
reader.nextLong();
break;
case STRING:
reader.nextString();
break;
case END_DOCUMENT:
return;
default:
throw new IllegalArgumentException("Unexpected token" + reader.peek());
}
}
}
}

0 comments on commit bdf2cac

Please sign in to comment.