mirror of
https://github.com/zeek/zeek.git
synced 2025-10-06 00:28:21 +00:00
GH-3060: Support negative index lookups for vectors
This commit is contained in:
parent
cdb9c74054
commit
351e68b66a
3 changed files with 15 additions and 1 deletions
|
@ -2932,7 +2932,13 @@ ValPtr IndexExpr::Fold(Val* v1, Val* v2) const
|
||||||
const ListVal* lv = v2->AsListVal();
|
const ListVal* lv = v2->AsListVal();
|
||||||
|
|
||||||
if ( lv->Length() == 1 )
|
if ( lv->Length() == 1 )
|
||||||
v = vect->ValAt(lv->Idx(0)->CoerceToUnsigned());
|
{
|
||||||
|
auto index = lv->Idx(0)->CoerceToInt();
|
||||||
|
if ( index < 0 )
|
||||||
|
index = vect->Size() + index;
|
||||||
|
|
||||||
|
v = vect->ValAt(index);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
return index_slice(vect, lv);
|
return index_slice(vect, lv);
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,3 +80,5 @@ hole in vector of managed types, 5, [[a=T], [a=T], , , [a=T]]
|
||||||
hole in vector of managed types after replacing slice, 3, [[a=T], [a=T], ]
|
hole in vector of managed types after replacing slice, 3, [[a=T], [a=T], ]
|
||||||
left shift (PASS)
|
left shift (PASS)
|
||||||
right shift (PASS)
|
right shift (PASS)
|
||||||
|
negative index (PASS)
|
||||||
|
negative index (PASS)
|
||||||
|
|
|
@ -223,4 +223,10 @@ event zeek_init()
|
||||||
local v23 = v6 >> four_ones;
|
local v23 = v6 >> four_ones;
|
||||||
test_case( "left shift", all_set(v22 == vector(20, 40, 60, 80)) );
|
test_case( "left shift", all_set(v22 == vector(20, 40, 60, 80)) );
|
||||||
test_case( "right shift", all_set(v23 == vector(5, 10, 15, 20)) );
|
test_case( "right shift", all_set(v23 == vector(5, 10, 15, 20)) );
|
||||||
|
|
||||||
|
# negative indices
|
||||||
|
local v24 = vector( 1, 2, 3, 4, 5 );
|
||||||
|
test_case( "negative index", v24[-1] == 5 );
|
||||||
|
test_case( "negative index", v24[-3] == 3 );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue