Skip to content

Commit

Permalink
fix: Reduce memory allocations and improve performance in JSONObject
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Nov 15, 2024
1 parent de8fab6 commit db76e69
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ public static JSONObject fromJSONTokener(JSONTokener tokener, JsonConfig jsonCon
String key;
Object value;

if (tokener.matches("null.*")) {
if (tokener.startsWith("null")) {
fireObjectStartEvent(jsonConfig);
fireObjectEndEvent(jsonConfig);
return new JSONObject(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public boolean matches(String pattern) {
.matches(str);
}

public boolean startsWith(String prefix) {
return this.mySource.startsWith(prefix, this.myIndex);
}

/**
* Determine if the source string still contains characters that next() can
* consume.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ public void testNextChar() {
}
}

public void testStartsWith() {
assertFalse(new JSONTokener("").startsWith("null"));
assertFalse(new JSONTokener("n").startsWith("null"));
assertFalse(new JSONTokener("nu").startsWith("null"));
assertFalse(new JSONTokener("nul").startsWith("null"));
assertTrue(new JSONTokener("null").startsWith("null"));
assertTrue(new JSONTokener("nulll").startsWith("null"));
assertFalse(new JSONTokener("nn").startsWith("null"));
assertFalse(new JSONTokener("nnu").startsWith("null"));
assertFalse(new JSONTokener("nnul").startsWith("null"));
assertFalse(new JSONTokener("nnull").startsWith("null"));
assertFalse(new JSONTokener("nnulll").startsWith("null"));
}

public void testReset() {
JSONTokener tok = new JSONTokener("abc");
tok.next();
Expand Down

0 comments on commit db76e69

Please sign in to comment.