OpenTTD Source  1.11.0-beta2
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:
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 */
StringFilter::IsEmpty
bool IsEmpty() const
Check whether any filter words were entered.
Definition: stringfilter_type.h:59
StringFilter::SetFilterTerm
void SetFilterTerm(const char *str)
Set the term to filter on.
Definition: stringfilter.cpp:27
smallvec_type.hpp
StringFilter::StringFilter
StringFilter(const bool *case_sensitive=nullptr)
Constructor for filter.
Definition: stringfilter_type.h:50
StringFilter::AddLine
void AddLine(const char *str)
Pass another text line from the current item to the filter.
Definition: stringfilter.cpp:104
strings_type.h
StringFilter::WordState
State of a single filter word.
Definition: stringfilter_type.h:34
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
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::WordState::match
bool match
Already matched?
Definition: stringfilter_type.h:36
StringFilter::ResetState
void ResetState()
Reset the matching state to process a new item.
Definition: stringfilter.cpp:88
StringFilter::WordState::start
const char * start
Word to filter for.
Definition: stringfilter_type.h:35
StringFilter::GetState
bool GetState() const
Get the matching state of the current item.
Definition: stringfilter_type.h:69
StringFilter::filter_buffer
const char * filter_buffer
Parsed filter string. Words separated by 0.
Definition: stringfilter_type.h:39
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
StringFilter
String filter and state.
Definition: stringfilter_type.h:31