OpenTTD Source 20261112-master-gbcbec5e63f
stringfilter.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "string_func.h"
12#include "core/utf8.hpp"
14#include "stringfilter_type.h"
15
16#include "safeguards.h"
17
18static const char32_t STATE_WHITESPACE = ' ';
19static const char32_t STATE_WORD = 'w';
20static const char32_t STATE_QUOTE1 = '\'';
21static const char32_t STATE_QUOTE2 = '"';
22
27void StringFilter::SetFilterTerm(std::string_view str)
28{
29 this->word_index.clear();
30 this->word_index.shrink_to_fit();
31 this->word_matches = 0;
32
33 char32_t state = STATE_WHITESPACE;
34 std::string word;
35 StringBuilder builder(word);
36 auto add_word = [this, &word]() {
37 if (!word.empty()) this->word_index.emplace_back(std::move(word), false);
38 word.clear();
39 };
40
41 for (char32_t c : Utf8View(str)) {
42 if (state == STATE_WORD && IsWhitespace(c)) {
43 /* Finish word */
44 add_word();
45 state = STATE_WHITESPACE;
46 continue;
47 }
48
49 if (state == STATE_WHITESPACE) {
50 /* Skip whitespace */
51 if (IsWhitespace(c)) continue;
52 state = STATE_WORD;
53 }
54
55 if (c == STATE_QUOTE1 || c == STATE_QUOTE2) {
56 if (state == c) {
57 /* Stop quoting */
58 state = STATE_WORD;
59 continue;
60 } else if (state == STATE_WORD) {
61 /* Start quoting */
62 state = c;
63 continue;
64 }
65 }
66
67 /* Add to word */
68 builder.PutUtf8(c);
69 }
70
71 /* Add the last word of the string. */
72 add_word();
73}
74
79{
80 this->word_matches = 0;
81 for (WordState &ws : this->word_index) {
82 ws.match = false;
83 }
84}
85
94void StringFilter::AddLine(std::string_view str)
95{
96 bool match_case = this->case_sensitive != nullptr && *this->case_sensitive;
97 for (WordState &ws : this->word_index) {
98 if (!ws.match) {
99 if (this->locale_aware) {
100 if (match_case ? StrNaturalContains(str, ws.word) : StrNaturalContainsIgnoreCase(str, ws.word)) {
101 ws.match = true;
102 this->word_matches++;
103 }
104 } else {
105 if (match_case ? str.find(ws.word) != str.npos : StrContainsIgnoreCase(str, ws.word)) {
106 ws.match = true;
107 this->word_matches++;
108 }
109 }
110 }
111 }
112}
void PutUtf8(char32_t c)
Append UTF-8 char.
Compose data into a growing std::string.
Constant span of UTF-8 encoded data.
Definition utf8.hpp:28
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
bool StrNaturalContainsIgnoreCase(std::string_view str, std::string_view value)
Checks if a string is contained in another string with a locale-aware comparison that is case insensi...
Definition string.cpp:524
bool StrNaturalContains(std::string_view str, std::string_view value)
Checks if a string is contained in another string with a locale-aware comparison that is case sensiti...
Definition string.cpp:497
bool StrContainsIgnoreCase(std::string_view str, std::string_view value)
Checks if a string is contained in another string, while ignoring the case of the characters.
Definition string.cpp:337
Compose strings from textual and binary data.
Functions related to low-level strings.
bool IsWhitespace(char32_t c)
Check whether UNICODE character is whitespace or not, i.e.
Searching and filtering using a stringterm.
State of a single filter word.
uint word_matches
Summary of filter state: Number of words matched.
void SetFilterTerm(std::string_view str)
Set the term to filter on.
std::vector< WordState > word_index
Word index and filter state.
bool locale_aware
Match words using the current locale.
void ResetState()
Reset the matching state to process a new item.
const bool * case_sensitive
Match case-sensitively (usually a static variable).
Handling of UTF-8 encoded data.