Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

refactor(userspace/libsinsp): improve performance of endswith filter operator #1796

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading