Use BufferedSource.indexOf instead of loops. (#677)

Cleanup of a rare code path for block comments in lenient readers.
This commit is contained in:
Eric Cochran
2018-09-20 21:20:11 -07:00
committed by GitHub
parent 1ba25ef3f9
commit 5a46cd6bd5

View File

@@ -31,6 +31,7 @@ final class JsonUtf8Reader extends JsonReader {
private static final ByteString UNQUOTED_STRING_TERMINALS private static final ByteString UNQUOTED_STRING_TERMINALS
= ByteString.encodeUtf8("{}[]:, \n\t\r\f/\\;#="); = ByteString.encodeUtf8("{}[]:, \n\t\r\f/\\;#=");
private static final ByteString LINEFEED_OR_CARRIAGE_RETURN = ByteString.encodeUtf8("\n\r"); private static final ByteString LINEFEED_OR_CARRIAGE_RETURN = ByteString.encodeUtf8("\n\r");
private static final ByteString CLOSING_BLOCK_COMMENT = ByteString.encodeUtf8("*/");
private static final int PEEKED_NONE = 0; private static final int PEEKED_NONE = 0;
private static final int PEEKED_BEGIN_OBJECT = 1; private static final int PEEKED_BEGIN_OBJECT = 1;
@@ -990,11 +991,9 @@ final class JsonUtf8Reader extends JsonReader {
// skip a /* c-style comment */ // skip a /* c-style comment */
buffer.readByte(); // '/' buffer.readByte(); // '/'
buffer.readByte(); // '*' buffer.readByte(); // '*'
if (!skipTo("*/")) { if (!skipToEndOfBlockComment()) {
throw syntaxError("Unterminated comment"); throw syntaxError("Unterminated comment");
} }
buffer.readByte(); // '*'
buffer.readByte(); // '/'
p = 0; p = 0;
continue; continue;
@@ -1043,20 +1042,13 @@ final class JsonUtf8Reader extends JsonReader {
} }
/** /**
* @param toFind a string to search for. Must not contain a newline. * Skips through the next closing block comment.
*/ */
private boolean skipTo(String toFind) throws IOException { private boolean skipToEndOfBlockComment() throws IOException {
outer: long index = source.indexOf(CLOSING_BLOCK_COMMENT);
for (; source.request(toFind.length()); ) { boolean found = index != -1;
for (int c = 0; c < toFind.length(); c++) { buffer.skip(found ? index + CLOSING_BLOCK_COMMENT.size() : buffer.size());
if (buffer.getByte(c) != toFind.charAt(c)) { return found;
buffer.readByte();
continue outer;
}
}
return true;
}
return false;
} }
@Override public String toString() { @Override public String toString() {