mirror of
https://github.com/zeek/zeek.git
synced 2025-10-07 09:08:20 +00:00
Add table_keys function
This commit is contained in:
parent
cb71b15eab
commit
ba552ceeaf
5 changed files with 73 additions and 21 deletions
|
@ -932,6 +932,8 @@ public:
|
|||
|
||||
const PDict<TableEntryVal>* Get() const { return table_val; }
|
||||
|
||||
const detail::CompositeHash* GetTableHash() const { return table_hash; }
|
||||
|
||||
// Returns the size of the table.
|
||||
int Size() const;
|
||||
int RecursiveSize() const;
|
||||
|
|
30
src/zeek.bif
30
src/zeek.bif
|
@ -28,6 +28,7 @@
|
|||
#include "zeek/IntrusivePtr.h"
|
||||
#include "zeek/input.h"
|
||||
#include "zeek/Hash.h"
|
||||
#include "zeek/CompHash.h"
|
||||
#include "zeek/packet_analysis/Manager.h"
|
||||
|
||||
using namespace std;
|
||||
|
@ -1190,6 +1191,8 @@ function clear_table%(v: any%): any
|
|||
## t: The :zeek:type:`table`
|
||||
##
|
||||
## Returns: A ``vector of T`` of all the values in t.
|
||||
##
|
||||
## .. zeek:see:: table_keys
|
||||
function table_values%(t: any%): any
|
||||
%{
|
||||
if ( ! t->GetType()->IsTable() )
|
||||
|
@ -1208,6 +1211,33 @@ function table_values%(t: any%): any
|
|||
return vv;
|
||||
%}
|
||||
|
||||
## Gets all keys from a table.
|
||||
##
|
||||
## t: The :zeek:type:`table`
|
||||
##
|
||||
## Returns: A ``set of T`` of all the keys in t.
|
||||
##
|
||||
## .. zeek:see:: table_values
|
||||
function table_keys%(t: any%): any
|
||||
%{
|
||||
if ( ! t->GetType()->IsTable() )
|
||||
{
|
||||
zeek::emit_builtin_error("table_keys() requires a table argument");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto tl = t->GetType()->AsTableType()->GetIndices();
|
||||
auto st = make_intrusive<zeek::SetType>(std::move(tl), nullptr);
|
||||
auto sv = zeek::make_intrusive<zeek::TableVal>(std::move(st));
|
||||
auto th = t->AsTableVal()->GetTableHash();
|
||||
for ( const auto& iter : *t->AsTable() )
|
||||
{
|
||||
sv->Assign(th->RecoverVals(*iter.GetHashKey()), nullptr);
|
||||
}
|
||||
|
||||
return sv;
|
||||
%}
|
||||
|
||||
## Gets all subnets that contain a given subnet from a set/table[subnet].
|
||||
##
|
||||
## search: the subnet to search for.
|
||||
|
|
5
testing/btest/Baseline/bifs.table_keys/out
Normal file
5
testing/btest/Baseline/bifs.table_keys/out
Normal file
|
@ -0,0 +1,5 @@
|
|||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
|
||||
{
|
||||
http,
|
||||
https
|
||||
}
|
15
testing/btest/bifs/table_keys.zeek
Normal file
15
testing/btest/bifs/table_keys.zeek
Normal file
|
@ -0,0 +1,15 @@
|
|||
#
|
||||
# @TEST-EXEC: zeek -b %INPUT > out
|
||||
# @TEST-EXEC: btest-diff out
|
||||
|
||||
event zeek_init()
|
||||
{
|
||||
local t = table(
|
||||
["http"] = "http://www.google.com/",
|
||||
["https"] = "https://www.google.com/"
|
||||
);
|
||||
|
||||
local v: set[string] = table_keys(t);
|
||||
|
||||
print v;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue