OpenTTD Source: src/stringfilter.cpp Source File

OpenTTD Source 1.11.0-RC1

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 "stringfilter_type.h"
14 #include "gfx_func.h"
15
16 #include "safeguards.h"
17
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 = '"';
22
27 void StringFilter::SetFilterTerm(const char *str)
28 {
29 this->word_index.clear();
30 this->word_index.shrink_to_fit();
31 this->word_matches = 0;
32 free(this->filter_buffer);
33
34 assert(str != nullptr);
35
36 char *dest = MallocT<char>(strlen(str) + 1);
37 this->filter_buffer = dest;
38
39 WChar state = STATE_WHITESPACE;
40 const char *pos = str;
41 WordState *word = nullptr;
42 size_t len;
43 for (;; pos += len) {
44 WChar c;
45 len = Utf8Decode(&c, pos);
46
47 if (c == 0 || (state == STATE_WORD && IsWhitespace(c))) {
48 /* Finish word */
49 if (word != nullptr) {
50 *(dest++) = '\0';
51 word = nullptr;
52 }
53 state = STATE_WHITESPACE;
54 if (c != 0) continue; else break;
55 }
56
57 if (state == STATE_WHITESPACE) {
58 /* Skip whitespace */
59 if (IsWhitespace(c)) continue;
60 state = STATE_WORD;
61 }
62
63 if (c == STATE_QUOTE1 || c == STATE_QUOTE2) {
64 if (state == c) {
65 /* Stop quoting */
66 state = STATE_WORD;
67 continue;
68 } else if (state == STATE_WORD) {
69 /* Start quoting */
70 state = c;
71 continue;
72 }
73 }
74
75 /* Add to word */
76 if (word == nullptr) {
77 word = &this->word_index.emplace_back(WordState{ dest, false });
78 }
79
80 memcpy(dest, pos, len);
81 dest += len;
82 }
83 }
84
88 void StringFilter::ResetState()
89 {
90 this->word_matches = 0;
91 for (WordState &ws : this->word_index) {
92 ws.match = false;
93 }
94 }
95
104 void StringFilter::AddLine(const char *str)
105 {
106 if (str == nullptr) return;
107
108 bool match_case = this->case_sensitive != nullptr && *this->case_sensitive;
109 for (WordState &ws : this->word_index) {
110 if (!ws.match) {
111 if ((match_case ? strstr(str, ws.start) : strcasestr(str, ws.start)) != nullptr) {
112 ws.match = true;
113 this->word_matches++;
114 }
115 }
116 }
117 }
118
127 void StringFilter::AddLine(StringID str)
128 {
129 char buffer[DRAW_STRING_BUFFER];
130 GetString(buffer, str, lastof(buffer));
131 AddLine(buffer);
132 }
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:35
StringFilter::SetFilterTerm
void SetFilterTerm(const char *str)
Set the term to filter on.
Definition: stringfilter.cpp:27
StringFilter::AddLine
void AddLine(const char *str)
Pass another text line from the current item to the filter.
Definition: stringfilter.cpp:104
gfx_func.h
DRAW_STRING_BUFFER
static const int DRAW_STRING_BUFFER
Size of the buffer used for drawing strings.
Definition: gfx_func.h:85
StringFilter::WordState
State of a single filter word.
Definition: stringfilter_type.h:34
safeguards.h
stdafx.h
Utf8Decode
size_t Utf8Decode(WChar *c, const char *s)
Decode and consume the next UTF-8 encoded character.
Definition: string.cpp:481
string_func.h
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
strings_func.h
StringFilter::case_sensitive
const bool * case_sensitive
Match case-sensitively (usually a static variable).
Definition: stringfilter_type.h:43
StringFilter::word_matches
uint word_matches
Summary of filter state: Number of words matched.
Definition: stringfilter_type.h:41
StringFilter::ResetState
void ResetState()
Reset the matching state to process a new item.
Definition: stringfilter.cpp:88
StringFilter::filter_buffer
const char * filter_buffer
Parsed filter string. Words separated by 0.
Definition: stringfilter_type.h:39
stringfilter_type.h
IsWhitespace
static bool IsWhitespace(WChar c)
Check whether UNICODE character is whitespace or not, i.e.
Definition: string_func.h:252
StringFilter::word_index
std::vector< WordState > word_index
Word index and filter state.
Definition: stringfilter_type.h:40
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:454
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383