Skip to content

Commit df66ea7

Browse files
committed
Fix NodePath::slice() incorrect behavior for subname indexing
Adjust slice boundaries in `NodePath` logic to correctly handle subnames. Update test cases to reflect these changes.
1 parent 2582793 commit df66ea7

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

core/string/node_path.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ NodePath NodePath::slice(int p_begin, int p_end) const {
255255
if (end < 0) {
256256
end += total_count;
257257
}
258-
const int sub_begin = MAX(begin - name_count - 1, 0);
258+
const int sub_begin = MAX(begin - name_count, 0);
259259
const int sub_end = MAX(end - name_count, 0);
260260

261261
const Vector<StringName> names = get_names().slice(begin, end);

tests/core/string/test_node_path.h

+10-7
Original file line numberDiff line numberDiff line change
@@ -169,28 +169,31 @@ TEST_CASE("[NodePath] Empty path") {
169169
}
170170

171171
TEST_CASE("[NodePath] Slice") {
172-
const NodePath node_path_relative = NodePath("Parent/Child:prop");
172+
const NodePath node_path_relative = NodePath("Parent/Child:prop:subprop");
173173
const NodePath node_path_absolute = NodePath("/root/Parent/Child:prop");
174174
CHECK_MESSAGE(
175175
node_path_relative.slice(0, 2) == NodePath("Parent/Child"),
176176
"The slice lower bound should be inclusive and the slice upper bound should be exclusive.");
177177
CHECK_MESSAGE(
178-
node_path_relative.slice(3) == NodePath(":prop"),
179-
"Slicing on the length of the path should return the last entry.");
178+
node_path_relative.slice(3) == NodePath(":subprop"),
179+
"Slicing on the last index (length - 1) should return the last entry.");
180+
CHECK_MESSAGE(
181+
node_path_relative.slice(1) == NodePath("Child:prop:subprop"),
182+
"Slicing without upper bound should return remaining entries after index.");
180183
CHECK_MESSAGE(
181184
node_path_relative.slice(1, 3) == NodePath("Child:prop"),
182185
"Slicing should include names and subnames.");
183186
CHECK_MESSAGE(
184-
node_path_relative.slice(-1) == NodePath(":prop"),
187+
node_path_relative.slice(-1) == NodePath(":subprop"),
185188
"Slicing on -1 should return the last entry.");
186189
CHECK_MESSAGE(
187-
node_path_relative.slice(0, -1) == NodePath("Parent/Child"),
190+
node_path_relative.slice(0, -1) == NodePath("Parent/Child:prop"),
188191
"Slicing up to -1 should include the second-to-last entry.");
189192
CHECK_MESSAGE(
190-
node_path_relative.slice(-2, -1) == NodePath("Child"),
193+
node_path_relative.slice(-2, -1) == NodePath(":prop"),
191194
"Slicing from negative to negative should treat lower bound as inclusive and upper bound as exclusive.");
192195
CHECK_MESSAGE(
193-
node_path_relative.slice(0, 10) == NodePath("Parent/Child:prop"),
196+
node_path_relative.slice(0, 10) == NodePath("Parent/Child:prop:subprop"),
194197
"Slicing past the length of the path should work like slicing up to the last entry.");
195198
CHECK_MESSAGE(
196199
node_path_relative.slice(-10, 2) == NodePath("Parent/Child"),

0 commit comments

Comments
 (0)