OpenTTD Source 20250610-master-g8c90be8c9f
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 <http://www.gnu.org/licenses/>.
6 */
7
10#include "stdafx.h"
11#include "string_func.h"
12#include "strings_func.h"
13#include "core/utf8.hpp"
15#include "stringfilter_type.h"
16#include "gfx_func.h"
17
18#include "safeguards.h"
19
20static const char32_t STATE_WHITESPACE = ' ';
21static const char32_t STATE_WORD = 'w';
22static const char32_t STATE_QUOTE1 = '\'';
23static const char32_t STATE_QUOTE2 = '"';
24
29void 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
81{
82 this->word_matches = 0;
83 for (WordState &ws : this->word_index) {
84 ws.match = false;
85 }
86}
87
96void StringFilter::AddLine(std::string_view str)
97{
98 bool match_case = this->case_sensitive != nullptr && *this->case_sensitive;
99 for (WordState &ws : this->word_index) {
100 if (!ws.match) {
101 if (this->locale_aware) {
102 if (match_case ? StrNaturalContains(str, ws.word) : StrNaturalContainsIgnoreCase(str, ws.word)) {
103 ws.match = true;
104 this->word_matches++;
105 }
106 } else {
107 if (match_case ? str.find(ws.word) != str.npos : StrContainsIgnoreCase(str, ws.word)) {
108 ws.match = true;
109 this->word_matches++;
110 }
111 }
112 }
113 }
114}
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:30
Functions related to the gfx engine.
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:523
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:496
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:334
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.
Functions related to OTTD's strings.
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.