From accd08139f9c6782847fc74dcecec9b6729b4d08 Mon Sep 17 00:00:00 2001 From: freestrings Date: Sun, 16 Feb 2020 13:14:31 +0900 Subject: [PATCH] Negative slice out of bounds does not return values #35 --- src/select/mod.rs | 4 ++-- tests/filter.rs | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/select/mod.rs b/src/select/mod.rs index 20ee770..c6f7716 100644 --- a/src/select/mod.rs +++ b/src/select/mod.rs @@ -439,9 +439,9 @@ fn walk<'a, F>(vec: &[&'a Value], tmp: &mut Vec<&'a Value>, fun: &F) fn abs_index(n: isize, len: usize) -> usize { if n < 0_isize { - (n + len as isize) as usize + (n + len as isize).max(0) as usize } else { - n as usize + n.min(len as isize) as usize } } diff --git a/tests/filter.rs b/tests/filter.rs index f64588c..8fd220d 100644 --- a/tests/filter.rs +++ b/tests/filter.rs @@ -602,6 +602,16 @@ fn range() { json!(["first", "second", "third", "forth", "fifth"]), json!(["first", "third"]), ); + select_and_then_compare( + "$[-4:]", + json!(["first", "second", "third"]), + json!(["first", "second", "third"]), + ); + select_and_then_compare( + "$[:4]", + json!(["first", "second", "third"]), + json!(["first", "second", "third"]), + ); } #[test]