Add: INR currency (#8136)
[openttd-github.git] / src / stringfilter.cpp
blob30876a82d80995369ce1c9bc0074166d2a50b504
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 */
8 /** @file stringfilter.cpp Searching and filtering using a stringterm. */
10 #include "stdafx.h"
11 #include "string_func.h"
12 #include "strings_func.h"
13 #include "stringfilter_type.h"
14 #include "gfx_func.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 = '"';
23 /**
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;
42 size_t len;
43 for (;; pos += len) {
44 WChar c;
45 len = Utf8Decode(&c, pos);
47 if (c == 0 || (state == STATE_WORD && IsWhitespace(c))) {
48 /* Finish word */
49 if (word != nullptr) {
50 *(dest++) = '\0';
51 word = nullptr;
53 state = STATE_WHITESPACE;
54 if (c != 0) continue; else break;
57 if (state == STATE_WHITESPACE) {
58 /* Skip whitespace */
59 if (IsWhitespace(c)) continue;
60 state = STATE_WORD;
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;
75 /* Add to 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);
82 dest += len;
86 /**
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) {
93 ws.match = false;
97 /**
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) {
111 if (!ws.match) {
112 if ((match_case ? strstr(str, ws.start) : strcasestr(str, ws.start)) != nullptr) {
113 ws.match = true;
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));
132 AddLine(buffer);