Skip to content

Commit

Permalink
Fixes more position errors from stepping to new lines and then back.
Browse files Browse the repository at this point in the history
  • Loading branch information
John J. Aylward authored and johnjaylward committed Jul 3, 2017
1 parent e7e6ed9 commit 5284536
Showing 1 changed file with 50 additions and 20 deletions.
70 changes: 50 additions & 20 deletions JSONTokener.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ public class JSONTokener {
private final Reader reader;
/** flag to indicate that a previous character was requested. */
private boolean usePrevious;
/** the number of characters read in the previous line. */
private long characterPreviousLine;


/**
* Construct a JSONTokener from a Reader.
* Construct a JSONTokener from a Reader. The caller must close the Reader.
*
* @param reader A reader.
*/
Expand All @@ -69,12 +71,13 @@ public JSONTokener(Reader reader) {
this.previous = 0;
this.index = 0;
this.character = 1;
this.characterPreviousLine = 0;
this.line = 1;
}


/**
* Construct a JSONTokener from an InputStream.
* Construct a JSONTokener from an InputStream. The caller must close the input stream.
* @param inputStream The source.
*/
public JSONTokener(InputStream inputStream) {
Expand Down Expand Up @@ -103,12 +106,23 @@ public void back() throws JSONException {
if (this.usePrevious || this.index <= 0) {
throw new JSONException("Stepping back two steps is not supported");
}
this.index--;
this.character--;
this.decrementIndexes();
this.usePrevious = true;
this.eof = false;
}

/**
* Decrements the indexes for the {@link #back()} method based on the previous character read.
*/
private void decrementIndexes() {
this.index--;
if(this.previous=='\r' || this.previous == '\n') {
this.line--;
this.character=this.characterPreviousLine ;
} else if(this.character > 0){
this.character--;
}
}

/**
* Get the hex value of a character (base16).
Expand Down Expand Up @@ -183,26 +197,39 @@ public char next() throws JSONException {
} catch (IOException exception) {
throw new JSONException(exception);
}

if (c <= 0) { // End of stream
this.eof = true;
return 0;
}
}
this.index += 1;
if (this.previous == '\r') {
this.line += 1;
this.character = c == '\n' ? 0 : 1;
} else if (c == '\n') {
this.line += 1;
this.character = 0;
} else {
this.character += 1;
if (c <= 0) { // End of stream
this.eof = true;
return 0;
}
this.incrementIndexes(c);
this.previous = (char) c;
return this.previous;
}

/**
* Increments the internal indexes according to the previous character
* read and the character passed as the current character.
* @param c the current character read.
*/
private void incrementIndexes(int c) {
if(c > 0) {
this.index++;
if(c=='\r') {
this.line++;
this.characterPreviousLine = this.character;
this.character=0;
}else if (c=='\n') {
if(this.previous != '\r') {
this.line++;
this.characterPreviousLine = this.character;
}
this.character=0;
} else {
this.character++;
}
}
}

/**
* Consume the next character, and check that it matches a specified
Expand Down Expand Up @@ -447,21 +474,24 @@ public char skipTo(char to) throws JSONException {
do {
c = this.next();
if (c == 0) {
// in some readers, reset() may throw an exception if
// the remaining portion of the input is greater than
// the mark size (1,000,000 above).
this.reader.reset();
this.index = startIndex;
this.character = startCharacter;
this.line = startLine;
return c;
return 0;
}
} while (c != to);
this.reader.mark(1);
} catch (IOException exception) {
throw new JSONException(exception);
}
this.back();
return c;
}


/**
* Make a JSONException to signal a syntax error.
*
Expand Down

0 comments on commit 5284536

Please sign in to comment.