Fix #8316: Make sort industries by production and transported with a cargo filter...
[openttd-github.git] / src / stringfilter.cpp
blob4765a880e15d3f06de714bc7eac4bbca07fc9b06
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 word = &this->word_index.emplace_back(WordState{ dest, false });
80 memcpy(dest, pos, len);
81 dest += len;
85 /**
86 * Reset the matching state to process a new item.
88 void StringFilter::ResetState()
90 this->word_matches = 0;
91 for (WordState &ws : this->word_index) {
92 ws.match = false;
96 /**
97 * Pass another text line from the current item to the filter.
99 * You can call this multiple times for a single item, if the filter shall apply to multiple things.
100 * Before processing the next item you have to call ResetState().
102 * @param str Another line from the item.
104 void StringFilter::AddLine(const char *str)
106 if (str == nullptr) return;
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++;
120 * Pass another text line from the current item to the filter.
122 * You can call this multiple times for a single item, if the filter shall apply to multiple things.
123 * Before processing the next item you have to call ResetState().
125 * @param str Another line from the item.
127 void StringFilter::AddLine(StringID str)
129 char buffer[DRAW_STRING_BUFFER];
130 GetString(buffer, str, lastof(buffer));
131 AddLine(buffer);