Skip to content

Commit 0ffd6ff

Browse files
committed
Filter: add startswith/endswith operations
1 parent b74793d commit 0ffd6ff

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

src/filter/operations/str.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,19 @@ contains_str(fds_filter_value_u *big, fds_filter_value_u *little, fds_filter_val
8888
result->b = strnnstr(big->str.chars, little->str.chars, big->str.len, little->str.len) != NULL;
8989
}
9090

91+
void
92+
startswith_str(fds_filter_value_u *big, fds_filter_value_u *little, fds_filter_value_u *result)
93+
{
94+
result->b = (big->str.len >= little->str.len
95+
&& memcmp(big->str.chars, little->str.chars, little->str.len) == 0);
96+
}
9197

92-
98+
void
99+
endswith_str(fds_filter_value_u *big, fds_filter_value_u *little, fds_filter_value_u *result)
100+
{
101+
result->b = (big->str.len >= little->str.len
102+
&& memcmp(&big->str.chars[big->str.len - little->str.len], little->str.chars, little->str.len) == 0);
103+
}
93104

94105
void
95106
str_in_list(fds_filter_value_u *item, fds_filter_value_u *list, fds_filter_value_u *result)
@@ -136,6 +147,8 @@ const fds_filter_op_s str_operations[] = {
136147
FDS_FILTER_DEF_BINARY_OP(FDS_FDT_STR, "!=", FDS_FDT_STR, ne_str, FDS_FDT_BOOL),
137148

138149
FDS_FILTER_DEF_BINARY_OP(FDS_FDT_STR, "contains", FDS_FDT_STR, contains_str, FDS_FDT_BOOL),
150+
FDS_FILTER_DEF_BINARY_OP(FDS_FDT_STR, "startswith", FDS_FDT_STR, startswith_str, FDS_FDT_BOOL),
151+
FDS_FILTER_DEF_BINARY_OP(FDS_FDT_STR, "endswith", FDS_FDT_STR, endswith_str, FDS_FDT_BOOL),
139152

140153
FDS_FILTER_DEF_BINARY_OP(FDS_FDT_STR, "in", FDS_FDT_LIST | FDS_FDT_STR, str_in_list, FDS_FDT_BOOL),
141154

src/filter/parser.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ struct operator_s op_parse_def_table[] = {
8888
{ "exists" , 2, OP_KIND_PREFIX, OP_ASSOC_NONE },
8989
{ "in" , 2, OP_KIND_INFIX , OP_ASSOC_LEFT },
9090
{ "contains", 2, OP_KIND_INFIX , OP_ASSOC_LEFT },
91+
{ "startswith", 2, OP_KIND_INFIX , OP_ASSOC_LEFT },
92+
{ "endswith", 2, OP_KIND_INFIX , OP_ASSOC_LEFT },
9193

9294
{ "" , 1, OP_KIND_INFIX , OP_ASSOC_LEFT },
9395
{ "==" , 1, OP_KIND_INFIX , OP_ASSOC_LEFT },

src/filter/scanner.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ const struct {
7575
// Strings that are treated as symbols
7676
const char *symbols[] = {
7777
"~", "not", "*", "/", "+", "-", "|", "&", "^", "%",
78-
"and", "or", "in", "contains", "exists", "[", "]", "(", ")", ",",
78+
"and", "or", "in", "contains", "startswith", "endswith", "exists",
79+
"[", "]", "(", ")", ",",
7980
"<", ">", "==", "!=", ">=", "<=", "<<", ">>",
8081
"in", "out", "ingress", "egress", "src", "dst"
8182
};

0 commit comments

Comments
 (0)