Skip to content

Commit

Permalink
Do not use exceptions for control flow in StringTokenizer.lookAhead
Browse files Browse the repository at this point in the history
It's easier to check if we are out of buffer bounds and return '\0'.
Motivation:
- it's easier to debug cases when we need to set breakpoint on any
  exeception because we have a much less noise.
- theoretically it should be a little bit faster. But I'm too lazy
  to write a benchmark.
  • Loading branch information
andrey-vorobiev committed Jan 1, 2020
1 parent 31d96d6 commit b20d7db
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/gov/nist/core/StringTokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,8 @@ public char lookAhead() throws ParseException {

public char lookAhead(int k) throws ParseException {
// Debug.out.println("ptr = " + ptr);
try {
return buffer[ptr + k];
}
catch (IndexOutOfBoundsException e) {
return '\0';
}
int index = ptr + k;
return index < buffer.length ? buffer[index] : '\0';
}

public char getNextChar() throws ParseException {
Expand Down

0 comments on commit b20d7db

Please sign in to comment.