[ADD] builtin function enum_to_int()

[ADD] added tests for the new enum_to_int function
This commit is contained in:
Christian Struck 2014-11-10 17:58:38 -08:00
parent 9045288ad3
commit b36d5fc81b
3 changed files with 50 additions and 0 deletions

View file

@ -2159,6 +2159,21 @@ function counts_to_addr%(v: index_vec%): addr
}
%}
## Converts an :bro:type:`enum` to an :bro:type:`int`
##
## e: The :bro:type:`enum` to convert.
##
## Returns: The :bro:type:`enum` as :bro:type:`int`.
function enum_to_int%(e: any%): int
%{
if ( e->Type()->Tag() != TYPE_ENUM )
{
builtin_error("enum_to_int() requires enum_val");
return new Val(-1, TYPE_INT);
}
return new Val(e->AsEnum(), TYPE_INT);
%}
## Converts a :bro:type:`string` to an :bro:type:`int`.
##
## str: The :bro:type:`string` to convert.

View file

@ -0,0 +1,6 @@
A, 0
B, 1
C, 2
AV, 10
BV, 11
CV, 12

View file

@ -0,0 +1,29 @@
#
# @TEST-EXEC: bro -b %INPUT >out
# @TEST-EXEC: btest-diff out
export {
type test_enum: enum {
A,
B,
C,
};
type test_enum_with_val: enum {
AV = 0xA,
BV = 0xB,
CV = 0xC,
};
}
event bro_init()
{
print A, enum_to_int(A);
print B, enum_to_int(B);
print C, enum_to_int(C);
print AV, enum_to_int(AV);
print BV, enum_to_int(BV);
print CV, enum_to_int(CV);
}