Berry fix ranges with negative indices

This commit is contained in:
Stephan Hadinger 2021-11-28 19:41:26 +01:00
parent 3eb6c550e1
commit 0fad84fe06
2 changed files with 17 additions and 0 deletions

View File

@ -134,9 +134,15 @@ static int item_range(bvm *vm)
/* get index range */ /* get index range */
be_getmember(vm, 2, "__lower__"); be_getmember(vm, 2, "__lower__");
lower = be_toint(vm, -1); lower = be_toint(vm, -1);
if (lower < 0) {
lower = size + lower;
}
be_pop(vm, 1); be_pop(vm, 1);
be_getmember(vm, 2, "__upper__"); be_getmember(vm, 2, "__upper__");
upper = be_toint(vm, -1); upper = be_toint(vm, -1);
if (upper < 0) {
upper = size + upper;
}
be_pop(vm, 1); be_pop(vm, 1);
/* protection scope */ /* protection scope */
upper = upper < size ? upper : size - 1; upper = upper < size ? upper : size - 1;

View File

@ -127,3 +127,14 @@ assert([1,2,3].concat('-') == "1-2-3")
assert([1,"2",3].concat('-') == "1-2-3") assert([1,"2",3].concat('-') == "1-2-3")
assert(["",2,3].concat('-') == "-2-3") assert(["",2,3].concat('-') == "-2-3")
#- negative indices -#
assert([1,2,3,4][0] == 1)
assert([1,2,3,4][-1] == 4)
assert([1,2,3,4][-2] == 3)
assert([1,2,3,4][1..10] == [2,3,4])
assert([1,2,3,4][1..-1] == [2,3,4])
assert([1,2,3,4][1..-2] == [2,3])
assert([1,2,3,4][3..2] == [])
assert([1,2,3,4][2..-3] == [])