Skip to content

Commit

Permalink
refactor(userspace/libsinsp): improve performance of endswith filter …
Browse files Browse the repository at this point in the history
…operator

Signed-off-by: Jason Dellaluce <[email protected]>
  • Loading branch information
jasondellaluce authored and poiana committed Apr 16, 2024
1 parent 039a5f5 commit 03e34ed
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
2 changes: 1 addition & 1 deletion userspace/libsinsp/sinsp_filtercheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ bool flt_compare_string(cmpop op, char* operand1, char* operand2)
case CO_BSTARTSWITH:
throw sinsp_exception("'bstartswith' not supported for string filters");
case CO_ENDSWITH:
return (sinsp_utils::endswith(operand1, operand2));
return (sinsp_utils::endswith(operand1, operand2, strlen(operand1), strlen(operand2)));
case CO_GLOB:
return sinsp_utils::glob_match(operand2, operand1);
case CO_IGLOB:
Expand Down
20 changes: 0 additions & 20 deletions userspace/libsinsp/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1432,26 +1432,6 @@ std::string replace(const std::string& str, const std::string& search, const std
return s;
}


bool sinsp_utils::endswith(const std::string& str, const std::string& ending)
{
if (ending.size() <= str.size())
{
return (0 == str.compare(str.length() - ending.length(), ending.length(), ending));
}
return false;
}


bool sinsp_utils::endswith(const char *str, const char *ending, uint32_t lstr, uint32_t lend)
{
if (lstr >= lend)
{
return (0 == memcmp(ending, str + (lstr - lend), lend));
}
return 0;
}

bool sinsp_utils::startswith(const std::string& s, const std::string& prefix)
{
if(prefix.empty())
Expand Down
20 changes: 18 additions & 2 deletions userspace/libsinsp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ limitations under the License.

#include <algorithm>
#include <cctype>
#include <cstring>
#include <list>
#include <locale>
#include <set>
Expand Down Expand Up @@ -80,8 +81,23 @@ class sinsp_utils
//
// Check if string ends with another
//
static bool endswith(const std::string& str, const std::string& ending);
static bool endswith(const char *str, const char *ending, uint32_t lstr, uint32_t lend);
static inline bool endswith(const std::string& str, const std::string& ending)
{
if (ending.size() <= str.size())
{
return (0 == str.compare(str.length() - ending.length(), ending.length(), ending));
}
return false;
}

static inline bool endswith(const char *str, const char *ending, uint32_t lstr, uint32_t lend)
{
if (lstr >= lend)
{
return (0 == memcmp(ending, str + (lstr - lend), lend));
}
return 0;
}

//
// Check if string starts with another
Expand Down

0 comments on commit 03e34ed

Please sign in to comment.