Codechange: rework StringFilter to not need MallocT anymore

This commit is contained in:
Rubidium
2025-02-15 22:23:56 +01:00
committed by rubidium42
parent ff6ae5b87b
commit 8b7460759a
3 changed files with 31 additions and 40 deletions

View File

@@ -25,34 +25,34 @@ static const char32_t STATE_QUOTE2 = '"';
* Set the term to filter on.
* @param str Filter term
*/
void StringFilter::SetFilterTerm(const char *str)
void StringFilter::SetFilterTerm(std::string_view str)
{
this->word_index.clear();
this->word_index.shrink_to_fit();
this->word_matches = 0;
free(this->filter_buffer);
assert(str != nullptr);
char *dest = MallocT<char>(strlen(str) + 1);
this->filter_buffer = dest;
char32_t state = STATE_WHITESPACE;
const char *pos = str;
WordState *word = nullptr;
size_t len;
for (;; pos += len) {
auto pos = str.begin();
auto word_begin = str.end();
auto word_end = pos;
/* Helper to prevent duplicating code. */
auto add_word = [&] () {
if (word_begin != str.end()) {
this->word_index.emplace_back(std::string(word_begin, word_end + 1), false);
word_begin = str.end();
}
};
for (size_t len; pos < str.end(); pos += len) {
char32_t c;
len = Utf8Decode(&c, pos);
if (c == 0 || (state == STATE_WORD && IsWhitespace(c))) {
if (state == STATE_WORD && IsWhitespace(c)) {
/* Finish word */
if (word != nullptr) {
*(dest++) = '\0';
word = nullptr;
}
add_word();
state = STATE_WHITESPACE;
if (c != 0) continue; else break;
continue;
}
if (state == STATE_WHITESPACE) {
@@ -74,22 +74,14 @@ void StringFilter::SetFilterTerm(const char *str)
}
/* Add to word */
if (word == nullptr) {
word = &this->word_index.emplace_back(WordState{ dest, false });
if (word_begin == str.end()) {
word_begin = pos;
}
memcpy(dest, pos, len);
dest += len;
word_end = pos;
}
}
/**
* Set the term to filter on.
* @param str Filter term
*/
void StringFilter::SetFilterTerm(const std::string &str)
{
this->SetFilterTerm(str.c_str());
/* Add the last word of the string. */
add_word();
}
/**
@@ -119,12 +111,12 @@ void StringFilter::AddLine(const char *str)
for (WordState &ws : this->word_index) {
if (!ws.match) {
if (this->locale_aware) {
if (match_case ? StrNaturalContains(str, ws.start) : StrNaturalContainsIgnoreCase(str, ws.start)) {
if (match_case ? StrNaturalContains(str, ws.word) : StrNaturalContainsIgnoreCase(str, ws.word)) {
ws.match = true;
this->word_matches++;
}
} else {
if ((match_case ? strstr(str, ws.start) : strcasestr(str, ws.start)) != nullptr) {
if ((match_case ? strstr(str, ws.word.c_str()) : strcasestr(str, ws.word.c_str())) != nullptr) {
ws.match = true;
this->word_matches++;
}