From 289bc3ce47a6f2fc8fd8c7bf95b4ae1e26dabbfa Mon Sep 17 00:00:00 2001 From: capak07 <69026837+capak07@users.noreply.github.com> Date: Mon, 12 Feb 2024 17:42:37 -0400 Subject: [PATCH 1/2] Fixed delpaths function to use sort with slicing --- src/jv_aux.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jv_aux.c b/src/jv_aux.c index 6004799c6a..4a518b2b01 100644 --- a/src/jv_aux.c +++ b/src/jv_aux.c @@ -500,7 +500,7 @@ jv jv_delpaths(jv object, jv paths) { jv_free(paths); return jv_invalid_with_msg(jv_string("Paths must be specified as an array")); } - paths = jv_sort(paths, jv_copy(paths)); + paths = jv_sort(jv_array_slice(paths, 0, jv_array_length(jv_copy(paths))), jv_copy(paths)); jv_array_foreach(paths, i, elem) { if (jv_get_kind(elem) != JV_KIND_ARRAY) { jv_free(object); From bb21c85a8d309b7c63d05f09042f7a0858aaca43 Mon Sep 17 00:00:00 2001 From: capak07 <69026837+capak07@users.noreply.github.com> Date: Tue, 20 Feb 2024 17:35:08 -0400 Subject: [PATCH 2/2] Addressed comments and added tests --- src/jv_aux.c | 10 ++++++---- tests/jq.test | 4 ++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/jv_aux.c b/src/jv_aux.c index 4a518b2b01..aec3765196 100644 --- a/src/jv_aux.c +++ b/src/jv_aux.c @@ -500,8 +500,10 @@ jv jv_delpaths(jv object, jv paths) { jv_free(paths); return jv_invalid_with_msg(jv_string("Paths must be specified as an array")); } - paths = jv_sort(jv_array_slice(paths, 0, jv_array_length(jv_copy(paths))), jv_copy(paths)); - jv_array_foreach(paths, i, elem) { + int paths_length = jv_array_length(jv_copy(paths)); + paths = jv_sort(jv_array_slice(paths, 0, paths_length), jv_copy(paths)); + for(int i = 0; i < paths_length; i++) { + jv elem = jv_array_get(jv_copy(paths), i); if (jv_get_kind(elem) != JV_KIND_ARRAY) { jv_free(object); jv_free(paths); @@ -512,7 +514,7 @@ jv jv_delpaths(jv object, jv paths) { } jv_free(elem); } - if (jv_array_length(jv_copy(paths)) == 0) { + if (paths_length == 0) { // nothing is being deleted jv_free(paths); return object; @@ -522,7 +524,7 @@ jv jv_delpaths(jv object, jv paths) { jv_free(paths); jv_free(object); return jv_null(); - } + } return delpaths_sorted(object, paths, 0); } diff --git a/tests/jq.test b/tests/jq.test index b94f29d245..b0e9f0975e 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -1023,6 +1023,10 @@ try delpaths(0) catch . {} "Paths must be specified as an array" +map(delpaths([[6], [7], [8], [9], [10]])) +[[0, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]] +[[0, 1, 2, 3, 4, 5]] + del(.), del(empty), del((.foo,.bar,.baz) | .[2,3,0]), del(.foo[0], .bar[0], .foo, .baz.bar[0].x) {"foo": [0,1,2,3,4], "bar": [0,1]} null