* Set the term to filter on. * @param str Filter term */
| 27 | * @param str Filter term |
| 28 | */ |
| 29 | void StringFilter::SetFilterTerm(std::string_view str) |
| 30 | { |
| 31 | this->word_index.clear(); |
| 32 | this->word_index.shrink_to_fit(); |
| 33 | this->word_matches = 0; |
| 34 | |
| 35 | char32_t state = STATE_WHITESPACE; |
| 36 | std::string word; |
| 37 | StringBuilder builder(word); |
| 38 | auto add_word = [this, &word]() { |
| 39 | if (!word.empty()) this->word_index.emplace_back(std::move(word), false); |
| 40 | word.clear(); |
| 41 | }; |
| 42 | |
| 43 | for (char32_t c : Utf8View(str)) { |
| 44 | if (state == STATE_WORD && IsWhitespace(c)) { |
| 45 | /* Finish word */ |
| 46 | add_word(); |
| 47 | state = STATE_WHITESPACE; |
| 48 | continue; |
| 49 | } |
| 50 | |
| 51 | if (state == STATE_WHITESPACE) { |
| 52 | /* Skip whitespace */ |
| 53 | if (IsWhitespace(c)) continue; |
| 54 | state = STATE_WORD; |
| 55 | } |
| 56 | |
| 57 | if (c == STATE_QUOTE1 || c == STATE_QUOTE2) { |
| 58 | if (state == c) { |
| 59 | /* Stop quoting */ |
| 60 | state = STATE_WORD; |
| 61 | continue; |
| 62 | } else if (state == STATE_WORD) { |
| 63 | /* Start quoting */ |
| 64 | state = c; |
| 65 | continue; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /* Add to word */ |
| 70 | builder.PutUtf8(c); |
| 71 | } |
| 72 | |
| 73 | /* Add the last word of the string. */ |
| 74 | add_word(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Reset the matching state to process a new item. |
no test coverage detected