Skip to content

Commit

Permalink
stleary#863 improve performance of JSONTokener#nextString
Browse files Browse the repository at this point in the history
replacing a switch-case statement with few branches
by if-else cases
  • Loading branch information
Simulant87 committed Mar 5, 2024
1 parent f38452a commit 63625b3
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/main/java/org/json/JSONTokener.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,9 @@ public String nextString(char quote) throws JSONException {
StringBuilder sb = new StringBuilder();
for (;;) {
c = this.next();
switch (c) {
case 0:
case '\n':
case '\r':
throw this.syntaxError("Unterminated string");
case '\\':
if (c == quote) {
return sb.toString();
} else if (c == '\\') {
c = this.next();
switch (c) {
case 'b':
Expand Down Expand Up @@ -334,11 +331,9 @@ public String nextString(char quote) throws JSONException {
default:
throw this.syntaxError("Illegal escape.");
}
break;
default:
if (c == quote) {
return sb.toString();
}
} else if (c == 0 || c == '\n' || c == '\r') {
throw this.syntaxError("Unterminated string");
} else {
sb.append(c);
}
}
Expand Down

0 comments on commit 63625b3

Please sign in to comment.