GH-213: change type of vector for-loop index to a count

This commit is contained in:
Jon Siwek 2019-01-14 18:02:05 -06:00
parent 89da20339d
commit ed1a50ec5b
4 changed files with 11 additions and 7 deletions

5
NEWS
View file

@ -30,6 +30,11 @@ New Functionality
Changed Functionality
---------------------
- The for-loop index variable for vectors has been changed from
'int' to 'count' type. It's unlikely this would alter/break any
script behavior unless they were explicitly inspecting the variable's
type (and there's typically no reason to do that).
Removed Functionality
---------------------

View file

@ -559,9 +559,8 @@ Here is a more detailed description of each type:
.. bro:type:: vector
A vector is like a :bro:type:`table`, except it's always indexed by a
:bro:type:`count` (and vector indexing is always zero-based). A vector
is declared like:
A vector is like a :bro:type:`table`, except its indices are non-negative
integers, starting from zero. A vector is declared like:
.. sourcecode:: bro

View file

@ -1387,7 +1387,7 @@ ForStmt::ForStmt(id_list* arg_loop_vars, Expr* loop_expr)
BroType* t = (*loop_vars)[0]->Type();
if ( ! t )
delete add_local((*loop_vars)[0], base_type(TYPE_INT),
delete add_local((*loop_vars)[0], base_type(TYPE_COUNT),
INIT_NONE, 0, 0, VAR_REGULAR);
else if ( ! IsIntegral(t->Tag()) )
@ -1470,7 +1470,7 @@ Val* ForStmt::DoExec(Frame* f, Val* v, stmt_flow_type& flow) const
{
VectorVal* vv = v->AsVectorVal();
for ( int i = 0; i <= int(vv->Size()); ++i )
for ( auto i = 0u; i <= vv->Size(); ++i )
{
// Skip unassigned vector indices.
if ( ! vv->Lookup(i) )
@ -1479,7 +1479,7 @@ Val* ForStmt::DoExec(Frame* f, Val* v, stmt_flow_type& flow) const
// Set the loop variable to the current index, and make
// another pass over the loop body.
f->SetElement((*loop_vars)[0]->Offset(),
new Val(i, TYPE_INT));
new Val(i, TYPE_COUNT));
flow = FLOW_NEXT;
ret = body->Exec(f, flow);

View file

@ -63,7 +63,7 @@ event bro_init()
ct = 0;
for ( c in v1 )
{
if ( type_name(c) != "int" )
if ( type_name(c) != "count" )
print "Error: wrong index type";
if ( type_name(v1[c]) != "string" )
print "Error: wrong vector type";