1
0
Fork 0

Codechange: StringFilter now uses std::string_view entirely (#13869)

pull/13870/head
frosch 2025-03-22 20:35:31 +01:00 committed by GitHub
parent 667d013726
commit d4ae0f70da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 6 deletions

View File

@ -368,6 +368,20 @@ bool StrEqualsIgnoreCase(const std::string_view str1, const std::string_view str
return StrCompareIgnoreCase(str1, str2) == 0;
}
/**
* Checks if a string is contained in another string, while ignoring the case of the characters.
*
* @param str The string to search in.
* @param value The string to search for.
* @return True if a match was found.
*/
bool StrContainsIgnoreCase(const std::string_view str, const std::string_view value)
{
CaseInsensitiveStringView ci_str{ str.data(), str.size() };
CaseInsensitiveStringView ci_value{ value.data(), value.size() };
return ci_str.find(ci_value) != ci_str.npos;
}
/**
* Get the length of an UTF-8 encoded string in number of characters
* and thus not the number of bytes that the encoded string contains.

View File

@ -36,6 +36,7 @@ std::string_view StrTrimView(std::string_view str);
[[nodiscard]] int StrCompareIgnoreCase(const std::string_view str1, const std::string_view str2);
[[nodiscard]] bool StrEqualsIgnoreCase(const std::string_view str1, const std::string_view str2);
[[nodiscard]] bool StrContainsIgnoreCase(const std::string_view str, const std::string_view value);
[[nodiscard]] int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front = false);
[[nodiscard]] bool StrNaturalContains(const std::string_view str, const std::string_view value);
[[nodiscard]] bool StrNaturalContainsIgnoreCase(const std::string_view str, const std::string_view value);

View File

@ -102,10 +102,8 @@ void StringFilter::ResetState()
*
* @param str Another line from the item.
*/
void StringFilter::AddLine(const char *str)
void StringFilter::AddLine(std::string_view str)
{
if (str == nullptr) return;
bool match_case = this->case_sensitive != nullptr && *this->case_sensitive;
for (WordState &ws : this->word_index) {
if (!ws.match) {
@ -115,7 +113,7 @@ void StringFilter::AddLine(const char *str)
this->word_matches++;
}
} else {
if ((match_case ? strstr(str, ws.word.c_str()) : strcasestr(str, ws.word.c_str())) != nullptr) {
if (match_case ? str.find(ws.word) != str.npos : StrContainsIgnoreCase(str, ws.word)) {
ws.match = true;
this->word_matches++;
}

View File

@ -57,8 +57,8 @@ public:
bool IsEmpty() const { return this->word_index.empty(); }
void ResetState();
void AddLine(const char *str);
void AddLine(const std::string &str) { this->AddLine(str.c_str()); }
void AddLine(const char *) = delete; // prevent implicit construction of string_view from potential nullptr
void AddLine(std::string_view str);
/**
* Get the matching state of the current item.