mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
[ADD] builtin function enum_to_int()
[ADD] added tests for the new enum_to_int function
This commit is contained in:
parent
9045288ad3
commit
b36d5fc81b
3 changed files with 50 additions and 0 deletions
15
src/bro.bif
15
src/bro.bif
|
@ -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.
|
||||
|
|
6
testing/btest/Baseline/bifs.enum_to_int/out
Normal file
6
testing/btest/Baseline/bifs.enum_to_int/out
Normal file
|
@ -0,0 +1,6 @@
|
|||
A, 0
|
||||
B, 1
|
||||
C, 2
|
||||
AV, 10
|
||||
BV, 11
|
||||
CV, 12
|
29
testing/btest/bifs/enum_to_int.bro
Normal file
29
testing/btest/bifs/enum_to_int.bro
Normal 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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue