Skip to content

Commit 1dd2938

Browse files
committed
Issue #178: share normalizeIndex through JsonPathHelpers
Moves the slice index normalization into the helpers shared by both engines so the interpreted and compiled engines normalize slice bounds identically; the interpreter now delegates. All 100 existing JsonPath tests pass on both JDK 21 and JDK 25. To verify: mvn -pl json-java21-jsonpath test -Djava.util.logging.ConsoleHandler.level=INFO (100 tests green).
1 parent 826f437 commit 1dd2938

2 files changed

Lines changed: 12 additions & 11 deletions

File tree

‎json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathHelpers.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ static boolean compareValues(Object left, JsonPathAst.ComparisonOp op, Object ri
118118
};
119119
}
120120

121+
/// Normalizes an array index against an array size.
122+
/// @param index the index, possibly counting from the end
123+
/// @param size the array size
124+
/// @return the resolved non-negative index (may be out of bounds)
125+
static int normalizeIndex(int index, int size) {
126+
return index < 0 ? size + index : index;
127+
}
128+
121129
/// Logs an unsupported script expression in the same way the interpreter does.
122130
/// Generated executors call this so compiled queries surface the same diagnostics.
123131
/// @param script the unsupported script text

‎json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathInterpreted.java‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ private static void evaluateArraySlice(
142142
}
143143

144144
if (step > 0) {
145-
int start = slice.start() != null ? normalizeIndex(slice.start(), size) : 0;
146-
int end = slice.end() != null ? normalizeIndex(slice.end(), size) : size;
145+
int start = slice.start() != null ? JsonPathHelpers.normalizeIndex(slice.start(), size) : 0;
146+
int end = slice.end() != null ? JsonPathHelpers.normalizeIndex(slice.end(), size) : size;
147147

148148
start = Math.max(0, Math.min(start, size));
149149
end = Math.max(0, Math.min(end, size));
@@ -152,8 +152,8 @@ private static void evaluateArraySlice(
152152
evaluateSegments(segments, index + 1, elements.get(i), root, results);
153153
}
154154
} else {
155-
int start = slice.start() != null ? normalizeIndex(slice.start(), size) : size - 1;
156-
final int end = slice.end() != null ? normalizeIndex(slice.end(), size) : -1;
155+
int start = slice.start() != null ? JsonPathHelpers.normalizeIndex(slice.start(), size) : size - 1;
156+
final int end = slice.end() != null ? JsonPathHelpers.normalizeIndex(slice.end(), size) : -1;
157157

158158
start = Math.max(0, Math.min(start, size - 1));
159159

@@ -164,13 +164,6 @@ private static void evaluateArraySlice(
164164
}
165165
}
166166

167-
private static int normalizeIndex(int index, int size) {
168-
if (index < 0) {
169-
return size + index;
170-
}
171-
return index;
172-
}
173-
174167
private static void evaluateWildcard(
175168
List<JsonPathAst.Segment> segments,
176169
int index,

0 commit comments

Comments
 (0)