Add table_keys function

This commit is contained in:
AmazingPP 2022-07-09 01:11:31 +08:00
parent cb71b15eab
commit ba552ceeaf
5 changed files with 73 additions and 21 deletions

View file

@ -932,6 +932,8 @@ public:
const PDict<TableEntryVal>* Get() const { return table_val; } const PDict<TableEntryVal>* Get() const { return table_val; }
const detail::CompositeHash* GetTableHash() const { return table_hash; }
// Returns the size of the table. // Returns the size of the table.
int Size() const; int Size() const;
int RecursiveSize() const; int RecursiveSize() const;

View file

@ -28,6 +28,7 @@
#include "zeek/IntrusivePtr.h" #include "zeek/IntrusivePtr.h"
#include "zeek/input.h" #include "zeek/input.h"
#include "zeek/Hash.h" #include "zeek/Hash.h"
#include "zeek/CompHash.h"
#include "zeek/packet_analysis/Manager.h" #include "zeek/packet_analysis/Manager.h"
using namespace std; using namespace std;
@ -1190,6 +1191,8 @@ function clear_table%(v: any%): any
## t: The :zeek:type:`table` ## t: The :zeek:type:`table`
## ##
## Returns: A ``vector of T`` of all the values in t. ## Returns: A ``vector of T`` of all the values in t.
##
## .. zeek:see:: table_keys
function table_values%(t: any%): any function table_values%(t: any%): any
%{ %{
if ( ! t->GetType()->IsTable() ) if ( ! t->GetType()->IsTable() )
@ -1208,6 +1211,33 @@ function table_values%(t: any%): any
return vv; 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]. ## Gets all subnets that contain a given subnet from a set/table[subnet].
## ##
## search: the subnet to search for. ## search: the subnet to search for.

View 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
}

View 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;
}