OpenTTD
stringfilter_type.h
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 #ifndef STRINGFILTER_TYPE_H
11 #define STRINGFILTER_TYPE_H
12 
13 #include "core/smallvec_type.hpp"
14 #include "strings_type.h"
15 
31 struct StringFilter {
32 private:
34  struct WordState {
35  const char *start;
36  bool match;
37  };
38 
39  const char *filter_buffer;
40  std::vector<WordState> word_index;
41  uint word_matches;
42 
43  const bool *case_sensitive;
44 
45 public:
50  StringFilter(const bool *case_sensitive = nullptr) : filter_buffer(nullptr), word_matches(0), case_sensitive(case_sensitive) {}
51  ~StringFilter() { free(this->filter_buffer); }
52 
53  void SetFilterTerm(const char *str);
54 
59  bool IsEmpty() const { return this->word_index.size() == 0; }
60 
61  void ResetState();
62  void AddLine(const char *str);
63  void AddLine(StringID str);
64 
69  bool GetState() const { return this->word_matches == this->word_index.size(); }
70 };
71 
72 #endif /* STRINGFILTER_TYPE_H */
bool match
Already matched?
Simple vector class that allows allocating an item without the need to copy this->data needlessly...
void ResetState()
Reset the matching state to process a new item.
bool GetState() const
Get the matching state of the current item.
String filter and state.
const char * filter_buffer
Parsed filter string. Words separated by 0.
void SetFilterTerm(const char *str)
Set the term to filter on.
const char * start
Word to filter for.
uint word_matches
Summary of filter state: Number of words matched.
State of a single filter word.
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
StringFilter(const bool *case_sensitive=nullptr)
Constructor for filter.
void AddLine(const char *str)
Pass another text line from the current item to the filter.
std::vector< WordState > word_index
Word index and filter state.
const bool * case_sensitive
Match case-sensitively (usually a static variable).
Types related to strings.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:129
bool IsEmpty() const
Check whether any filter words were entered.