OpenTTD Source  20240917-master-g9ab0a47812
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 "strings_type.h"
14 
30 struct StringFilter {
31 private:
33  struct WordState {
34  const char *start;
35  bool match;
36  };
37 
38  const char *filter_buffer;
39  std::vector<WordState> word_index;
40  uint word_matches;
41 
42  const bool *case_sensitive;
43  bool locale_aware;
44 
45 public:
51  ~StringFilter() { free(this->filter_buffer); }
52 
53  void SetFilterTerm(const char *str);
54  void SetFilterTerm(const std::string &str);
55 
60  bool IsEmpty() const { return this->word_index.empty(); }
61 
62  void ResetState();
63  void AddLine(const char *str);
64  void AddLine(const std::string &str);
65  void AddLine(StringID str);
66 
71  bool GetState() const { return this->word_matches == this->word_index.size(); }
72 };
73 
74 #endif /* STRINGFILTER_TYPE_H */
StringFilter::IsEmpty
bool IsEmpty() const
Check whether any filter words were entered.
Definition: stringfilter_type.h:60
StringFilter::SetFilterTerm
void SetFilterTerm(const char *str)
Set the term to filter on.
Definition: stringfilter.cpp:28
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
StringFilter::AddLine
void AddLine(const char *str)
Pass another text line from the current item to the filter.
Definition: stringfilter.cpp:114
strings_type.h
free
void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:334
StringFilter::WordState
State of a single filter word.
Definition: stringfilter_type.h:33
StringFilter::case_sensitive
const bool * case_sensitive
Match case-sensitively (usually a static variable).
Definition: stringfilter_type.h:42
StringFilter::word_matches
uint word_matches
Summary of filter state: Number of words matched.
Definition: stringfilter_type.h:40
StringFilter::WordState::match
bool match
Already matched?
Definition: stringfilter_type.h:35
StringFilter::ResetState
void ResetState()
Reset the matching state to process a new item.
Definition: stringfilter.cpp:98
StringFilter::WordState::start
const char * start
Word to filter for.
Definition: stringfilter_type.h:34
StringFilter::GetState
bool GetState() const
Get the matching state of the current item.
Definition: stringfilter_type.h:71
StringFilter::filter_buffer
const char * filter_buffer
Parsed filter string. Words separated by 0.
Definition: stringfilter_type.h:38
StringFilter::StringFilter
StringFilter(const bool *case_sensitive=nullptr, bool locale_aware=true)
Constructor for filter.
Definition: stringfilter_type.h:50
StringFilter::locale_aware
bool locale_aware
Match words using the current locale.
Definition: stringfilter_type.h:43
StringFilter::word_index
std::vector< WordState > word_index
Word index and filter state.
Definition: stringfilter_type.h:39
StringFilter
String filter and state.
Definition: stringfilter_type.h:30