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/>.
8 /** @file stringfilter.cpp Searching and filtering using a stringterm. */
11 #include "string_func.h"
12 #include "strings_func.h"
13 #include "stringfilter_type.h"
16 #include "safeguards.h"
18 static const WChar STATE_WHITESPACE
= ' ';
19 static const WChar STATE_WORD
= 'w';
20 static const WChar STATE_QUOTE1
= '\'';
21 static const WChar STATE_QUOTE2
= '"';
24 * Set the term to filter on.
25 * @param str Filter term
27 void StringFilter::SetFilterTerm(const char *str
)
29 this->word_index
.clear();
30 this->word_index
.shrink_to_fit();
31 this->word_matches
= 0;
32 free(this->filter_buffer
);
34 assert(str
!= nullptr);
36 char *dest
= MallocT
<char>(strlen(str
) + 1);
37 this->filter_buffer
= dest
;
39 WChar state
= STATE_WHITESPACE
;
40 const char *pos
= str
;
41 WordState
*word
= nullptr;
45 len
= Utf8Decode(&c
, pos
);
47 if (c
== 0 || (state
== STATE_WORD
&& IsWhitespace(c
))) {
49 if (word
!= nullptr) {
53 state
= STATE_WHITESPACE
;
54 if (c
!= 0) continue; else break;
57 if (state
== STATE_WHITESPACE
) {
59 if (IsWhitespace(c
)) continue;
63 if (c
== STATE_QUOTE1
|| c
== STATE_QUOTE2
) {
68 } else if (state
== STATE_WORD
) {
76 if (word
== nullptr) {
77 /*C++17: word = &*/ this->word_index
.push_back({dest
, false});
78 word
= &this->word_index
.back();
81 memcpy(dest
, pos
, len
);
87 * Reset the matching state to process a new item.
89 void StringFilter::ResetState()
91 this->word_matches
= 0;
92 for (WordState
&ws
: this->word_index
) {
98 * Pass another text line from the current item to the filter.
100 * You can call this multiple times for a single item, if the filter shall apply to multiple things.
101 * Before processing the next item you have to call ResetState().
103 * @param str Another line from the item.
105 void StringFilter::AddLine(const char *str
)
107 if (str
== nullptr) return;
109 bool match_case
= this->case_sensitive
!= nullptr && *this->case_sensitive
;
110 for (WordState
&ws
: this->word_index
) {
112 if ((match_case
? strstr(str
, ws
.start
) : strcasestr(str
, ws
.start
)) != nullptr) {
114 this->word_matches
++;
121 * Pass another text line from the current item to the filter.
123 * You can call this multiple times for a single item, if the filter shall apply to multiple things.
124 * Before processing the next item you have to call ResetState().
126 * @param str Another line from the item.
128 void StringFilter::AddLine(StringID str
)
130 char buffer
[DRAW_STRING_BUFFER
];
131 GetString(buffer
, str
, lastof(buffer
));