Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce memory allocations and improve performance in JSONObject #68

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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