OpenTTD Source: src/stringfilter_type.h Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef STRINGFILTER_TYPE_H
11#define STRINGFILTER_TYPE_H
12
13#include "strings_type.h"
14
30 struct StringFilter {
31private:
33 struct WordState {
34 std::string word;
35 bool match;
36 };
37
38 std::vector<WordState> word_index;
39 uint word_matches = 0;
40
41 const bool *case_sensitive;
42 bool locale_aware;
43
44public:
49 StringFilter(const bool *case_sensitive = nullptr, bool locale_aware = true) : case_sensitive(case_sensitive), locale_aware(locale_aware) {}
50
51 void SetFilterTerm(std::string_view str);
52
57 bool IsEmpty() const { return this->word_index.empty(); }
58
59 void ResetState();
60 void AddLine(const char *) = delete; // prevent implicit construction of string_view from potential nullptr
61 void AddLine(std::string_view str);
62
67 bool GetState() const { return this->word_matches == this->word_index.size(); }
68};
69
70#endif /* STRINGFILTER_TYPE_H */
strings_type.h
Types related to strings.
StringFilter::WordState
State of a single filter word.
Definition stringfilter_type.h:33
StringFilter::WordState::match
bool match
Already matched?
Definition stringfilter_type.h:35
StringFilter::WordState::word
std::string word
Word to filter for.
Definition stringfilter_type.h:34
StringFilter
String filter and state.
Definition stringfilter_type.h:30
StringFilter::StringFilter
StringFilter(const bool *case_sensitive=nullptr, bool locale_aware=true)
Constructor for filter.
Definition stringfilter_type.h:49
StringFilter::IsEmpty
bool IsEmpty() const
Check whether any filter words were entered.
Definition stringfilter_type.h:57
StringFilter::word_matches
uint word_matches
Summary of filter state: Number of words matched.
Definition stringfilter_type.h:39
StringFilter::SetFilterTerm
void SetFilterTerm(std::string_view str)
Set the term to filter on.
Definition stringfilter.cpp:29
StringFilter::word_index
std::vector< WordState > word_index
Word index and filter state.
Definition stringfilter_type.h:38
StringFilter::locale_aware
bool locale_aware
Match words using the current locale.
Definition stringfilter_type.h:42
StringFilter::ResetState
void ResetState()
Reset the matching state to process a new item.
Definition stringfilter.cpp:80
StringFilter::case_sensitive
const bool * case_sensitive
Match case-sensitively (usually a static variable).
Definition stringfilter_type.h:41
StringFilter::GetState
bool GetState() const
Get the matching state of the current item.
Definition stringfilter_type.h:67