Skip to content

Commit

Permalink
Avoid calling source.select() if the value is buffered.
Browse files Browse the repository at this point in the history
  • Loading branch information
serj-lotutovici committed Jan 22, 2017
1 parent db30df1 commit ddf2489
Showing 1 changed file with 43 additions and 15 deletions.
Expand Up @@ -563,9 +563,12 @@ private boolean isLiteral(int c) throws IOException {
if (p == PEEKED_NONE) {
p = doPeek();
}
if (p < PEEKED_SINGLE_QUOTED_NAME || p > PEEKED_BUFFERED_NAME) {
if (p < PEEKED_SINGLE_QUOTED_NAME || p > PEEKED_BUFFERED_NAME) {
return -1;
}
if (p == PEEKED_BUFFERED_NAME) {
return findName(peekedString, options);
}

int result = source.select(options.doubleQuoteSuffix);
if (result != -1) {
Expand All @@ -580,20 +583,31 @@ private boolean isLiteral(int c) throws IOException {
String lastPathName = pathNames[stackSize - 1];

String nextName = nextName();
result = findName(nextName, options);

if (result == -1) {
peeked = PEEKED_BUFFERED_NAME;
peekedString = nextName;
// We can't push the path further, make it seem like nothing happened.
pathNames[stackSize - 1] = lastPathName;
}

return result;
}

/**
* If {@code name} is in {@code options} this consumes it and returns it's index.
* Otherwise this returns -1 and no name is consumed.
*/
private int findName(String name, Options options) {
for (int i = 0, size = options.strings.length; i < size; i++) {
if (nextName.equals(options.strings[i])) {
if (name.equals(options.strings[i])) {
peeked = PEEKED_NONE;
pathNames[stackSize - 1] = nextName;
pathNames[stackSize - 1] = name;

return i;
}
}

peeked = PEEKED_BUFFERED_NAME;
peekedString = nextName;
// We can't push the path further, make it seem like nothing happened.
pathNames[stackSize - 1] = lastPathName;

return -1;
}

Expand Down Expand Up @@ -632,6 +646,9 @@ private boolean isLiteral(int c) throws IOException {
if (p < PEEKED_SINGLE_QUOTED || p > PEEKED_BUFFERED) {
return -1;
}
if (p == PEEKED_BUFFERED) {
return findString(peekedString, options);
}

int result = source.select(options.doubleQuoteSuffix);
if (result != -1) {
Expand All @@ -642,19 +659,30 @@ private boolean isLiteral(int c) throws IOException {
}

String nextString = nextString();
result = findString(nextString, options);

if (result == -1) {
peeked = PEEKED_BUFFERED;
peekedString = nextString;
pathIndices[stackSize - 1]--;
}

return result;
}

/**
* If {@code string} is in {@code options} this consumes it and returns it's index.
* Otherwise this returns -1 and no string is consumed.
*/
private int findString(String string, Options options) {
for (int i = 0, size = options.strings.length; i < size; i++) {
if (nextString.equals(options.strings[i])) {
if (string.equals(options.strings[i])) {
peeked = PEEKED_NONE;
pathIndices[stackSize - 1]++;

return i;
}
}

peeked = PEEKED_BUFFERED;
peekedString = nextString;
pathIndices[stackSize - 1]--;

return -1;
}

Expand Down

0 comments on commit ddf2489

Please sign in to comment.