Skip to content

Commit

Permalink
Last optimizations, test verification
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 31, 2024
1 parent f2c9a07 commit 32c7a19
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
20 changes: 16 additions & 4 deletions src/main/java/com/fasterxml/jackson/core/JsonPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ protected JsonPointer(JsonPointer src, JsonPointer next) {
_matchingElementIndex = src._matchingElementIndex;
}

// @since 2.19
protected JsonPointer(JsonPointer src, String newFullString, int newFullStringOffset) {
_asString = newFullString;
_asStringOffset = newFullStringOffset;
_nextSegment = null;
_matchingPropertyName = src._matchingPropertyName;
_matchingElementIndex = src._matchingElementIndex;
}

/*
/**********************************************************
/* Factory methods
Expand Down Expand Up @@ -836,12 +845,15 @@ protected JsonPointer _constructHead()
ArrayList<JsonPointer> pointers = new ArrayList<>();

JsonPointer current = this;
String fullString = toString();
// Make sure to share the new full string for path segments
fullString = fullString.substring(0, fullString.length() - suffixLength);
int offset = 0;

while (current != last) {
String str = current.toString();
JsonPointer nextSegment = new JsonPointer(str.substring(0, str.length() - suffixLength), 0,
current._matchingPropertyName,
current._matchingElementIndex, null);
JsonPointer nextSegment = new JsonPointer(current,
fullString, offset);
offset += suffixLength;
pointers.add(nextSegment);
current = current._nextSegment;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.core.JsonPointer;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

class JsonPointer1328Test extends JUnit5TestBase
{
Expand All @@ -17,8 +18,15 @@ class JsonPointer1328Test extends JUnit5TestBase
void deepHead()
{
final String INPUT = repeat("/a", DEPTH);
JsonPointer ptr = JsonPointer.compile(INPUT);
assertEquals(repeat("/a", DEPTH - 1), ptr.head().toString());
JsonPointer origPtr = JsonPointer.compile(INPUT);
JsonPointer head = origPtr.head();
final String fullHead = head.toString();
assertEquals(repeat("/a", DEPTH - 1), fullHead);

// But let's also traverse the hierarchy to make sure:
for (JsonPointer ctr = head; ctr != JsonPointer.empty(); ctr = ctr.tail()) {
assertThat(fullHead).endsWith(ctr.toString());
}
}

private final static String repeat(String part, int count) {
Expand Down

0 comments on commit 32c7a19

Please sign in to comment.